KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)MultipleAnalyzersIUTest.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.compiler;
28
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 import junit.framework.Test;
34 import junit.framework.TestCase;
35 import junit.framework.TestSuite;
36 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
37 import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil;
38 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
39 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
40 import net.sourceforge.groboutils.codecoverage.v2.datastore.DirMetaDataWriter;
41 import net.sourceforge.groboutils.codecoverage.v2.module.BranchCountMeasure;
42 import net.sourceforge.groboutils.codecoverage.v2.module.BytecodeCountMeasure;
43 import net.sourceforge.groboutils.codecoverage.v2.module.CallPairMeasure;
44 import net.sourceforge.groboutils.codecoverage.v2.module.FunctionMeasure;
45 import net.sourceforge.groboutils.codecoverage.v2.module.LineCountMeasure;
46 import net.sourceforge.groboutils.junit.v1.SubTestTestCase;
47
48
49 /**
50  * Attempts to load a collection of simple class files, add in all known
51  * analyzer module markings on them, and run the recompiled classes.
52  *
53  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
54  * @version $Date: 2004/04/15 05:48:27 $
55  * @since May 2, 2003
56  */

57 public class MultipleAnalyzersIUTest extends SubTestTestCase
58 {
59     //-------------------------------------------------------------------------
60
// Standard JUnit Class-specific declarations
61

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

75     public static class MyLogger2
76     {
77         public static void cover( String JavaDoc classSig,
78                 short methodIndex, short channel, short markIndex )
79         {
80             System.out.println( "MyLogger2.cover" );
81             DOC.getLog().info( getMeasureName( channel ) );
82         }
83     }
84     
85     
86     private static final String JavaDoc COVER_METHOD_NAME = "cover";
87     private static final String JavaDoc CLASSFILE_PATH =
88         "net/sourceforge/groboutils/codecoverage/v2/compiler/testcode/";
89     private static final String JavaDoc CLASSNAME_PACKAGE =
90         "net.sourceforge.groboutils.codecoverage.v2.compiler.testcode.";
91     private static final String JavaDoc MAIN_SIG = "main([Ljava/lang/String;)V";
92
93
94
95     public static final String JavaDoc[] TESTCLASSNAMES = {
96             "Main1",
97             "If1",
98             "If2",
99             "If3",
100             "Case1",
101             "Case2",
102             "Case3",
103             "Case4",
104             "Case5",
105             "Except1",
106             "Except2",
107             "Finally1",
108             "Finally2",
109             "Finally3",
110             "Finally4",
111             "Finally5",
112             "Synchronized1",
113             "Synchronized2",
114             "Synchronized3",
115         };
116     public void testRebuildMain()
117             throws Exception JavaDoc
118     {
119         File JavaDoc metadir = CCCreatorUtil.createNewDirectory();
120         for (int i = 0; i < TESTCLASSNAMES.length; ++i)
121         {
122             addSubTest( new Recompile( TESTCLASSNAMES[i], metadir ) );
123         }
124     }
125     
126     
127     public static class Recompile extends TestCase
128     {
129         public String JavaDoc TESTNAME;
130         public File JavaDoc dir;
131         public Recompile( String JavaDoc testname, File JavaDoc dir )
132         {
133             super( "testRecompile" );
134             this.TESTNAME = testname;
135             this.dir = dir;
136             DOC.getLog().info("===== Adding test for "+testname);
137         }
138         public String JavaDoc getName()
139         {
140             return "testRecompile:"+TESTNAME;
141         }
142         public void testRecompile() throws Exception JavaDoc
143         {
144             File JavaDoc f = new File JavaDoc( TESTNAME+".passed" );
145             if (f.exists()) f.delete();
146             
147             String JavaDoc filename = CLASSFILE_PATH+TESTNAME+".class";
148             String JavaDoc classname = CLASSNAME_PACKAGE+TESTNAME;
149             byte[] bytecode = compileClass( dir, filename );
150             //DOC.getLog().info( "Original Classfile:" );
151
//RebuildClassIUTest.debugClass( origClassBytes, filename );
152
DOC.getLog().info( "Recompiled Classfile:" );
153             RebuildClassIUTest.debugClass( bytecode, filename );
154             
155             BytecodeLoaderUtil.verifyClass( classname, bytecode );
156             Class JavaDoc clazz = BytecodeLoaderUtil.loadClassFromBytecode(
157                 classname, bytecode );
158             BytecodeLoaderUtil.runMain( clazz );
159             
160             // check for errors
161
assertTrue(
162                 TESTNAME+" failed: recompilation didn't work.",
163                 f.exists() );
164         }
165     }
166     
167     
168     
169     
170     //-------------------------------------------------------------------------
171
// helpers
172

173     
174     protected static byte[] compileClass( File JavaDoc metadir, String JavaDoc filename )
175             throws IOException JavaDoc
176     {
177         byte[] bytecode = null;
178         DirMetaDataWriter dmdw = new DirMetaDataWriter( metadir );
179         try
180         {
181             PostCompileClass pcc = new PostCompileClass(
182                 new ParseCoverageLogger( MyLogger2.class,
183                     COVER_METHOD_NAME ),
184                 dmdw, getAnalysisModules() );
185             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
186             byte[] origBytecode = BytecodeLoaderUtil.loadBytecode(
187                 filename );
188             pcc.postCompile( filename, origBytecode, baos );
189             bytecode = baos.toByteArray();
190         }
191         finally
192         {
193             dmdw.close();
194         }
195         return bytecode;
196     }
197     
198     
199     
200     
201     /**
202      *
203      */

204     private static IAnalysisModule[] getAnalysisModules()
205     {
206         final IAnalysisModule[] amL = new IAnalysisModule[] {
207                 new BranchCountMeasure(),
208                 new BytecodeCountMeasure(),
209                 new CallPairMeasure(),
210                 new FunctionMeasure(),
211                 new LineCountMeasure()
212             };
213         return amL;
214     }
215     
216     
217     private static String JavaDoc getMeasureName( short i )
218     {
219         if (i < 0) return null;
220         String JavaDoc s[] = getMeasureNames();
221         if (s.length <= i) return null;
222         return s[i];
223     }
224     
225     
226     private static String JavaDoc[] getMeasureNames()
227     {
228         IAnalysisModule[] am = getAnalysisModules();
229         String JavaDoc s[] = new String JavaDoc[ am.length ];
230         for (int i = 0; i < am.length; ++i)
231         {
232             s[i] = am[i].getMeasureName();
233         }
234         return s;
235     }
236    
237     
238     
239     
240     //-------------------------------------------------------------------------
241
// Standard JUnit declarations
242

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

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

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

278     protected void tearDown() throws Exception JavaDoc
279     {
280         // tear ourself down
281

282         
283         super.tearDown();
284     }
285 }
286
287
Popular Tags