KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)BranchCountMeasureUTest.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 BranchCountMeasure 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 BranchCountMeasureUTest extends TestCase
54 {
55     //-------------------------------------------------------------------------
56
// Standard JUnit Class-specific declarations
57

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

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

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

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

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

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

168     protected void tearDown() throws Exception JavaDoc
169     {
170         // tear ourself down
171

172         
173         super.tearDown();
174     }
175 }
176
177
Popular Tags