]> source.dussan.org Git - sonarqube.git/commitdiff
fix some quality flaws and lacking coverage 1205/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 8 Sep 2016 14:59:02 +0000 (16:59 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 9 Sep 2016 07:11:42 +0000 (09:11 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/filemove/Match.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/filemove/SourceSimilarityImpl.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/filemove/MatchTest.java [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidAndAnalysisUuidOfDuplicationsIndex.java
sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidColumnsOfSnapshots.java

index 6f0bc2c97264bd56df0dadcf297c12be5d9a268c..105fac3eed6dc9f5ad464f7edba365e3809357bf 100644 (file)
@@ -28,7 +28,7 @@ final class Match {
   private final String dbKey;
   private final String reportKey;
 
-  public Match(String dbKey, String reportKey) {
+  Match(String dbKey, String reportKey) {
     this.dbKey = dbKey;
     this.reportKey = reportKey;
   }
index b3b37b7d9ce60c375534a1679757ee16938ad08c..a315fbbb397360a837faa1e81ba6c96e17822c04 100644 (file)
@@ -24,18 +24,14 @@ import java.util.List;
 import static java.lang.Math.max;
 import static java.lang.Math.min;
 
-// TODO evaluate https://commons.apache.org/sandbox/commons-text/jacoco/org.apache.commons.text.similarity/CosineSimilarity.java.html
-// TODO another possible algorithm : just take the intersection of line hashes. That would allow to support block moving.
 public class SourceSimilarityImpl implements SourceSimilarity {
 
-  // TODO verify algorithm http://stackoverflow.com/questions/6087281/similarity-score-levenshtein
   @Override
   public <T extends Object> int score(List<T> left, List<T> right) {
     int distance = levenshteinDistance(left, right);
     return (int) (100 * (1.0 - ((double) distance) / (max(left.size(), right.size()))));
   }
 
-  // TODO verify https://commons.apache.org/sandbox/commons-text/jacoco/org.apache.commons.text.similarity/LevenshteinDistance.java.html
   <T extends Object> int levenshteinDistance(List<T> left, List<T> right) {
     int len0 = left.size() + 1;
     int len1 = right.size() + 1;
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/filemove/MatchTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/filemove/MatchTest.java
new file mode 100644 (file)
index 0000000..b9bec9b
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.computation.task.projectanalysis.filemove;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class MatchTest {
+
+  private static final String SOME_REPORT_KEY = "reportKey";
+  private static final String SOME_KEY = "key";
+
+  private Match underTest = new Match(SOME_KEY, SOME_REPORT_KEY);
+
+  @Test
+  public void constructor_key_argument_can_be_null() {
+    new Match(null, SOME_REPORT_KEY);
+  }
+
+  @Test
+  public void constructor_reportKey_argument_can_be_null() {
+    new Match(SOME_KEY, null);
+  }
+
+  @Test
+  public void getDbKey_returns_first_constructor_argument() {
+    assertThat(underTest.getDbKey()).isEqualTo(SOME_KEY);
+  }
+
+  @Test
+  public void getDbKey_returns_second_constructor_argument() {
+    assertThat(underTest.getReportKey()).isEqualTo(SOME_REPORT_KEY);
+  }
+
+  @Test
+  public void equals_is_based_on_both_properties() {
+    assertThat(underTest).isEqualTo(new Match(SOME_KEY, SOME_REPORT_KEY));
+    assertThat(underTest).isNotEqualTo(new Match("other key", SOME_REPORT_KEY));
+    assertThat(underTest).isNotEqualTo(new Match(SOME_KEY, "other report key"));
+    assertThat(underTest).isNotEqualTo(new Match(null, SOME_REPORT_KEY));
+    assertThat(underTest).isNotEqualTo(new Match(SOME_KEY, null));
+    assertThat(underTest).isNotEqualTo(new Match(null, null));
+  }
+
+  @Test
+  public void hashcode_is_base_on_both_properties() {
+    int hashCode = underTest.hashCode();
+    assertThat(hashCode).isEqualTo(new Match(SOME_KEY, SOME_REPORT_KEY).hashCode());
+    assertThat(hashCode).isNotEqualTo(new Match("other key", SOME_REPORT_KEY).hashCode());
+    assertThat(hashCode).isNotEqualTo(new Match(SOME_KEY, "other report key").hashCode());
+    assertThat(hashCode).isNotEqualTo(new Match(null, SOME_REPORT_KEY).hashCode());
+    assertThat(hashCode).isNotEqualTo(new Match(SOME_KEY, null).hashCode());
+    assertThat(hashCode).isNotEqualTo(new Match(null, null).hashCode());
+  }
+
+  @Test
+  public void toString_prints_both_properties() {
+    assertThat(underTest.toString()).isEqualTo("{key=>reportKey}");
+  }
+}
index 00b20e211dcf230501c77abced6851dfdddcf633..bf8924cf8c7f30e30dc529bf03ec77532f2ab868 100644 (file)
@@ -38,7 +38,7 @@ public class PopulateComponentUuidAndAnalysisUuidOfDuplicationsIndex extends Bas
     populateAnalysisUuid(context);
   }
 
-  private void populateComponentUuid(Context context) throws SQLException {
+  private static void populateComponentUuid(Context context) throws SQLException {
     MassUpdate massUpdate = context.prepareMassUpdate();
     massUpdate.select("select distinct di.snapshot_id, s.component_uuid from duplications_index di" +
       " inner join snapshots s on s.id=di.snapshot_id" +
@@ -58,7 +58,7 @@ public class PopulateComponentUuidAndAnalysisUuidOfDuplicationsIndex extends Bas
     return true;
   }
   
-  public static void populateAnalysisUuid(Context context) throws SQLException {
+  private static void populateAnalysisUuid(Context context) throws SQLException {
     MassUpdate massUpdate = context.prepareMassUpdate();
     massUpdate.select("select distinct di.project_snapshot_id, s.uuid from duplications_index di" +
         " inner join snapshots s on s.id=di.project_snapshot_id" +
index 991a7cac183c489c8da3a3c5f492a0f3915d9a36..59c5b3b43643de663f42df9ccff04e15bc441ba1 100644 (file)
@@ -55,7 +55,7 @@ public class PopulateComponentUuidColumnsOfSnapshots extends BaseDataChange {
     return componentUuidById;
   }
 
-  private void populateUuidColumns(Context context, Map<Long, String> componentUuidById) throws SQLException {
+  private static void populateUuidColumns(Context context, Map<Long, String> componentUuidById) throws SQLException {
     MassUpdate massUpdate = context.prepareMassUpdate();
     massUpdate.select("SELECT sn.id, sn.project_id, sn.root_project_id from snapshots sn where sn.component_uuid is null or sn.root_component_uuid is null");
     massUpdate.update("UPDATE snapshots SET component_uuid=?, root_component_uuid=? WHERE id=?");