From 44094c161d794bf3d107f6e0d3b5fc71df72b5ac Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Thu, 15 Mar 2012 21:44:29 +0400 Subject: [PATCH] SONAR-3072 sonar-diff module is useless for the moment, so remove it Move all code directly into sonar-core-plugin. --- plugins/sonar-core-plugin/pom.xml | 5 - .../ViolationTrackingBlocksRecognizer.java | 8 +- .../timemachine/tracking}/HashedSequence.java | 2 +- .../tracking}/HashedSequenceComparator.java | 4 +- .../core/timemachine/tracking}/Sequence.java | 2 +- .../tracking}/SequenceComparator.java | 8 +- .../timemachine/tracking}/StringText.java | 2 +- .../tracking}/StringTextComparator.java | 4 +- .../timemachine/tracking}/package-info.java | 2 +- .../tracking/StringTextComparatorTest.java | 25 +++ .../timemachine/tracking/StringTextTest.java | 22 +++ pom.xml | 1 - sonar-batch/pom.xml | 5 - sonar-diff/pom.xml | 35 ---- .../main/java/org/sonar/diff/CodeChurn.java | 95 ---------- .../java/org/sonar/diff/DiffAlgorithm.java | 112 ------------ .../src/main/java/org/sonar/diff/Edit.java | 73 -------- .../src/main/java/org/sonar/diff/Text.java | 87 --------- .../java/org/sonar/diff/TextComparator.java | 141 --------------- .../org/sonar/diff/DiffFunctionalTest.java | 58 ------ .../test/java/org/sonar/diff/DiffTest.java | 171 ------------------ .../org/sonar/diff/TextComparatorTest.java | 57 ------ .../test/java/org/sonar/diff/TextTest.java | 41 ----- .../test/resources/examples/example0/v1.java | 10 - .../test/resources/examples/example0/v2.java | 20 -- .../test/resources/examples/example1/v1.java | 5 - .../test/resources/examples/example1/v2.java | 7 - 27 files changed, 63 insertions(+), 939 deletions(-) rename {sonar-diff/src/main/java/org/sonar/diff => plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking}/HashedSequence.java (95%) rename {sonar-diff/src/main/java/org/sonar/diff => plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking}/HashedSequenceComparator.java (89%) rename {sonar-diff/src/main/java/org/sonar/diff => plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking}/Sequence.java (94%) rename {sonar-diff/src/main/java/org/sonar/diff => plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking}/SequenceComparator.java (87%) rename {sonar-diff/src/main/java/org/sonar/diff => plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking}/StringText.java (97%) rename {sonar-diff/src/main/java/org/sonar/diff => plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking}/StringTextComparator.java (95%) rename {sonar-diff/src/main/java/org/sonar/diff => plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking}/package-info.java (94%) create mode 100644 plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparatorTest.java create mode 100644 plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextTest.java delete mode 100644 sonar-diff/pom.xml delete mode 100644 sonar-diff/src/main/java/org/sonar/diff/CodeChurn.java delete mode 100644 sonar-diff/src/main/java/org/sonar/diff/DiffAlgorithm.java delete mode 100644 sonar-diff/src/main/java/org/sonar/diff/Edit.java delete mode 100644 sonar-diff/src/main/java/org/sonar/diff/Text.java delete mode 100644 sonar-diff/src/main/java/org/sonar/diff/TextComparator.java delete mode 100644 sonar-diff/src/test/java/org/sonar/diff/DiffFunctionalTest.java delete mode 100644 sonar-diff/src/test/java/org/sonar/diff/DiffTest.java delete mode 100644 sonar-diff/src/test/java/org/sonar/diff/TextComparatorTest.java delete mode 100644 sonar-diff/src/test/java/org/sonar/diff/TextTest.java delete mode 100644 sonar-diff/src/test/resources/examples/example0/v1.java delete mode 100644 sonar-diff/src/test/resources/examples/example0/v2.java delete mode 100644 sonar-diff/src/test/resources/examples/example1/v1.java delete mode 100644 sonar-diff/src/test/resources/examples/example1/v2.java diff --git a/plugins/sonar-core-plugin/pom.xml b/plugins/sonar-core-plugin/pom.xml index 987bb0e93a8..88d2101ab66 100644 --- a/plugins/sonar-core-plugin/pom.xml +++ b/plugins/sonar-core-plugin/pom.xml @@ -13,11 +13,6 @@ Sonar :: Plugins :: Core - - org.codehaus.sonar - sonar-diff - ${project.version} - org.codehaus.sonar sonar-plugin-api diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingBlocksRecognizer.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingBlocksRecognizer.java index 0ba647693d9..7193c6b940d 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingBlocksRecognizer.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingBlocksRecognizer.java @@ -19,10 +19,10 @@ */ package org.sonar.plugins.core.timemachine; -import org.sonar.diff.HashedSequence; -import org.sonar.diff.HashedSequenceComparator; -import org.sonar.diff.StringText; -import org.sonar.diff.StringTextComparator; +import org.sonar.plugins.core.timemachine.tracking.HashedSequence; +import org.sonar.plugins.core.timemachine.tracking.HashedSequenceComparator; +import org.sonar.plugins.core.timemachine.tracking.StringText; +import org.sonar.plugins.core.timemachine.tracking.StringTextComparator; public class ViolationTrackingBlocksRecognizer { diff --git a/sonar-diff/src/main/java/org/sonar/diff/HashedSequence.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequence.java similarity index 95% rename from sonar-diff/src/main/java/org/sonar/diff/HashedSequence.java rename to plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequence.java index 4cc1996120c..332d6d32456 100644 --- a/sonar-diff/src/main/java/org/sonar/diff/HashedSequence.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequence.java @@ -17,7 +17,7 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.diff; +package org.sonar.plugins.core.timemachine.tracking; /** * Wraps a {@link Sequence} to assign hash codes to elements. diff --git a/sonar-diff/src/main/java/org/sonar/diff/HashedSequenceComparator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequenceComparator.java similarity index 89% rename from sonar-diff/src/main/java/org/sonar/diff/HashedSequenceComparator.java rename to plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequenceComparator.java index c84cd1323bd..f7a88a4596e 100644 --- a/sonar-diff/src/main/java/org/sonar/diff/HashedSequenceComparator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequenceComparator.java @@ -17,12 +17,12 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.diff; +package org.sonar.plugins.core.timemachine.tracking; /** * Wrap another {@link SequenceComparator} for use with {@link HashedSequence}. */ -public class HashedSequenceComparator extends SequenceComparator> { +public class HashedSequenceComparator implements SequenceComparator> { private final SequenceComparator cmp; diff --git a/sonar-diff/src/main/java/org/sonar/diff/Sequence.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/Sequence.java similarity index 94% rename from sonar-diff/src/main/java/org/sonar/diff/Sequence.java rename to plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/Sequence.java index 54a793d273f..28a4277b265 100644 --- a/sonar-diff/src/main/java/org/sonar/diff/Sequence.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/Sequence.java @@ -17,7 +17,7 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.diff; +package org.sonar.plugins.core.timemachine.tracking; /** * Arbitrary sequence of elements. diff --git a/sonar-diff/src/main/java/org/sonar/diff/SequenceComparator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/SequenceComparator.java similarity index 87% rename from sonar-diff/src/main/java/org/sonar/diff/SequenceComparator.java rename to plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/SequenceComparator.java index 6c099c98535..4bf73dd93ba 100644 --- a/sonar-diff/src/main/java/org/sonar/diff/SequenceComparator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/SequenceComparator.java @@ -17,17 +17,17 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.diff; +package org.sonar.plugins.core.timemachine.tracking; /** * Equivalence function for a {@link Sequence}. */ -public abstract class SequenceComparator { +public interface SequenceComparator { /** * Compare two items to determine if they are equivalent. */ - public abstract boolean equals(S a, int ai, S b, int bi); + boolean equals(S a, int ai, S b, int bi); /** * Get a hash value for an item in a sequence. @@ -37,6 +37,6 @@ public abstract class SequenceComparator { * then this hash method must produce the same integer result for both items. * However not required to have different hash values for different items. */ - public abstract int hash(S seq, int i); + int hash(S seq, int i); } diff --git a/sonar-diff/src/main/java/org/sonar/diff/StringText.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringText.java similarity index 97% rename from sonar-diff/src/main/java/org/sonar/diff/StringText.java rename to plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringText.java index e529f118677..756c6e6fce6 100644 --- a/sonar-diff/src/main/java/org/sonar/diff/StringText.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringText.java @@ -17,7 +17,7 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.diff; +package org.sonar.plugins.core.timemachine.tracking; import com.google.common.collect.Lists; diff --git a/sonar-diff/src/main/java/org/sonar/diff/StringTextComparator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparator.java similarity index 95% rename from sonar-diff/src/main/java/org/sonar/diff/StringTextComparator.java rename to plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparator.java index ba6e9c7ce6f..3c4430add31 100644 --- a/sonar-diff/src/main/java/org/sonar/diff/StringTextComparator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparator.java @@ -17,12 +17,12 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.diff; +package org.sonar.plugins.core.timemachine.tracking; /** * Equivalence function for {@link StringText}. */ -public abstract class StringTextComparator extends SequenceComparator { +public abstract class StringTextComparator implements SequenceComparator { /** * Ignores all whitespace. diff --git a/sonar-diff/src/main/java/org/sonar/diff/package-info.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/package-info.java similarity index 94% rename from sonar-diff/src/main/java/org/sonar/diff/package-info.java rename to plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/package-info.java index b67e363ec6f..344eb1dd688 100644 --- a/sonar-diff/src/main/java/org/sonar/diff/package-info.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/package-info.java @@ -19,7 +19,7 @@ */ @ParametersAreNonnullByDefault -package org.sonar.diff; +package org.sonar.plugins.core.timemachine.tracking; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparatorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparatorTest.java new file mode 100644 index 00000000000..5d437a3e360 --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparatorTest.java @@ -0,0 +1,25 @@ +package org.sonar.plugins.core.timemachine.tracking; + +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class StringTextComparatorTest { + + @Test + public void testEquals() { + StringTextComparator cmp = StringTextComparator.IGNORE_WHITESPACE; + + StringText a = new StringText("abc\nabc\na bc"); + StringText b = new StringText("abc\nabc d\nab c"); + + assertThat("abc == abc", cmp.equals(a, 0, b, 0), is(true)); + assertThat("abc != abc d", cmp.equals(a, 1, b, 1), is(false)); + assertThat("a bc == ab c", cmp.equals(a, 2, b, 2), is(true)); + assertThat(cmp.hash(a, 0), equalTo(cmp.hash(b, 0))); + assertThat(cmp.hash(a, 2), equalTo(cmp.hash(b, 2))); + } + +} diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextTest.java new file mode 100644 index 00000000000..15898084af1 --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextTest.java @@ -0,0 +1,22 @@ +package org.sonar.plugins.core.timemachine.tracking; + +import org.junit.Test; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class StringTextTest { + + @Test + public void testEmpty() { + StringText r = new StringText(""); + assertThat(r.length(), is(0)); + } + + @Test + public void testTwoLines() { + StringText r = new StringText("a\nb"); + assertThat(r.length(), is(2)); + } + +} diff --git a/pom.xml b/pom.xml index b0b502c3a2e..da37f3e975a 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,6 @@ sonar-colorizer sonar-core sonar-deprecated - sonar-diff sonar-duplications sonar-graph sonar-gwt-api diff --git a/sonar-batch/pom.xml b/sonar-batch/pom.xml index 4d16d123833..d807eb16c55 100644 --- a/sonar-batch/pom.xml +++ b/sonar-batch/pom.xml @@ -17,11 +17,6 @@ org.codehaus.sonar sonar-core - - org.codehaus.sonar - sonar-diff - ${project.version} - org.codehaus.sonar sonar-deprecated diff --git a/sonar-diff/pom.xml b/sonar-diff/pom.xml deleted file mode 100644 index 01eae44f624..00000000000 --- a/sonar-diff/pom.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - 4.0.0 - - - org.codehaus.sonar - sonar - 2.15-SNAPSHOT - - - sonar-diff - - Sonar :: Diff - Detect changes in files - - - - com.google.guava - guava - - - - org.hamcrest - hamcrest-all - test - - - junit - junit - test - - - - diff --git a/sonar-diff/src/main/java/org/sonar/diff/CodeChurn.java b/sonar-diff/src/main/java/org/sonar/diff/CodeChurn.java deleted file mode 100644 index 11e6e9bcac1..00000000000 --- a/sonar-diff/src/main/java/org/sonar/diff/CodeChurn.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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.diff; - -import com.google.common.io.Files; - -import java.io.File; -import java.util.BitSet; -import java.util.List; - -public final class CodeChurn { - - public static void main(String[] args) { - if (args.length != 2) { - System.err.println("2 arguments required"); - System.exit(1); - } - try { - Text a = new Text(Files.toByteArray(new File(args[0]))); - Text b = new Text(Files.toByteArray(new File(args[1]))); - CodeChurn r = new CodeChurn(a, b, TextComparator.IGNORE_WHITESPACE); - System.out.println("Deleted: " + r.getDeleted()); - System.out.println("Added: " + r.getAdded()); - for (Edit edit : r.getDiff()) { - System.out.println(edit); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - public CodeChurn(Text a, Text b, TextComparator cmp) { - diff = new DiffAlgorithm().diff(a, b, cmp); - - int added = 0; - int deleted = 0; - BitSet remains = new BitSet(a.length()); - for (Edit edit : diff) { - switch (edit.getType()) { - case INSERT: - added += edit.endB - edit.beginB + 1; - break; - case MOVE: - for (int i = edit.beginA; i <= edit.endA; i++) { - remains.set(i); - } - break; - default: - throw new IllegalStateException(); - } - } - for (int i = 0; i < a.length(); i++) { - if (!remains.get(i)) { - deleted++; - } - } - - this.added = added; - this.deleted = deleted; - } - - private final List diff; - private final int added; - private final int deleted; - - public int getAdded() { - return added; - } - - public int getDeleted() { - return deleted; - } - - public List getDiff() { - return diff; - } - -} diff --git a/sonar-diff/src/main/java/org/sonar/diff/DiffAlgorithm.java b/sonar-diff/src/main/java/org/sonar/diff/DiffAlgorithm.java deleted file mode 100644 index 74e59612de5..00000000000 --- a/sonar-diff/src/main/java/org/sonar/diff/DiffAlgorithm.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.diff; - -import com.google.common.collect.Lists; -import com.google.common.io.Files; - -import java.io.File; -import java.util.List; - -/** - * Diff algorithm, based on - * "The String-to-String Correction Problem with Block Moves", by Waller F. Tichy. - */ -public class DiffAlgorithm { - - public static void main(String[] args) { - if (args.length != 2) { - System.err.println("2 arguments required"); - System.exit(1); - } - try { - Text a = new Text(Files.toByteArray(new File(args[0]))); - Text b = new Text(Files.toByteArray(new File(args[1]))); - List r = new DiffAlgorithm().diff(a, b, TextComparator.IGNORE_WHITESPACE); - for (Edit edit : r) { - System.out.println(edit); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - public List diff(Text a, Text b, TextComparator cmp) { - return diff(wrap(a, cmp), wrap(b, cmp), cmp); - } - - private static HashedSequence wrap(Text seq, TextComparator cmp) { - int size = seq.length(); - int[] hashes = new int[size]; - for (int i = 0; i < size; i++) { - hashes[i] = cmp.hash(seq, i); - } - return new HashedSequence(seq, hashes); - } - - private List diff(HashedSequence s, HashedSequence t, TextComparator cmp) { - HashedSequenceComparator comparator = new HashedSequenceComparator(cmp); - Edit lastEdit = null; - List r = Lists.newArrayList(); - - int m = s.length(); - int n = t.length(); - - int q = 0; - while (q < n) { - // find p and l such that (p,q,l) is a maximal block move - int l = 0; - int p = 0; - int pCur = 0; - - while ((pCur + l < m) && (q + l < n)) { - int lCur = 0; - while ((pCur + lCur < m) && (q + lCur < n) - && (comparator.equals(s, pCur + lCur, t, q + lCur))) { - lCur++; - } - if (lCur > l) { - l = lCur; - p = pCur; - } - pCur++; - } - - if (l > 0) { - Edit edit = new Edit(Edit.Type.MOVE, p, p + l - 1, q, q + l - 1); - r.add(edit); - lastEdit = edit; - q += l; - } else { - if (lastEdit == null || lastEdit.getType() != Edit.Type.INSERT) { - Edit edit = new Edit(Edit.Type.INSERT, -1, -1, q, q); - r.add(edit); - lastEdit = edit; - } else { - lastEdit.endB++; - } - q++; - } - } - - return r; - } - -} diff --git a/sonar-diff/src/main/java/org/sonar/diff/Edit.java b/sonar-diff/src/main/java/org/sonar/diff/Edit.java deleted file mode 100644 index 946cb49c5be..00000000000 --- a/sonar-diff/src/main/java/org/sonar/diff/Edit.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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.diff; - -import com.google.common.base.Objects; - -public class Edit { - - public static enum Type { - INSERT, - MOVE - } - - int beginA; - int endA; - int beginB; - int endB; - final Type type; - - public Edit(Type type, int beginA, int endA, int beginB, int endB) { - this.beginA = beginA; - this.endA = endA; - this.beginB = beginB; - this.endB = endB; - this.type = type; - } - - public Type getType() { - return type; - } - - @Override - public boolean equals(Object obj) { - if (obj == null || !(obj instanceof Edit)) { - return false; - } - Edit e = (Edit) obj; - return type == e.type - && beginA == e.beginA - && endA == e.endA - && beginB == e.beginB - && endB == e.endB; - } - - @Override - public String toString() { - return Objects.toStringHelper(this) - .add("type", type) - .add("beginA", beginA) - .add("endA", endA) - .add("beginB", beginB) - .add("endB", endB) - .toString(); - } - -} diff --git a/sonar-diff/src/main/java/org/sonar/diff/Text.java b/sonar-diff/src/main/java/org/sonar/diff/Text.java deleted file mode 100644 index f9c55576c72..00000000000 --- a/sonar-diff/src/main/java/org/sonar/diff/Text.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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.diff; - -import com.google.common.collect.Lists; - -import java.util.List; - -/** - * Text is a {@link Sequence} of lines. - */ -public class Text implements Sequence { - - final byte[] content; - - /** - * Map of line number to starting position within {@link #content}. - */ - final List lines; - - public Text(byte[] bytes) { - this.content = bytes; - lines = lineMap(content, 0, content.length); - } - - public int length() { - return lines.size() - 2; - } - - /** - * Get the text for a single line. - */ - public String getString(int line) { - int s = getStart(line); - int e = getEnd(line); - return new String(content, s, e - s); - } - - private int getStart(final int line) { - return lines.get(line + 1); - } - - private int getEnd(final int line) { - return lines.get(line + 2); - } - - private static List lineMap(final byte[] buf, int ptr, int end) { - List lines = Lists.newArrayList(); - lines.add(Integer.MIN_VALUE); - for (; ptr < end; ptr = nextLF(buf, ptr)) { - lines.add(ptr); - } - lines.add(end); - return lines; - } - - private static final int nextLF(final byte[] b, int ptr) { - return next(b, ptr, '\n'); - } - - private static final int next(final byte[] b, int ptr, final char chrA) { - final int sz = b.length; - while (ptr < sz) { - if (b[ptr++] == chrA) - return ptr; - } - return ptr; - } - -} diff --git a/sonar-diff/src/main/java/org/sonar/diff/TextComparator.java b/sonar-diff/src/main/java/org/sonar/diff/TextComparator.java deleted file mode 100644 index 00c02874327..00000000000 --- a/sonar-diff/src/main/java/org/sonar/diff/TextComparator.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * 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.diff; - -/** - * Equivalence function for {@link Text}. - */ -public abstract class TextComparator extends SequenceComparator { - - public static final TextComparator DEFAULT = new TextComparator() { - @Override - public boolean equals(Text a, int ai, Text b, int bi) { - ai++; - bi++; - int as = a.lines.get(ai); - int bs = b.lines.get(bi); - int ae = a.lines.get(ai + 1); - int be = b.lines.get(bi + 1); - if (ae - as != be - bs) { - return false; - } - while (as < ae) { - if (a.content[as++] != b.content[bs++]) { - return false; - } - } - return true; - } - - @Override - protected int hashRegion(final byte[] raw, int start, final int end) { - int hash = 5381; - for (; start < end; start++) { - hash = ((hash << 5) + hash) + (raw[start] & 0xff); - } - return hash; - } - }; - - /** - * Ignores all whitespace. - */ - public static final TextComparator IGNORE_WHITESPACE = new TextComparator() { - @Override - public boolean equals(Text a, int ai, Text b, int bi) { - ai++; - bi++; - int as = a.lines.get(ai); - int bs = b.lines.get(bi); - int ae = a.lines.get(ai + 1); - int be = b.lines.get(bi + 1); - ae = trimTrailingWhitespace(a.content, as, ae); - be = trimTrailingWhitespace(b.content, bs, be); - while ((as < ae) && (bs < be)) { - byte ac = a.content[as]; - byte bc = b.content[bs]; - while ((as < ae - 1) && (isWhitespace(ac))) { - as++; - ac = a.content[as]; - } - while ((bs < be - 1) && (isWhitespace(bc))) { - bs++; - bc = b.content[bs]; - } - if (ac != bc) { - return false; - } - as++; - bs++; - } - return (as == ae) && (bs == be); - } - - @Override - protected int hashRegion(byte[] raw, int start, int end) { - int hash = 5381; - for (; start < end; start++) { - byte c = raw[start]; - if (!isWhitespace(c)) { - hash = ((hash << 5) + hash) + (c & 0xff); - } - } - return hash; - } - }; - - @Override - public int hash(Text seq, int line) { - final int begin = seq.lines.get(line + 1); - final int end = seq.lines.get(line + 2); - return hashRegion(seq.content, begin, end); - } - - protected abstract int hashRegion(final byte[] raw, int start, final int end); - - private static final boolean[] WHITESPACE = new boolean[256]; - - static { - WHITESPACE['\r'] = true; - WHITESPACE['\n'] = true; - WHITESPACE['\t'] = true; - WHITESPACE[' '] = true; - } - - public static boolean isWhitespace(byte c) { - return WHITESPACE[c & 0xff]; - } - - public static int trimTrailingWhitespace(byte[] raw, int start, int end) { - end--; - while (start <= end && isWhitespace(raw[end])) { - end--; - } - return end + 1; - } - - public static int trimLeadingWhitespace(byte[] raw, int start, int end) { - while (start < end && isWhitespace(raw[start])) { - start++; - } - return start; - } - -} diff --git a/sonar-diff/src/test/java/org/sonar/diff/DiffFunctionalTest.java b/sonar-diff/src/test/java/org/sonar/diff/DiffFunctionalTest.java deleted file mode 100644 index 03cb890da4e..00000000000 --- a/sonar-diff/src/test/java/org/sonar/diff/DiffFunctionalTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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.diff; - -import com.google.common.io.Resources; -import org.junit.Test; - -import java.io.IOException; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -public class DiffFunctionalTest { - - @Test - public void example0() throws Exception { - CodeChurn r = diff("example0"); - assertThat(r.getAdded(), is(5)); - assertThat(r.getDeleted(), is(0)); - assertThat(r.getDiff().size(), is(8)); - } - - @Test - public void example1() throws Exception { - CodeChurn r = diff("example1"); - assertThat(r.getAdded(), is(2)); - assertThat(r.getDeleted(), is(1)); - assertThat(r.getDiff().size(), is(4)); - } - - private CodeChurn diff(String name) throws IOException { - return diff("examples/" + name + "/v1.java", "examples/" + name + "/v2.java"); - } - - private CodeChurn diff(String resourceA, String resourceB) throws IOException { - Text a = new Text(Resources.toByteArray(Resources.getResource(resourceA))); - Text b = new Text(Resources.toByteArray(Resources.getResource(resourceB))); - return new CodeChurn(a, b, TextComparator.IGNORE_WHITESPACE); - } - -} diff --git a/sonar-diff/src/test/java/org/sonar/diff/DiffTest.java b/sonar-diff/src/test/java/org/sonar/diff/DiffTest.java deleted file mode 100644 index ee8c1601f56..00000000000 --- a/sonar-diff/src/test/java/org/sonar/diff/DiffTest.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * 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.diff; - -import org.junit.Test; -import org.sonar.diff.Edit.Type; - -import java.io.UnsupportedEncodingException; -import java.util.List; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -public class DiffTest { - - @Test - public void emptyInputs() { - List r = diff(t(""), t("")); - assertThat(r.isEmpty(), is(true)); - } - - @Test - public void createFile() { - List r = diff(t(""), t("AB")); - assertThat(r.size(), is(1)); - assertThat(r.get(0), is(new Edit(Type.INSERT, -1, -1, 0, 1))); - } - - @Test - public void deleteFile() { - List r = diff(t("AB"), t("")); - assertThat(r.size(), is(0)); - } - - @Test - public void insertMiddle() { - List r = diff(t("ac"), t("aBc")); - assertThat(r.size(), is(3)); - assertThat(r.get(0), is(new Edit(Type.MOVE, 0, 0, 0, 0))); - assertThat(r.get(1), is(new Edit(Type.INSERT, -1, -1, 1, 1))); - assertThat(r.get(2), is(new Edit(Type.MOVE, 1, 1, 2, 2))); - } - - @Test - public void deleteMiddle() { - List r = diff(t("aBc"), t("ac")); - assertThat(r.size(), is(2)); - assertThat(r.get(0), is(new Edit(Type.MOVE, 0, 0, 0, 0))); - assertThat(r.get(1), is(new Edit(Type.MOVE, 2, 2, 1, 1))); - } - - @Test - public void replaceMiddle() { - List r = diff(t("aBc"), t("aDc")); - assertThat(r.size(), is(3)); - assertThat(r.get(0), is(new Edit(Type.MOVE, 0, 0, 0, 0))); - assertThat(r.get(1), is(new Edit(Type.INSERT, -1, -1, 1, 1))); - assertThat(r.get(2), is(new Edit(Type.MOVE, 2, 2, 2, 2))); - } - - @Test - public void insertStart() { - List r = diff(t("bc"), t("Abc")); - assertThat(r.size(), is(2)); - assertThat(r.get(0), is(new Edit(Type.INSERT, -1, -1, 0, 0))); - assertThat(r.get(1), is(new Edit(Type.MOVE, 0, 1, 1, 2))); - } - - @Test - public void deleteStart() { - List r = diff(t("Abc"), t("bc")); - assertThat(r.size(), is(1)); - assertThat(r.get(0), is(new Edit(Type.MOVE, 1, 2, 0, 1))); - } - - @Test - public void insertEnd() { - List r = diff(t("ab"), t("abC")); - assertThat(r.size(), is(2)); - assertThat(r.get(0), is(new Edit(Type.MOVE, 0, 1, 0, 1))); - assertThat(r.get(1), is(new Edit(Type.INSERT, -1, -1, 2, 2))); - } - - @Test - public void deleteEnd() { - List r = diff(t("abC"), t("ab")); - assertThat(r.size(), is(1)); - assertThat(r.get(0), is(new Edit(Type.MOVE, 0, 1, 0, 1))); - } - - /** - * This is important special case, for which other algorithms can not detect movement. - */ - @Test - public void move() { - List r = diff(t("Abc"), t("bcA")); - assertThat(r.size(), is(2)); - assertThat(r.get(0), is(new Edit(Type.MOVE, 1, 2, 0, 1))); - assertThat(r.get(1), is(new Edit(Type.MOVE, 0, 0, 2, 2))); - } - - @Test - public void move2() { - List r = diff(t("abcd"), t("abcda")); - assertThat(r.size(), is(2)); - assertThat(r.get(0), is(new Edit(Type.MOVE, 0, 3, 0, 3))); - assertThat(r.get(1), is(new Edit(Type.MOVE, 0, 0, 4, 4))); - } - - @Test - public void move3() { - List r = diff(t("abcd"), t("bcdaa")); - assertThat(r.size(), is(3)); - assertThat(r.get(0), is(new Edit(Type.MOVE, 1, 3, 0, 2))); - assertThat(r.get(1), is(new Edit(Type.MOVE, 0, 0, 3, 3))); - assertThat(r.get(2), is(new Edit(Type.MOVE, 0, 0, 4, 4))); - } - - @Test - public void severalInserts() { - List r = diff(t("ac"), t("aBcD")); - assertThat(r.size(), is(4)); - assertThat(r.get(0), is(new Edit(Type.MOVE, 0, 0, 0, 0))); - assertThat(r.get(1), is(new Edit(Type.INSERT, -1, -1, 1, 1))); - assertThat(r.get(2), is(new Edit(Type.MOVE, 1, 1, 2, 2))); - assertThat(r.get(3), is(new Edit(Type.INSERT, -1, -1, 3, 3))); - } - - @Test - public void insertSeveralLines() { - List r = diff(t("ade"), t("aBCde")); - assertThat(r.size(), is(3)); - assertThat(r.get(0), is(new Edit(Type.MOVE, 0, 0, 0, 0))); - assertThat(r.get(1), is(new Edit(Type.INSERT, -1, -1, 1, 2))); - assertThat(r.get(2), is(new Edit(Type.MOVE, 1, 2, 3, 4))); - } - - private List diff(Text a, Text b) { - return new DiffAlgorithm().diff(a, b, TextComparator.DEFAULT); - } - - public static Text t(String text) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < text.length(); i++) { - sb.append(text.charAt(i)).append('\n'); - } - try { - return new Text(sb.toString().getBytes("UTF-8")); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - -} diff --git a/sonar-diff/src/test/java/org/sonar/diff/TextComparatorTest.java b/sonar-diff/src/test/java/org/sonar/diff/TextComparatorTest.java deleted file mode 100644 index 4082d0d05ce..00000000000 --- a/sonar-diff/src/test/java/org/sonar/diff/TextComparatorTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.diff; - -import org.junit.Test; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -public class TextComparatorTest { - - @Test - public void testEqualsWithoutWhitespace() { - TextComparator cmp = TextComparator.DEFAULT; - - Text a = new Text("abc\nabc\na bc".getBytes()); - Text b = new Text("abc\nabc d\nab c".getBytes()); - - assertThat("abc == abc", cmp.equals(a, 0, b, 0), is(true)); - assertThat("abc != abc d", cmp.equals(a, 1, b, 1), is(false)); - assertThat("a bc == ab c", cmp.equals(a, 2, b, 2), is(false)); - assertThat(cmp.hash(a, 0), equalTo(cmp.hash(b, 0))); - } - - @Test - public void testEqualsWithWhitespace() { - TextComparator cmp = TextComparator.IGNORE_WHITESPACE; - - Text a = new Text("abc\nabc\na bc".getBytes()); - Text b = new Text("abc\nabc d\nab c".getBytes()); - - assertThat("abc == abc", cmp.equals(a, 0, b, 0), is(true)); - assertThat("abc != abc d", cmp.equals(a, 1, b, 1), is(false)); - assertThat("a bc == ab c", cmp.equals(a, 2, b, 2), is(true)); - assertThat(cmp.hash(a, 0), equalTo(cmp.hash(b, 0))); - assertThat(cmp.hash(a, 2), equalTo(cmp.hash(b, 2))); - } - -} diff --git a/sonar-diff/src/test/java/org/sonar/diff/TextTest.java b/sonar-diff/src/test/java/org/sonar/diff/TextTest.java deleted file mode 100644 index f0c0132a1a0..00000000000 --- a/sonar-diff/src/test/java/org/sonar/diff/TextTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.diff; - -import org.junit.Test; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -public class TextTest { - - @Test - public void testEmpty() { - Text r = new Text(new byte[0]); - assertThat(r.length(), is(0)); - } - - @Test - public void testTwoLines() { - Text r = new Text("a\nb".getBytes()); - assertThat(r.length(), is(2)); - } - -} diff --git a/sonar-diff/src/test/resources/examples/example0/v1.java b/sonar-diff/src/test/resources/examples/example0/v1.java deleted file mode 100644 index ba040df7039..00000000000 --- a/sonar-diff/src/test/resources/examples/example0/v1.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Toto { - - public void doSomething() { - System.out.println("doSomething"); - } - - public void doSomethingElse() { - System.out.println("doSomethingElse"); - } -} diff --git a/sonar-diff/src/test/resources/examples/example0/v2.java b/sonar-diff/src/test/resources/examples/example0/v2.java deleted file mode 100644 index 9843ff7c69f..00000000000 --- a/sonar-diff/src/test/resources/examples/example0/v2.java +++ /dev/null @@ -1,20 +0,0 @@ -public class Toto { - - public Toto(){} - - public void doSomethingNew() { - System.out.println("doSomethingNew"); - } - - public void doSomethingElseNew() { - System.out.println("doSomethingElseNew"); - } - - public void doSomething() { - System.out.println("doSomething"); - } - - public void doSomethingElse() { - System.out.println("doSomethingElse"); - } -} diff --git a/sonar-diff/src/test/resources/examples/example1/v1.java b/sonar-diff/src/test/resources/examples/example1/v1.java deleted file mode 100644 index 82e00445d69..00000000000 --- a/sonar-diff/src/test/resources/examples/example1/v1.java +++ /dev/null @@ -1,5 +0,0 @@ -public class HelloWorld { - public void sayHello() { - System.out.println("Hello"); - } -} diff --git a/sonar-diff/src/test/resources/examples/example1/v2.java b/sonar-diff/src/test/resources/examples/example1/v2.java deleted file mode 100644 index 49ed45670fe..00000000000 --- a/sonar-diff/src/test/resources/examples/example1/v2.java +++ /dev/null @@ -1,7 +0,0 @@ -public class HelloWorld { - public void sayHello(int i) { - if (i > 0) { - System.out.println("Hello"); - } - } -} -- 2.39.5