From 7f9b292fed77d92db09a38b90b1cc7d0de5ec178 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 8 Sep 2016 16:59:02 +0200 Subject: [PATCH] fix some quality flaws and lacking coverage --- .../task/projectanalysis/filemove/Match.java | 2 +- .../filemove/SourceSimilarityImpl.java | 4 - .../projectanalysis/filemove/MatchTest.java | 78 +++++++++++++++++++ ...uidAndAnalysisUuidOfDuplicationsIndex.java | 4 +- ...pulateComponentUuidColumnsOfSnapshots.java | 2 +- 5 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/filemove/MatchTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/filemove/Match.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/filemove/Match.java index 6f0bc2c9726..105fac3eed6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/filemove/Match.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/filemove/Match.java @@ -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; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/filemove/SourceSimilarityImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/filemove/SourceSimilarityImpl.java index b3b37b7d9ce..a315fbbb397 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/filemove/SourceSimilarityImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/filemove/SourceSimilarityImpl.java @@ -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 int score(List left, List 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 int levenshteinDistance(List left, List 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 index 00000000000..b9bec9bd0ca --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/filemove/MatchTest.java @@ -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}"); + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidAndAnalysisUuidOfDuplicationsIndex.java b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidAndAnalysisUuidOfDuplicationsIndex.java index 00b20e211dc..bf8924cf8c7 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidAndAnalysisUuidOfDuplicationsIndex.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidAndAnalysisUuidOfDuplicationsIndex.java @@ -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" + diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidColumnsOfSnapshots.java b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidColumnsOfSnapshots.java index 991a7cac183..59c5b3b4364 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidColumnsOfSnapshots.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidColumnsOfSnapshots.java @@ -55,7 +55,7 @@ public class PopulateComponentUuidColumnsOfSnapshots extends BaseDataChange { return componentUuidById; } - private void populateUuidColumns(Context context, Map componentUuidById) throws SQLException { + private static void populateUuidColumns(Context context, Map 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=?"); -- 2.39.5