KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > IMethodCodeUTestI


1 /*
2  * @(#)IMethodCodeUTestI.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;
28
29 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
30 import net.sourceforge.groboutils.codecoverage.v2.module.DefaultAnalysisMetaData;
31 import net.sourceforge.groboutils.junit.v1.iftc.ImplFactory;
32 import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestCase;
33 import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite;
34
35 import org.apache.bcel.classfile.Method;
36
37
38 /**
39  * Tests the IMethodCode interface.
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @version $Date: 2004/04/15 05:48:27 $
43  * @since December 28, 2002
44  */

45 public class IMethodCodeUTestI extends InterfaceTestCase
46 {
47     //-------------------------------------------------------------------------
48
// Standard JUnit Class-specific declarations
49

50     private static final Class JavaDoc THIS_CLASS = IMethodCodeUTestI.class;
51     private static final AutoDoc DOC = new AutoDoc( THIS_CLASS );
52     
53     public IMethodCodeUTestI( String JavaDoc name, ImplFactory f )
54     {
55         super( name, IMethodCode.class, f );
56     }
57
58     
59     public IMethodCode createIMethodCode()
60     {
61         return (IMethodCode)createImplObject();
62     }
63
64
65     //-------------------------------------------------------------------------
66
// Tests
67

68     
69     public void testGetOriginalMethod1()
70     {
71         IMethodCode mc = createIMethodCode();
72         Method m = mc.getOriginalMethod();
73         assertNotNull(
74             "Must not return null original method.",
75             m );
76     }
77     
78     
79     public void testGetMethod1()
80     {
81         IMethodCode mc = createIMethodCode();
82         assertNotNull(
83             "Method name is null.",
84             mc.getMethodName() );
85     }
86     
87     
88     public void testGetClassName1()
89     {
90         IMethodCode mc = createIMethodCode();
91         assertNotNull(
92             "Class name is null.",
93             mc.getClassName() );
94     }
95     
96     
97     public void testGetInstructionCount1()
98     {
99         IMethodCode mc = createIMethodCode();
100         int count = mc.getInstructionCount();
101         assertTrue(
102             "Count is not valid",
103             count >= 0 );
104     }
105     
106     
107     public void testMethodName1()
108     {
109         IMethodCode mc = createIMethodCode();
110         Method m = mc.getOriginalMethod();
111         String JavaDoc name = mc.getMethodName();
112         assertEquals(
113             "Method name doesn't match returned method's name",
114             m.getName()+m.getSignature(),
115             name
116             );
117     }
118     
119     
120     public void testInstructionConsistency1()
121     {
122         IMethodCode mc = createIMethodCode();
123         int count = mc.getInstructionCount();
124         for (int i = 0; i < count; ++i)
125         {
126             assertNotNull(
127                 "Null instruction at position "+i+".",
128                 mc.getInstructionAt( i ) );
129         }
130     }
131     
132     
133     public void testInstructionConsistency2()
134     {
135         IMethodCode mc = createIMethodCode();
136         int count = mc.getInstructionCount();
137         try
138         {
139             mc.getInstructionAt( -1 );
140             fail( "Did not throw excption w/ -1 index." );
141         }
142         catch (IndexOutOfBoundsException JavaDoc e)
143         {
144             // test exception
145
}
146         
147         try
148         {
149             mc.getInstructionAt( count );
150             fail( "Did not throw exception w/ "+count+" index." );
151         }
152         catch (IndexOutOfBoundsException JavaDoc e)
153         {
154             // test exception
155
}
156     }
157     
158     
159     public void testMarkInstruction1()
160     {
161         IMethodCode mc = createIMethodCode();
162         int count = mc.getInstructionCount();
163         if (count <= 0)
164         {
165             DOC.getLog().warn( "No instructions to mark for '"+mc+"'." );
166             return;
167         }
168         
169         // something to test
170
// note: we can mark the last index + one over it!
171
for (int i = 0; i <= count; ++i)
172         {
173             IAnalysisMetaData meta = new DefaultAnalysisMetaData(
174                 "", "", (byte)0 );
175             mc.markInstruction( i, meta );
176         }
177     }
178     
179     
180     public void testMarkInstruction2()
181     {
182         IMethodCode mc = createIMethodCode();
183         
184         try
185         {
186             IAnalysisMetaData meta = new DefaultAnalysisMetaData(
187                 "", "", (byte)0 );
188             mc.markInstruction( -1, meta );
189             fail( "Did not throw IndexOutOfBoundsException." );
190         }
191         catch (IndexOutOfBoundsException JavaDoc e)
192         {
193             // test exception
194
}
195     }
196     
197     
198     public void testMarkInstruction4()
199     {
200         IMethodCode mc = createIMethodCode();
201         int count = mc.getInstructionCount();
202         
203         try
204         {
205             IAnalysisMetaData meta = new DefaultAnalysisMetaData(
206                 "", "", (byte)0 );
207             mc.markInstruction( count+1, meta );
208             fail( "Did not throw IndexOutOfBoundsException." );
209         }
210         catch (IndexOutOfBoundsException JavaDoc e)
211         {
212             // test exception
213
}
214     }
215     
216     
217     public void testMarkInstruction5()
218     {
219         IMethodCode mc = createIMethodCode();
220         int count = mc.getInstructionCount();
221         
222         // even if the instruction count is == 0, we can still mark it,
223
// hence this will be a valid test for 'null' metadata.
224

225         // something to test
226
try
227         {
228             mc.markInstruction( 0, null );
229             fail( "Did not throw IllegalArgumentException." );
230         }
231         catch (IllegalArgumentException JavaDoc e)
232         {
233             // test exception
234
}
235     }
236     
237     
238     //-------------------------------------------------------------------------
239
// Standard JUnit declarations
240

241     
242     public static InterfaceTestSuite suite()
243     {
244         InterfaceTestSuite suite = new InterfaceTestSuite( THIS_CLASS );
245         
246         return suite;
247     }
248     
249     public static void main( String JavaDoc[] args )
250     {
251         String JavaDoc[] name = { THIS_CLASS.getName() };
252         
253         // junit.textui.TestRunner.main( name );
254
// junit.swingui.TestRunner.main( name );
255

256         junit.textui.TestRunner.main( name );
257     }
258     
259     
260     /**
261      *
262      * @exception Exception thrown under any exceptional condition.
263      */

264     protected void setUp() throws Exception JavaDoc
265     {
266         super.setUp();
267         
268         // set ourself up
269
}
270     
271     
272     /**
273      *
274      * @exception Exception thrown under any exceptional condition.
275      */

276     protected void tearDown() throws Exception JavaDoc
277     {
278         // tear ourself down
279

280         
281         super.tearDown();
282     }
283 }
284
285
Popular Tags