From 0c63695f7b1f986b5c9f35dbccdf2033db691475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Tue, 10 Nov 2015 17:54:27 +0100 Subject: [PATCH] ASTBytecodeTest erstellt --- test/bytecode/ASTBytecodeTest.java | 79 ++++++++++++++++++++++ test/bytecode/types/ExtendsObject.jav | 3 - test/bytecode/types/ExtendsObjectTest.java | 54 ++++++++------- 3 files changed, 110 insertions(+), 26 deletions(-) create mode 100644 test/bytecode/ASTBytecodeTest.java delete mode 100644 test/bytecode/types/ExtendsObject.jav diff --git a/test/bytecode/ASTBytecodeTest.java b/test/bytecode/ASTBytecodeTest.java new file mode 100644 index 00000000..382459c7 --- /dev/null +++ b/test/bytecode/ASTBytecodeTest.java @@ -0,0 +1,79 @@ +package bytecode; + +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; + +import org.apache.commons.bcel6.classfile.JavaClass; + +import de.dhbwstuttgart.bytecode.ClassGenerator; +import de.dhbwstuttgart.core.MyCompiler; +import de.dhbwstuttgart.core.MyCompilerAPI; +import de.dhbwstuttgart.logger.LoggerConfiguration; +import de.dhbwstuttgart.logger.Section; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.typeinference.ByteCodeResult; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.TypeinferenceResults; + +public abstract class ASTBytecodeTest { + public static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + + protected String testName = ""; + + protected SourceFile sourceFile = new SourceFile(); + protected TypeinferenceResults results = new TypeinferenceResults(); + + protected Class getClassToTest(){ + Class classToTest = null; + try { + ClassLoader classLoader = getClassLoader(); + + classToTest = classLoader.loadClass(testName); + + + } catch (Exception e) { + throw new RuntimeException(e); + } + + return classToTest; + } + + protected ClassLoader getClassLoader() throws Exception{ + File file = new File(rootDirectory); + URL url = file.toURL(); + URL[] urls = new URL[]{url}; + + return new URLClassLoader(urls); + } + + public ASTBytecodeTest(){ + init(); + + LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out); + MyCompilerAPI compiler = MyCompiler.getAPI(logConfig); + try { + + Menge sourceFiles = new Menge<>(); + sourceFiles.add(sourceFile); + + Menge bytecode = compiler.generateBytecode(sourceFiles, results); + + ByteCodeResult result = bytecode.firstElement(); + + JavaClass javaClass = result.getByteCode().getJavaClass(); + javaClass.dump(new File(rootDirectory+javaClass.getClassName()+".class")); + + System.out.println(new File(rootDirectory+javaClass.getClassName()+".class").getAbsolutePath()); + + for(ClassGenerator cg: result.getByteCode().getExtraClasses().values()){ + JavaClass jc = cg.getJavaClass(); + jc.dump(new File(rootDirectory+jc.getClassName()+".class")); + } + }catch(Exception e){ + throw new RuntimeException(e); + } + } + + protected abstract void init(); +} diff --git a/test/bytecode/types/ExtendsObject.jav b/test/bytecode/types/ExtendsObject.jav deleted file mode 100644 index 758def9c..00000000 --- a/test/bytecode/types/ExtendsObject.jav +++ /dev/null @@ -1,3 +0,0 @@ -class ExtendsObject extends Object{ - -} \ No newline at end of file diff --git a/test/bytecode/types/ExtendsObjectTest.java b/test/bytecode/types/ExtendsObjectTest.java index 964d52c9..5c8ed76a 100644 --- a/test/bytecode/types/ExtendsObjectTest.java +++ b/test/bytecode/types/ExtendsObjectTest.java @@ -2,33 +2,27 @@ package bytecode.types; import static org.junit.Assert.*; -import java.io.File; -import java.io.IOException; import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.util.Vector; - -import junit.framework.TestCase; - import org.junit.Test; -import bytecode.BytecodeTest; -import plugindevelopment.TypeInsertTester; -import de.dhbwstuttgart.core.MyCompiler; -import de.dhbwstuttgart.core.MyCompilerAPI; -import de.dhbwstuttgart.logger.LoggerConfiguration; -import de.dhbwstuttgart.logger.Section; -import de.dhbwstuttgart.parser.JavaParser.yyException; -import de.dhbwstuttgart.typeinference.ByteCodeResult; -import de.dhbwstuttgart.typeinference.Menge; -import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; -import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet; +import bytecode.ASTBytecodeTest; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; -public class ExtendsObjectTest extends BytecodeTest{ - @Override - protected void init() { - testName = "ExtendsObject"; - rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + +public class ExtendsObjectTest extends ASTBytecodeTest{ + public static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + + protected String testName = "ExtendsObjectTest2"; + + protected void init(){ + /* + class ExtendsObject extends Object{ + + } + */ + de.dhbwstuttgart.syntaxtree.Class classToTest = ASTFactory.createClass("ExtendsObjectTest2", null, null, null, sourceFile); + + sourceFile.addElement(classToTest); } @Test @@ -48,5 +42,19 @@ public class ExtendsObjectTest extends BytecodeTest{ fail(); } } + + @Test + public void testSupertype(){ + try{ + ClassLoader classLoader = getClassLoader(); + + Class cls = classLoader.loadClass(testName); + + assertEquals("java.lang.Object", cls.getSuperclass().getName()); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } }