diff --git a/src/test/java/bytecode/OLFunTest.java b/src/test/java/bytecode/OLFunTest.java index 1277cdde..e2352098 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.reflect.Method; import java.net.URL; import java.net.URLClassLoader; -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 etiennezink + */ 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 classFun1IntInt; + private static Class classFun1DoubleDouble; + private static Class classFun1StringString; private static Object instanceOfClass; - - @Test - public void generateBC() throws Exception { + + private static String generatedByteCodeDirectory = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/"; + + @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"); - /* + classFun1IntInt = loader.loadClass("Fun1$$Ljava$lang$Integer$_$Ljava$lang$Integer$_$"); + classFun1DoubleDouble = loader.loadClass("Fun1$$Ljava$lang$Double$_$Ljava$lang$Double$_$"); + classFun1StringString = loader.loadClass("Fun1$$Ljava$lang$String$_$Ljava$lang$String$_$"); 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", classFun1IntInt ,Integer.class); + assertNotNull(m); + } + @Test + public void mExistsWithDouble() throws Exception{ + Method m = classToTest.getDeclaredMethod("m", classFun1DoubleDouble ,Double.class); + assertNotNull(m); + } + + @Test + public void mExistsWithString() throws Exception{ + Method m = classToTest.getDeclaredMethod("m", classFun1StringString ,String.class); + assertNotNull(m); + } + + @AfterClass + public static void cleanUp(){ + TestCleanUp.cleanUpDirectory(new File(generatedByteCodeDirectory), f -> f.getName().contains(".class")); + } } 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(); + } + } +}