From 19989bfe114a7d35a0d68e2a1a77c253344ee4d1 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 26 Dec 2019 19:43:00 +0100 Subject: [PATCH] Add Console Interface parameters, output directory and classpath --- pom.xml | 1 + .../dhbwstuttgart/core/ConsoleInterface.java | 26 ++++- .../java/packages/ConsoleInterfaceTest.java | 101 ++++++++++++++++++ 3 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/test/java/packages/ConsoleInterfaceTest.java diff --git a/pom.xml b/pom.xml index 98ea3933..5b9b2e73 100644 --- a/pom.xml +++ b/pom.xml @@ -164,6 +164,7 @@ http://maven.apache.org/maven-v4_0_0.xsd"> 1.8 1.8 0.23.0 + de.dhbwstuttgart.core.ConsoleInterface diff --git a/src/main/java/de/dhbwstuttgart/core/ConsoleInterface.java b/src/main/java/de/dhbwstuttgart/core/ConsoleInterface.java index 77a172bf..2742bbbf 100644 --- a/src/main/java/de/dhbwstuttgart/core/ConsoleInterface.java +++ b/src/main/java/de/dhbwstuttgart/core/ConsoleInterface.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.core; import java.io.File; import java.io.IOException; +import java.net.URL; import java.util.*; import java.util.stream.Collectors; @@ -9,8 +10,29 @@ public class ConsoleInterface { private static final String directory = System.getProperty("user.dir"); public static void main(String[] args) throws IOException, ClassNotFoundException { - List input = Arrays.asList(args).stream().map((s -> new File(s))).collect(Collectors.toList()); - JavaTXCompiler compiler = new JavaTXCompiler(input); + List input = new ArrayList<>(); + List classpath = new ArrayList<>(); + String outputPath = null; + Iterator it = Arrays.asList(args).iterator(); + while(it.hasNext()){ + String arg = it.next(); + if(arg.equals("-d")){ + outputPath = it.next(); + }else if(arg.startsWith("-d")) { + outputPath = arg.substring(2); + }else if(arg.equals("-cp")){ + String[] cps = it.next().split(":"); + for(String cp : cps){ + classpath.add(new URL("file://"+cp)); + } + }else{ + input.add(new File(arg)); + } + } + JavaTXCompiler compiler = new JavaTXCompiler(input, classpath); compiler.typeInference(); + compiler.generateBytecode(outputPath); } + + } diff --git a/src/test/java/packages/ConsoleInterfaceTest.java b/src/test/java/packages/ConsoleInterfaceTest.java new file mode 100644 index 00000000..32fffe0d --- /dev/null +++ b/src/test/java/packages/ConsoleInterfaceTest.java @@ -0,0 +1,101 @@ +package packages; + +import de.dhbwstuttgart.core.ConsoleInterface; +import de.dhbwstuttgart.core.JavaTXCompiler; +import junit.framework.TestCase; +import org.junit.Test; + +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; + +public class ConsoleInterfaceTest extends TestCase { + + public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/"; + + @Test + public void testCompileSingleJavFile() throws Exception { + File f = new File(rootDirectory + "de/test/TestClass.class"); + if(f.exists() && !f.isDirectory()) { + f.delete(); + } + + ConsoleInterface.main(new String[]{rootDirectory + "de/test/TestClass.jav"}); + + f = new File(rootDirectory + "de/test/TestClass.class"); + assertTrue(f.exists()); + } + + @Test + public void testCompileSingleJavFileWithOutputDirectory() throws Exception { + File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class"); + if(f.exists() && !f.isDirectory()) { + f.delete(); + } + + ConsoleInterface.main(new String[]{"-d", rootDirectory + "de/test/output/" ,rootDirectory + "de/test/TestClass.jav"}); + + f = new File(rootDirectory + "de/test/output/de/test/TestClass.class"); + assertTrue(f.exists()); + } + + @Test + public void testCompileSingleJavFileWithClassPath() throws Exception { + JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"/de/test/ToImport.jav")); + compiler.typeInference(); + compiler.generateBytecode(rootDirectory + "output/"); + File f = new File(rootDirectory + "output/de/test/ToImport.class"); + assertTrue(f.exists()); + + f = new File(rootDirectory + "de/test/ImportTest.class"); + if(f.exists() && !f.isDirectory()) { + f.delete(); + } + + ConsoleInterface.main(new String[]{"-cp", rootDirectory + "de/test/output/" ,rootDirectory + "de/test/ImportTest.jav"}); + + f = new File(rootDirectory + "de/test/ImportTest.class"); + assertTrue(f.exists()); + } + + @Test + public void testCompileSingleJavFileWithMultipleClassPath() throws Exception { + JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"/de/test/ToImport.jav")); + compiler.typeInference(); + compiler.generateBytecode(rootDirectory + "output/"); + File f = new File(rootDirectory + "output/de/test/ToImport.class"); + assertTrue(f.exists()); + + f = new File(rootDirectory + "de/test/ImportTest.class"); + if(f.exists() && !f.isDirectory()) { + f.delete(); + } + + ConsoleInterface.main(new String[]{"-cp", rootDirectory + "de/test/output/:"+rootDirectory+"de", + rootDirectory + "de/test/ImportTest.jav"}); + + f = new File(rootDirectory + "de/test/ImportTest.class"); + assertTrue(f.exists()); + } + + @Test + public void testCompileSingleJavFileWithClassPathAndOutputDir() throws Exception { + JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"/de/test/ToImport.jav")); + compiler.typeInference(); + compiler.generateBytecode(rootDirectory + "output/"); + File f = new File(rootDirectory + "output/de/test/ToImport.class"); + assertTrue(f.exists()); + + f = new File(rootDirectory + "de/test/output/de/test/ImportTest.class"); + if(f.exists() && !f.isDirectory()) { + f.delete(); + } + + ConsoleInterface.main(new String[]{"-cp", rootDirectory + "de/test/output/", + "-d"+rootDirectory + "de/test/output/" ,rootDirectory + "de/test/ImportTest.jav"}); + + f = new File(rootDirectory + "de/test/output/de/test/ImportTest.class"); + assertTrue(f.exists()); + } + +}