From dbc988359bac8e7a5ca426fa42f27ae0b8c36063 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Mon, 25 Apr 2011 23:12:50 +0200 Subject: SONAR-2327 split actions for resource viewer and reviews page --- .../java/org/sonar/plugins/core/CorePlugin.java | 2 +- .../core/sensors/CloseReviewsDecorator.java | 72 ++++++++++++++++++++++ .../plugins/core/sensors/ReviewsDecorator.java | 72 ---------------------- .../core/sensors/CloseReviewsDecoratorTest.java | 45 ++++++++++++++ .../plugins/core/sensors/ReviewsDecoratorTest.java | 46 -------------- .../sensors/CloseReviewsDecoratorTest/fixture.xml | 62 +++++++++++++++++++ ...eReviewWithoutCorrespondingViolation-result.xml | 22 +++++++ .../core/sensors/ReviewsDecoratorTest/fixture.xml | 62 ------------------- ...eReviewWithoutCorrespondingViolation-result.xml | 22 ------- 9 files changed, 202 insertions(+), 203 deletions(-) create mode 100644 plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/CloseReviewsDecorator.java delete mode 100644 plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewsDecorator.java create mode 100644 plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest.java delete mode 100644 plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewsDecoratorTest.java create mode 100644 plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest/fixture.xml create mode 100644 plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest/shouldCloseReviewWithoutCorrespondingViolation-result.xml delete mode 100644 plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/fixture.xml delete mode 100644 plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/shouldCloseReviewWithoutCorrespondingViolation-result.xml (limited to 'plugins') diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index b0827f546f4..4634bcb01fe 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -214,7 +214,7 @@ public class CorePlugin extends SonarPlugin { extensions.add(NoSonarFilter.class); extensions.add(DirectoriesDecorator.class); extensions.add(FilesDecorator.class); - extensions.add(ReviewsDecorator.class); + extensions.add(CloseReviewsDecorator.class); // time machine extensions.add(TendencyDecorator.class); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/CloseReviewsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/CloseReviewsDecorator.java new file mode 100644 index 00000000000..e037cd03877 --- /dev/null +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/CloseReviewsDecorator.java @@ -0,0 +1,72 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.core.sensors; + +import javax.persistence.Query; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.batch.Decorator; +import org.sonar.api.batch.DecoratorContext; +import org.sonar.api.batch.DependsUpon; +import org.sonar.api.database.DatabaseSession; +import org.sonar.api.database.model.Snapshot; +import org.sonar.api.resources.Project; +import org.sonar.api.resources.Resource; +import org.sonar.batch.index.ResourcePersister; + +/** + * Decorator that currently only closes a review when its corresponding violation has been fixed. + */ +@DependsUpon("ViolationPersisterDecorator") +public class CloseReviewsDecorator implements Decorator { + + private static final Logger LOG = LoggerFactory.getLogger(CloseReviewsDecorator.class); + + private ResourcePersister resourcePersister; + private DatabaseSession databaseSession; + + public CloseReviewsDecorator(ResourcePersister resourcePersister, DatabaseSession databaseSession) { + this.resourcePersister = resourcePersister; + this.databaseSession = databaseSession; + } + + public boolean shouldExecuteOnProject(Project project) { + return true; + } + + public void decorate(Resource resource, DecoratorContext context) { + Snapshot currentSnapshot = resourcePersister.getSnapshot(resource); + if (currentSnapshot != null) { + int resourceId = currentSnapshot.getResourceId(); + int snapshotId = currentSnapshot.getId(); + Query query = databaseSession.createNativeQuery(generateSqlRequest(resourceId, snapshotId)); + int rowUpdated = query.executeUpdate(); + LOG.debug("- {} reviews set to 'closed' on resource #{}", rowUpdated, resourceId); + databaseSession.commit(); + } + } + + String generateSqlRequest(int resourceId, int snapshotId) { + return "UPDATE reviews SET status='CLOSED' " + "WHERE resource_id = " + resourceId + " AND rule_failure_permanent_id NOT IN " + + "(SELECT permanent_id FROM rule_failures WHERE snapshot_id = " + snapshotId + " AND permanent_id IS NOT NULL)"; + } + +} diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewsDecorator.java deleted file mode 100644 index c4be5daf4f0..00000000000 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewsDecorator.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2011 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.plugins.core.sensors; - -import javax.persistence.Query; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.Decorator; -import org.sonar.api.batch.DecoratorContext; -import org.sonar.api.batch.DependsUpon; -import org.sonar.api.database.DatabaseSession; -import org.sonar.api.database.model.Snapshot; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Resource; -import org.sonar.batch.index.ResourcePersister; - -/** - * Decorator that currently only closes a review when its corresponding violation has been fixed. - */ -@DependsUpon("ViolationPersisterDecorator") -public class ReviewsDecorator implements Decorator { - - private static final Logger LOG = LoggerFactory.getLogger(ReviewsDecorator.class); - - private ResourcePersister resourcePersister; - private DatabaseSession databaseSession; - - public ReviewsDecorator(ResourcePersister resourcePersister, DatabaseSession databaseSession) { - this.resourcePersister = resourcePersister; - this.databaseSession = databaseSession; - } - - public boolean shouldExecuteOnProject(Project project) { - return true; - } - - public void decorate(Resource resource, DecoratorContext context) { - Snapshot currentSnapshot = resourcePersister.getSnapshot(resource); - if (currentSnapshot != null) { - int resourceId = currentSnapshot.getResourceId(); - int snapshotId = currentSnapshot.getId(); - Query query = databaseSession.createNativeQuery(generateSqlRequest(resourceId, snapshotId)); - int rowUpdated = query.executeUpdate(); - LOG.debug("- {} reviews set to 'closed' on resource #{}", rowUpdated, resourceId); - databaseSession.commit(); - } - } - - protected String generateSqlRequest(int resourceId, int snapshotId) { - return "UPDATE reviews SET status='closed' " + "WHERE resource_id = " + resourceId + " AND rule_failure_permanent_id NOT IN " - + "(SELECT permanent_id FROM rule_failures WHERE snapshot_id = " + snapshotId + ")"; - } - -} diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest.java new file mode 100644 index 00000000000..493352c1147 --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest.java @@ -0,0 +1,45 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.core.sensors; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.sql.Statement; + +import org.junit.Test; +import org.sonar.test.persistence.DatabaseTestCase; + +public class CloseReviewsDecoratorTest extends DatabaseTestCase { + + @Test + public void shouldCloseReviewWithoutCorrespondingViolation() throws Exception { + setupData("fixture"); + + CloseReviewsDecorator reviewsDecorator = new CloseReviewsDecorator(null, null); + String sqlRequest = reviewsDecorator.generateSqlRequest(666, 222); + + Statement stmt = getConnection().createStatement(); + int count = stmt.executeUpdate(sqlRequest); + + assertThat(count, is(1)); + assertTables("shouldCloseReviewWithoutCorrespondingViolation", "reviews"); + } +} diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewsDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewsDecoratorTest.java deleted file mode 100644 index 53c42d1b06a..00000000000 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewsDecoratorTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2011 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.plugins.core.sensors; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -import java.sql.Statement; - -import org.junit.Test; -import org.sonar.test.persistence.DatabaseTestCase; - -public class ReviewsDecoratorTest extends DatabaseTestCase { - - @Test - public void shouldCloseReviewWithoutCorrespondingViolation() throws Exception { - setupData("fixture"); - - ReviewsDecorator reviewsDecorator = new ReviewsDecorator(null, null); - String sqlRequest = reviewsDecorator.generateSqlRequest(666, 222); - System.out.println(sqlRequest); - - Statement stmt = getConnection().createStatement(); - int count = stmt.executeUpdate(sqlRequest); - - assertThat(count, is(1)); - assertTables("shouldCloseReviewWithoutCorrespondingViolation", "reviews"); - } -} diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest/fixture.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest/fixture.xml new file mode 100644 index 00000000000..6142cbf4f3b --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest/fixture.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest/shouldCloseReviewWithoutCorrespondingViolation-result.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest/shouldCloseReviewWithoutCorrespondingViolation-result.xml new file mode 100644 index 00000000000..d46429ddf3d --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/CloseReviewsDecoratorTest/shouldCloseReviewWithoutCorrespondingViolation-result.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/fixture.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/fixture.xml deleted file mode 100644 index d57973770cb..00000000000 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/fixture.xml +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/shouldCloseReviewWithoutCorrespondingViolation-result.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/shouldCloseReviewWithoutCorrespondingViolation-result.xml deleted file mode 100644 index 7d614f3112c..00000000000 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/shouldCloseReviewWithoutCorrespondingViolation-result.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - \ No newline at end of file -- cgit v1.2.3