diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2018-04-25 17:18:39 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-05-09 20:20:46 +0200 |
commit | 9f372f97c4e7419d84322f1c23c9a3e1df0d5967 (patch) | |
tree | 09f0bf1dafbac3204694eec985fa189d7c97e423 /sonar-core | |
parent | 9aa90ae28dc7ce6c22c6e51c6d847c848a30c949 (diff) | |
download | sonarqube-9f372f97c4e7419d84322f1c23c9a3e1df0d5967.tar.gz sonarqube-9f372f97c4e7419d84322f1c23c9a3e1df0d5967.zip |
SONAR-10647 Compare lines of code taking into account significant code
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/hash/LineRange.java | 41 | ||||
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/hash/SourceLineHashesComputer.java (renamed from sonar-core/src/main/java/org/sonar/core/hash/SourceLinesHashesComputer.java) | 6 | ||||
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/issue/tracking/LineHashSequence.java | 4 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java | 45 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java | 6 |
5 files changed, 94 insertions, 8 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/hash/LineRange.java b/sonar-core/src/main/java/org/sonar/core/hash/LineRange.java new file mode 100644 index 00000000000..7eb1592a16d --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/hash/LineRange.java @@ -0,0 +1,41 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.core.hash; + +import com.google.common.base.Preconditions; + +public class LineRange { + private final int startOffset; + private final int endOffset; + + public LineRange(int startOffset, int endOffset) { + Preconditions.checkArgument(startOffset <= endOffset, "Line range is not valid: %s must be greater or equal than %s", endOffset, startOffset); + this.startOffset = startOffset; + this.endOffset = endOffset; + } + + public int startOffset() { + return startOffset; + } + + public int endOffset() { + return endOffset; + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/hash/SourceLinesHashesComputer.java b/sonar-core/src/main/java/org/sonar/core/hash/SourceLineHashesComputer.java index b64413f0687..df4a586b6a6 100644 --- a/sonar-core/src/main/java/org/sonar/core/hash/SourceLinesHashesComputer.java +++ b/sonar-core/src/main/java/org/sonar/core/hash/SourceLineHashesComputer.java @@ -34,15 +34,15 @@ import static java.util.Objects.requireNonNull; * Computes the hash of each line of a given file by simply added lines of that file one by one in order with * {@link #addLine(String)}. */ -public class SourceLinesHashesComputer { +public class SourceLineHashesComputer { private final MessageDigest md5Digest = DigestUtils.getMd5Digest(); private final List<String> lineHashes; - public SourceLinesHashesComputer() { + public SourceLineHashesComputer() { this.lineHashes = new ArrayList<>(); } - public SourceLinesHashesComputer(int expectedLineCount) { + public SourceLineHashesComputer(int expectedLineCount) { this.lineHashes = new ArrayList<>(expectedLineCount); } diff --git a/sonar-core/src/main/java/org/sonar/core/issue/tracking/LineHashSequence.java b/sonar-core/src/main/java/org/sonar/core/issue/tracking/LineHashSequence.java index e7d9a931019..94aff1000b5 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/tracking/LineHashSequence.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/tracking/LineHashSequence.java @@ -26,7 +26,7 @@ import com.google.common.base.Strings; import java.util.List; import java.util.Set; -import org.sonar.core.hash.SourceLinesHashesComputer; +import org.sonar.core.hash.SourceLineHashesComputer; /** * Sequence of hash of lines for a given file @@ -88,7 +88,7 @@ public class LineHashSequence { } public static LineHashSequence createForLines(List<String> lines) { - SourceLinesHashesComputer hashesComputer = new SourceLinesHashesComputer(lines.size()); + SourceLineHashesComputer hashesComputer = new SourceLineHashesComputer(lines.size()); for (String line : lines) { hashesComputer.addLine(line); } diff --git a/sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java b/sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java new file mode 100644 index 00000000000..07cc5fe6bdd --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java @@ -0,0 +1,45 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.core.hash; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LineRangeTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void should_throw_ISE_if_range_is_invalid() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Line range is not valid: 1 must be greater or equal than 2"); + new LineRange(2, 1); + } + + @Test + public void check_getters() { + LineRange range = new LineRange(1, 2); + assertThat(range.startOffset()).isEqualTo(1); + assertThat(range.endOffset()).isEqualTo(2); + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java b/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java index 967e5638a9c..ee07becc43d 100644 --- a/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java @@ -37,7 +37,7 @@ public class SourceLinesHashesComputerTest { expectedException.expect(NullPointerException.class); expectedException.expectMessage("line can not be null"); - new SourceLinesHashesComputer(1).addLine(null); + new SourceLineHashesComputer(1).addLine(null); } @Test @@ -70,7 +70,7 @@ public class SourceLinesHashesComputerTest { String line2 = "line 1 + 1"; String line3 = "line 10 - 7"; - SourceLinesHashesComputer underTest = new SourceLinesHashesComputer(); + SourceLineHashesComputer underTest = new SourceLineHashesComputer(); underTest.addLine(line1); underTest.addLine(line2); underTest.addLine(line3); @@ -80,7 +80,7 @@ public class SourceLinesHashesComputerTest { } private static String hashSingleLine(@Nullable String line) { - SourceLinesHashesComputer sourceLinesHashesComputer = new SourceLinesHashesComputer(1); + SourceLineHashesComputer sourceLinesHashesComputer = new SourceLineHashesComputer(1); sourceLinesHashesComputer.addLine(line); return sourceLinesHashesComputer.getLineHashes().iterator().next(); } |