diff --git a/pom.xml b/pom.xml index 631896f..84326da 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,12 @@ 5.11.0-M2 test + + junit + junit + 4.13.1 + test + org.antlr antlr4-runtime @@ -56,18 +62,6 @@ 3.26.0 test - - org.mockito - mockito-core - 5.11.0 - test - - - junit - junit - 4.13.1 - test - diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..da9d90a --- /dev/null +++ b/readme.md @@ -0,0 +1,77 @@ +# "Nicht Haskel 2.0" Java Compiler + +Realisation of a subset of the Java Standard Compiler in the course Compiler Construction of the 4th semester Computer Science at the Duale Hochschule Suttgart (Horb). + +This project aims to provide a simplified version of the Java compiler, focusing on key language features and demonstrating the principles of compiler construction. + +## Realised Java syntax + +- **Data types**: `int`, `boolean`, `char` +- **Access modifier**: `public`, `protected`, `private` +- **Operators**: `=` `+` `-` `*` `%` `/` `>` `<` `>=` `<=` `==` `!=` `!` `&&` `||` `++` `--` +- **Keywords**: `class`, `this`, `while`, `do`, `if`, `else`, `for`, `return`, `new`, `switch`, `case`, `break`, `default`, `:` +- **Statements**: + - `if` ... `if else` ... `else`; + - `while` ... ; + - `do` ... `while`; + - `for`; + - `switch` ... `case` ... ; +- **Comments**: + - Single line: `// comment` + - Multi-line: `/* comment */` +- **Further functions**: + - All methods are overloadable + - High maintainability and expandability through implementation of the visitor pattern + - Logging Input and Outputs + - Error Handling in the Semantic Check + +## Project Structure + +```plain +src/ +└── main/ + ├── java/ + │ ├── ast/ -> Defining the structure of the AST + │ ├── bytecode/ -> Generate Java bytecode + │ ├── main/ -> Running the compiler + │ ├── parser/ + │ │ ├── astBuilder/ -> Builder creating the AST + │ │ ├── generated/ -> Antlr generated grammar + │ │ └── grammar/ -> Antlr grammar + │ ├── semantic/ -> Running the semantic check + │ └── visitor/ -> Visitor interface + └── resources/ +test/ +└── java/ +│ ├── main/ +│ ├── parser/ -> Performs tests on the parser +│ └── semantic/ +└── resources/ -> Ressources for running the Tests + ├──input + │ ├── combinedFeatureTests + │ ├── endabgabeTests + │ ├── failureTests + │ ├── singleFeatureSemanticTests + │ ├── singleFeatureTests + │ ├── typedAstExceptionsTests + │ └── typedAstFeatureTests + └──output + ├── javac + └── raupenpiler +``` + +## Class-Diagramm AST + +![AST Diagramm](ast.png) + +## Used Tools + +- [Maven 4.0](https://maven.apache.org/index.html) + - Used for automating the build process and managing dependencies. +- [ANTLR4 v.13.1](https://www.antlr.org/) + - Used to parse the input Java code into the Abstract Syntax Tree. + + +## How to run the compiler +## Download +```bash \ No newline at end of file diff --git a/src/test/Makefile b/src/test/Makefile index 44aa134..2092bac 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -29,11 +29,13 @@ test-raupenpiler: clean: - # clean output folders + # clean main output folders rm -f ../main/resources/output/*.class rm -f ../main/resources/output/*.jar + # clean resources output folders rm -f ./resources/output/javac/*.class rm -f ./resources/output/raupenpiler/*.class + rm -f ./resources/output/raupenpiler/*.jar # clean logs rm -f ../main/resources/logs/*.log # clean test/java/main folders from .class files for End-to-End tests diff --git a/src/test/java/main/E2EReflectionsTest.java b/src/test/java/main/E2EReflectionsTest.java index f461417..cf4383b 100644 --- a/src/test/java/main/E2EReflectionsTest.java +++ b/src/test/java/main/E2EReflectionsTest.java @@ -1,9 +1,14 @@ package main; +import java.io.IOException; + import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.Arrays; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import org.junit.jupiter.api.Test; @@ -14,77 +19,79 @@ import static org.junit.jupiter.api.Assertions.*; public class E2EReflectionsTest { @Test - public void AllFeaturesClassExampleTest(){ + public void CharTest() { + final String FILE_NAME = "Char"; try { - Main.main(new String[]{"src/test/resources/input/combinedFeatureTests/AllFeaturesClassExample.java", "src/test/resources/output/raupenpiler"}); + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); // Get the system Java compiler JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); // Assert that the compiler is available assertNotNull(javac, "Java Compiler is not available"); - javac.run(null, null, null, "src/test/resources/input/combinedFeatureTests/AllFeaturesClassExample.java"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); - Class clazz = Class.forName("src.resources.input.combinedFeatureTests.AllFeaturesClassExample"); - ClassLoader classLoader = getClass().getClassLoader(); - // Class clazz = classLoader.loadClass("main.AllFeaturesClassExample"); // Class Name - assertEquals("main.AllFeaturesClassExample", clazz.getName()); + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + // Constructors - Constructor[] actualConstructors = clazz.getDeclaredConstructors(); - assertTrue(actualConstructors.length > 0, "No constructors found"); + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); - Constructor expectedConstructor = clazz.getConstructor(int.class, boolean.class, char.class); + assertEquals(javacConstructors.length, raupenpilerConstructors.length); - boolean constructorFound = false; - for (Constructor constructor : actualConstructors) { - if (constructor.equals(expectedConstructor)) { - constructorFound = true; - break; + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } } } - assertTrue(constructorFound, "Expected constructor not found in actual constructors"); + // Methods - Method[] actualMethodNames = clazz.getDeclaredMethods(); - assertTrue(actualMethodNames.length > 0); - for (Method method : actualMethodNames) { - System.out.println("Method: " + method.getName()); + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); } - // Method Names - String[] expectedMethodNames = { - "controlStructures", - "logicalOperations", - "add", - "subtract", - "multiply", - "divide", - "modulo", - "main" - }; - - for (Method method : actualMethodNames) { - assertTrue(Arrays.asList(expectedMethodNames).contains(method.getName())); - } // Fields - Field[] actualFields = clazz.getDeclaredFields(); - assertTrue(actualFields.length > 0, "No fields found"); + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); - Field expectedField = clazz.getDeclaredField("c"); - assertEquals(expectedField.getType(), char.class); + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); - boolean fieldFound = false; - for (Field field : actualFields) { - if (field.equals(expectedField)) { - fieldFound = true; - break; + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } } } - assertTrue(fieldFound, "Expected field not found in actual fields"); - - } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { e.printStackTrace(); @@ -92,4 +99,1968 @@ public class E2EReflectionsTest { throw new RuntimeException(e); } } + + @Test + public void CommentsTest() { + final String FILE_NAME = "Comments"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void ConstructorMethodCallTest() { + final String FILE_NAME = "ConstructorMethodCall"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void ConstructorMethodCallParametersTest() { + final String FILE_NAME = "ConstructorMethodCallParameters"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void ConstructorParameterTest() { + final String FILE_NAME = "ConstructorParameter"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void ConstructorThisDotTest() { + final String FILE_NAME = "ConstructorThisDot"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void DoWhileTest() { + final String FILE_NAME = "DoWhile"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void EmptyClassTest() { + final String FILE_NAME = "EmptyClass"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void EmptyClassWithConstructorTest() { + final String FILE_NAME = "EmptyClassWithConstructor"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void FieldTest() { + final String FILE_NAME = "Field"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void FieldWithAccessModifierTest() { + final String FILE_NAME = "FieldWithAccessModifier"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void ForTest() { + final String FILE_NAME = "For"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + @Test + public void IfTest() { + final String FILE_NAME = "If"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void IfElseTest() { + final String FILE_NAME = "IfElse"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void IfElseIfElseTest() { + final String FILE_NAME = "IfElseIfElse"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void IncrementTest() { + final String FILE_NAME = "Increment"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void MainMethodTest() { + final String FILE_NAME = "MainMethod"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void MultipleClassesTest() { + final String FILE_NAME = "MultipleClasses"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void NullTest() { + final String FILE_NAME = "Null"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void SelfReferenceTest() { + final String FILE_NAME = "SelfReference"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void ThisDotTest() { + final String FILE_NAME = "ThisDot"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void VariableCalculationTest() { + final String FILE_NAME = "VariableCalculation"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void VariableCompareTest() { + final String FILE_NAME = "VariableCompare"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void VoidMethodTest() { + final String FILE_NAME = "VoidMethod"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test + public void WhileTest() { + final String FILE_NAME = "While"; + try { + // compile with raupenpiler + Main.main(new String[]{"src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java", "src/test/resources/output/raupenpiler"}); + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + // compile with javac + javac.run(null, null, null, "src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".java"); + moveFile("src/test/resources/input/singleFeatureTests/" + FILE_NAME + ".class", "src/test/resources/output/javac/" + FILE_NAME + ".class"); + + // Load the javac class + Path javacClassPath = Paths.get("src/test/resources/output/javac"); + ClassLoader javacClassLoader = new java.net.URLClassLoader(new java.net.URL[]{javacClassPath.toUri().toURL()}); + Class javacLoadedClass = javacClassLoader.loadClass(FILE_NAME); + + // Load the raupenpiler class + Path raupenpilerClassPath = Paths.get("src/test/resources/output/raupenpiler"); + ClassLoader raupenpilerClassLoader = new java.net.URLClassLoader(new java.net.URL[]{raupenpilerClassPath.toUri().toURL()}); + Class raupenpilerLoadedClass = raupenpilerClassLoader.loadClass(FILE_NAME); + + + // Class Name + assertEquals(FILE_NAME, javacLoadedClass.getName()); + assertEquals(FILE_NAME, raupenpilerLoadedClass.getName()); + + + // Constructors + Constructor[] javacConstructors = javacLoadedClass.getDeclaredConstructors(); + Constructor[] raupenpilerConstructors = raupenpilerLoadedClass.getDeclaredConstructors(); + + assertEquals(javacConstructors.length, raupenpilerConstructors.length); + + for(Constructor c : javacConstructors) { + for(Constructor raupenpilerConstructor : raupenpilerConstructors) { + assertEquals(c.getParameterCount(), raupenpilerConstructor.getParameterCount()); + if (c.getParameterCount() == raupenpilerConstructor.getParameterCount()) { + assertEquals(c.getName(), raupenpilerConstructor.getName()); + } + } + } + + + // Methods + Method[] javacMethods = javacLoadedClass.getDeclaredMethods(); + Method[] raupenpilerMethods = raupenpilerLoadedClass.getDeclaredMethods(); + + assertEquals(javacMethods.length, raupenpilerMethods.length); + + for (int i = 0; i < javacMethods.length; i++) { + assertEquals(javacMethods[i].getName(), raupenpilerMethods[i].getName()); + assertEquals(javacMethods[i].getReturnType(), raupenpilerMethods[i].getReturnType()); + assertEquals(javacMethods[i].getParameterCount(), raupenpilerMethods[i].getParameterCount()); + // assertEquals(javacMethods[i].getModifiers(), raupenpilerMethods[i].getModifiers()); + } + + + // Fields + Field[] javacLoadedClassDeclaredFields = javacLoadedClass.getDeclaredFields(); + Field[] raupenpilerLoadedClassDeclaredFields = raupenpilerLoadedClass.getDeclaredFields(); + + assertEquals(javacLoadedClassDeclaredFields.length, raupenpilerLoadedClassDeclaredFields.length); + + for (Field field : javacLoadedClassDeclaredFields) { + for (Field raupenpilerField : raupenpilerLoadedClassDeclaredFields) { + if (field.getName().equals(raupenpilerField.getName())) { + assertEquals(field.getType(), raupenpilerField.getType()); + // assertEquals(field.getModifiers(), raupenpilerField.getModifiers()); + } + } + } + + } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + // Helpers + + public static void moveFile(String sourceFilePath, String destinationFilePath) throws IOException { + Path sourcePath = Paths.get(sourceFilePath); + Path destinationPath = Paths.get(destinationFilePath); + Files.move(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING); + } } \ No newline at end of file diff --git a/src/test/java/main/InputFilesTest.java b/src/test/java/main/InputFilesTest.java index 5d5e4a4..3ba18f3 100644 --- a/src/test/java/main/InputFilesTest.java +++ b/src/test/java/main/InputFilesTest.java @@ -1,5 +1,6 @@ package main; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import javax.tools.JavaCompiler; @@ -51,6 +52,133 @@ public class InputFilesTest { } } + @Test + public void areCombinedFeatureTestsValid() throws IOException { + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + + File combinedFeatureTests = new File("src/test/resources/input/combinedFeatureTests"); + + List files = getJavaFilesFromDirectory(combinedFeatureTests); + + if (!files.isEmpty()) { + for (File file : files) { + // Try to compile the file and get the result + int result = javac.run(null, null, null, file.getPath()); + + // Assert that the compilation succeeded (i.e., the result is zero) + assertEquals(0, result, "Expected compilation success for " + file.getName()); + } + } else { + System.out.println("No files found in the directories."); + } + } + + @Test + @Disabled + public void areEndabgabeTestsActuallyValid() throws IOException { + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + + File endabgabeTests = new File("src/test/resources/input/endabgabeTests"); + + List files = getJavaFilesFromDirectory(endabgabeTests); + + if (!files.isEmpty()) { + for (File file : files) { + // Try to compile the file and get the result + int result = javac.run(null, null, null, file.getPath()); + + // Assert that the compilation succeeded (i.e., the result is zero) + assertEquals(0, result, "Expected compilation success for " + file.getName()); + } + } else { + System.out.println("No files found in the directories."); + } + } + + @Test + @Disabled + public void areSingleFeatureSemanticTestsActuallyValid() throws IOException { + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + + File singleFeatureSemanticTests = new File("src/test/resources/input/singleFeatureSemanticTests"); + + List files = getJavaFilesFromDirectory(singleFeatureSemanticTests); + + if (!files.isEmpty()) { + for (File file : files) { + // Try to compile the file and get the result + int result = javac.run(null, null, null, file.getPath()); + + // Assert that the compilation succeeded (i.e., the result is zero) + assertEquals(0, result, "Expected compilation success for " + file.getName()); + } + } else { + System.out.println("No files found in the directories."); + } + } + + + @Test + public void areSingleFeatureTestsActuallyValid() throws IOException { + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + + File singleFeatureTests = new File("src/test/resources/input/singleFeatureTests"); + + List files = getJavaFilesFromDirectory(singleFeatureTests); + + if (!files.isEmpty()) { + for (File file : files) { + // Try to compile the file and get the result + int result = javac.run(null, null, null, file.getPath()); + + // Assert that the compilation succeeded (i.e., the result is zero) + assertEquals(0, result, "Expected compilation success for " + file.getName()); + } + } else { + System.out.println("No files found in the directories."); + } + } + + + @Test + @Disabled + public void areTypedAstFeatureTestsActuallyValid() throws IOException { + // Get the system Java compiler + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); + // Assert that the compiler is available + assertNotNull(javac, "Java Compiler is not available"); + + File typedAstFeatureTests = new File("src/test/resources/input/typedAstFeatureTests"); + + List files = getJavaFilesFromDirectory(typedAstFeatureTests); + + if (!files.isEmpty()) { + for (File file : files) { + // Try to compile the file and get the result + int result = javac.run(null, null, null, file.getPath()); + + // Assert that the compilation succeeded (i.e., the result is zero) + assertEquals(0, result, "Expected compilation success for " + file.getName()); + } + } else { + System.out.println("No files found in the directories."); + } + } + + + /** * This test method checks if invalid Java files fail to compile as expected. diff --git a/src/test/java/main/ReflectionsTest.java b/src/test/java/main/ReflectionsTest.java deleted file mode 100644 index d47d95a..0000000 --- a/src/test/java/main/ReflectionsTest.java +++ /dev/null @@ -1,145 +0,0 @@ -package main; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; - -import org.antlr.v4.runtime.CharStream; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - - -public class ReflectionsTest { - - @Test - public void testSimpleJavaLexerClass() throws ClassNotFoundException, NoSuchMethodException { - Class clazz = Class.forName("parser.generated.SimpleJavaLexer"); - - // Class Name - assertEquals("parser.generated.SimpleJavaLexer", clazz.getName()); - - // Constructors - Constructor[] actualConstructors = clazz.getDeclaredConstructors(); - assertTrue(actualConstructors.length > 0, "No constructors found"); - - Constructor expectedConstructor = clazz.getConstructor(CharStream.class); - - boolean constructorFound = false; - for (Constructor constructor : actualConstructors) { - if (constructor.equals(expectedConstructor)) { - constructorFound = true; - break; - } - } - assertTrue(constructorFound, "Expected constructor not found in actual constructors"); - - - - // Methods - Method[] actualMethodNames = clazz.getDeclaredMethods(); - assertTrue(actualMethodNames.length > 0); - Arrays.stream(actualMethodNames).forEach(method -> System.out.println("Method: " + method.getName())); - - List expectedMethodNames = Arrays.asList( - "getTokenNames", - "getVocabulary", - "getGrammarFileName", - "getRuleNames", - "getSerializedATN", - "getChannelNames", - "getModeNames", - "getATN", - "makeRuleNames", - "makeLiteralNames", - "makeSymbolicNames" - ); - - for (Method method : actualMethodNames) { - assertTrue(expectedMethodNames.contains(method.getName())); - } - - for (String expectedMethodName : expectedMethodNames) { - boolean methodFound = false; - for (Method method : actualMethodNames) { - if (method.getName().equals(expectedMethodName)) { - methodFound = true; - break; - } - } - assertTrue(methodFound, "Expected method " + expectedMethodName + " not found in actual methods"); - } - - - // Fields - Field[] actualFieldNames = clazz.getDeclaredFields(); - assertTrue(actualFieldNames.length > 0); - Arrays.stream(actualFieldNames).forEach(field -> System.out.println("Field: " + field.getName())); - - List expectedFieldNames = Arrays.asList( - "_decisionToDFA", - "_sharedContextCache", - "channelNames", - "modeNames", - "ruleNames", - "_LITERAL_NAMES", - "_SYMBOLIC_NAMES", - "VOCABULARY", - "tokenNames", - "_serializedATN", - "_ATN" - ); - - for (Field field : actualFieldNames) { - assertTrue(expectedFieldNames.contains(field.getName())); - } - } - - @Test - public void testSimpleJavaParserClass() throws ClassNotFoundException { - Class clazz = Class.forName("parser.generated.SimpleJavaParser"); - - // Class Name - assertEquals("parser.generated.SimpleJavaParser", clazz.getName()); - - // Constructors - Constructor[] constructors = clazz.getDeclaredConstructors(); - assertTrue(constructors.length > 0); - - // Methods - Method[] methods = clazz.getDeclaredMethods(); - assertTrue(methods.length > 0); - Arrays.stream(methods).forEach(method -> System.out.println("Method: " + method.getName())); - - // Fields - Field[] fields = clazz.getDeclaredFields(); - assertTrue(fields.length > 0); - Arrays.stream(fields).forEach(field -> System.out.println("Field: " + field.getName())); - } - - @Test - public void testASTBuilderClass() throws ClassNotFoundException { - Class clazz = Class.forName("parser.astBuilder.ASTBuilder"); - - // Class Name - assertEquals("parser.astBuilder.ASTBuilder", clazz.getName()); - - // Constructors - Constructor[] constructors = clazz.getDeclaredConstructors(); - assertTrue(constructors.length > 0); - - // Methods - Method[] methods = clazz.getDeclaredMethods(); - assertTrue(methods.length > 0); - Arrays.stream(methods).forEach(method -> System.out.println("Method: " + method.getName())); - - // Fields - Field[] fields = clazz.getDeclaredFields(); - assertTrue(fields.length > 0); - Arrays.stream(fields).forEach(field -> System.out.println("Field: " + field.getName())); - } - - // Similarly, you can add tests for SemanticAnalyzer and ByteCodeGenerator -}