KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ModifiedClassUTest.java
3  *
4  * Copyright (C) 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.IOException JavaDoc;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
35 import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil;
36 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
37 import net.sourceforge.groboutils.codecoverage.v2.datastore.AnalysisModuleSet;
38 import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord;
39
40
41 /**
42  * Tests the ModifiedClass class.
43  *
44  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
45  * @version $Date: 2004/04/15 05:48:28 $
46  * @since January 13, 2003
47  */

48 public class ModifiedClassUTest extends TestCase
49 {
50     //-------------------------------------------------------------------------
51
// Standard JUnit Class-specific declarations
52

53     private static final Class JavaDoc THIS_CLASS = ModifiedClassUTest.class;
54     private static final AutoDoc DOC = new AutoDoc( THIS_CLASS );
55     
56     public ModifiedClassUTest( String JavaDoc name )
57     {
58         super( name );
59     }
60
61
62     //-------------------------------------------------------------------------
63
// Tests
64

65     
66     public void testConstructor1() throws Exception JavaDoc
67     {
68         try
69         {
70             new ModifiedClass( null, null );
71             fail( "Did not throw IllegalArgumentException." );
72         }
73         catch (IllegalArgumentException JavaDoc iae)
74         {
75             // test exception
76
}
77     }
78     
79     
80     public void testConstructor2() throws Exception JavaDoc
81     {
82         try
83         {
84             new ModifiedClass( "", null );
85             fail( "Did not throw IllegalArgumentException." );
86         }
87         catch (IllegalArgumentException JavaDoc iae)
88         {
89             // test exception
90
}
91     }
92     
93     
94     public void testConstructor3() throws Exception JavaDoc
95     {
96         try
97         {
98             new ModifiedClass( null, new byte[0] );
99             fail( "Did not throw IllegalArgumentException." );
100         }
101         catch (IllegalArgumentException JavaDoc iae)
102         {
103             // test exception
104
}
105     }
106     
107     
108     public void testConstructor4() throws Exception JavaDoc
109     {
110         try
111         {
112             new ModifiedClass( null, null, null );
113             fail( "Did not throw IllegalArgumentException." );
114         }
115         catch (IllegalArgumentException JavaDoc iae)
116         {
117             // test exception
118
}
119     }
120     
121     
122     public void testConstructor5() throws Exception JavaDoc
123     {
124         try
125         {
126             new ModifiedClass( null, "", null );
127             fail( "Did not throw IllegalArgumentException." );
128         }
129         catch (IllegalArgumentException JavaDoc iae)
130         {
131             // test exception
132
}
133     }
134     
135     
136     public void testConstructor6() throws Exception JavaDoc
137     {
138         try
139         {
140             new ModifiedClass( null, null, new byte[0] );
141             fail( "Did not throw IllegalArgumentException." );
142         }
143         catch (IllegalArgumentException JavaDoc iae)
144         {
145             // test exception
146
}
147     }
148     
149     
150     public void testConstructor7() throws Exception JavaDoc
151     {
152         try
153         {
154             new ModifiedClass( new ParseCoverageLogger(), null, null );
155             fail( "Did not throw IllegalArgumentException." );
156         }
157         catch (IllegalArgumentException JavaDoc iae)
158         {
159             // test exception
160
}
161     }
162     
163     
164     public void testConstructor8() throws Exception JavaDoc
165     {
166         try
167         {
168             new ModifiedClass( new ParseCoverageLogger(), "", null );
169             fail( "Did not throw IllegalArgumentException." );
170         }
171         catch (IllegalArgumentException JavaDoc iae)
172         {
173             // test exception
174
}
175     }
176     
177     
178     public void testConstructor9() throws Exception JavaDoc
179     {
180         try
181         {
182             new ModifiedClass( new ParseCoverageLogger(), null, new byte[0] );
183             fail( "Did not throw IllegalArgumentException." );
184         }
185         catch (IllegalArgumentException JavaDoc iae)
186         {
187             // test exception
188
}
189     }
190     
191     
192     public void testConstructor10() throws Exception JavaDoc
193     {
194         try
195         {
196             new ModifiedClass( new ParseCoverageLogger(), "", new byte[0] );
197             fail( "Did not throw an exception." );
198         }
199         catch (IOException JavaDoc io)
200         {
201             // test exception
202
}
203     }
204     
205     
206     public void testConstructor11() throws Exception JavaDoc
207     {
208         String JavaDoc filename = BytecodeLoaderUtil.getClassFilename( THIS_CLASS );
209         new ModifiedClass( filename,
210             BytecodeLoaderUtil.loadBytecode( filename ) );
211     }
212     
213     
214     public void testConstructor12() throws Exception JavaDoc
215     {
216         String JavaDoc filename = BytecodeLoaderUtil.getClassFilename( THIS_CLASS );
217         new ModifiedClass( new ParseCoverageLogger(), filename,
218             BytecodeLoaderUtil.loadBytecode( filename ) );
219     }
220     
221     
222     public void testGetClassName1() throws Exception JavaDoc
223     {
224         ModifiedClass mc = createModifiedClass( THIS_CLASS );
225         assertEquals(
226             "Created class and returned classname don't match.",
227             THIS_CLASS.getName(),
228             mc.getClassName() );
229     }
230     
231     
232     public void testGetClassCRC1() throws Exception JavaDoc
233     {
234         ModifiedClass mc = createModifiedClass( THIS_CLASS );
235         assertTrue(
236             "Checksum is zero or -1.",
237             mc.getClassCRC() != 0L && mc.getClassCRC() != -1L );
238     }
239     
240     
241     public void testGetClassSignature1() throws Exception JavaDoc
242     {
243         ModifiedClass mc = createModifiedClass( THIS_CLASS );
244         long crc = mc.getClassCRC();
245         String JavaDoc name = THIS_CLASS.getName();
246         assertTrue(
247             "Does not contain the class name.",
248             mc.getClassSignature().indexOf( name ) >= 0 );
249         assertTrue(
250             "Does not contain the class CRC.",
251             mc.getClassSignature().indexOf( Long.toString( crc ) ) >= 0 );
252     }
253     
254     
255     public void testCreateClassRecord1() throws Exception JavaDoc
256     {
257         ModifiedClass mc = createModifiedClass( THIS_CLASS );
258         ClassRecord cr = mc.createClassRecord( createAnalysisModuleSet() );
259         assertNotNull(
260             "Returned null class record.",
261             cr );
262         assertEquals(
263             "Mismatch class name.",
264             THIS_CLASS.getName(),
265             cr.getClassName() );
266         assertEquals(
267             "Mismatch class CRC.",
268             mc.getClassCRC(),
269             cr.getClassCRC() );
270         assertEquals(
271             "Mismatch signature.",
272             mc.getClassSignature(),
273             cr.getClassSignature() );
274         assertEquals(
275             "Mismatch method count.",
276             mc.getMethods().length,
277             cr.getMethodCount() );
278     }
279     
280     
281     public void testGetMethods1() throws Exception JavaDoc
282     {
283         ModifiedClass mc = createModifiedClass( THIS_CLASS );
284         ModifiedMethod[] mmL = mc.getMethods();
285         assertNotNull(
286             "Returned null method list.",
287             mmL );
288         /* never could get this to work
289         java.lang.reflect.Method mL[] = THIS_CLASS.getDeclaredMethods();
290         assertTrue(
291             "Returned incorrect list size (found "+mmL.length+
292                 ", expected close to "+mL.length+").",
293             mmL.length == mL.length || mmL.length == mL.length + 1 );
294         */

295     }
296     
297     
298     public void testGetModifiedClass1() throws Exception JavaDoc
299     {
300         ModifiedClass mc = createModifiedClass( THIS_CLASS );
301         byte[] orig = BytecodeLoaderUtil.loadBytecode(
302             BytecodeLoaderUtil.getClassFilename( THIS_CLASS ) );
303         byte[] c = mc.getModifiedClass();
304         assertNotNull(
305             "Returned null class bytecode.",
306             c );
307         assertTrue(
308             "Returned smaller length bytecode than the original.",
309             c.length >= orig.length );
310     }
311     
312     
313     
314     
315     
316     //-------------------------------------------------------------------------
317
// Helpers
318

319     
320     protected ModifiedClass createModifiedClass( Class JavaDoc c )
321             throws IOException JavaDoc
322     {
323         return CCCreatorUtil.createModifiedClass( c );
324     }
325     
326     protected AnalysisModuleSet createAnalysisModuleSet()
327     {
328         return CCCreatorUtil.createAnalysisModuleSet( 2 );
329     }
330     
331     //-------------------------------------------------------------------------
332
// Standard JUnit declarations
333

334     
335     public static Test suite()
336     {
337         TestSuite suite = new TestSuite( THIS_CLASS );
338         
339         return suite;
340     }
341     
342     public static void main( String JavaDoc[] args )
343     {
344         String JavaDoc[] name = { THIS_CLASS.getName() };
345         
346         // junit.textui.TestRunner.main( name );
347
// junit.swingui.TestRunner.main( name );
348

349         junit.textui.TestRunner.main( name );
350     }
351     
352     
353     /**
354      *
355      * @exception Exception thrown under any exceptional condition.
356      */

357     protected void setUp() throws Exception JavaDoc
358     {
359         super.setUp();
360         
361         // set ourself up
362
}
363     
364     
365     /**
366      *
367      * @exception Exception thrown under any exceptional condition.
368      */

369     protected void tearDown() throws Exception JavaDoc
370     {
371         // tear ourself down
372

373         
374         super.tearDown();
375     }
376 }
377
378
Popular Tags