diff --git a/src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java b/src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java index c3c16e8..88a2f0f 100644 --- a/src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java +++ b/src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java @@ -2,6 +2,9 @@ import java.util.List; import java.util.Map; +import java.io.InputStream; +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; import org.junit.Assert; import org.junit.Rule; @@ -16,26 +19,30 @@ public class InMemoryJavaCompilerTest { @Rule public ExpectedException thrown = ExpectedException.none(); - @Test - public void compile_WhenTypical() throws Exception { - StringBuffer sourceCode = new StringBuffer(); + public String getResourceAsString(String path) throws Exception { + ClassLoader classloader = Thread.currentThread().getContextClassLoader(); + InputStream srcResStream = classloader.getResourceAsStream(path); - sourceCode.append("package org.mdkt;\n"); - sourceCode.append("public class HelloClass {\n"); - sourceCode.append(" public String hello() { return \"hello\"; }"); - sourceCode.append("}"); + // Thanks to https://www.baeldung.com/convert-input-stream-to-string + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + byte[] chunk = new byte[4096]; + int bytesRead = 0; + while ((bytesRead = srcResStream.read(chunk, 0, chunk.length)) != -1) { + buf.write(chunk, 0, bytesRead); + } + return new String(buf.toByteArray(), StandardCharsets.UTF_8); + } - Class helloClass = InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString()); + @Test + public void compile_WhenTypical() throws Exception { + Class helloClass = InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", getResourceAsString("compile_WhenTypical/HelloClass.java")); Assert.assertNotNull(helloClass); Assert.assertEquals(1, helloClass.getDeclaredMethods().length); } @Test public void compileAll_WhenTypical() throws Exception { - String cls1 = "public class A{ public B b() { return new B(); }}"; - String cls2 = "public class B{ public String toString() { return \"B!\"; }}"; - - Map> compiled = InMemoryJavaCompiler.newInstance().addSource("A", cls1).addSource("B", cls2).compileAll(); + Map> compiled = InMemoryJavaCompiler.newInstance().addSource("A", getResourceAsString("compileAll_WhenTypical/A.java")).addSource("B", getResourceAsString("compileAll_WhenTypical/B.java")).compileAll(); Assert.assertNotNull(compiled.get("A")); Assert.assertNotNull(compiled.get("B")); @@ -47,15 +54,7 @@ public void compileAll_WhenTypical() throws Exception { @Test public void compile_WhenSourceContainsInnerClasses() throws Exception { - StringBuffer sourceCode = new StringBuffer(); - - sourceCode.append("package org.mdkt;\n"); - sourceCode.append("public class HelloClass {\n"); - sourceCode.append(" private static class InnerHelloWorld { int inner; }\n"); - sourceCode.append(" public String hello() { return \"hello\"; }"); - sourceCode.append("}"); - - Class helloClass = InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString()); + Class helloClass = InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", getResourceAsString("compile_WhenSourceContainsInnerClasses/HelloClass.java")); Assert.assertNotNull(helloClass); Assert.assertEquals(1, helloClass.getDeclaredMethods().length); } @@ -64,36 +63,18 @@ public void compile_WhenSourceContainsInnerClasses() throws Exception { public void compile_whenError() throws Exception { thrown.expect(CompilationException.class); thrown.expectMessage("Unable to compile the source"); - StringBuffer sourceCode = new StringBuffer(); - - sourceCode.append("package org.mdkt;\n"); - sourceCode.append("public classHelloClass {\n"); - sourceCode.append(" public String hello() { return \"hello\"; }"); - sourceCode.append("}"); - InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString()); + InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", getResourceAsString("compile_whenError/HelloClass.java")); } @Test public void compile_WhenFailOnWarnings() throws Exception { thrown.expect(CompilationException.class); - StringBuffer sourceCode = new StringBuffer(); - - sourceCode.append("package org.mdkt;\n"); - sourceCode.append("public class HelloClass {\n"); - sourceCode.append(" public java.util.List hello() { return new java.util.ArrayList(); }"); - sourceCode.append("}"); - InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString()); + InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", getResourceAsString("compile_WhenFailOnWarnings/HelloClass.java")); } @Test public void compile_WhenIgnoreWarnings() throws Exception { - StringBuffer sourceCode = new StringBuffer(); - - sourceCode.append("package org.mdkt;\n"); - sourceCode.append("public class HelloClass {\n"); - sourceCode.append(" public java.util.List hello() { return new java.util.ArrayList(); }"); - sourceCode.append("}"); - Class helloClass = InMemoryJavaCompiler.newInstance().ignoreWarnings().compile("org.mdkt.HelloClass", sourceCode.toString()); + Class helloClass = InMemoryJavaCompiler.newInstance().ignoreWarnings().compile("org.mdkt.HelloClass", getResourceAsString("compile_WhenIgnoreWarnings/HelloClass.java")); List res = (List) helloClass.getMethod("hello").invoke(helloClass.newInstance()); Assert.assertEquals(0, res.size()); } @@ -101,14 +82,8 @@ public void compile_WhenIgnoreWarnings() throws Exception { @Test public void compile_WhenWarningsAndErrors() throws Exception { thrown.expect(CompilationException.class); - StringBuffer sourceCode = new StringBuffer(); - - sourceCode.append("package org.mdkt;\n"); - sourceCode.append("public class HelloClass extends xxx {\n"); - sourceCode.append(" public java.util.List hello() { return new java.util.ArrayList(); }"); - sourceCode.append("}"); try { - InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString()); + InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", getResourceAsString("compile_WhenWarningsAndErrors/HelloClass.java")); } catch (Exception e) { logger.info("Exception caught: {}", e); throw e; diff --git a/src/test/resources/compileAll_WhenTypical/A.java b/src/test/resources/compileAll_WhenTypical/A.java new file mode 100644 index 0000000..5831159 --- /dev/null +++ b/src/test/resources/compileAll_WhenTypical/A.java @@ -0,0 +1 @@ +public class A{ public B b() { return new B(); }} diff --git a/src/test/resources/compileAll_WhenTypical/B.java b/src/test/resources/compileAll_WhenTypical/B.java new file mode 100644 index 0000000..bbe2c89 --- /dev/null +++ b/src/test/resources/compileAll_WhenTypical/B.java @@ -0,0 +1 @@ +public class B{ public String toString() { return "B!"; }} diff --git a/src/test/resources/compile_WhenFailOnWarnings/HelloClass.java b/src/test/resources/compile_WhenFailOnWarnings/HelloClass.java new file mode 100644 index 0000000..028f6d5 --- /dev/null +++ b/src/test/resources/compile_WhenFailOnWarnings/HelloClass.java @@ -0,0 +1,4 @@ +package org.mdkt; +public class HelloClass { + public java.util.List hello() { return new java.util.ArrayList(); } +} diff --git a/src/test/resources/compile_WhenIgnoreWarnings/HelloClass.java b/src/test/resources/compile_WhenIgnoreWarnings/HelloClass.java new file mode 100644 index 0000000..028f6d5 --- /dev/null +++ b/src/test/resources/compile_WhenIgnoreWarnings/HelloClass.java @@ -0,0 +1,4 @@ +package org.mdkt; +public class HelloClass { + public java.util.List hello() { return new java.util.ArrayList(); } +} diff --git a/src/test/resources/compile_WhenSourceContainsInnerClasses/HelloClass.java b/src/test/resources/compile_WhenSourceContainsInnerClasses/HelloClass.java new file mode 100644 index 0000000..9250916 --- /dev/null +++ b/src/test/resources/compile_WhenSourceContainsInnerClasses/HelloClass.java @@ -0,0 +1,5 @@ +package org.mdkt; +public class HelloClass { + private static class InnerHelloWorld { int inner; } + public String hello() { return "hello"; } +} diff --git a/src/test/resources/compile_WhenTypical/HelloClass.java b/src/test/resources/compile_WhenTypical/HelloClass.java new file mode 100644 index 0000000..10982c7 --- /dev/null +++ b/src/test/resources/compile_WhenTypical/HelloClass.java @@ -0,0 +1,4 @@ +package org.mdkt; +public class HelloClass { + public String hello() { return "hello"; } +} diff --git a/src/test/resources/compile_WhenWarningsAndErrors/HelloClass.java b/src/test/resources/compile_WhenWarningsAndErrors/HelloClass.java new file mode 100644 index 0000000..0abfcf4 --- /dev/null +++ b/src/test/resources/compile_WhenWarningsAndErrors/HelloClass.java @@ -0,0 +1,4 @@ +package org.mdkt; +public class HelloClass extends xxx { + public java.util.List hello() { return new java.util.ArrayList(); } +} diff --git a/src/test/resources/compile_whenError/HelloClass.java b/src/test/resources/compile_whenError/HelloClass.java new file mode 100644 index 0000000..4679a8e --- /dev/null +++ b/src/test/resources/compile_whenError/HelloClass.java @@ -0,0 +1,4 @@ +package org.mdkt; +public classHelloClass { + public String hello() { return "hello"; } +}