diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-05-19 16:14:21 +0200 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-05-19 16:14:21 +0200 |
commit | ff3117718a1642e99d97416b88b2db702d1b96f8 (patch) | |
tree | aa3afbd405482ee72453b9dca17266b5465d8e66 /plugins/sonar-core-plugin | |
parent | 1dfe5d781675f0a7dfe19dce93757a47262abbae (diff) | |
download | sonarqube-ff3117718a1642e99d97416b88b2db702d1b96f8.tar.gz sonarqube-ff3117718a1642e99d97416b88b2db702d1b96f8.zip |
Fix merge of release 2.8
Diffstat (limited to 'plugins/sonar-core-plugin')
3 files changed, 52 insertions, 13 deletions
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 index b46108b8840..0b32e50b288 100644 --- 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 @@ -19,6 +19,8 @@ */ package org.sonar.plugins.core.sensors; +import javax.persistence.Query; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.Decorator; @@ -29,10 +31,9 @@ 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.api.resources.ResourceUtils; import org.sonar.batch.index.ResourcePersister; -import javax.persistence.Query; - /** * Decorator that currently only closes a review when its corresponding violation has been fixed. */ @@ -50,7 +51,7 @@ public class CloseReviewsDecorator implements Decorator { } public boolean shouldExecuteOnProject(Project project) { - return true; + return project.isLatestAnalysis(); } public void decorate(Resource resource, DecoratorContext context) { @@ -58,16 +59,32 @@ public class CloseReviewsDecorator implements Decorator { if (currentSnapshot != null) { int resourceId = currentSnapshot.getResourceId(); int snapshotId = currentSnapshot.getId(); - Query query = databaseSession.createNativeQuery(generateSqlRequest(resourceId, snapshotId)); + Query query = databaseSession.createNativeQuery(generateUpdateOnResourceSqlRequest(resourceId, snapshotId)); int rowUpdated = query.executeUpdate(); LOG.debug("- {} reviews set to 'closed' on resource #{}", rowUpdated, resourceId); + + if (ResourceUtils.isRootProject(resource)) { + query = databaseSession.createNativeQuery(generateUpdateOnProjectSqlRequest(resourceId, currentSnapshot.getId())); + query.setParameter(1, Boolean.TRUE); + rowUpdated = query.executeUpdate(); + LOG.debug("- {} reviews set to 'closed' on project #{}", 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)"; + protected String generateUpdateOnResourceSqlRequest(int resourceId, int snapshotId) { + return "UPDATE reviews SET status='CLOSED', updated_at=CURRENT_TIMESTAMP 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)"; + } + + protected String generateUpdateOnProjectSqlRequest(int projectId, int projectSnapshotId) { + return "UPDATE reviews SET status='CLOSED', updated_at=CURRENT_TIMESTAMP WHERE status='OPEN' AND project_id=" + projectId + + " AND resource_id IN ( SELECT prev.project_id FROM snapshots prev WHERE prev.root_project_id=" + projectId + + " AND prev.islast=? AND NOT EXISTS ( SELECT cur.id FROM snapshots cur WHERE cur.root_snapshot_id=" + projectSnapshotId + + " AND cur.created_at > prev.created_at AND cur.root_project_id=" + projectId + " AND cur.project_id=prev.project_id ) )"; } } 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 index 493352c1147..2ea8a7c1de4 100644 --- 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 @@ -21,25 +21,47 @@ package org.sonar.plugins.core.sensors; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.sql.Statement; +import junit.framework.ComparisonFailure; + import org.junit.Test; +import org.sonar.api.resources.Project; import org.sonar.test.persistence.DatabaseTestCase; public class CloseReviewsDecoratorTest extends DatabaseTestCase { @Test + public void testShouldExecuteOnProject() throws Exception { + Project project = mock(Project.class); + when(project.isLatestAnalysis()).thenReturn(true); + CloseReviewsDecorator reviewsDecorator = new CloseReviewsDecorator(null, null); + assertTrue(reviewsDecorator.shouldExecuteOnProject(project)); + } + + @Test public void shouldCloseReviewWithoutCorrespondingViolation() throws Exception { setupData("fixture"); CloseReviewsDecorator reviewsDecorator = new CloseReviewsDecorator(null, null); - String sqlRequest = reviewsDecorator.generateSqlRequest(666, 222); - + String sqlRequest = reviewsDecorator.generateUpdateOnResourceSqlRequest(666, 222); + Statement stmt = getConnection().createStatement(); int count = stmt.executeUpdate(sqlRequest); assertThat(count, is(1)); - assertTables("shouldCloseReviewWithoutCorrespondingViolation", "reviews"); + assertTables("shouldCloseReviewWithoutCorrespondingViolation", new String[] { "reviews" }, new String[] { "updated_at" }); + + try { + assertTables("shouldCloseReviewWithoutCorrespondingViolation", new String[] { "reviews" }); + fail("'updated_at' columns are identical whereas they should be different."); + } catch (ComparisonFailure e) { + // "updated_at" column must be different, so the comparison should raise this exception + } } } 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 index d46429ddf3d..db7bf76f0e0 100644 --- 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 @@ -5,18 +5,18 @@ status="OPEN" rule_failure_permanent_id="1" resource_id="555" - created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]"/> + created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]" project_id="[null]"/> <reviews id="2" status="CLOSED" rule_failure_permanent_id="2" resource_id="666" - created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]"/> + created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]" project_id="[null]"/> <reviews id="3" status="OPEN" rule_failure_permanent_id="3" resource_id="666" - created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]"/> + created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]" project_id="[null]"/> </dataset>
\ No newline at end of file |