Skip to content

Restructured tests. Source code is now in own files, so I can reuse them for testing from other programming languages. #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 24 additions & 49 deletions src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Class<?>> compiled = InMemoryJavaCompiler.newInstance().addSource("A", cls1).addSource("B", cls2).compileAll();
Map<String, Class<?>> 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"));
Expand All @@ -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);
}
Expand All @@ -64,51 +63,27 @@ 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<String> 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<String> 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());
}

@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<String> 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;
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/compileAll_WhenTypical/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public class A{ public B b() { return new B(); }}
1 change: 1 addition & 0 deletions src/test/resources/compileAll_WhenTypical/B.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public class B{ public String toString() { return "B!"; }}
4 changes: 4 additions & 0 deletions src/test/resources/compile_WhenFailOnWarnings/HelloClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.mdkt;
public class HelloClass {
public java.util.List<String> hello() { return new java.util.ArrayList(); }
}
4 changes: 4 additions & 0 deletions src/test/resources/compile_WhenIgnoreWarnings/HelloClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.mdkt;
public class HelloClass {
public java.util.List<String> hello() { return new java.util.ArrayList(); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mdkt;
public class HelloClass {
private static class InnerHelloWorld { int inner; }
public String hello() { return "hello"; }
}
4 changes: 4 additions & 0 deletions src/test/resources/compile_WhenTypical/HelloClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.mdkt;
public class HelloClass {
public String hello() { return "hello"; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.mdkt;
public class HelloClass extends xxx {
public java.util.List<String> hello() { return new java.util.ArrayList(); }
}
4 changes: 4 additions & 0 deletions src/test/resources/compile_whenError/HelloClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.mdkt;
public classHelloClass {
public String hello() { return "hello"; }
}