-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathInMemoryJavaCompilerTest.java
More file actions
92 lines (77 loc) · 3.52 KB
/
InMemoryJavaCompilerTest.java
File metadata and controls
92 lines (77 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package org.mdkt.compiler;
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;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class InMemoryJavaCompilerTest {
private static final Logger logger = LoggerFactory.getLogger(InMemoryJavaCompilerTest.class);
@Rule
public ExpectedException thrown = ExpectedException.none();
public String getResourceAsString(String path) throws Exception {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream srcResStream = classloader.getResourceAsStream(path);
// 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);
}
@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 {
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"));
Class<?> aClass = compiled.get("A");
Object a = aClass.newInstance();
Assert.assertEquals("B!", aClass.getMethod("b").invoke(a).toString());
}
@Test
public void compile_WhenSourceContainsInnerClasses() throws Exception {
Class<?> helloClass = InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", getResourceAsString("compile_WhenSourceContainsInnerClasses/HelloClass.java"));
Assert.assertNotNull(helloClass);
Assert.assertEquals(1, helloClass.getDeclaredMethods().length);
}
@Test
public void compile_whenError() throws Exception {
thrown.expect(CompilationException.class);
thrown.expectMessage("Unable to compile the source");
InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", getResourceAsString("compile_whenError/HelloClass.java"));
}
@Test
public void compile_WhenFailOnWarnings() throws Exception {
thrown.expect(CompilationException.class);
InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", getResourceAsString("compile_WhenFailOnWarnings/HelloClass.java"));
}
@Test
public void compile_WhenIgnoreWarnings() throws Exception {
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);
try {
InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", getResourceAsString("compile_WhenWarningsAndErrors/HelloClass.java"));
} catch (Exception e) {
logger.info("Exception caught: {}", e);
throw e;
}
}
}