001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.jci.compilers;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.commons.jci.problems.CompilationProblem;
026: import org.apache.commons.jci.readers.ResourceReader;
027: import org.apache.commons.jci.stores.MemoryResourceStore;
028:
029: /**
030: * Providing convenience methods for JavaCompiler TestCases
031: *
032: * @author tcurdt
033: */
034: public abstract class AbstractCompilerTestCase extends TestCase {
035:
036: public abstract JavaCompiler createJavaCompiler();
037:
038: public abstract String getCompilerName();
039:
040: public void testFactoryCreation() {
041: final JavaCompiler factoryCompiler = new JavaCompilerFactory()
042: .createCompiler(getCompilerName());
043: assertNotNull(factoryCompiler);
044:
045: final JavaCompiler compiler = createJavaCompiler();
046: assertEquals(factoryCompiler.getClass().getName(), compiler
047: .getClass().getName());
048: }
049:
050: public void testSimpleCompile() throws Exception {
051: final JavaCompiler compiler = createJavaCompiler();
052:
053: final ResourceReader reader = new ResourceReader() {
054: final private Map sources = new HashMap() {
055: private static final long serialVersionUID = 1L;
056: {
057: put("jci/Simple.java", ("package jci;\n"
058: + "public class Simple {\n"
059: + " public String toString() {\n"
060: + " return \"Simple\";\n" + " }\n"
061: + "}").getBytes());
062: }
063: };
064:
065: public byte[] getBytes(final String pResourceName) {
066: return (byte[]) sources.get(pResourceName);
067: }
068:
069: public boolean isAvailable(final String pResourceName) {
070: return sources.containsKey(pResourceName);
071: }
072:
073: };
074:
075: final MemoryResourceStore store = new MemoryResourceStore();
076: final CompilationResult result = compiler.compile(
077: new String[] { "jci/Simple.java" }, reader, store);
078:
079: assertEquals(toString(result.getErrors()), 0, result
080: .getErrors().length);
081: assertEquals(toString(result.getWarnings()), 0, result
082: .getWarnings().length);
083:
084: final byte[] clazzBytes = store.read("jci/Simple.class");
085: assertNotNull(clazzBytes);
086: assertTrue(clazzBytes.length > 0);
087: }
088:
089: public void testExtendedCompile() throws Exception {
090: final JavaCompiler compiler = createJavaCompiler();
091:
092: final ResourceReader reader = new ResourceReader() {
093: final private Map sources = new HashMap() {
094: private static final long serialVersionUID = 1L;
095: {
096: put("jci/Simple.java", ("package jci;\n"
097: + "public class Simple {\n"
098: + " public String toString() {\n"
099: + " return \"Simple\";\n" + " }\n"
100: + "}").getBytes());
101: put(
102: "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" + "}").getBytes());
108: }
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
123: .compile(new String[] { "jci/Extended.java",
124: "jci/Simple.java" }, reader, store);
125:
126: assertEquals(toString(result.getErrors()), 0, result
127: .getErrors().length);
128: assertEquals(toString(result.getWarnings()), 0, result
129: .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
136: .read("jci/Extended.class");
137: assertNotNull(clazzBytesExtended);
138: assertTrue(clazzBytesExtended.length > 0);
139: }
140:
141: public void testInternalClassCompile() throws Exception {
142: final JavaCompiler compiler = createJavaCompiler();
143:
144: final ResourceReader reader = new ResourceReader() {
145: final private Map sources = new HashMap() {
146: private static final long serialVersionUID = 1L;
147: {
148: put("jci/Simple.java", ("package jci;\n"
149: + "public class Simple {\n"
150: + " private class Sub {\n" + " }\n"
151: + " public String toString() {\n"
152: + " new Sub();\n"
153: + " return \"Simple\";\n" + " }\n"
154: + "}").getBytes());
155: }
156: };
157:
158: public byte[] getBytes(final String pResourceName) {
159: return (byte[]) sources.get(pResourceName);
160: }
161:
162: public boolean isAvailable(final String pResourceName) {
163: return sources.containsKey(pResourceName);
164: }
165:
166: };
167:
168: final MemoryResourceStore store = new MemoryResourceStore();
169: final CompilationResult result = compiler.compile(
170: new String[] { "jci/Simple.java" }, reader, store);
171:
172: assertEquals(toString(result.getErrors()), 0, result
173: .getErrors().length);
174: assertEquals(toString(result.getWarnings()), 0, result
175: .getWarnings().length);
176:
177: final byte[] clazzBytes = store.read("jci/Simple.class");
178: assertNotNull(clazzBytes);
179: assertTrue(clazzBytes.length > 0);
180:
181: final byte[] subClazzBytes = store.read("jci/Simple$Sub.class");
182: assertNotNull(subClazzBytes);
183: assertTrue(subClazzBytes.length > 0);
184:
185: }
186:
187: public void testUppercasePackageNameCompile() throws Exception {
188: final JavaCompiler compiler = createJavaCompiler();
189:
190: final ResourceReader reader = new ResourceReader() {
191: final private Map sources = new HashMap() {
192: private static final long serialVersionUID = 1L;
193: {
194: put("Jci/Simple.java", ("package Jci;\n"
195: + "public class Simple {\n"
196: + " public String toString() {\n"
197: + " return \"Simple\";\n" + " }\n"
198: + "}").getBytes());
199: }
200: };
201:
202: public byte[] getBytes(final String pResourceName) {
203: return (byte[]) sources.get(pResourceName);
204: }
205:
206: public boolean isAvailable(final String pResourceName) {
207: return sources.containsKey(pResourceName);
208: }
209:
210: };
211:
212: final MemoryResourceStore store = new MemoryResourceStore();
213: final CompilationResult result = compiler.compile(
214: new String[] { "Jci/Simple.java" }, reader, store);
215:
216: assertEquals(toString(result.getErrors()), 0, result
217: .getErrors().length);
218: assertEquals(toString(result.getWarnings()), 0, result
219: .getWarnings().length);
220:
221: final byte[] clazzBytes = store.read("Jci/Simple.class");
222: assertNotNull(clazzBytes);
223: assertTrue(clazzBytes.length > 0);
224: }
225:
226: public final String toString(final CompilationProblem[] pProblems) {
227: final StringBuffer sb = new StringBuffer();
228:
229: for (int i = 0; i < pProblems.length; i++) {
230: final CompilationProblem problem = pProblems[i];
231: sb.append(problem.getMessage()).append(", ");
232: }
233:
234: return sb.toString();
235: }
236:
237: }
|