-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathInMemoryJavaCompilerTest.java
More file actions
143 lines (109 loc) · 5 KB
/
InMemoryJavaCompilerTest.java
File metadata and controls
143 lines (109 loc) · 5 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package org.mdkt.compiler;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
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();
@Test
public void compile_WhenTypical() throws Exception {
StringBuffer sourceCode = new StringBuffer();
sourceCode.append("package org.mdkt;\n");
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" public String hello() { return \"hello\"; }");
sourceCode.append("}");
Class<?> helloClass = InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString());
Assert.assertNotNull(helloClass);
Assert.assertEquals(1, helloClass.getDeclaredMethods().length);
}
@Test
public void compile_WhenTypicalUpdateClass() throws Exception {
StringBuffer sourceCode = new StringBuffer();
sourceCode.append("package org.mdkt.compiler;\n");
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" public String hello() { return \"hello1\"; }");
sourceCode.append("}");
Class<?> oldClass = HelloClass.class;
Class<?> newClass = InMemoryJavaCompiler.newInstance().compile("org.mdkt.compiler.HelloClass", sourceCode.toString());
Assert.assertNotEquals(oldClass.hashCode() , newClass.hashCode());
Assert.assertNotEquals(oldClass.getDeclaredMethod("hello").invoke(oldClass.newInstance()) ,
newClass.getDeclaredMethod("hello").invoke(newClass.newInstance()));
}
@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();
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 {
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());
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");
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());
}
@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());
}
@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());
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());
} catch (Exception e) {
logger.info("Exception caught: {}", e);
throw e;
}
}
}