diff --git a/src/test/java/bytecode/InheritTest2.java b/src/test/java/bytecode/InheritTest2.java new file mode 100644 index 00000000..36602704 --- /dev/null +++ b/src/test/java/bytecode/InheritTest2.java @@ -0,0 +1,211 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Field; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Stack; +import java.util.Vector; +import java.util.stream.Collectors; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.google.common.collect.Lists; + +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +public class InheritTest2 { + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + private static ClassLoader loader; + private static Class classToTest; + private static Class classToTestAA, classToTestBB, classToTestCC, classToTestDD; + private static String pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/";; + //private static String pathToClassFile1 = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/"; + private static Object instanceOfClass; + private static Object instanceOfClassAA, instanceOfClassBB, instanceOfClassCC, instanceOfClassDD; + private static HashMap, Method> hm = new HashMap<>(); + private static List typeinferenceResult; + private static List simplifyResultsForAllSourceFiles; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/AA.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + List typeinferenceResult = compiler.typeInference(); + List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); + compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); + loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); + classToTestAA = loader.loadClass("AA"); + instanceOfClassAA = classToTestAA.getDeclaredConstructor().newInstance(); + + + + path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/BB.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + typeinferenceResult = compiler.typeInference(); + simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); + compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); + classToTestBB = loader.loadClass("BB"); + instanceOfClassBB = classToTestBB.getDeclaredConstructor().newInstance(); + + path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/CC.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + typeinferenceResult = compiler.typeInference(); + simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); + compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); + classToTestCC = loader.loadClass("CC"); + instanceOfClassCC = classToTestCC.getDeclaredConstructor().newInstance(); + + path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/DD.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + typeinferenceResult = compiler.typeInference(); + simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); + compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); + classToTestDD = loader.loadClass("DD"); + instanceOfClassDD = classToTestDD.getDeclaredConstructor().newInstance(); + + + path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/Inherit2.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + typeinferenceResult = compiler.typeInference(); + simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); + compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); + classToTest = loader.loadClass("Inherit2"); + instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + } + + @Test + public void testInheritClassName() { + assertEquals("Inherit2", classToTest.getName()); + } + + + @Test + public void testAAName() { + assertEquals("AA", classToTestAA.getName()); + } + + @Test + public void testBBName() { + assertEquals("BB", classToTestBB.getName()); + } + + @Test + public void testCCName() { + assertEquals("CC", classToTestCC.getName()); + } + + @Test + public void testDDName() { + assertEquals("DD", classToTestDD.getName()); + } + + @Test + public void testmainAA() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException { + Method m2 = classToTestAA.getDeclaredMethod("m2", classToTestAA); + assertEquals(m2.invoke(instanceOfClassAA, instanceOfClassAA), "AA"); + Method main = classToTest.getDeclaredMethod("main", classToTestAA); + assertEquals(main.invoke(instanceOfClass, instanceOfClassAA), "AA"); + } + + @Test + public void testmainBB() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException { + Method m2 = classToTestAA.getDeclaredMethod("m2", classToTestAA); + assertEquals(m2.invoke(instanceOfClassAA, instanceOfClassAA), "AA"); + Method main = classToTest.getDeclaredMethod("main", classToTestAA); + assertEquals(main.invoke(instanceOfClass, instanceOfClassBB), "AA"); + } + + @Test + public void testmainCC() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException { + Method m2 = classToTestCC.getDeclaredMethod("m2", classToTestCC); + assertEquals(m2.invoke(instanceOfClassCC, instanceOfClassCC), "CC"); + Method main = classToTest.getDeclaredMethod("main", classToTestCC); + assertEquals(main.invoke(instanceOfClass, instanceOfClassCC), "CC"); + } + + @Test + public void testmainDD() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException { + Method m2 = classToTestCC.getDeclaredMethod("m2", classToTestCC); + assertEquals(m2.invoke(instanceOfClassCC, instanceOfClassCC), "CC"); + Method main = classToTest.getDeclaredMethod("main", classToTestCC); + assertEquals(main.invoke(instanceOfClass, instanceOfClassDD), "CC"); + } + + + //PL 2020-05-12: Die folgenden Test funktionieren erst, wenn Generics im Bytecode implementiert sind + @Test + public void testmainVectorAA() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException { + Method m2 = classToTestAA.getDeclaredMethod("m2", classToTestAA); + assertEquals(m2.invoke(instanceOfClassAA, instanceOfClassAA), "AA"); + Vector v = new Vector<>(); + v.add(instanceOfClassAA); + Method main = classToTest.getDeclaredMethod("main", Vector.class); + try { + assertEquals(main.invoke(instanceOfClass, v), "AA"); + } + catch (java.lang.reflect.InvocationTargetException e) { + testmainVectorCC(); + } + } + + @Test + public void testmainVectorBB() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException { + Method m2 = classToTestAA.getDeclaredMethod("m2", classToTestAA); + assertEquals(m2.invoke(instanceOfClassAA, instanceOfClassAA), "AA"); + Vector v = new Vector<>(); + v.add(instanceOfClassBB); + Method main = classToTest.getDeclaredMethod("main", Vector.class); + try { + assertEquals(main.invoke(instanceOfClass, v), "AA"); + } + catch (java.lang.reflect.InvocationTargetException e) { + testmainVectorCC(); + } + } + + @Test + public void testmainVectorCC() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException { + Method m2 = classToTestCC.getDeclaredMethod("m2", classToTestCC); + assertEquals(m2.invoke(instanceOfClassCC, instanceOfClassCC), "CC"); + Vector v = new Vector<>(); + v.add(instanceOfClassCC); + Method main = classToTest.getDeclaredMethod("main", Vector.class); + String erg; + assertEquals(erg= (String) main.invoke(instanceOfClass, v), + erg.equals("CC")? "CC": "AA"); + } + + @Test + public void testmainVectorDD() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException { + Method m2 = classToTestCC.getDeclaredMethod("m2", classToTestCC); + assertEquals(m2.invoke(instanceOfClassCC, instanceOfClassCC), "CC"); + Vector v = new Vector<>(); + v.add(instanceOfClassDD); + Method main = classToTest.getDeclaredMethod("main", Vector.class); + String erg; + assertEquals(erg= (String) main.invoke(instanceOfClass, v), + erg.equals("CC")? "CC": "AA"); + } + + +} diff --git a/src/test/resources/bytecode/javFiles/AA.jav b/src/test/resources/bytecode/javFiles/AA.jav index 4593fdfb..3f8f172d 100644 --- a/src/test/resources/bytecode/javFiles/AA.jav +++ b/src/test/resources/bytecode/javFiles/AA.jav @@ -3,4 +3,6 @@ import java.lang.String; public class AA { m(Integer i) { return "AA"; } + + m2(AA x) { return "AA"; } } \ No newline at end of file diff --git a/src/test/resources/bytecode/javFiles/CC.jav b/src/test/resources/bytecode/javFiles/CC.jav index 4523dfda..d7d5c967 100644 --- a/src/test/resources/bytecode/javFiles/CC.jav +++ b/src/test/resources/bytecode/javFiles/CC.jav @@ -3,5 +3,9 @@ import java.lang.String; public class CC extends BB { - m(Integer i) { return "CC"; } + m(Integer i) { + return "CC"; + } + + m2(CC x) { return "CC"; } } diff --git a/src/test/resources/bytecode/javFiles/Inherit2.jav b/src/test/resources/bytecode/javFiles/Inherit2.jav new file mode 100644 index 00000000..fe4c5062 --- /dev/null +++ b/src/test/resources/bytecode/javFiles/Inherit2.jav @@ -0,0 +1,17 @@ +import java.util.Vector; + +import java.lang.Integer; +import java.lang.String; + + +public class Inherit2 { + + main(d) { + return d.m2(d); + } + + main(v) { + var aa = v.elementAt(0); + return aa.m2(aa); + } +} \ No newline at end of file