KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)DefaultMethodCodeUTest.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.TestCase;
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.codecoverage.v2.IAnalysisModule;
35 import net.sourceforge.groboutils.codecoverage.v2.IMethodCodeUTestI;
36 import net.sourceforge.groboutils.codecoverage.v2.datastore.AnalysisModuleSet;
37 import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord;
38 import net.sourceforge.groboutils.junit.v1.iftc.CxFactory;
39 import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite;
40
41 import org.apache.bcel.classfile.JavaClass;
42 import org.apache.bcel.classfile.Method;
43 import org.apache.bcel.generic.MethodGen;
44
45
46 /**
47  * Tests the DefaultMethodCode class.
48  *
49  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
50  * @version $Date: 2004/04/15 05:48:28 $
51  * @since January 11, 2003
52  */

53 public class DefaultMethodCodeUTest extends TestCase
54 {
55     //-------------------------------------------------------------------------
56
// Standard JUnit Class-specific declarations
57

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

70     
71     public void testConstructor1()
72     {
73         try
74         {
75             new DefaultMethodCode( (short)0, null, null );
76             fail( "Did not throw IllegalArgumentException" );
77         }
78         catch (IllegalArgumentException JavaDoc e)
79         {
80             // test exception
81
}
82     }
83     
84     
85     public void testConstructor2()
86             throws Exception JavaDoc
87     {
88         try
89         {
90             new DefaultMethodCode( (short)0, createModifiedMethod(), null );
91             fail( "Did not throw IllegalArgumentException" );
92         }
93         catch (IllegalArgumentException JavaDoc e)
94         {
95             // test exception
96
}
97     }
98     
99     
100     public void testConstructor3()
101             throws Exception JavaDoc
102     {
103         try
104         {
105             new DefaultMethodCode( (short)0, null, createClassRecord(
106                 createModifiedMethod() ) );
107             fail( "Did not throw IllegalArgumentException" );
108         }
109         catch (IllegalArgumentException JavaDoc e)
110         {
111             // test exception
112
}
113     }
114     
115     
116     public void testConstructor4()
117             throws Exception JavaDoc
118     {
119         ModifiedMethod mm = createModifiedMethod();
120         new DefaultMethodCode( (short)0, mm, createClassRecord( mm ) );
121     }
122     
123     
124     public void testConstructor5() throws Exception JavaDoc
125     {
126         ModifiedMethod mm = createModifiedMethod( GCI1.class, "m" );
127         try
128         {
129             new DefaultMethodCode( (short)0, mm,
130                 createClassRecord( mm ) );
131             fail( "Did not throw an IllegalStateException." );
132         }
133         catch (IllegalStateException JavaDoc ise)
134         {
135             assertTrue(
136                 "Did not mention the abstract method.",
137                 ise.getMessage().indexOf( "abstract method" ) >= 0 );
138         }
139     }
140     
141     
142     public void testGetOriginalMethod1()
143             throws Exception JavaDoc
144     {
145         ModifiedMethod mm = createModifiedMethod();
146         DefaultMethodCode dmc = new DefaultMethodCode( (short)0, mm,
147             createClassRecord( mm ) );
148         assertNotNull(
149             "Returned null original method.",
150             dmc.getOriginalMethod() );
151         assertSame(
152             "Not the same original method.",
153             mm.getOriginalMethod(),
154             dmc.getOriginalMethod() );
155     }
156     
157     
158     public void testGetMethodName1()
159             throws Exception JavaDoc
160     {
161         ModifiedMethod mm = createModifiedMethod();
162         Method m = mm.getOriginalMethod();
163         DefaultMethodCode dmc = new DefaultMethodCode( (short)0, mm,
164             createClassRecord( mm ) );
165         assertNotNull(
166             "Returned null method name.",
167             dmc.getMethodName() );
168         assertEquals(
169             "Not the same original method.",
170             m.getName()+m.getSignature(),
171             dmc.getMethodName() );
172     }
173     
174     
175     public void testGetClassName1()
176             throws Exception JavaDoc
177     {
178         ModifiedMethod mm = createModifiedMethod();
179         DefaultMethodCode dmc = new DefaultMethodCode( (short)0, mm,
180             createClassRecord( mm ) );
181         assertNotNull(
182             "Returned null class name.",
183             dmc.getClassName() );
184         assertSame(
185             "Not the same original method.",
186             mm.getOriginalClass().getClassName(),
187             dmc.getClassName() );
188     }
189     
190     
191     // the other methods are sufficiently tested by IMethodCodeUTestI
192

193     
194     //-------------------------------------------------------------------------
195
// Helpers
196

197     
198     protected static ModifiedMethod createModifiedMethod()
199             throws Exception JavaDoc
200     {
201         return CCCreatorUtil.createModifiedMethod( THIS_CLASS, 0 );
202     }
203     
204     
205     protected static ModifiedMethod getModifiedMethod( Class JavaDoc c,
206             String JavaDoc methodName ) throws Exception JavaDoc
207     {
208         // return the first method with the given name (not sig)
209

210         ModifiedClass mc = CCCreatorUtil.createModifiedClass( c );
211         ModifiedMethod mmL[] = mc.getMethods();
212         
213         for (int i = 0; i < mmL.length; ++i)
214         {
215             if (methodName.equals( mmL[i].getOriginalMethod().getName() ))
216             {
217                 return mmL[i];
218             }
219         }
220         fail( "Could not find method named '"+methodName+"' in class "+c+"." );
221         // for the fans...
222
throw new Exception JavaDoc();
223     }
224     
225     
226     protected static ModifiedMethod createModifiedMethod( Class JavaDoc c,
227             String JavaDoc methodName ) throws Exception JavaDoc
228     {
229         // This is really difficult now, because the non-modifiable
230
// methods are not returned by ModifierClass.
231
JavaClass jc = BCELCreatorUtil.createJavaClass( c );
232         Method mL[] = jc.getMethods();
233         for (int i = 0; i < mL.length; ++i)
234         {
235             if (methodName.equals( mL[i].getName()))
236             {
237                 Method m = mL[i];
238                 MethodGen mg = BCELCreatorUtil.createMethodGen( jc, m );
239                 ModifiedMethod mm = CCCreatorUtil.createModifiedMethod( jc,
240                     i, m, mg );
241                 return mm;
242             }
243         }
244         fail( "Could not find method named '"+methodName+"' in class "+c+"." );
245         // for the fans...
246
throw new Exception JavaDoc();
247     }
248     
249     
250     protected static AnalysisModuleSet createAnalysisModuleSet()
251     {
252         IAnalysisModule amL[] = new IAnalysisModule[] {
253                 CCCreatorUtil.createIAnalysisModule( 0 ),
254                 CCCreatorUtil.createIAnalysisModule( 1 ),
255                 CCCreatorUtil.createIAnalysisModule( 2 ),
256             };
257         return CCCreatorUtil.createAnalysisModuleSet( amL );
258     }
259     
260     
261     protected static ClassRecord createClassRecord( ModifiedMethod mm )
262     {
263         return CCCreatorUtil.createClassRecord( THIS_CLASS, mm,
264             createAnalysisModuleSet() );
265     }
266
267     private static abstract class GCI1
268     {
269         public abstract void m();
270     }
271     
272     
273     //-------------------------------------------------------------------------
274
// Standard JUnit declarations
275

276     public static Test suite()
277     {
278         InterfaceTestSuite suite = IMethodCodeUTestI.suite();
279         suite.addTestSuite( THIS_CLASS );
280         suite.addFactory( new CxFactory( "A" ) {
281             public Object JavaDoc createImplObject() throws Exception JavaDoc {
282                 ModifiedMethod mm = createModifiedMethod();
283                 return new DefaultMethodCode( (short)0, mm,
284                     createClassRecord( mm ) );
285             }
286         } );
287         
288         return suite;
289     }
290     
291     public static void main( String JavaDoc[] args )
292     {
293         String JavaDoc[] name = { THIS_CLASS.getName() };
294         
295         // junit.textui.TestRunner.main( name );
296
// junit.swingui.TestRunner.main( name );
297

298         junit.textui.TestRunner.main( name );
299     }
300     
301     
302     /**
303      *
304      * @exception Exception thrown under any exceptional condition.
305      */

306     protected void setUp() throws Exception JavaDoc
307     {
308         super.setUp();
309         
310         // set ourself up
311
}
312     
313     
314     /**
315      *
316      * @exception Exception thrown under any exceptional condition.
317      */

318     protected void tearDown() throws Exception JavaDoc
319     {
320         // tear ourself down
321

322         
323         super.tearDown();
324     }
325 }
326
327
Popular Tags