From 65448c3bf3be727ceb2b30836aaba6a471a931c7 Mon Sep 17 00:00:00 2001 From: Etienne Zink Date: Wed, 16 Mar 2022 18:42:48 +0100 Subject: [PATCH] =?UTF-8?q?Anpassung=20der=20Tests=20f=C3=BCr=20OLFun=20bz?= =?UTF-8?q?w.=20aktiv=20Nahme=20dieser.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utilities/ByteCodeForFunNGenerator.java | 3 +- src/test/java/bytecode/OLFunTest.java | 69 ++++++++++++------- src/test/java/general/TestCleanUp.java | 14 ++++ .../resources/bytecode/javFiles/OLFun.jav | 4 +- 4 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 src/test/java/general/TestCleanUp.java diff --git a/src/main/java/de/dhbwstuttgart/bytecode/utilities/ByteCodeForFunNGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/utilities/ByteCodeForFunNGenerator.java index 127cba5b..4fe8140b 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/utilities/ByteCodeForFunNGenerator.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/utilities/ByteCodeForFunNGenerator.java @@ -28,6 +28,7 @@ public class ByteCodeForFunNGenerator { */ private static HashSet 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 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(); diff --git a/src/test/java/bytecode/OLFunTest.java b/src/test/java/bytecode/OLFunTest.java index 1277cdde..c342d4a3 100644 --- a/src/test/java/bytecode/OLFunTest.java +++ b/src/test/java/bytecode/OLFunTest.java @@ -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;} } diff --git a/src/test/java/general/TestCleanUp.java b/src/test/java/general/TestCleanUp.java new file mode 100644 index 00000000..f1b82cc8 --- /dev/null +++ b/src/test/java/general/TestCleanUp.java @@ -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(); + } + } +} diff --git a/src/test/resources/bytecode/javFiles/OLFun.jav b/src/test/resources/bytecode/javFiles/OLFun.jav index e780a06f..379b82c5 100644 --- a/src/test/resources/bytecode/javFiles/OLFun.jav +++ b/src/test/resources/bytecode/javFiles/OLFun.jav @@ -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); } } \ No newline at end of file