]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some quality flaws
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 6 Feb 2012 12:44:20 +0000 (13:44 +0100)
committerFabrice Bellingard <bellingard@gmail.com>
Mon, 6 Feb 2012 15:27:33 +0000 (16:27 +0100)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ViolationsDecorator.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java
sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java
sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java
sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java
sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
sonar-core/src/test/java/org/sonar/core/purge/PurgeableSnapshotDtoTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml [new file with mode: 0644]

index c739a57e287e10d0fd8aa7f39564528019a4dcd9..eb4ade51a1832af8789c86e8ffd2277fbc6e5c70 100644 (file)
@@ -113,7 +113,7 @@ public class ViolationsDecorator implements Decorator {
       if (rulesBag != null) {
         for (Multiset.Entry<Rule> entry : rulesBag.entrySet()) {
           RuleMeasure measure = RuleMeasure.createForRule(metric, entry.getElement(), (double) entry.getCount());
-          measure.setRulePriority(severity);
+          measure.setSeverity(severity);
           context.saveMeasure(measure);
         }
       }
index 48b97f0a60e19f3810de1c7991a722b766bec7ab..96c4e6aa5890efceb146d67d5da0b22d4aa1b5b6 100644 (file)
@@ -156,7 +156,7 @@ public class NewViolationsDecorator implements Decorator {
 
       for (Rule rule : rules) {
         RuleMeasure measure = RuleMeasure.createForRule(metric, rule, null);
-        measure.setRulePriority(severity);
+        measure.setSeverity(severity);
         for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) {
           int variationIndex = pastSnapshot.getIndex();
           int count = countViolations(violationsPerRule.get(rule), pastSnapshot.getTargetDate());
index 620519345f00d11c14450ece94d3d463c66eefc1..8552118e54c74d11592fcc1792e876a49f30158a 100644 (file)
@@ -158,7 +158,7 @@ public final class MeasurePersister {
     }
     if (measure instanceof RuleMeasure) {
       RuleMeasure ruleMeasure = (RuleMeasure) measure;
-      merge.setRulePriority(ruleMeasure.getRulePriority());
+      merge.setRulePriority(ruleMeasure.getSeverity());
       if (ruleMeasure.getRule() != null) {
         Rule ruleWithId = ruleFinder.findByKey(ruleMeasure.getRule().getRepositoryKey(), ruleMeasure.getRule().getKey());
         if (ruleWithId != null) {
index de93c7b503bad9c4df3a2eaad8c829065e9a42cd..baf8fc3868096adcf67c3db9462966c94961fa31 100644 (file)
@@ -43,7 +43,8 @@ public class PurgeDao {
     SqlSession session = mybatis.openBatchSession();
     PurgeMapper purgeMapper = session.getMapper(PurgeMapper.class);
     try {
-      List<Long> projectIds = resourceDao.getDescendantProjectIdsAndSelf(rootProjectId, session);
+      List<Long> projectIds = Lists.newArrayList(rootProjectId);
+      projectIds.addAll(resourceDao.getDescendantProjectIds(rootProjectId, session));
       for (Long projectId : projectIds) {
         purgeProject(projectId, session, purgeMapper);
       }
index b9b1b44be9db3b1328b7ae563338155af9931cc5..5e9d085372f0d764cb8c14cba183fbb06323dbdd 100644 (file)
@@ -51,7 +51,7 @@ public final class PurgeSnapshotQuery {
   }
 
   public PurgeSnapshotQuery setScopes(String[] scopes) {
-    this.scopes = scopes;
+    this.scopes = scopes; //NOSONAR May expose internal representation by incorporating reference to mutable object
     return this;
   }
 
@@ -60,7 +60,7 @@ public final class PurgeSnapshotQuery {
   }
 
   public PurgeSnapshotQuery setQualifiers(String[] qualifiers) {
-    this.qualifiers = qualifiers;
+    this.qualifiers = qualifiers;//NOSONAR May expose internal representation by incorporating reference to mutable object
     return this;
   }
 
index 3703c6b7ed4f33e6eef36116bcd49b57c4f472b3..421eca7304d56b9012db569deace7f5ded74d38e 100644 (file)
@@ -32,16 +32,16 @@ public class ResourceDao {
     this.mybatis = mybatis;
   }
 
-  public List<Long> getDescendantProjectIdsAndSelf(long projectId) {
+  public List<Long> getDescendantProjectIds(long projectId) {
     SqlSession session = mybatis.openSession();
     try {
-      return getDescendantProjectIdsAndSelf(projectId, session);
+      return getDescendantProjectIds(projectId, session);
     } finally {
       MyBatis.closeQuietly(session);
     }
   }
 
-  public List<Long> getDescendantProjectIdsAndSelf(long projectId, SqlSession session) {
+  public List<Long> getDescendantProjectIds(long projectId, SqlSession session) {
     ResourceMapper mapper = session.getMapper(ResourceMapper.class);
     List<Long> ids = Lists.newArrayList();
     appendChildProjectIds(projectId, mapper, ids);
@@ -49,7 +49,6 @@ public class ResourceDao {
   }
 
   private void appendChildProjectIds(long projectId, ResourceMapper mapper, List<Long> ids) {
-    ids.add(projectId);
     List<Long> subProjectIds = mapper.selectDescendantProjectIds(projectId);
     for (Long subProjectId : subProjectIds) {
       ids.add(subProjectId);
diff --git a/sonar-core/src/test/java/org/sonar/core/purge/PurgeableSnapshotDtoTest.java b/sonar-core/src/test/java/org/sonar/core/purge/PurgeableSnapshotDtoTest.java
new file mode 100644 (file)
index 0000000..48fb490
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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.core.purge;
+
+import org.junit.Test;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.number.OrderingComparisons.greaterThan;
+import static org.junit.Assert.assertThat;
+
+public class PurgeableSnapshotDtoTest {
+  @Test
+  public void testEquals() {
+    PurgeableSnapshotDto dto1 = new PurgeableSnapshotDto().setSnapshotId(3L);
+    PurgeableSnapshotDto dto2 = new PurgeableSnapshotDto().setSnapshotId(4L);
+    assertThat(dto1.equals(dto2), is(false));
+    assertThat(dto2.equals(dto1), is(false));
+    assertThat(dto1.equals(dto1), is(true));
+    assertThat(dto1.equals(new PurgeableSnapshotDto().setSnapshotId(3L)), is(true));
+    assertThat(dto1.equals("bi_bop_a_lou_la"), is(false));
+    assertThat(dto1.equals(null), is(false));
+  }
+
+  @Test
+  public void testHasCode() {
+    PurgeableSnapshotDto dto = new PurgeableSnapshotDto().setSnapshotId(3L);
+    assertThat(dto.hashCode(), is(dto.hashCode()));
+
+    // no id
+    dto = new PurgeableSnapshotDto();
+    assertThat(dto.hashCode(), is(dto.hashCode()));
+  }
+
+  @Test
+  public void testToString() {
+    PurgeableSnapshotDto dto = new PurgeableSnapshotDto().setSnapshotId(3L);
+    assertThat(dto.toString().length(), greaterThan(0));
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
new file mode 100644 (file)
index 0000000..0e22ee4
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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.core.resource;
+
+import org.hamcrest.core.Is;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.core.persistence.DaoTestCase;
+
+import java.util.List;
+
+import static org.hamcrest.core.IsNot.not;
+import static org.hamcrest.core.IsNull.nullValue;
+import static org.junit.Assert.assertThat;
+import static org.junit.matchers.JUnitMatchers.hasItems;
+
+public class ResourceDaoTest extends DaoTestCase {
+
+  private ResourceDao dao;
+
+  @Before
+  public void createDao() {
+    dao = new ResourceDao(getMyBatis());
+  }
+
+  @Test
+  public void testDescendantProjectIdsAndSelf() {
+    setupData("fixture");
+
+    List<Long> ids = dao.getDescendantProjectIds(1L);
+
+    assertThat(ids, hasItems(2L));
+    assertThat(ids.size(), Is.is(1));
+  }
+
+  @Test
+  public void testDescendantProjectIdsAndSelf_id_not_found() {
+    setupData("fixture");
+
+    List<Long> ids = dao.getDescendantProjectIds(33333L);
+
+    assertThat(ids, not(nullValue()));
+    assertThat(ids.size(), Is.is(0));
+  }
+}
+
diff --git a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml
new file mode 100644 (file)
index 0000000..41ab615
--- /dev/null
@@ -0,0 +1,61 @@
+<dataset>
+
+  <!-- root project -->
+  <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+            description="[null]" long_name="Struts"
+            enabled="[true]" language="java" copy_resource_id="[null]"/>
+  <snapshots id="1" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]"
+               status="P" islast="[false]" purge_status="[null]"
+               period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+               period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+               period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+               period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+               period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+               depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+               version="[null]" path="[null]"/>
+
+  <!-- project -->
+  <projects id="2" root_id="1" kee="org.struts:struts-core" name="Struts Core"
+            scope="PRJ" qualifier="BRC" long_name="Struts Core"
+            description="[null]" enabled="[true]" language="java" copy_resource_id="[null]"/>
+  <snapshots id="2" project_id="2" parent_snapshot_id="1" root_project_id="1" root_snapshot_id="1"
+               status="P" islast="[false]" purge_status="[null]"
+               period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+               period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+               period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+               period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+               period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+               depth="[null]" scope="PRJ" qualifier="BRC" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+               version="[null]" path="[null]"/>
+
+  <!-- directory -->
+  <projects long_name="org.struts" id="3" scope="DIR" qualifier="PAC" kee="org.struts:struts:org.struts"
+              name="org.struts" root_id="1"
+              description="[null]"
+              enabled="[true]" language="java" copy_resource_id="[null]"/>
+  <snapshots id="3" project_id="3" parent_snapshot_id="2" root_project_id="1" root_snapshot_id="1"
+               status="P" islast="[false]" purge_status="[null]"
+               period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+               period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+               period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+               period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+               period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+               depth="[null]" scope="DIR" qualifier="PAC" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+               version="[null]" path="[null]"/>
+
+  <!-- file -->
+  <projects long_name="org.struts.RequestContext" id="4" scope="FIL" qualifier="CLA" kee="org.struts:struts:org.struts.RequestContext"
+            name="RequestContext" root_id="1"
+            description="[null]"
+            enabled="[true]" language="java" copy_resource_id="[null]"/>
+
+  <snapshots id="4" project_id="4" parent_snapshot_id="3" root_project_id="1" root_snapshot_id="1"
+               status="P" islast="[false]" purge_status="[null]"
+               period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+               period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+               period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+               period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+               period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+               depth="[null]" scope="FIL" qualifier="CLA" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+               version="[null]" path="[null]"/>
+</dataset>