KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > module > LineCountMeasureUTest


1 /*
2  * @(#)LineCountMeasureUTest.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.module;
28
29 import java.io.IOException JavaDoc;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
34 import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil;
35 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
36 import net.sourceforge.groboutils.codecoverage.v2.CreateMainClassHelper;
37 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
38 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModuleUTestI;
39 import net.sourceforge.groboutils.codecoverage.v2.IMethodCode;
40 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedClass;
41 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedMethod;
42 import net.sourceforge.groboutils.codecoverage.v2.logger.TestLogger;
43 import net.sourceforge.groboutils.junit.v1.iftc.CxFactory;
44 import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite;
45
46 import org.apache.bcel.Constants;
47 import org.apache.bcel.generic.InstructionConstants;
48 import org.apache.bcel.generic.ObjectType;
49 import org.apache.bcel.generic.PUSH;
50 import org.apache.bcel.generic.Type;
51
52
53 /**
54  * Tests the LineCountMeasure class.
55  *
56  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
57  * @version $Date: 2004/04/15 05:48:28 $
58  * @since January 22, 2003
59  */

60 public class LineCountMeasureUTest extends TestCase
61 {
62     //-------------------------------------------------------------------------
63
// Standard JUnit Class-specific declarations
64

65     private static final Class JavaDoc THIS_CLASS = LineCountMeasureUTest.class;
66     private static final AutoDoc DOC = new AutoDoc( THIS_CLASS );
67     
68     public LineCountMeasureUTest( String JavaDoc name )
69     {
70         super( name );
71     }
72
73
74     //-------------------------------------------------------------------------
75
// Tests
76

77     public static class MyTestClass
78     {
79         public static void main( String JavaDoc[] args )
80         {
81             int i = 0; i += 10;
82             System.out.println( ""+i );
83         }
84     }
85     
86     
87     public void testAnalyze1() throws Exception JavaDoc
88     {
89         LineCountMeasure bcm = new LineCountMeasure();
90         ModifiedClass mc = CCCreatorUtil.createModifiedClass(
91             TestLogger.createPCL(), MyTestClass.class );
92         ModifiedMethod mm = CCCreatorUtil.getMainModifiedMethod( mc );
93         IMethodCode imc = createMethodCode( MyTestClass.class, mm, bcm );
94         
95         // populate the method with marks
96
bcm.analyze( imc );
97         
98         // Generate the class from the modified bytecode, and run it.
99
Class JavaDoc c = BytecodeLoaderUtil.loadClassFromBytecode(
100             mc.getClassName(), mc.getModifiedClass() );
101         TestLogger.reset();
102         BytecodeLoaderUtil.runMain( c );
103         assertEquals(
104             "Did not mark every bytecode in method.",
105             2,
106             TestLogger.size() );
107     }
108     
109     
110     public void testAnalyze2() throws Exception JavaDoc
111     {
112         LineCountMeasure bcm = new LineCountMeasure();
113         CreateMainClassHelper cmch = createHelloWorld();
114         byte bytecode[] = cmch.getBytecode();
115         Class JavaDoc cc = cmch.getGenClass();
116         ModifiedClass mc = CCCreatorUtil.createModifiedClass(
117             TestLogger.createPCL(), "test/HelloWorld.class",
118             bytecode );
119         ModifiedMethod mm = CCCreatorUtil.getMainModifiedMethod( mc );
120         IMethodCode imc = createMethodCode( cc, mm, bcm );
121         
122         // populate the method with marks
123
bcm.analyze( imc );
124         
125         // Generate the class from the modified bytecode, and run it.
126
Class JavaDoc c = BytecodeLoaderUtil.loadClassFromBytecode(
127             mc.getClassName(), mc.getModifiedClass() );
128         TestLogger.reset();
129         BytecodeLoaderUtil.runMain( c );
130         assertEquals(
131             "Somehow we found a line number.",
132             0,
133             TestLogger.size() );
134     }
135     
136     
137     
138     //-------------------------------------------------------------------------
139
// Helpers
140

141     
142     protected IMethodCode createMethodCode( Class JavaDoc c, ModifiedMethod mm,
143             IAnalysisModule am )
144     {
145         return CCCreatorUtil.createIMethodCode( c, mm,
146             CCCreatorUtil.createAnalysisModuleSet(
147                 new IAnalysisModule[] { am } ),
148             0 );
149     }
150     
151     
152     protected CreateMainClassHelper createHelloWorld()
153             throws IOException JavaDoc
154     {
155         CreateMainClassHelper cmch = new CreateMainClassHelper(
156             "test.HelloWorld" );
157         ObjectType p_stream = new ObjectType("java.io.PrintStream");
158         cmch.il.append(
159             cmch.factory.createFieldAccess("java.lang.System", "out", p_stream,
160             Constants.GETSTATIC ) );
161         cmch.il.append( new PUSH(cmch.cp, "Hello, world.")) ;
162         cmch.il.append( cmch.factory.createInvoke("java.io.PrintStream",
163             "println", Type.VOID, new Type[] { Type.STRING },
164             Constants.INVOKEVIRTUAL ) );
165         cmch.il.append(InstructionConstants.RETURN);
166         
167         cmch.close();
168         return cmch;
169     }
170     
171     
172     //-------------------------------------------------------------------------
173
// Standard JUnit declarations
174

175     
176     public static Test suite()
177     {
178         InterfaceTestSuite suite = IAnalysisModuleUTestI.suite();
179         suite.addTestSuite( THIS_CLASS );
180         suite.addFactory( new CxFactory( "A" ) {
181             public Object JavaDoc createImplObject() throws IOException JavaDoc {
182                 return new LineCountMeasure();
183             }
184         } );
185         
186         return suite;
187     }
188     
189     public static void main( String JavaDoc[] args )
190     {
191         String JavaDoc[] name = { THIS_CLASS.getName() };
192         
193         // junit.textui.TestRunner.main( name );
194
// junit.swingui.TestRunner.main( name );
195

196         junit.textui.TestRunner.main( name );
197     }
198     
199     
200     /**
201      *
202      * @exception Exception thrown under any exceptional condition.
203      */

204     protected void setUp() throws Exception JavaDoc
205     {
206         super.setUp();
207
208        
209         // set ourself up
210
}
211     
212     
213     /**
214      *
215      * @exception Exception thrown under any exceptional condition.
216      */

217     protected void tearDown() throws Exception JavaDoc
218     {
219         // tear ourself down
220

221         
222         super.tearDown();
223     }
224 }
225
226
Popular Tags