From 572f41ffd49819bc82609fb556c37b12d4ae9afd Mon Sep 17 00:00:00 2001 From: Till Schnell Date: Thu, 8 Apr 2021 18:31:02 +0200 Subject: [PATCH] Add tests for different classes --- .../TestInferWildcardsJavaTx.java | 108 ++++++++++++++---- .../inferWildcards/TestClassWildcardsMap.java | 13 +++ .../TestClassWildcardsMapNested.java | 16 +++ .../TestClassWildcardsNested.java | 15 +++ .../TestClassWildcardsSingle.java | 8 +- 5 files changed, 133 insertions(+), 27 deletions(-) create mode 100644 src/test/resources/inferWildcards/TestClassWildcardsMap.java create mode 100644 src/test/resources/inferWildcards/TestClassWildcardsMapNested.java create mode 100644 src/test/resources/inferWildcards/TestClassWildcardsNested.java diff --git a/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java b/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java index 5fd63cac..fdf764f9 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java +++ b/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java @@ -1,12 +1,9 @@ package inferWildcards; import java.io.File; +import java.io.IOException; import java.util.Map; -import javax.tools.JavaCompiler; -import javax.tools.StandardJavaFileManager; -import javax.tools.ToolProvider; - import org.junit.Before; import org.junit.Test; @@ -22,40 +19,111 @@ import de.dhbwstuttgart.typeinference.constraints.Pair; public class TestInferWildcardsJavaTx { - private JavaCompiler compiler; - private StandardJavaFileManager fileManager; + private String resourcePath; @Before public void setup () { - compiler = ToolProvider.getSystemJavaCompiler(); - fileManager = compiler.getStandardFileManager(null, null, null); + resourcePath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards"; + } - @Test - public void test () throws Exception { - String resourcePath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards"; - File[] files1 = { new File(resourcePath + "/TestClassWildcards.java") }; + private JavaTXCompiler getCompiler (String filename) throws ClassNotFoundException, IOException { + File[] files1 = { new File(resourcePath + "/" + filename) }; + return new JavaTXCompiler(files1); + } - JavaTXCompiler javaTXCompiler = new JavaTXCompiler(files1); - - // Manipulate AST + private ReplaceTypeparamVisitor generateTph (JavaTXCompiler javaTXCompiler) { Map sourceFiles = javaTXCompiler.getSourceFiles(); ReplaceTypeparamVisitor visitor = new ReplaceTypeparamVisitor(); sourceFiles.entrySet().forEach(e -> e.getValue().accept(visitor)); + return visitor; + } - // Generate Constraints + private ConstraintSet getGeneratedConstraints (ReplaceTypeparamVisitor visitor) { Map tphMap = visitor.getTphMap(); System.out.println(tphMap); - ConstraintSet generatedConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); - // System.out.println(generatedConstraints); + return ConstraintsGenerationUtils.generateConstraints(tphMap); + } - - // Constraints + private ConstraintSet combineConstraints (JavaTXCompiler javaTXCompiler, ConstraintSet generatedConstraints) + throws ClassNotFoundException, IOException { ConstraintSet constraints = javaTXCompiler.getConstraints(); constraints.addAll(generatedConstraints); System.out.println(constraints); + return constraints; + } + + @Test + public void testSingle () throws Exception { + JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsSingle.java"); + + // Manipulate AST + ReplaceTypeparamVisitor visitor = generateTph(javaTXCompiler); + + // Generate Constraints + ConstraintSet generatedConstraints = getGeneratedConstraints(visitor); + // System.out.println(generatedConstraints); + + // Constraints + ConstraintSet constraints = combineConstraints(javaTXCompiler, generatedConstraints); + + // List typeInference = javaTXCompiler.typeInference(); + // System.out.println(typeInference); + } + + + @Test + public void testNested () throws Exception { + JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsNested.java"); + + // Manipulate AST + ReplaceTypeparamVisitor visitor = generateTph(javaTXCompiler); + + // Generate Constraints + ConstraintSet generatedConstraints = getGeneratedConstraints(visitor); + // System.out.println(generatedConstraints); + + // Constraints + ConstraintSet constraints = combineConstraints(javaTXCompiler, generatedConstraints); + + // List typeInference = javaTXCompiler.typeInference(); + // System.out.println(typeInference); + } + + @Test + public void testMap () throws Exception { + JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsMap.java"); + + // Manipulate AST + ReplaceTypeparamVisitor visitor = generateTph(javaTXCompiler); + + // Generate Constraints + ConstraintSet generatedConstraints = getGeneratedConstraints(visitor); + // System.out.println(generatedConstraints); + + // Constraints + ConstraintSet constraints = combineConstraints(javaTXCompiler, generatedConstraints); + + // List typeInference = javaTXCompiler.typeInference(); + // System.out.println(typeInference); + } + + @Test + public void testMapNested () throws Exception { + JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsMapNested.java"); + + // Manipulate AST + ReplaceTypeparamVisitor visitor = generateTph(javaTXCompiler); + + // Generate Constraints + ConstraintSet generatedConstraints = getGeneratedConstraints(visitor); + // System.out.println(generatedConstraints); + + // Constraints + ConstraintSet constraints = combineConstraints(javaTXCompiler, generatedConstraints); + // List typeInference = javaTXCompiler.typeInference(); // System.out.println(typeInference); } diff --git a/src/test/resources/inferWildcards/TestClassWildcardsMap.java b/src/test/resources/inferWildcards/TestClassWildcardsMap.java new file mode 100644 index 00000000..cf48a2e1 --- /dev/null +++ b/src/test/resources/inferWildcards/TestClassWildcardsMap.java @@ -0,0 +1,13 @@ +import java.lang.String; +import java.lang.Object; +import java.util.Map; +import java.lang.Integer; + +class TestClassWildcardsMap +{ + + public Map test4(Map input){ + Map listOfObjects = input; + return listOfObjects; + } +} \ No newline at end of file diff --git a/src/test/resources/inferWildcards/TestClassWildcardsMapNested.java b/src/test/resources/inferWildcards/TestClassWildcardsMapNested.java new file mode 100644 index 00000000..7fb0ea89 --- /dev/null +++ b/src/test/resources/inferWildcards/TestClassWildcardsMapNested.java @@ -0,0 +1,16 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.lang.String; +import java.lang.Object; +import java.util.Map; +import java.lang.Integer; + +class TestClassWildcardsMapNested +{ + + public Map> test(Map> input){ + Map> listOfObjects = input; + return listOfObjects; + } +} \ No newline at end of file diff --git a/src/test/resources/inferWildcards/TestClassWildcardsNested.java b/src/test/resources/inferWildcards/TestClassWildcardsNested.java new file mode 100644 index 00000000..128dda10 --- /dev/null +++ b/src/test/resources/inferWildcards/TestClassWildcardsNested.java @@ -0,0 +1,15 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.lang.String; +import java.lang.Object; +import java.util.Map; +import java.lang.Integer; + +class TestClassWildcardsNested +{ + public List> test3(List> input){ + List> listOfObjects = input; + return listOfObjects; + } +} \ No newline at end of file diff --git a/src/test/resources/inferWildcards/TestClassWildcardsSingle.java b/src/test/resources/inferWildcards/TestClassWildcardsSingle.java index 7e03d578..68d02d8e 100644 --- a/src/test/resources/inferWildcards/TestClassWildcardsSingle.java +++ b/src/test/resources/inferWildcards/TestClassWildcardsSingle.java @@ -1,10 +1,9 @@ import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.lang.String; import java.lang.Object; -class TestClassWildcards +class TestClassWildcardsSingle { private List field1; @@ -39,9 +38,4 @@ class TestClassWildcards input.add(string); return listOfObjects; } - - public List> test3(List> input){ - List> listOfObjects = input; - return listOfObjects; - } } \ No newline at end of file