diff --git a/.gitignore b/.gitignore index 7a6adf01..090344c0 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ target # Spoon spooned .settings +spoon.classpath.tmp diff --git a/pom.xml b/pom.xml index 7c3e4f0a..f0cec152 100644 --- a/pom.xml +++ b/pom.xml @@ -28,8 +28,6 @@ 1.9.0 - - 4.13.2 @@ -82,11 +80,24 @@ - junit - junit - ${junit.version} + org.junit.jupiter + junit-jupiter-api + 5.11.1 test + + org.junit.jupiter + junit-jupiter-params + 5.11.1 + test + + + org.hamcrest + hamcrest + 3.0 + test + + diff --git a/src/main/java/gumtree/spoon/AstComparator.java b/src/main/java/gumtree/spoon/AstComparator.java index 5194001c..e86da732 100644 --- a/src/main/java/gumtree/spoon/AstComparator.java +++ b/src/main/java/gumtree/spoon/AstComparator.java @@ -66,7 +66,7 @@ public Diff compare(File f1, File f2) throws Exception { return compare(ctPackage1, ctPackage2); } } - + /** * compares two AST nodes from two files according with a given configuration */ @@ -101,8 +101,8 @@ public Diff compare(String left, String right, DiffConfiguration configuration) return compare(getCtPackage(left, getFilename(left)), getCtPackage(right, getFilename(right)),configuration); } - - + + /** * compares two snippets that come from the files given as argument */ @@ -118,7 +118,7 @@ public Diff compare(CtElement left, CtElement right) { return new DiffImpl(scanner.getTreeContext(), scanner.getTree(left), scanner.getTree(right)); } - + /** * compares two AST nodes according with a given configuration */ @@ -126,7 +126,7 @@ public Diff compare(CtElement left, CtElement right, DiffConfiguration configura final SpoonGumTreeBuilder scanner = new SpoonGumTreeBuilder(); return new DiffImpl(scanner.getTreeContext(), scanner.getTree(left), scanner.getTree(right),configuration); } - + public CtType getCtType(File file) throws Exception { diff --git a/src/test/java/gumtree/spoon/AstComparatorTest.java b/src/test/java/gumtree/spoon/AstComparatorTest.java index d07387c3..0f25fe8c 100644 --- a/src/test/java/gumtree/spoon/AstComparatorTest.java +++ b/src/test/java/gumtree/spoon/AstComparatorTest.java @@ -1,63 +1,85 @@ package gumtree.spoon; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.List; -import java.util.Optional; - -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; - import com.github.gumtreediff.actions.model.Move; import com.github.gumtreediff.matchers.CompositeMatchers; import com.github.gumtreediff.matchers.Mapping; - import gumtree.spoon.builder.SpoonGumTreeBuilder; import gumtree.spoon.diff.ActionClassifier; import gumtree.spoon.diff.Diff; import gumtree.spoon.diff.DiffConfiguration; import gumtree.spoon.diff.DiffImpl; -import gumtree.spoon.diff.operations.DeleteOperation; -import gumtree.spoon.diff.operations.InsertOperation; -import gumtree.spoon.diff.operations.MoveOperation; -import gumtree.spoon.diff.operations.Operation; -import gumtree.spoon.diff.operations.OperationKind; -import gumtree.spoon.diff.operations.UpdateOperation; +import gumtree.spoon.diff.operations.*; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import spoon.Launcher; +import spoon.MavenLauncher; import spoon.SpoonModelBuilder; -import spoon.reflect.code.CtBinaryOperator; -import spoon.reflect.code.CtComment; -import spoon.reflect.code.CtConstructorCall; -import spoon.reflect.code.CtIf; -import spoon.reflect.code.CtInvocation; -import spoon.reflect.code.CtLocalVariable; -import spoon.reflect.code.CtNewClass; -import spoon.reflect.code.CtReturn; -import spoon.reflect.code.CtThrow; +import spoon.reflect.code.*; import spoon.reflect.declaration.CtClass; import spoon.reflect.declaration.CtElement; +import spoon.reflect.declaration.CtPackage; import spoon.reflect.declaration.CtType; import spoon.reflect.factory.Factory; import spoon.support.compiler.VirtualFile; import spoon.support.compiler.jdt.JDTBasedSpoonCompiler; import spoon.support.compiler.jdt.JDTSnippetCompiler; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + /** * Test Spoon Diff - * - * @author Matias Martinez, matias.martinez@inria.fr * + * @author Matias Martinez, matias.martinez@inria.fr */ public class AstComparatorTest { + public static CtType getCtType(Factory factory, String content) { + SpoonModelBuilder compiler = new JDTBasedSpoonCompiler(factory); + compiler.addInputSource(new VirtualFile(content, "/test")); + compiler.build(); + return factory.Type().getAll().get(0); + } + + private static CtType getSpoonType(Factory factory, String content) { + try { + canBuild(factory, content); + } catch (Exception e) { + // must fails + } + List> types = factory.Type().getAll(); + if (types.isEmpty()) { + throw new RuntimeException("No Type was created by spoon"); + } + CtType spt = types.get(0); + spt.getPackage().getTypes().remove(spt); + + return spt; + } + + private static void canBuild(Factory factory, String content) { + SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content); + try { + builder.build(); + } catch (Exception e) { + throw new RuntimeException("snippet compilation error while compiling: " + content, e); + } + } + + private static String readFile(File f) throws IOException { + FileReader reader = new FileReader(f); + char[] chars = new char[(int) f.length()]; + reader.read(chars); + String content = new String(chars); + reader.close(); + return content; + } + @Test public void propertiesCorrectlySet() { new AstComparator(); @@ -152,7 +174,7 @@ public void exampleInsert() throws Exception { public void testMain() throws Exception { File fl = new File("src/test/resources/examples/test4/CommandLine1.java"); File fr = new File("src/test/resources/examples/test4/CommandLine2.java"); - AstComparator.main(new String[] { fl.getAbsolutePath(), fr.getAbsolutePath() }); + AstComparator.main(new String[]{fl.getAbsolutePath(), fr.getAbsolutePath()}); } @Test @@ -162,47 +184,6 @@ public void testContent() throws Exception { assertNotNull(getSpoonType(factory, readFile(fl))); } - public static CtType getCtType(Factory factory, String content) { - SpoonModelBuilder compiler = new JDTBasedSpoonCompiler(factory); - compiler.addInputSource(new VirtualFile(content, "/test")); - compiler.build(); - return factory.Type().getAll().get(0); - } - - private static CtType getSpoonType(Factory factory, String content) { - try { - canBuild(factory, content); - } catch (Exception e) { - // must fails - } - List> types = factory.Type().getAll(); - if (types.isEmpty()) { - throw new RuntimeException("No Type was created by spoon"); - } - CtType spt = types.get(0); - spt.getPackage().getTypes().remove(spt); - - return spt; - } - - private static void canBuild(Factory factory, String content) { - SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content); - try { - builder.build(); - } catch (Exception e) { - throw new RuntimeException("snippet compilation error while compiling: " + content, e); - } - } - - private static String readFile(File f) throws IOException { - FileReader reader = new FileReader(f); - char[] chars = new char[(int) f.length()]; - reader.read(chars); - String content = new String(chars); - reader.close(); - return content; - } - @Test public void testJDTBasedSpoonCompiler() { String content1 = "package spoon1.test; " // @@ -217,7 +198,7 @@ public void testJDTBasedSpoonCompiler() { compiler.build(); CtClass clazz1 = (CtClass) factory.Type().getAll().get(0); - Assert.assertNotNull(clazz1); + assertNotNull(clazz1); } @Test @@ -272,8 +253,7 @@ public void test8() throws Exception { List actions = result.getRootOperations(); assertEquals(1, actions.size()); - assertTrue(actions.toString(), - result.containsOperation(OperationKind.Update, "VARIABLE_TYPE", "java.lang.Throwable")); + assertTrue(result.containsOperation(OperationKind.Update, "VARIABLE_TYPE", "java.lang.Throwable"), actions.toString()); } @Test @@ -287,7 +267,7 @@ public void test9() throws Exception { List actions = result.getRootOperations(); // result.debugInformation(); assertEquals(1, actions.size()); - assertTrue(actions.toString(), result.containsOperation(OperationKind.Update, "VARIABLE_TYPE", "boolean")); + assertTrue(result.containsOperation(OperationKind.Update, "VARIABLE_TYPE", "boolean"), actions.toString()); } @Test @@ -410,16 +390,16 @@ public void test_t_211903() throws Exception { File fl = new File("src/test/resources/examples/t_211903/left_MemberFilePersister_1.4.java"); File fr = new File("src/test/resources/examples/t_211903/right_MemberFilePersister_1.5.java"); Diff resultDefault = diff.compare(fl, fr); - - + + DiffConfiguration diffConfiguration = new DiffConfiguration(); diffConfiguration.setMatcher(new CompositeMatchers.ClassicGumtree()); - + Diff resultClassicMatcher = diff.compare(fl, fr, diffConfiguration); - + // We check that the property has an effect on the edit script assertNotEquals(resultClassicMatcher.getAllOperations().size(), resultDefault.getAllOperations().size()); - + // result.debugInformation(); CtElement ancestor = resultClassicMatcher.commonAncestor(); @@ -593,7 +573,7 @@ public void test_t_221345() throws Exception { Diff result = diff.compare(fl, fr); - + List actions = result.getRootOperations(); result.debugInformation(); assertEquals(actions.size(), 1); @@ -902,16 +882,22 @@ public void test_t_224798() throws Exception { @Test public void test_t_224834() throws Exception { // wonderful example where the text diff is impossible to comprehend - AstComparator diff = new AstComparator(); + AstComparator diff = new AstComparator() { + @Override + public CtPackage getCtPackage(File file) throws Exception { + MavenLauncher launcher = new MavenLauncher(file.getAbsolutePath(), MavenLauncher.SOURCE_TYPE.ALL_SOURCE, true); + return launcher.buildModel().getRootPackage(); + } + }; // meld src/test/resources/examples/t_224834/left_TestPriorityQueue_1.2.java // src/test/resources/examples/t_224834/right_TestPriorityQueue_1.3.java - File fl = new File("src/test/resources/examples/t_224834/left_TestPriorityQueue_1.2.java"); - File fr = new File("src/test/resources/examples/t_224834/right_TestPriorityQueue_1.3.java"); + File fl = new File("src/test/resources/examples/classpath-collision/left"); + File fr = new File("src/test/resources/examples/classpath-collision/right"); Diff result = diff.compare(fl, fr); List actions = result.getRootOperations(); result.debugInformation(); - assertEquals(3, actions.size()); + assertEquals(1, actions.size()); assertTrue(result.containsOperation(OperationKind.Insert, "Method", "testClear")); } @@ -975,11 +961,11 @@ public void test_t_224890() throws Exception { * reproducible when using a Java debugger even without any breakpoint - it * appears when changing the version of Spoon but without clear relation of what * changes - * + *

