aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabrice Bellingard <bellingard@gmail.com>2011-04-20 17:07:07 +0200
committerFabrice Bellingard <bellingard@gmail.com>2011-04-20 17:15:30 +0200
commit3c653a2018b745f32b2c6ed9a7ecea9edb153d08 (patch)
tree1c77a2963e058ba2c258a8bd9aeeba4d5cdb93ba
parentb77474c58f3056fc8291c639fb1e66921605f4cb (diff)
downloadsonarqube-3c653a2018b745f32b2c6ed9a7ecea9edb153d08.tar.gz
sonarqube-3c653a2018b745f32b2c6ed9a7ecea9edb153d08.zip
SONAR-2347 Add unit test to ReviewsDecorator
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewsDecorator.java11
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewsDecoratorTest.java46
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/fixture.xml37
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/shouldCloseReviewWithoutCorrespondingViolation-result.xml22
-rw-r--r--sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl25
5 files changed, 93 insertions, 48 deletions
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
index c0b5b7ef94c..c4be5daf4f0 100644
--- 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
@@ -19,15 +19,12 @@
*/
package org.sonar.plugins.core.sensors;
-import javax.persistence.EntityManager;
-import javax.persistence.EntityTransaction;
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.DependedUpon;
import org.sonar.api.batch.DependsUpon;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.Snapshot;
@@ -60,12 +57,16 @@ public class ReviewsDecorator implements Decorator {
if (currentSnapshot != null) {
int resourceId = currentSnapshot.getResourceId();
int snapshotId = currentSnapshot.getId();
- Query query = databaseSession.createNativeQuery("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 + ")");
+ 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/ReviewsDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewsDecoratorTest.java
index 256cca68c04..53c42d1b06a 100644
--- 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
@@ -19,40 +19,28 @@
*/
package org.sonar.plugins.core.sensors;
-import org.junit.Ignore;
-import org.junit.Test;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.File;
-import org.sonar.api.resources.Resource;
-import org.sonar.api.utils.DateUtils;
-import org.sonar.batch.components.PastSnapshot;
-import org.sonar.batch.components.TimeMachineConfiguration;
-import org.sonar.batch.index.ResourcePersister;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
-import java.text.ParseException;
-import java.util.Arrays;
+import java.sql.Statement;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
+import org.junit.Test;
+import org.sonar.test.persistence.DatabaseTestCase;
-public class ReviewsDecoratorTest extends AbstractDbUnitTestCase {
+public class ReviewsDecoratorTest extends DatabaseTestCase {
@Test
- @Ignore("DBUnit needs Hibernate mapping...")
- public void shouldSaveConfigurationInSnapshotsTable() throws ParseException {
+ public void shouldCloseReviewWithoutCorrespondingViolation() throws Exception {
setupData("fixture");
-
- File resource = new File("Foo");
- Snapshot snapshot = new Snapshot();
- snapshot.setId(666);
- snapshot.setResourceId(111);
- ResourcePersister persister = mock(ResourcePersister.class);
- when(persister.getSnapshot(resource)).thenReturn(snapshot);
-
- ReviewsDecorator reviewsDecorator = new ReviewsDecorator(persister, getSession());
- reviewsDecorator.decorate(resource, null);
-
- //checkTables("shouldSaveConfigurationInSnapshotsTable", "snapshots");
+
+ 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/ReviewsDecoratorTest/fixture.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/fixture.xml
index ea16316886c..d57973770cb 100644
--- 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
@@ -2,37 +2,46 @@
<snapshots
id="11"
- project_id="555"/>
+ project_id="555"
+ status="P" islast="false"/>
<snapshots
id="111"
- project_id="555"/>
+ project_id="555"
+ status="P" islast="false"/>
<snapshots
id="22"
- project_id="666"/>
+ project_id="666"
+ status="P" islast="false"/>
<snapshots
id="222"
- project_id="666"/>
+ project_id="666"
+ status="P" islast="false"/>
<rule_failures
id="1"
- permament_id="1"
- snapshot_id="11"/>
+ permanent_id="1"
+ snapshot_id="11"
+ rule_id="1" failure_level="1"/>
<rule_failures
id="2"
- permament_id="2"
- snapshot_id="22"/>
+ permanent_id="2"
+ snapshot_id="22"
+ rule_id="1" failure_level="1"/>
<rule_failures
id="3"
- permament_id="3"
- snapshot_id="22"/>
+ permanent_id="3"
+ snapshot_id="22"
+ rule_id="1" failure_level="1"/>
<rule_failures
id="4"
- permament_id="1"
- snapshot_id="111"/>
+ permanent_id="1"
+ snapshot_id="111"
+ rule_id="1" failure_level="1"/>
<rule_failures
id="5"
- permament_id="3"
- snapshot_id="222"/>
+ permanent_id="3"
+ snapshot_id="222"
+ rule_id="1" failure_level="1"/>
<reviews
id="1"
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
new file mode 100644
index 00000000000..7d614f3112c
--- /dev/null
+++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/shouldCloseReviewWithoutCorrespondingViolation-result.xml
@@ -0,0 +1,22 @@
+<dataset>
+
+ <reviews
+ id="1"
+ 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]"/>
+ <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]"/>
+ <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]"/>
+
+</dataset> \ No newline at end of file
diff --git a/sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl b/sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl
index eab1ca343af..c5125179cbf 100644
--- a/sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl
+++ b/sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl
@@ -481,3 +481,28 @@ create table WIDGET_PROPERTIES (
);
CREATE INDEX WIDGET_PROPERTIES_WIDGETS ON WIDGET_PROPERTIES (WIDGET_ID);
+CREATE TABLE REVIEWS (
+ ID INTEGER NOT NULL,
+ CREATED_AT TIMESTAMP,
+ UPDATED_AT TIMESTAMP,
+ USER_ID INTEGER,
+ ASSIGNEE_ID INTEGER,
+ TITLE VARCHAR(500),
+ REVIEW_TYPE VARCHAR(10),
+ STATUS VARCHAR(10),
+ SEVERITY VARCHAR(10),
+ RULE_FAILURE_PERMANENT_ID INTEGER,
+ RESOURCE_ID INTEGER,
+ RESOURCE_LINE INTEGER,
+ primary key (id)
+);
+
+CREATE TABLE REVIEW_COMMENTS (
+ ID INTEGER NOT NULL,
+ CREATED_AT TIMESTAMP,
+ UPDATED_AT TIMESTAMP,
+ REVIEW_ID INTEGER,
+ USER_ID INTEGER,
+ REVIEW_TEXT CLOB(2147483647),
+ primary key (id)
+);