From 54a2dbfedc5147770b52f5e96b34069cf4f7119a Mon Sep 17 00:00:00 2001 From: Till Schnell Date: Mon, 22 Mar 2021 17:47:35 +0100 Subject: [PATCH] add simple test --- .gitignore | 1 + .../java/inferWildcards/TestClass1Test.java | 58 +++++++++++++++++++ .../resources/inferWildcards/TestClass1.java | 17 ++++++ 3 files changed, 76 insertions(+) create mode 100644 src/test/java/inferWildcards/TestClass1Test.java create mode 100644 src/test/resources/inferWildcards/TestClass1.java diff --git a/.gitignore b/.gitignore index 2d0d6112..b080f27c 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ logFiles/** src/main/java/de/dhbwstuttgart/parser/antlr/ src/main/java/de/dhbwstuttgart/sat/asp/parser/antlr/ +/pull.sh diff --git a/src/test/java/inferWildcards/TestClass1Test.java b/src/test/java/inferWildcards/TestClass1Test.java new file mode 100644 index 00000000..b0163c07 --- /dev/null +++ b/src/test/java/inferWildcards/TestClass1Test.java @@ -0,0 +1,58 @@ +package inferWildcards; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; + +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; + +public class TestClass1Test { + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + private static ClassLoader loader; + private static Class classToTest; + private static String pathToClassFile; + private static Object instanceOfClass; + + @Test + public void generateBC() throws Exception { + String resourcePath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards"; + path = resourcePath + "/TestClass1.java"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + compiler.generateBytecode(resourcePath + "/generatedBC/"); + pathToClassFile = resourcePath + "/generatedBC/"; + loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); + classToTest = loader.loadClass("TestClass1"); + classToTest.getConstructors(); // this will fail + instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + + Method getFact = classToTest.getDeclaredMethod("test", Integer.class); +// Field fact = classToTest.getDeclaredField("fact"); +// Class lambda = m.invoke(instanceOfClass).getClass(); +// Class lambda = fact.getType(); +// System.out.println(fact.getType().getName()); +// Method apply = lambda.getMethod("apply", Object.class); +// System.out.println(lambda.getMethods()[0]); +// System.out.println(instanceOfClass.toString()); +// // Damit man auf die Methode zugreifen kann +// apply.setAccessible(true); + // Field value +// Object fieldVal = fact.get(instanceOfClass); + Integer i = 3; +// Method applyFromField = fieldVal.getClass().getDeclaredMethod("apply", Object.class); +// applyFromField.setAccessible(true); +// Integer result = (Integer) apply.invoke(lambda,i); + Integer result = (Integer) getFact.invoke(instanceOfClass,i); + System.out.println(result); + assertEquals(new Integer(3), result); + } + + +} diff --git a/src/test/resources/inferWildcards/TestClass1.java b/src/test/resources/inferWildcards/TestClass1.java new file mode 100644 index 00000000..459a6a9e --- /dev/null +++ b/src/test/resources/inferWildcards/TestClass1.java @@ -0,0 +1,17 @@ +import java.lang.Integer; + +class TestClass1 { + + var field1; + + public TestClass1(){ + field1 = 0; + } + + public test(param1){ + var localVar = param1 + field1; + field1 = localVar - 1; + return localVar; + } + +} \ No newline at end of file