KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)BytecodeCountMeasureUTest.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.IAnalysisModule;
37 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModuleUTestI;
38 import net.sourceforge.groboutils.codecoverage.v2.IMethodCode;
39 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedClass;
40 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedMethod;
41 import net.sourceforge.groboutils.codecoverage.v2.logger.TestLogger;
42 import net.sourceforge.groboutils.junit.v1.iftc.CxFactory;
43 import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite;
44
45
46 /**
47  * Tests the BytecodeCountMeasure 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 26, 2003
52  */

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

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

70     
71     public static class MyTestClass
72     {
73         public static void main( String JavaDoc[] args )
74         {
75             int i = 0;
76             i += 10;
77             System.out.println( ""+i );
78         }
79     }
80     
81     
82     public void testAnalyze1() throws Exception JavaDoc
83     {
84         BytecodeCountMeasure bcm = new BytecodeCountMeasure();
85         ModifiedClass mc = CCCreatorUtil.createModifiedClass(
86             TestLogger.createPCL(), MyTestClass.class );
87         ModifiedMethod mm = CCCreatorUtil.getMainModifiedMethod( mc );
88         IMethodCode imc = createMethodCode( MyTestClass.class, mm, bcm );
89         int expectedCount = mm.getInstructionList().getInstructionCount();
90         
91         // populate the method with marks
92
bcm.analyze( imc );
93         
94         // Generate the class from the modified bytecode, and run it.
95
Class JavaDoc c = BytecodeLoaderUtil.loadClassFromBytecode(
96             mc.getClassName(), mc.getModifiedClass() );
97         TestLogger.reset();
98         BytecodeLoaderUtil.runMain( c );
99         assertEquals(
100             "Did not mark every bytecode in method.",
101             expectedCount,
102             TestLogger.size() );
103     }
104     
105     
106     
107     //-------------------------------------------------------------------------
108
// Helpers
109

110     
111     protected IMethodCode createMethodCode( Class JavaDoc c, ModifiedMethod mm,
112             IAnalysisModule am )
113     {
114         return CCCreatorUtil.createIMethodCode( c, mm,
115             CCCreatorUtil.createAnalysisModuleSet(
116                 new IAnalysisModule[] { am } ),
117             0 );
118     }
119     
120     
121     //-------------------------------------------------------------------------
122
// Standard JUnit declarations
123

124     
125     public static Test suite()
126     {
127         InterfaceTestSuite suite = IAnalysisModuleUTestI.suite();
128         suite.addTestSuite( THIS_CLASS );
129         suite.addFactory( new CxFactory( "A" ) {
130             public Object JavaDoc createImplObject() throws IOException JavaDoc {
131                 return new BytecodeCountMeasure();
132             }
133         } );
134         
135         return suite;
136     }
137     
138     public static void main( String JavaDoc[] args )
139     {
140         String JavaDoc[] name = { THIS_CLASS.getName() };
141         
142         // junit.textui.TestRunner.main( name );
143
// junit.swingui.TestRunner.main( name );
144

145         junit.textui.TestRunner.main( name );
146     }
147     
148     
149     /**
150      *
151      * @exception Exception thrown under any exceptional condition.
152      */

153     protected void setUp() throws Exception JavaDoc
154     {
155         super.setUp();
156
157        
158         // set ourself up
159
}
160     
161     
162     /**
163      *
164      * @exception Exception thrown under any exceptional condition.
165      */

166     protected void tearDown() throws Exception JavaDoc
167     {
168         // tear ourself down
169

170         
171         super.tearDown();
172     }
173 }
174
175
Popular Tags