Anpassung der Tests für OLFun bzw. aktiv Nahme dieser.

This commit is contained in:
Etienne Zink 2022-03-16 18:42:48 +01:00
parent ac6980b5e0
commit 65448c3bf3
4 changed files with 61 additions and 29 deletions

View File

@ -28,6 +28,7 @@ public class ByteCodeForFunNGenerator {
*/
private static HashSet<Integer> alreadyGeneratedFunN = new HashSet<>();
//ToDo Etienne: wird in Test OLFun nicht verwendet!
public static void generateBCForFunN(LambdaExpression lambdaExpression, String methDesc, File path) {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
@ -57,6 +58,7 @@ public class ByteCodeForFunNGenerator {
public static void generateBCForFunN(ArgumentList argumentList, String methDesc, File path) {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
List<Expression> arguments = argumentList.getArguments();
/* //ToDo neu machen
int numberOfParams = arguments.size();
//ToDo Classname anpassen
@ -68,7 +70,6 @@ public class ByteCodeForFunNGenerator {
writeClassFile(className, classWriter.toByteArray(), path);
*/
SignatureWriter signatureWriter = new SignatureWriter();
int numberOfParams = 0;
SignatureVisitor paramVisitor = signatureWriter.visitParameterType();

View File

@ -1,52 +1,69 @@
package bytecode;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.invoke.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.function.Function;
import org.junit.Test;
import general.TestCleanUp;
import org.junit.*;
import de.dhbwstuttgart.core.JavaTXCompiler;
import static org.junit.Assert.*;
/**
* Test which only checks if the class {@code OLFun} was correctly compiled.
* This is achived by verifying the existence of the three implementations of {@code m}.
*
* @since Studienarbeit Type Erasure
* @author Etienne Zink
*/
public class OLFunTest {
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 Class<?> classFun1;
private static Object instanceOfClass;
private static String generatedByteCodeDirectory = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/";
@Test
public void generateBC() throws Exception {
@BeforeClass
public static void setUp() throws Exception {
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/OLFun.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
compiler.generateBytecode(System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/");
pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/";
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
compiler.generateBytecode(generatedByteCodeDirectory);
loader = new URLClassLoader(new URL[] {new URL("file://"+generatedByteCodeDirectory)});
classToTest = loader.loadClass("OLFun");
/*
classFun1 = loader.loadClass("Fun1$$");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
Method m = classToTest.getDeclaredMethod("m");
Class<?> lambda = m.invoke(instanceOfClass).getClass();
Method apply = lambda.getMethod("apply", Object.class);
// Damit man auf die Methode zugreifen kann
apply.setAccessible(true);
Integer i = 77;
Integer result = (Integer) apply.invoke(m.invoke(instanceOfClass), i);
assertEquals(77, result);
*/
}
@Test
public void mExistsWithInteger() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1 ,Integer.class);
assertNotNull(m);
}
@Test
public void mExistsWithString() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1 ,String.class);
assertNotNull(m);
}
@Test
public void mExistsWithDouble() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1 ,Double.class);
assertNotNull(m);
}
@AfterClass
public static void cleanUp(){
TestCleanUp.cleanUpDirectory(new File(generatedByteCodeDirectory), f -> f.getName().contains(".class"));
}
public static Integer apply(Integer x) {return x;}
}

View File

@ -0,0 +1,14 @@
package general;
import java.io.File;
import java.io.FileFilter;
public class TestCleanUp {
public static void cleanUpDirectory(File directory, FileFilter fileFilter){
if(!directory.isDirectory()) throw new RuntimeException("Directory for bytecode generation is wrong!");
for (File file: directory.listFiles(fileFilter)) {
file.delete();
}
}
}

View File

@ -10,8 +10,8 @@ import java.lang.Boolean;
public class OLFun {
//f = x -> {return x + x;};
y;
m(f, x) {
x = f.apply(x+x);
y = f.apply(x+x);
}
}