KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ModifiedMethodUTest.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 junit.framework.Test;
30 import junit.framework.TestSuite;
31 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
32 import net.sourceforge.groboutils.codecoverage.v2.BCELCreatorUtil;
33 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
34 import net.sourceforge.groboutils.junit.v1.SubTestTestCase;
35
36 import org.apache.bcel.classfile.JavaClass;
37 import org.apache.bcel.classfile.Method;
38 import org.apache.bcel.generic.MethodGen;
39
40
41 /**
42  * Tests the ModifiedMethod 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 ModifiedMethodUTest extends SubTestTestCase
49 {
50     //-------------------------------------------------------------------------
51
// Standard JUnit Class-specific declarations
52

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

65     public void testConstructor1() throws Exception JavaDoc
66     {
67         try
68         {
69             new ModifiedMethod( (short)0, 0, 0, null, null, null );
70             fail( "Did not throw IllegalArgumentException." );
71         }
72         catch (IllegalArgumentException JavaDoc e)
73         {
74             // test exception
75
}
76     }
77     
78     
79     public void testConstructor2() throws Exception JavaDoc
80     {
81         try
82         {
83             new ModifiedMethod( (short)0, 0, 0,
84                 createJavaClass( THIS_CLASS ), null, null );
85             fail( "Did not throw IllegalArgumentException." );
86         }
87         catch (IllegalArgumentException JavaDoc e)
88         {
89             // test exception
90
}
91     }
92     
93     
94     public void testConstructor3() throws Exception JavaDoc
95     {
96         try
97         {
98             new ModifiedMethod( (short)0, 0, 0,
99                 null, getMethod( createJavaClass( THIS_CLASS ), 0 ), null );
100             fail( "Did not throw IllegalArgumentException." );
101         }
102         catch (IllegalArgumentException JavaDoc e)
103         {
104             // test exception
105
}
106     }
107     
108     
109     public void testConstructor4() throws Exception JavaDoc
110     {
111         try
112         {
113             new ModifiedMethod( (short)0, 0, 0,
114                 null, null,
115                 createMethodGen( createJavaClass( THIS_CLASS ), 0 ) );
116             fail( "Did not throw IllegalArgumentException." );
117         }
118         catch (IllegalArgumentException JavaDoc e)
119         {
120             // test exception
121
}
122     }
123     
124     
125     public void testConstructor5() throws Exception JavaDoc
126     {
127         JavaClass jc = createJavaClass( THIS_CLASS );
128         try
129         {
130             new ModifiedMethod( (short)0, 0, 0,
131                 null, getMethod( jc, 0 ), createMethodGen( jc, 0 ) );
132             fail( "Did not throw IllegalArgumentException." );
133         }
134         catch (IllegalArgumentException JavaDoc e)
135         {
136             // test exception
137
}
138     }
139     
140     
141     public void testConstructor6() throws Exception JavaDoc
142     {
143         JavaClass jc = createJavaClass( THIS_CLASS );
144         try
145         {
146             new ModifiedMethod( (short)0, 0, 0,
147                 jc, null, createMethodGen( jc, 0 ) );
148             fail( "Did not throw IllegalArgumentException." );
149         }
150         catch (IllegalArgumentException JavaDoc e)
151         {
152             // test exception
153
}
154     }
155     
156     
157     public void testConstructor7() throws Exception JavaDoc
158     {
159         JavaClass jc = createJavaClass( THIS_CLASS );
160         try
161         {
162             new ModifiedMethod( (short)0, 0, 0,
163                 jc, getMethod( jc, 0 ), null );
164             fail( "Did not throw IllegalArgumentException." );
165         }
166         catch (IllegalArgumentException JavaDoc e)
167         {
168             // test exception
169
}
170     }
171     
172     
173     public void testGetMethodName1() throws Exception JavaDoc
174     {
175         JavaClass jc = createJavaClass( THIS_CLASS );
176         Method m = getMethod( jc, 2 );
177         MethodGen mg = createMethodGen( jc, m );
178         ModifiedMethod mm = createModifiedMethod( jc, 2, m, mg );
179         assertEquals(
180             "Did not return correct method name.",
181             m.getName() + m.getSignature(),
182             mm.getMethodName() );
183     }
184     
185     public void testGetInstructionList1() throws Exception JavaDoc
186     {
187         JavaClass jc = createJavaClass( THIS_CLASS );
188         Method m = getMethod( jc, 4 );
189         MethodGen mg = createMethodGen( jc, m );
190         ModifiedMethod mm = createModifiedMethod( jc, 4, m, mg );
191         ModifiedInstructionList mil = mm.getInstructionList();
192         assertNotNull(
193             "Returned null instruction list.",
194             mil );
195         assertEquals(
196             "Instruction list size not correct.",
197             mg.getInstructionList().size(),
198             mil.getInstructionCount() );
199     }
200     
201     
202     public void testGetOriginalClass1() throws Exception JavaDoc
203     {
204         JavaClass jc = createJavaClass( THIS_CLASS );
205         Method m = getMethod( jc, 4 );
206         MethodGen mg = createMethodGen( jc, m );
207         ModifiedMethod mm = createModifiedMethod( jc, 4, m, mg );
208         assertSame(
209             "Did not return the original java class.",
210             jc,
211             mm.getOriginalClass() );
212     }
213     
214     
215     public void testGetOriginalMethod1() throws Exception JavaDoc
216     {
217         JavaClass jc = createJavaClass( THIS_CLASS );
218         Method m = getMethod( jc, 4 );
219         MethodGen mg = createMethodGen( jc, m );
220         ModifiedMethod mm = createModifiedMethod( jc, 4, m, mg );
221         assertSame(
222             "Did not return the original method.",
223             m,
224             mm.getOriginalMethod() );
225     }
226     
227     
228     public void testCanAddMarks1() throws Exception JavaDoc
229     {
230         ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 14 );
231         // every method in the test class should be able to be marked.
232
assertTrue( mm.canAddMarks() );
233     }
234     
235     
236     public void testGetModifiedMethodGen1() throws Exception JavaDoc
237     {
238         JavaClass jc = createJavaClass( THIS_CLASS );
239         Method m = getMethod( jc, 4 );
240         MethodGen mg = createMethodGen( jc, m );
241         ModifiedMethod mm = createModifiedMethod( jc, 4, m, mg );
242         assertSame(
243             "Did not return the original MethodGen.",
244             mg,
245             mm.getModifiedMethodGen() );
246     }
247     
248     
249     public void testGetNewMethod1() throws Exception JavaDoc
250     {
251         ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 7 );
252         try
253         {
254             mm.getNewMethod();
255             fail( "Did not throw IllegalStateException." );
256         }
257         catch (IllegalStateException JavaDoc e)
258         {
259             // test exception
260
}
261     }
262     
263     
264     public void testGetNewMethod2() throws Exception JavaDoc
265     {
266         JavaClass jc = createJavaClass( THIS_CLASS );
267         Method m = getMethod( jc, 7 );
268         MethodGen mg = createMethodGen( jc, m );
269         ModifiedMethod mm = createModifiedMethod( jc, 7, m, mg );
270         mm.close();
271         Method m2 = mm.getNewMethod();
272         assertNotSame(
273             "Returned the original object.",
274             m, m2 );
275         assertEquals(
276             "Method name was changed.",
277             m.getName(), m2.getName() );
278         assertEquals(
279             "Method signature was changed.",
280             m.getSignature(), m2.getSignature() );
281     }
282     
283     
284     public void testClose1() throws Exception JavaDoc
285     {
286         ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 1 );
287         mm.close();
288         try
289         {
290             mm.close();
291             fail( "Did not throw IllegalStateException." );
292         }
293         catch (IllegalStateException JavaDoc e)
294         {
295             // test exception
296
}
297     }
298     
299     
300     public void testClose2() throws Exception JavaDoc
301     {
302         ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 1 );
303         mm.close();
304         try
305         {
306             mm.getInstructionList();
307             fail( "Did not throw IllegalStateException." );
308         }
309         catch (IllegalStateException JavaDoc e)
310         {
311             // test exception
312
}
313     }
314     
315     
316     public void testClose3() throws Exception JavaDoc
317     {
318         ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 1 );
319         mm.close();
320         try
321         {
322             mm.getModifiedMethodGen();
323             fail( "Did not throw IllegalStateException." );
324         }
325         catch (IllegalStateException JavaDoc e)
326         {
327             // test exception
328
}
329     }
330     
331     
332     //-------------------------------------------------------------------------
333
// Helpers
334

335     
336     protected ModifiedMethod createModifiedMethod( Class JavaDoc c, int methodIndex )
337             throws Exception JavaDoc
338     {
339         return CCCreatorUtil.createModifiedMethod( c, methodIndex );
340     }
341     
342     protected ModifiedMethod createModifiedMethod( JavaClass jc,
343             int methodIndex, Method m, MethodGen mg )
344     {
345         return CCCreatorUtil.createModifiedMethod( jc, methodIndex, m, mg );
346     }
347     
348     
349     protected JavaClass createJavaClass( Class JavaDoc c ) throws Exception JavaDoc
350     {
351         return BCELCreatorUtil.createJavaClass( c );
352     }
353     
354     
355     protected Method getMethod( JavaClass jc, int methodIndex )
356     {
357         return BCELCreatorUtil.getMethod( jc, methodIndex );
358     }
359     
360     
361     protected MethodGen createMethodGen( JavaClass jc, Method m )
362     {
363         return BCELCreatorUtil.createMethodGen( jc, m );
364     }
365     
366     
367     protected MethodGen createMethodGen( JavaClass jc, int methodIndex )
368     {
369         return BCELCreatorUtil.createMethodGen( jc, methodIndex );
370     }
371     
372     
373     //-------------------------------------------------------------------------
374
// Standard JUnit declarations
375

376     
377     public static Test suite()
378     {
379         TestSuite suite = new TestSuite( THIS_CLASS );
380         
381         return suite;
382     }
383     
384     public static void main( String JavaDoc[] args )
385     {
386         String JavaDoc[] name = { THIS_CLASS.getName() };
387         
388         // junit.textui.TestRunner.main( name );
389
// junit.swingui.TestRunner.main( name );
390

391         junit.textui.TestRunner.main( name );
392     }
393     
394     
395     /**
396      *
397      * @exception Exception thrown under any exceptional condition.
398      */

399     protected void setUp() throws Exception JavaDoc
400     {
401         super.setUp();
402         
403         // set ourself up
404
}
405     
406     
407     /**
408      *
409      * @exception Exception thrown under any exceptional condition.
410      */

411     protected void tearDown() throws Exception JavaDoc
412     {
413         // tear ourself down
414

415         
416         super.tearDown();
417     }
418 }
419
420
Popular Tags