001: /*
002: * @(#)PostCompileClassUTest.java
003: *
004: * Copyright (C) 2002,2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.compiler;
028:
029: import java.io.ByteArrayOutputStream;
030: import java.io.IOException;
031: import java.util.HashMap;
032: import java.util.LinkedList;
033: import java.util.List;
034: import java.util.Map;
035:
036: import junit.framework.Test;
037: import junit.framework.TestCase;
038: import junit.framework.TestSuite;
039: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
040: import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil;
041: import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
042: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
043: import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord;
044: import net.sourceforge.groboutils.codecoverage.v2.datastore.IClassMetaDataWriter;
045: import net.sourceforge.groboutils.codecoverage.v2.datastore.IMetaDataWriter;
046: import net.sourceforge.groboutils.codecoverage.v2.logger.TestLogger;
047:
048: /**
049: * Tests the PostCompileClass class.
050: *
051: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
052: * @version $Date: 2004/04/15 05:48:28 $
053: * @since December 28, 2002
054: */
055: public class PostCompileClassUTest extends TestCase {
056: //-------------------------------------------------------------------------
057: // Standard JUnit Class-specific declarations
058:
059: private static final Class THIS_CLASS = PostCompileClassUTest.class;
060: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
061:
062: public PostCompileClassUTest(String name) {
063: super (name);
064: }
065:
066: //-------------------------------------------------------------------------
067: // Tests
068:
069: public void testConstructor1() {
070: try {
071: new PostCompileClass(null, null);
072: fail("Did not throw IllegalArgumentException.");
073: } catch (IllegalArgumentException ex) {
074: // test exception?
075: }
076: }
077:
078: public void testConstructor2() {
079: try {
080: new PostCompileClass(createIMetaDataWriter(), null);
081: fail("Did not throw IllegalArgumentException.");
082: } catch (IllegalArgumentException ex) {
083: // test exception?
084: }
085: }
086:
087: public void testConstructor3() {
088: try {
089: new PostCompileClass(null, createAnalysisModules());
090: fail("Did not throw IllegalArgumentException.");
091: } catch (IllegalArgumentException ex) {
092: // test exception?
093: }
094: }
095:
096: public void testConstructor4() {
097: try {
098: new PostCompileClass(null, new IAnalysisModule[1]);
099: fail("Did not throw IllegalArgumentException.");
100: } catch (IllegalArgumentException ex) {
101: // test exception?
102: }
103: }
104:
105: public void testConstructor5() {
106: try {
107: new PostCompileClass(createIMetaDataWriter(),
108: new IAnalysisModule[1]);
109: fail("Did not throw IllegalArgumentException.");
110: } catch (IllegalArgumentException ex) {
111: // test exception?
112: }
113: }
114:
115: public void testConstructor6() {
116: new PostCompileClass(createIMetaDataWriter(),
117: createAnalysisModules());
118: }
119:
120: //-------------
121:
122: public void testConstructor7() {
123: try {
124: new PostCompileClass(null, null, null);
125: fail("Did not throw IllegalArgumentException.");
126: } catch (IllegalArgumentException ex) {
127: // test exception?
128: }
129: }
130:
131: public void testConstructor8() {
132: try {
133: new PostCompileClass(null, createIMetaDataWriter(), null);
134: fail("Did not throw IllegalArgumentException.");
135: } catch (IllegalArgumentException ex) {
136: // test exception?
137: }
138: }
139:
140: public void testConstructor9() {
141: try {
142: new PostCompileClass(null, null, createAnalysisModules());
143: fail("Did not throw IllegalArgumentException.");
144: } catch (IllegalArgumentException ex) {
145: // test exception?
146: }
147: }
148:
149: public void testConstructor10() {
150: try {
151: new PostCompileClass(null, null, new IAnalysisModule[1]);
152: fail("Did not throw IllegalArgumentException.");
153: } catch (IllegalArgumentException ex) {
154: // test exception?
155: }
156: }
157:
158: public void testConstructor11() {
159: try {
160: new PostCompileClass(null, createIMetaDataWriter(),
161: new IAnalysisModule[1]);
162: fail("Did not throw IllegalArgumentException.");
163: } catch (IllegalArgumentException ex) {
164: // test exception?
165: }
166: }
167:
168: public void testConstructor12() {
169: try {
170: new PostCompileClass(null, createIMetaDataWriter(),
171: createAnalysisModules());
172: fail("Did not throw IllegalArgumentException.");
173: } catch (IllegalArgumentException ex) {
174: // test exception
175: }
176: }
177:
178: public void testConstructor13() {
179: try {
180: new PostCompileClass(createParseCoverageLogger(),
181: createIMetaDataWriter(), null);
182: fail("Did not throw IllegalArgumentException.");
183: } catch (IllegalArgumentException ex) {
184: // test exception?
185: }
186: }
187:
188: public void testConstructor14() {
189: try {
190: new PostCompileClass(createParseCoverageLogger(), null,
191: createAnalysisModules());
192: fail("Did not throw IllegalArgumentException.");
193: } catch (IllegalArgumentException ex) {
194: // test exception?
195: }
196: }
197:
198: public void testConstructor15() {
199: try {
200: new PostCompileClass(createParseCoverageLogger(), null,
201: new IAnalysisModule[1]);
202: fail("Did not throw IllegalArgumentException.");
203: } catch (IllegalArgumentException ex) {
204: // test exception?
205: }
206: }
207:
208: public void testConstructor16() {
209: try {
210: new PostCompileClass(createParseCoverageLogger(),
211: createIMetaDataWriter(), new IAnalysisModule[1]);
212: fail("Did not throw IllegalArgumentException.");
213: } catch (IllegalArgumentException ex) {
214: // test exception?
215: }
216: }
217:
218: public void testConstructor17() {
219: new PostCompileClass(createParseCoverageLogger(),
220: createIMetaDataWriter(), createAnalysisModules());
221: }
222:
223: //----------
224:
225: private static class Sample1 {
226: }
227:
228: public void testPostCompile1() throws Exception {
229: PostCompileClass pcc = new PostCompileClass(
230: createParseCoverageLogger(), createIMetaDataWriter(),
231: createAnalysisModules());
232: ByteArrayOutputStream baos = new ByteArrayOutputStream();
233: String filename = getClassFilename(Sample1.class);
234: byte[] bytecode = loadBytecode(filename);
235:
236: pcc.postCompile(filename, bytecode, baos);
237: assertTrue("Output bytecode is empty.",
238: baos.toByteArray().length > 0);
239: }
240:
241: public void testPostCompile2() throws Exception {
242: PostCompileClass pcc = new PostCompileClass(
243: createIMetaDataWriter(), createAnalysisModules());
244: ByteArrayOutputStream baos = new ByteArrayOutputStream();
245: String filename = getClassFilename(Sample1.class);
246: byte[] bytecode = loadBytecode(filename);
247:
248: pcc.postCompile(filename, bytecode, baos);
249: assertTrue("Output bytecode is empty.",
250: baos.toByteArray().length > 0);
251: }
252:
253: private static abstract class Sample3 {
254: public abstract void a();
255: }
256:
257: public void testPostCompile3() throws Exception {
258: PostCompileClass pcc = new PostCompileClass(
259: createIMetaDataWriter(), createAnalysisModules());
260: ByteArrayOutputStream baos = new ByteArrayOutputStream();
261: String filename = getClassFilename(Sample3.class);
262: byte[] bytecode = loadBytecode(filename);
263:
264: pcc.postCompile(filename, bytecode, baos);
265: assertTrue("Output bytecode is empty.",
266: baos.toByteArray().length > 0);
267: }
268:
269: //-------------------------------------------------------------------------
270: // Helpers
271:
272: public static class MyClassMetaDataWriter implements
273: IClassMetaDataWriter {
274: private List records;
275:
276: public MyClassMetaDataWriter(List l) {
277: this .records = l;
278: }
279:
280: public void writeClassRecord(ClassRecord cr) {
281: this .records.add(cr);
282: }
283:
284: public void close() {
285: this .records = null;
286: }
287: }
288:
289: public static class MyMetaDataWriter implements IMetaDataWriter {
290: public Map modulesToClassRecordList = new HashMap();
291:
292: public IClassMetaDataWriter getClassWriter(
293: IAnalysisModule module) {
294: List list = (List) this .modulesToClassRecordList
295: .get(module);
296: if (list == null) {
297: list = new LinkedList();
298: this .modulesToClassRecordList.put(module, list);
299: }
300: return new MyClassMetaDataWriter(list);
301: }
302:
303: public void close() {
304: }
305: }
306:
307: //------------------------------------------------------------------------
308:
309: protected ParseCoverageLogger createParseCoverageLogger() {
310: return TestLogger.createPCL();
311: }
312:
313: protected MyMetaDataWriter createIMetaDataWriter() {
314: return new MyMetaDataWriter();
315: }
316:
317: protected IAnalysisModule[] createAnalysisModules() {
318: return new IAnalysisModule[] {
319: CCCreatorUtil.createIAnalysisModule(2),
320: CCCreatorUtil.createIAnalysisModule(3),
321: CCCreatorUtil.createIAnalysisModule(4), };
322: }
323:
324: protected static String getClassFilename(Class c) {
325: return BytecodeLoaderUtil.getClassFilename(c);
326: }
327:
328: protected static byte[] loadBytecode(String filename)
329: throws IOException {
330: return BytecodeLoaderUtil.loadBytecode(filename);
331: }
332:
333: //-------------------------------------------------------------------------
334: // Standard JUnit declarations
335:
336: public static Test suite() {
337: TestSuite suite = new TestSuite(THIS_CLASS);
338:
339: return suite;
340: }
341:
342: public static void main(String[] args) {
343: String[] name = { THIS_CLASS.getName() };
344:
345: // junit.textui.TestRunner.main( name );
346: // junit.swingui.TestRunner.main( name );
347:
348: junit.textui.TestRunner.main(name);
349: }
350:
351: /**
352: *
353: * @exception Exception thrown under any exceptional condition.
354: */
355: protected void setUp() throws Exception {
356: super .setUp();
357:
358: // set ourself up
359: }
360:
361: /**
362: *
363: * @exception Exception thrown under any exceptional condition.
364: */
365: protected void tearDown() throws Exception {
366: // tear ourself down
367:
368: super.tearDown();
369: }
370: }
|