add simple test

This commit is contained in:
Till Schnell 2021-03-22 17:47:35 +01:00
parent 07c1eeeb36
commit 54a2dbfedc
3 changed files with 76 additions and 0 deletions

1
.gitignore vendored
View File

@ -29,3 +29,4 @@ logFiles/**
src/main/java/de/dhbwstuttgart/parser/antlr/
src/main/java/de/dhbwstuttgart/sat/asp/parser/antlr/
/pull.sh

View File

@ -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);
}
}

View File

@ -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;
}
}