KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > compiler > PostCompileClassUTest


1 /*
2  * @(#)PostCompileClassUTest.java
3  *
4  * Copyright (C) 2002,2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.compiler;
28
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import junit.framework.Test;
37 import junit.framework.TestCase;
38 import junit.framework.TestSuite;
39 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
40 import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil;
41 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
42 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
43 import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord;
44 import net.sourceforge.groboutils.codecoverage.v2.datastore.IClassMetaDataWriter;
45 import net.sourceforge.groboutils.codecoverage.v2.datastore.IMetaDataWriter;
46 import net.sourceforge.groboutils.codecoverage.v2.logger.TestLogger;
47
48
49 /**
50  * Tests the PostCompileClass class.
51  *
52  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
53  * @version $Date: 2004/04/15 05:48:28 $
54  * @since December 28, 2002
55  */

56 public class PostCompileClassUTest extends TestCase
57 {
58     //-------------------------------------------------------------------------
59
// Standard JUnit Class-specific declarations
60

61     private static final Class JavaDoc THIS_CLASS = PostCompileClassUTest.class;
62     private static final AutoDoc DOC = new AutoDoc( THIS_CLASS );
63     
64     public PostCompileClassUTest( String JavaDoc name )
65     {
66         super( name );
67     }
68
69
70     //-------------------------------------------------------------------------
71
// Tests
72

73     public void testConstructor1()
74     {
75         try
76         {
77             new PostCompileClass( null, null );
78             fail( "Did not throw IllegalArgumentException." );
79         }
80         catch (IllegalArgumentException JavaDoc ex)
81         {
82             // test exception?
83
}
84     }
85     
86     public void testConstructor2()
87     {
88         try
89         {
90             new PostCompileClass( createIMetaDataWriter(), null );
91             fail( "Did not throw IllegalArgumentException." );
92         }
93         catch (IllegalArgumentException JavaDoc ex)
94         {
95             // test exception?
96
}
97     }
98     
99     public void testConstructor3()
100     {
101         try
102         {
103             new PostCompileClass( null, createAnalysisModules() );
104             fail( "Did not throw IllegalArgumentException." );
105         }
106         catch (IllegalArgumentException JavaDoc ex)
107         {
108             // test exception?
109
}
110     }
111     
112     public void testConstructor4()
113     {
114         try
115         {
116             new PostCompileClass( null, new IAnalysisModule[1] );
117             fail( "Did not throw IllegalArgumentException." );
118         }
119         catch (IllegalArgumentException JavaDoc ex)
120         {
121             // test exception?
122
}
123     }
124     
125     public void testConstructor5()
126     {
127         try
128         {
129             new PostCompileClass( createIMetaDataWriter(),
130                 new IAnalysisModule[1] );
131             fail( "Did not throw IllegalArgumentException." );
132         }
133         catch (IllegalArgumentException JavaDoc ex)
134         {
135             // test exception?
136
}
137     }
138     
139     public void testConstructor6()
140     {
141         new PostCompileClass( createIMetaDataWriter(),
142             createAnalysisModules() );
143     }
144     
145     //-------------
146

147     public void testConstructor7()
148     {
149         try
150         {
151             new PostCompileClass( null, null, null );
152             fail( "Did not throw IllegalArgumentException." );
153         }
154         catch (IllegalArgumentException JavaDoc ex)
155         {
156             // test exception?
157
}
158     }
159     
160     public void testConstructor8()
161     {
162         try
163         {
164             new PostCompileClass( null, createIMetaDataWriter(), null );
165             fail( "Did not throw IllegalArgumentException." );
166         }
167         catch (IllegalArgumentException JavaDoc ex)
168         {
169             // test exception?
170
}
171     }
172     
173     public void testConstructor9()
174     {
175         try
176         {
177             new PostCompileClass( null, null, createAnalysisModules() );
178             fail( "Did not throw IllegalArgumentException." );
179         }
180         catch (IllegalArgumentException JavaDoc ex)
181         {
182             // test exception?
183
}
184     }
185     
186     public void testConstructor10()
187     {
188         try
189         {
190             new PostCompileClass( null, null, new IAnalysisModule[1] );
191             fail( "Did not throw IllegalArgumentException." );
192         }
193         catch (IllegalArgumentException JavaDoc ex)
194         {
195             // test exception?
196
}
197     }
198     
199     public void testConstructor11()
200     {
201         try
202         {
203             new PostCompileClass( null, createIMetaDataWriter(),
204                 new IAnalysisModule[1] );
205             fail( "Did not throw IllegalArgumentException." );
206         }
207         catch (IllegalArgumentException JavaDoc ex)
208         {
209             // test exception?
210
}
211     }
212     
213     public void testConstructor12()
214     {
215         try
216         {
217             new PostCompileClass( null, createIMetaDataWriter(),
218                 createAnalysisModules() );
219             fail( "Did not throw IllegalArgumentException." );
220         }
221         catch (IllegalArgumentException JavaDoc ex)
222         {
223             // test exception
224
}
225     }
226     
227     public void testConstructor13()
228     {
229         try
230         {
231             new PostCompileClass( createParseCoverageLogger(),
232                 createIMetaDataWriter(), null );
233             fail( "Did not throw IllegalArgumentException." );
234         }
235         catch (IllegalArgumentException JavaDoc ex)
236         {
237             // test exception?
238
}
239     }
240     
241     public void testConstructor14()
242     {
243         try
244         {
245             new PostCompileClass( createParseCoverageLogger(),
246                 null, createAnalysisModules() );
247             fail( "Did not throw IllegalArgumentException." );
248         }
249         catch (IllegalArgumentException JavaDoc ex)
250         {
251             // test exception?
252
}
253     }
254     
255     public void testConstructor15()
256     {
257         try
258         {
259             new PostCompileClass( createParseCoverageLogger(),
260                 null, new IAnalysisModule[1] );
261             fail( "Did not throw IllegalArgumentException." );
262         }
263         catch (IllegalArgumentException JavaDoc ex)
264         {
265             // test exception?
266
}
267     }
268     
269     public void testConstructor16()
270     {
271         try
272         {
273             new PostCompileClass( createParseCoverageLogger(),
274                 createIMetaDataWriter(), new IAnalysisModule[1] );
275             fail( "Did not throw IllegalArgumentException." );
276         }
277         catch (IllegalArgumentException JavaDoc ex)
278         {
279             // test exception?
280
}
281     }
282     
283     public void testConstructor17()
284     {
285         new PostCompileClass( createParseCoverageLogger(),
286             createIMetaDataWriter(), createAnalysisModules() );
287     }
288     
289     //----------
290

291     private static class Sample1 {}
292     public void testPostCompile1()
293             throws Exception JavaDoc
294     {
295         PostCompileClass pcc = new PostCompileClass(
296             createParseCoverageLogger(), createIMetaDataWriter(),
297             createAnalysisModules() );
298         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
299         String JavaDoc filename = getClassFilename( Sample1.class );
300         byte[] bytecode = loadBytecode( filename );
301         
302         pcc.postCompile( filename, bytecode, baos );
303         assertTrue(
304             "Output bytecode is empty.",
305             baos.toByteArray().length > 0 );
306     }
307     
308     
309     public void testPostCompile2()
310             throws Exception JavaDoc
311     {
312         PostCompileClass pcc = new PostCompileClass(
313             createIMetaDataWriter(), createAnalysisModules() );
314         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
315         String JavaDoc filename = getClassFilename( Sample1.class );
316         byte[] bytecode = loadBytecode( filename );
317         
318         pcc.postCompile( filename, bytecode, baos );
319         assertTrue(
320             "Output bytecode is empty.",
321             baos.toByteArray().length > 0 );
322     }
323     
324     
325     private static abstract class Sample3
326     {
327         public abstract void a();
328     }
329     public void testPostCompile3()
330             throws Exception JavaDoc
331     {
332         PostCompileClass pcc = new PostCompileClass(
333             createIMetaDataWriter(), createAnalysisModules() );
334         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
335         String JavaDoc filename = getClassFilename( Sample3.class );
336         byte[] bytecode = loadBytecode( filename );
337         
338         pcc.postCompile( filename, bytecode, baos );
339         assertTrue(
340             "Output bytecode is empty.",
341             baos.toByteArray().length > 0 );
342     }
343     
344     
345     
346     //-------------------------------------------------------------------------
347
// Helpers
348

349     
350     public static class MyClassMetaDataWriter implements IClassMetaDataWriter
351     {
352         private List JavaDoc records;
353         public MyClassMetaDataWriter( List JavaDoc l )
354         {
355             this.records = l;
356         }
357         public void writeClassRecord( ClassRecord cr )
358         {
359             this.records.add( cr );
360         }
361         public void close()
362         {
363             this.records = null;
364         }
365     }
366     
367     
368     public static class MyMetaDataWriter implements IMetaDataWriter
369     {
370         public Map JavaDoc modulesToClassRecordList = new HashMap JavaDoc();
371         
372         public IClassMetaDataWriter getClassWriter( IAnalysisModule module )
373         {
374             List JavaDoc list = (List JavaDoc)this.modulesToClassRecordList.get( module );
375             if (list == null)
376             {
377                 list = new LinkedList JavaDoc();
378                 this.modulesToClassRecordList.put( module, list );
379             }
380             return new MyClassMetaDataWriter( list );
381         }
382         public void close() {}
383     }
384     
385     //------------------------------------------------------------------------
386

387     protected ParseCoverageLogger createParseCoverageLogger()
388     {
389         return TestLogger.createPCL();
390     }
391
392     protected MyMetaDataWriter createIMetaDataWriter()
393     {
394         return new MyMetaDataWriter();
395     }
396     
397
398     protected IAnalysisModule[] createAnalysisModules()
399     {
400         return new IAnalysisModule[] {
401             CCCreatorUtil.createIAnalysisModule( 2 ),
402             CCCreatorUtil.createIAnalysisModule( 3 ),
403             CCCreatorUtil.createIAnalysisModule( 4 ),
404         };
405     }
406
407     protected static String JavaDoc getClassFilename( Class JavaDoc c )
408     {
409         return BytecodeLoaderUtil.getClassFilename( c );
410     }
411     
412     protected static byte[] loadBytecode( String JavaDoc filename ) throws IOException JavaDoc
413     {
414         return BytecodeLoaderUtil.loadBytecode( filename );
415     }
416
417     
418     //-------------------------------------------------------------------------
419
// Standard JUnit declarations
420

421     
422     public static Test suite()
423     {
424         TestSuite suite = new TestSuite( THIS_CLASS );
425         
426         return suite;
427     }
428     
429     public static void main( String JavaDoc[] args )
430     {
431         String JavaDoc[] name = { THIS_CLASS.getName() };
432         
433         // junit.textui.TestRunner.main( name );
434
// junit.swingui.TestRunner.main( name );
435

436         junit.textui.TestRunner.main( name );
437     }
438     
439     
440     /**
441      *
442      * @exception Exception thrown under any exceptional condition.
443      */

444     protected void setUp() throws Exception JavaDoc
445     {
446         super.setUp();
447         
448         // set ourself up
449
}
450     
451     
452     /**
453      *
454      * @exception Exception thrown under any exceptional condition.
455      */

456     protected void tearDown() throws Exception JavaDoc
457     {
458         // tear ourself down
459

460         
461         super.tearDown();
462     }
463 }
464
465
Popular Tags