1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jci.compilers;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import junit.framework.TestCase;
24
25 import org.apache.commons.jci.problems.CompilationProblem;
26 import org.apache.commons.jci.readers.ResourceReader;
27 import org.apache.commons.jci.stores.MemoryResourceStore;
28
29
30
31
32
33
34 public abstract class AbstractCompilerTestCase extends TestCase {
35
36 public abstract JavaCompiler createJavaCompiler();
37
38 public abstract String getCompilerName();
39
40 public void testFactoryCreation() {
41 final JavaCompiler factoryCompiler = new JavaCompilerFactory().createCompiler(getCompilerName());
42 assertNotNull(factoryCompiler);
43
44 final JavaCompiler compiler = createJavaCompiler();
45 assertEquals(factoryCompiler.getClass().getName(), compiler.getClass().getName());
46 }
47
48 public void testSimpleCompile() throws Exception {
49 final JavaCompiler compiler = createJavaCompiler();
50
51 final ResourceReader reader = new ResourceReader() {
52 final private Map sources = new HashMap() {
53 private static final long serialVersionUID = 1L;
54 {
55 put("jci/Simple.java", (
56 "package jci;\n" +
57 "public class Simple {\n" +
58 " public String toString() {\n" +
59 " return \"Simple\";\n" +
60 " }\n" +
61 "}").getBytes());
62 }};
63
64 public byte[] getBytes( final String pResourceName ) {
65 return (byte[]) sources.get(pResourceName);
66 }
67
68 public boolean isAvailable( final String pResourceName ) {
69 return sources.containsKey(pResourceName);
70 }
71
72 };
73
74 final MemoryResourceStore store = new MemoryResourceStore();
75 final CompilationResult result = compiler.compile(
76 new String[] {
77 "jci/Simple.java"
78 }, reader, store);
79
80 assertEquals(toString(result.getErrors()), 0, result.getErrors().length);
81 assertEquals(toString(result.getWarnings()), 0, result.getWarnings().length);
82
83 final byte[] clazzBytes = store.read("jci/Simple.class");
84 assertNotNull(clazzBytes);
85 assertTrue(clazzBytes.length > 0);
86 }
87
88 public void testExtendedCompile() throws Exception {
89 final JavaCompiler compiler = createJavaCompiler();
90
91 final ResourceReader reader = new ResourceReader() {
92 final private Map sources = new HashMap() {
93 private static final long serialVersionUID = 1L;
94 {
95 put("jci/Simple.java", (
96 "package jci;\n" +
97 "public class Simple {\n" +
98 " public String toString() {\n" +
99 " return \"Simple\";\n" +
100 " }\n" +
101 "}").getBytes());
102 put("jci/Extended.java", (
103 "package jci;\n" +
104 "public class Extended extends Simple {\n" +
105 " public String toString() {\n" +
106 " return \"Extended\" + super.toString();\n" +
107 " }\n" +
108 "}").getBytes());
109 }};
110
111 public byte[] getBytes( final String pResourceName ) {
112 return (byte[]) sources.get(pResourceName);
113 }
114
115 public boolean isAvailable( final String pResourceName ) {
116 return sources.containsKey(pResourceName);
117 }
118
119 };
120
121 final MemoryResourceStore store = new MemoryResourceStore();
122 final CompilationResult result = compiler.compile(
123 new String[] {
124 "jci/Extended.java",
125 "jci/Simple.java"
126 }, reader, store);
127
128 assertEquals(toString(result.getErrors()), 0, result.getErrors().length);
129 assertEquals(toString(result.getWarnings()), 0, result.getWarnings().length);
130
131 final byte[] clazzBytesSimple = store.read("jci/Simple.class");
132 assertNotNull(clazzBytesSimple);
133 assertTrue(clazzBytesSimple.length > 0);
134
135 final byte[] clazzBytesExtended = store.read("jci/Extended.class");
136 assertNotNull(clazzBytesExtended);
137 assertTrue(clazzBytesExtended.length > 0);
138 }
139
140 public void testInternalClassCompile() throws Exception {
141 final JavaCompiler compiler = createJavaCompiler();
142
143 final ResourceReader reader = new ResourceReader() {
144 final private Map sources = new HashMap() {
145 private static final long serialVersionUID = 1L;
146 {
147 put("jci/Simple.java", (
148 "package jci;\n" +
149 "public class Simple {\n" +
150 " private class Sub {\n" +
151 " }\n" +
152 " public String toString() {\n" +
153 " new Sub();\n" +
154 " return \"Simple\";\n" +
155 " }\n" +
156 "}").getBytes());
157 }};
158
159 public byte[] getBytes( final String pResourceName ) {
160 return (byte[]) sources.get(pResourceName);
161 }
162
163 public boolean isAvailable( final String pResourceName ) {
164 return sources.containsKey(pResourceName);
165 }
166
167 };
168
169 final MemoryResourceStore store = new MemoryResourceStore();
170 final CompilationResult result = compiler.compile(
171 new String[] {
172 "jci/Simple.java"
173 }, reader, store);
174
175 assertEquals(toString(result.getErrors()), 0, result.getErrors().length);
176 assertEquals(toString(result.getWarnings()), 0, result.getWarnings().length);
177
178 final byte[] clazzBytes = store.read("jci/Simple.class");
179 assertNotNull(clazzBytes);
180 assertTrue(clazzBytes.length > 0);
181
182 final byte[] subClazzBytes = store.read("jci/Simple$Sub.class");
183 assertNotNull(subClazzBytes);
184 assertTrue(subClazzBytes.length > 0);
185
186 }
187
188 public void testUppercasePackageNameCompile() throws Exception {
189 final JavaCompiler compiler = createJavaCompiler();
190
191 final ResourceReader reader = new ResourceReader() {
192 final private Map sources = new HashMap() {
193 private static final long serialVersionUID = 1L;
194 {
195 put("Jci/Simple.java", (
196 "package Jci;\n" +
197 "public class Simple {\n" +
198 " public String toString() {\n" +
199 " return \"Simple\";\n" +
200 " }\n" +
201 "}").getBytes());
202 }};
203
204 public byte[] getBytes( final String pResourceName ) {
205 return (byte[]) sources.get(pResourceName);
206 }
207
208 public boolean isAvailable( final String pResourceName ) {
209 return sources.containsKey(pResourceName);
210 }
211
212 };
213
214 final MemoryResourceStore store = new MemoryResourceStore();
215 final CompilationResult result = compiler.compile(
216 new String[] {
217 "Jci/Simple.java"
218 }, reader, store);
219
220 assertEquals(toString(result.getErrors()), 0, result.getErrors().length);
221 assertEquals(toString(result.getWarnings()), 0, result.getWarnings().length);
222
223 final byte[] clazzBytes = store.read("Jci/Simple.class");
224 assertNotNull(clazzBytes);
225 assertTrue(clazzBytes.length > 0);
226 }
227
228
229
230
231 public void testCrossReferenceCompilation() throws Exception {
232 final String javaVersion = System.getProperty("java.version");
233 if (!(javaVersion.startsWith("1.5") || javaVersion.startsWith("1.6"))) {
234 System.err.println("WARNING! Skipping testCrossReferenceCompilation() because your runtime does not support java 1.5+ yet");
235 return;
236 }
237
238 final JavaCompiler compiler = createJavaCompiler();
239
240 final ResourceReader reader = new ResourceReader() {
241 final private Map sources = new HashMap() {
242 private static final long serialVersionUID = 1L;
243 {
244 put("jci/Func1.java", (
245 "package jci;\n" +
246 "import static jci.Func2.func2;" +
247 "public class Func1 {\n" +
248 " public static boolean func1() throws Exception {\n" +
249 " return true;\n" +
250 " }\n" +
251 "}").getBytes());
252 put("jci/Func2.java", (
253 "package jci;\n" +
254 "import static jci.Func1.func1;" +
255 "public class Func2 {\n" +
256 " public static boolean func2() throws Exception {\n" +
257 " return true;\n" +
258 " }\n" +
259 "}").getBytes());
260 }};
261
262 public byte[] getBytes( final String pResourceName ) {
263 return (byte[]) sources.get(pResourceName);
264 }
265
266 public boolean isAvailable( final String pResourceName ) {
267 return sources.containsKey(pResourceName);
268 }
269
270 };
271
272 final JavaCompilerSettings settings = compiler.createDefaultSettings();
273 settings.setTargetVersion("1.5");
274 settings.setSourceVersion("1.5");
275
276 final MemoryResourceStore store = new MemoryResourceStore();
277 final CompilationResult result = compiler.compile(
278 new String[] {
279 "jci/Func1.java",
280 "jci/Func2.java"
281 }, reader, store, this.getClass().getClassLoader(), settings);
282
283 assertEquals(toString(result.getErrors()), 0, result.getErrors().length);
284 assertEquals(toString(result.getWarnings()), 0, result.getWarnings().length);
285
286 final byte[] clazzBytesFunc1 = store.read("jci/Func1.class");
287 assertNotNull(clazzBytesFunc1);
288 assertTrue(clazzBytesFunc1.length > 0);
289
290 final byte[] clazzBytesFunc2 = store.read("jci/Func2.class");
291 assertNotNull(clazzBytesFunc2);
292 assertTrue(clazzBytesFunc2.length > 0);
293 }
294
295
296
297 public final String toString( final CompilationProblem[] pProblems ) {
298 final StringBuffer sb = new StringBuffer();
299
300 for (int i = 0; i < pProblems.length; i++) {
301 final CompilationProblem problem = pProblems[i];
302 sb.append(problem.getMessage()).append(", ");
303 }
304
305 return sb.toString();
306 }
307
308 }