* Given those information we think that the bug might be related with some * optimization done in JVM or with the order of loading classes. */ - @Ignore + @Disabled @Test public void test_t_225008() throws Exception { AstComparator diff = new AstComparator(); @@ -997,7 +983,7 @@ public void test_t_225008() throws Exception { stringBuilder.append("\n"); } - assertEquals("Actions: " + stringBuilder.toString(), 1, actions.size()); + assertEquals(1, actions.size(), "Actions: " + stringBuilder); assertTrue(result.containsOperation(OperationKind.Update, "Modifier", "protected")); } @@ -1118,7 +1104,7 @@ public void test_t_225262() throws Exception { } @Test - @Ignore("Edit script generated is too complex. Ignoring in order to upgrade to GT3") + @Disabled("Edit script generated is too complex. Ignoring in order to upgrade to GT3") public void test_t_225391() throws Exception { AstComparator diff = new AstComparator(); // meld src/test/resources/examples/t_225391/left_IndexHTML_1.4.java @@ -1126,7 +1112,6 @@ public void test_t_225391() throws Exception { File fl = new File("src/test/resources/examples/t_225391/left_IndexHTML_1.4.java"); File fr = new File("src/test/resources/examples/t_225391/right_IndexHTML_1.5.java"); - Diff result = diff.compare(fl, fr); @@ -1875,9 +1860,9 @@ public void testDiff1Comment() throws Exception { AstComparator r = new AstComparator(includeComments); Diff diffOut = r.compare(s, t); System.out.println("Output: " + diffOut); - Assert.assertEquals(1, diffOut.getRootOperations().size()); + assertEquals(1, diffOut.getRootOperations().size()); Operation op = diffOut.getRootOperations().get(0); - Assert.assertTrue(op.getSrcNode().getComments().size() > 0); + assertTrue(op.getSrcNode().getComments().size() > 0); List allop = diffOut.getAllOperations(); boolean hasComment = false; @@ -1897,7 +1882,7 @@ public void testDiff2Comment() throws Exception { Diff diffOut = r.compare(s, t); System.out.println("Output: " + diffOut); - Assert.assertEquals(1, diffOut.getRootOperations().size()); + assertEquals(1, diffOut.getRootOperations().size()); Operation op = diffOut.getRootOperations().get(0); // Assert.assertTrue(op.getSrcNode().getComments().size() > 0); @@ -1919,9 +1904,9 @@ public void testDiff3Comment() throws Exception { Diff diffOut = r.compare(s, t); System.out.println("Output: " + diffOut); - Assert.assertEquals(1, diffOut.getRootOperations().size()); + assertEquals(1, diffOut.getRootOperations().size()); Operation op = diffOut.getRootOperations().get(0); - Assert.assertTrue(op.getSrcNode().getComments().size() > 0); + assertTrue(op.getSrcNode().getComments().size() > 0); assertFalse(op.getSrcNode() instanceof CtComment); @@ -1946,7 +1931,7 @@ public void testReplaceMovesFromRoot1() { + "};"; AstComparator diff = new AstComparator(); - + Diff editScript = diff.compare(c1, c2); @@ -2110,8 +2095,8 @@ public void test264() throws Exception { AstComparator r = new AstComparator(includeComments); Diff diffOut = r.compare(s, t); - Assert.assertEquals(3, diffOut.getRootOperations().size()); - Assert.assertEquals("Delete Literal at org.wso2.transport.http.netty.contractimpl.common.Util:147\n" + + assertEquals(3, diffOut.getRootOperations().size()); + assertEquals("Delete Literal at org.wso2.transport.http.netty.contractimpl.common.Util:147\n" + "\tfalse\n", diffOut.getRootOperations().get(0).toString()); } diff --git a/src/test/java/gumtree/spoon/SourcePositionTest.java b/src/test/java/gumtree/spoon/SourcePositionTest.java index 8db12af1..2908ca27 100644 --- a/src/test/java/gumtree/spoon/SourcePositionTest.java +++ b/src/test/java/gumtree/spoon/SourcePositionTest.java @@ -2,9 +2,9 @@ import com.github.gumtreediff.tree.Tree; import gumtree.spoon.builder.SpoonGumTreeBuilder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import spoon.Launcher; import spoon.reflect.code.CtTypeAccess; import spoon.reflect.declaration.CtElement; @@ -13,30 +13,20 @@ import java.io.File; import java.util.ArrayDeque; import java.util.Arrays; -import java.util.Collection; import java.util.List; import java.util.Objects; import java.util.Queue; -import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; -@RunWith(Parameterized.class) public class SourcePositionTest { - private File javaSourceFile; - private String fileName; - - public SourcePositionTest(File javaSourceFile, String fileName) { - this.javaSourceFile = javaSourceFile; - this.fileName = fileName; - } - - @Parameterized.Parameters(name = "{1}") - public static Collection data() { + public static Stream data() { File directory = new File("src/test/resources/source-positions"); - return Arrays.stream(Objects.requireNonNull(directory.listFiles())).map(file -> new Object[]{file, file.getName()}).collect(Collectors.toUnmodifiableList()); + return Arrays.stream(Objects.requireNonNull(directory.listFiles())) + .map(file -> Arguments.of(file, file.getName())); } private static Tree getRootNode(File javaSourceFile) { @@ -48,12 +38,8 @@ private static Tree getRootNode(File javaSourceFile) { } private static boolean ignoreNodes(Tree node) { - // they don't have a position in the Spoon model CtElement element = (CtElement) node.getMetadata(SpoonGumTreeBuilder.SPOON_OBJECT); - if (element instanceof CtPackage) { - return true; - } - if (element instanceof CtTypeAccess) { + if (element instanceof CtPackage || element instanceof CtTypeAccess) { return true; } return false; @@ -88,11 +74,10 @@ private static boolean siblingsDoNotContainEachOther(List siblings) { return true; } - @Test - public void childNodeMustBeEnclosedInParentNode() { - // Arrange + @ParameterizedTest(name = "[{index}] {1}") + @MethodSource("data") + public void childNodeMustBeEnclosedInParentNode(File javaSourceFile, String name) { Tree rootNode = getRootNode(javaSourceFile); - // Assert for (Tree child : rootNode.getDescendants()) { Tree parent = child.getParent(); if (ignoreNodes(child) || ignoreNodes(parent)) { @@ -105,24 +90,22 @@ public void childNodeMustBeEnclosedInParentNode() { } } - @Test - public void allNodesShouldHaveAtLeastZeroLength() { - // Arrange + @ParameterizedTest(name = "[{index}] {1}") + @MethodSource("data") + public void allNodesShouldHaveAtLeastZeroLength(File javaSourceFile, String name) { Tree rootNode = getRootNode(javaSourceFile); - // Assert for (Tree node : rootNode.getDescendants()) { if (ignoreNodes(node)) { continue; } - assertTrue(node + " has negative length", node.getLength() >= 0); + assertTrue(node.getLength() >= 0, node + " has negative length"); } } - @Test - public void siblingNodeShouldNotContainEachOther() { - // Arrange + @ParameterizedTest(name = "[{index}] {1}") + @MethodSource("data") + public void siblingNodeShouldNotContainEachOther(File javaSourceFile, String name) { Tree rootNode = getRootNode(javaSourceFile); - // Assert Queue roots = new ArrayDeque<>(); roots.add(rootNode); while (!roots.isEmpty()) { @@ -131,4 +114,4 @@ public void siblingNodeShouldNotContainEachOther() { roots.addAll(siblings); } } -} \ No newline at end of file +} diff --git a/src/test/java/gumtree/spoon/TreeTest.java b/src/test/java/gumtree/spoon/TreeTest.java index e7996f71..ad8dc4c4 100644 --- a/src/test/java/gumtree/spoon/TreeTest.java +++ b/src/test/java/gumtree/spoon/TreeTest.java @@ -17,24 +17,11 @@ package gumtree.spoon; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -import org.apache.commons.io.FileUtils; -import org.junit.Test; - import com.github.gumtreediff.io.TreeIoUtils; import com.github.gumtreediff.io.TreeIoUtils.TreeSerializer; import com.github.gumtreediff.tree.Tree; import com.github.gumtreediff.tree.TreeContext; import com.google.gson.JsonObject; - import gumtree.spoon.builder.CtWrapper; import gumtree.spoon.builder.Json4SpoonGenerator; import gumtree.spoon.builder.Json4SpoonGenerator.JSON_PROPERTIES; @@ -44,6 +31,8 @@ import gumtree.spoon.diff.operations.InsertOperation; import gumtree.spoon.diff.operations.Operation; import gumtree.spoon.diff.operations.UpdateOperation; +import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.Test; import spoon.Launcher; import spoon.compiler.SpoonResourceHelper; import spoon.reflect.code.CtStatement; @@ -57,6 +46,13 @@ import spoon.reflect.path.CtPath; import spoon.reflect.visitor.CtScanner; +import java.io.File; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.*; + public class TreeTest { @Test @@ -145,7 +141,7 @@ public void test_Path_ScannerWithSpoon_left() throws Exception { Launcher spoon = new Launcher(); Factory factory = spoon.createFactory(); spoon.createCompiler(factory, - SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/left_QuickNotepad_1.13.java")) + SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/left_QuickNotepad_1.13.java")) .build(); CtType astLeft = factory.Type().get("QuickNotepad"); @@ -162,7 +158,7 @@ public void test_Path_ScannerWithSpoon_Right() throws Exception { Launcher spoon = new Launcher(); Factory factory = spoon.createFactory(); spoon.createCompiler(factory, - SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/right_QuickNotepad_1.14.java")) + SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/right_QuickNotepad_1.14.java")) .build(); CtType astLeft = factory.Type().get("QuickNotepad"); @@ -179,7 +175,7 @@ public void test_JSON_from_GT() throws Exception { Launcher spoon = new Launcher(); Factory factory = spoon.createFactory(); spoon.createCompiler(factory, - SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/right_QuickNotepad_1.14.java")) + SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/right_QuickNotepad_1.14.java")) .build(); CtType astLeft = factory.Type().get("QuickNotepad"); @@ -199,7 +195,7 @@ public void test_JSON_manual_generation_1() throws Exception { Launcher spoon = new Launcher(); Factory factory = spoon.createFactory(); spoon.createCompiler(factory, - SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/right_QuickNotepad_1.14.java")) + SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/right_QuickNotepad_1.14.java")) .build(); CtType aType = factory.Type().get("QuickNotepad"); @@ -216,7 +212,7 @@ public void test_JSON_manual_generation_2() throws Exception { Factory factory = spoon.createFactory(); factory.getEnvironment().setCommentEnabled(false); spoon.createCompiler(factory, - SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/right_QuickNotepad_1.14.java")) + SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/right_QuickNotepad_1.14.java")) .build(); CtType aType = factory.Type().get("QuickNotepad"); @@ -288,7 +284,7 @@ public void test_Path_ScannerWithSpoon_both() throws Exception { Launcher spoon = new Launcher(); Factory factory = spoon.createFactory(); spoon.createCompiler(factory, - SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/left_QuickNotepad_1.13.java")) + SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/left_QuickNotepad_1.13.java")) .build(); CtType astLeft = factory.Type().get("QuickNotepad"); @@ -297,7 +293,7 @@ public void test_Path_ScannerWithSpoon_both() throws Exception { // Reusing the same factory, the path fails. factory = spoon.createFactory(); spoon.createCompiler(factory, - SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/left_QuickNotepad_1.13.java")) + SpoonResourceHelper.resources("src/test/resources/examples/roots/test8/left_QuickNotepad_1.13.java")) .build(); CtType astRight = factory.Type().get("QuickNotepad"); @@ -342,19 +338,6 @@ private void pathOfStmt(CtType ast) { } } - private final class PathScanner extends CtScanner { - @Override - public void scan(CtElement element) { - if (element != null) { - CtPath path = element.getPath(); - assertNotNull(path); - - } - // to avoid visiting all childs - super.scan(element); - } - } - @SuppressWarnings("rawtypes") @Test public void test_packageName_202564() throws Exception { @@ -430,7 +413,7 @@ public void test_bugissue84_1_insert() throws Exception { DiffImpl diffC = (DiffImpl) diff.compare(fl, fr); - + List ops = diffC.getAllOperations(); // to change a method to static means to change the type accesses that invoke @@ -463,7 +446,7 @@ public void test_bugissue84_2_update() throws Exception { DiffImpl diffC = (DiffImpl) diff.compare(fl, fr); - + List ops = diffC.getAllOperations(); assertEquals(1, ops.size()); System.out.println(ops); @@ -576,10 +559,9 @@ public void test_levelsOfNestingOfTypeArguments() throws Exception { long listLabelsCount = childLabels.stream().filter(listLabel::equals).count(); long stringLabelsCount = childLabels.stream().filter(stringLabel::equals).count(); - assertEquals("Not enough List containers", 4, listLabelsCount); - assertEquals("Not enough String type arguments", 1, stringLabelsCount); - assertEquals("There should only be list and string labels", childLabels.size(), - listLabelsCount + stringLabelsCount); + assertEquals(4, listLabelsCount, "Not enough List containers"); + assertEquals(1, stringLabelsCount, "Not enough String type arguments"); + assertEquals(childLabels.size(), listLabelsCount + stringLabelsCount, "There should only be list and string labels"); } @Test @@ -673,4 +655,17 @@ public void test_accessModifierVirtualElementInCodeString_shouldHaveSourcePositi CtElement accessModifiers = diff.getRootOperations().get(0).getSrcNode(); assertPosition(accessModifiers.getPosition(), 1, 10, 23); } + + private final class PathScanner extends CtScanner { + @Override + public void scan(CtElement element) { + if (element != null) { + CtPath path = element.getPath(); + assertNotNull(path); + + } + // to avoid visiting all childs + super.scan(element); + } + } } diff --git a/src/test/java/gumtree/spoon/diff/DiffTest.java b/src/test/java/gumtree/spoon/diff/DiffTest.java index f5c68fbd..abcbda60 100644 --- a/src/test/java/gumtree/spoon/diff/DiffTest.java +++ b/src/test/java/gumtree/spoon/diff/DiffTest.java @@ -1,36 +1,27 @@ package gumtree.spoon.diff; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - import com.github.gumtreediff.matchers.CompositeMatchers; -import org.junit.Ignore; -import org.junit.Test; - -import gumtree.spoon.builder.CtVirtualElement; import gumtree.spoon.AstComparator; +import gumtree.spoon.builder.CtVirtualElement; import gumtree.spoon.diff.operations.Operation; import gumtree.spoon.diff.operations.OperationKind; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import spoon.Launcher; import spoon.reflect.code.CtLiteral; import spoon.reflect.declaration.CtClass; +import java.io.File; +import java.util.*; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.*; + /** * Test Spoon Diff - * - * @author Matias Martinez, matias.martinez@inria.fr * + * @author Matias Martinez, matias.martinez@inria.fr */ public class DiffTest { private final String newline = System.getProperty("line.separator"); @@ -68,12 +59,12 @@ public void testToString() throws Exception { + newline + "\t return values.isEmpty() ? null : values.toArray(new java.lang.String[values.size()]);" + newline + "\t}" + newline; - assertTrue(result.toString(), result.toString().endsWith(deleteStr)); + assertTrue(result.toString().endsWith(deleteStr), result.toString()); } /** * Add new element + child - * + * * @throws Exception */ @Test @@ -90,7 +81,7 @@ public void test_actionClassifier_1() throws Exception { /** * changes an element and adds a new child to it - * + * * @throws Exception */ @Test @@ -113,7 +104,7 @@ public void test_actionClassifier_2() throws Exception { /** * Remove element, keep its only child - * + * * @throws Exception */ @Test @@ -132,7 +123,7 @@ public void test_actionClassifier_3() throws Exception { /** * Removes element with 2 children, keep one child, remove the other - * + * * @throws Exception */ @Test @@ -151,7 +142,7 @@ public void test_actionClassifier_4() throws Exception { /** * Add element with 2 children, one new, other from a move - * + * * @throws Exception */ @Test @@ -270,9 +261,9 @@ public void test_diffOfSuperInterfaces_class() throws Exception { DiffConfiguration configuration = new DiffConfiguration(); configuration.setMatcher(new CompositeMatchers.ClassicGumtree()); - + Diff diff = new AstComparator().compare(left, right, configuration); - + assertTrue(diff.containsOperation(OperationKind.Insert, "INTERFACE", "A")); assertTrue(diff.containsOperation(OperationKind.Insert, "INTERFACE", "D")); assertTrue(diff.containsOperation(OperationKind.Insert, "TYPE_ARGUMENT", "T")); @@ -290,7 +281,7 @@ public void test_diffOfSuperInterfaces_interface() throws Exception { DiffConfiguration configuration = new DiffConfiguration(); configuration.setMatcher(new CompositeMatchers.ClassicGumtree()); - + Diff diff = new AstComparator().compare(left, right, configuration); assertTrue(diff.containsOperation(OperationKind.Delete, "INTERFACE", "A")); @@ -332,7 +323,7 @@ public void test_diffOfThrownTypes_insertionOfRootNodeOfThrowable() throws Excep // verify children of the inserted root node CtVirtualElement thrownTypeRoot = (CtVirtualElement) diff.getRootOperations().get(0).getSrcNode(); assertNotNull(thrownTypeRoot); - assertArrayEquals(new String[] { + assertArrayEquals(new String[]{ "java.lang.ClassNotFoundException", "java.lang.ClassCastException" }, thrownTypeRoot.getChildren().stream().map(Object::toString).toArray()); @@ -368,7 +359,7 @@ public void test_diffOfAnnotations_firstInsertOfValue() throws Exception { } // ToDo - @Ignore("Can be fixed after #154") + @Disabled("Can be fixed after #154") @Test public void test_diffOfAnnotationValues_updateMethod() throws Exception { File left = new File("src/test/resources/examples/annotationValues/updateMethod/left.java"); diff --git a/src/test/java/gumtree/spoon/diff/support/SpoonSupportTest.java b/src/test/java/gumtree/spoon/diff/support/SpoonSupportTest.java index 7cf17ae6..7e3cee1a 100644 --- a/src/test/java/gumtree/spoon/diff/support/SpoonSupportTest.java +++ b/src/test/java/gumtree/spoon/diff/support/SpoonSupportTest.java @@ -1,20 +1,6 @@ package gumtree.spoon.diff.support; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.util.HashSet; -import java.util.Set; - -import org.junit.Test; - import com.github.gumtreediff.tree.Tree; - import gumtree.spoon.AstComparator; import gumtree.spoon.builder.CtVirtualElement; import gumtree.spoon.builder.CtWrapper; @@ -24,11 +10,20 @@ import gumtree.spoon.diff.operations.InsertOperation; import gumtree.spoon.diff.operations.Operation; import gumtree.spoon.diff.operations.UpdateOperation; +import org.junit.jupiter.api.Test; import spoon.reflect.declaration.CtMethod; import spoon.reflect.declaration.ModifierKind; import spoon.reflect.path.CtRole; import spoon.reflect.reference.CtTypeReference; +import java.io.File; +import java.util.HashSet; +import java.util.Set; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.*; + public class SpoonSupportTest { @Test diff --git a/src/test/resources/examples/classpath-collision/left/pom.xml b/src/test/resources/examples/classpath-collision/left/pom.xml new file mode 100644 index 00000000..83aa0a71 --- /dev/null +++ b/src/test/resources/examples/classpath-collision/left/pom.xml @@ -0,0 +1,21 @@ + + 4.0.0 + + org.example + left + 1.0-SNAPSHOT + + + + org.apache.lucene + lucene-core + 1.9.1 + + + junit + junit + 4.13.2 + test + + + \ No newline at end of file diff --git a/src/test/resources/examples/t_224834/left_TestPriorityQueue_1.2.java b/src/test/resources/examples/classpath-collision/left/src/main/java/TestPriorityQueue.java similarity index 100% rename from src/test/resources/examples/t_224834/left_TestPriorityQueue_1.2.java rename to src/test/resources/examples/classpath-collision/left/src/main/java/TestPriorityQueue.java diff --git a/src/test/resources/examples/classpath-collision/right/pom.xml b/src/test/resources/examples/classpath-collision/right/pom.xml new file mode 100644 index 00000000..35b67def --- /dev/null +++ b/src/test/resources/examples/classpath-collision/right/pom.xml @@ -0,0 +1,21 @@ + + 4.0.0 + + org.example + right + 1.0-SNAPSHOT + + + + org.apache.lucene + lucene-core + 1.9.1 + + + junit + junit + 4.13.2 + test + + + \ No newline at end of file diff --git a/src/test/resources/examples/t_224834/right_TestPriorityQueue_1.3.java b/src/test/resources/examples/classpath-collision/right/src/main/java/TestPriorityQueue.java similarity index 100% rename from src/test/resources/examples/t_224834/right_TestPriorityQueue_1.3.java rename to src/test/resources/examples/classpath-collision/right/src/main/java/TestPriorityQueue.java