KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)CCCreatorUtil.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;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import junit.framework.Assert;
33 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
34 import net.sourceforge.groboutils.codecoverage.v2.compiler.CompilerCreatorUtil;
35 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedClass;
36 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedMethod;
37 import net.sourceforge.groboutils.codecoverage.v2.compiler.ParseCoverageLogger;
38 import net.sourceforge.groboutils.codecoverage.v2.datastore.AnalysisModuleSet;
39 import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord;
40 import net.sourceforge.groboutils.codecoverage.v2.datastore.DatastoreCreatorUtil;
41 import net.sourceforge.groboutils.codecoverage.v2.datastore.DirMetaDataReader;
42 import net.sourceforge.groboutils.codecoverage.v2.datastore.DirMetaDataWriter;
43 import net.sourceforge.groboutils.codecoverage.v2.datastore.IClassMetaDataWriter;
44 import net.sourceforge.groboutils.codecoverage.v2.datastore.IMetaDataReader;
45 import net.sourceforge.groboutils.codecoverage.v2.datastore.IMetaDataWriter;
46 import net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLogReader;
47 import net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLogger;
48 import net.sourceforge.groboutils.codecoverage.v2.module.BranchCountMeasure;
49 import net.sourceforge.groboutils.codecoverage.v2.module.BytecodeCountMeasure;
50 import net.sourceforge.groboutils.codecoverage.v2.module.DefaultAnalysisMetaData;
51 import net.sourceforge.groboutils.codecoverage.v2.module.LineCountMeasure;
52 import net.sourceforge.groboutils.codecoverage.v2.report.AnalysisModuleData;
53
54 import org.apache.bcel.classfile.JavaClass;
55 import org.apache.bcel.classfile.Method;
56 import org.apache.bcel.generic.MethodGen;
57
58
59 /**
60  * Helper for creating code coverage test objects.
61  *
62  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
63  * @version $Date: 2004/04/15 05:48:27 $
64  * @since January 23, 2003
65  */

66 public class CCCreatorUtil
67 {
68     private static final Class JavaDoc THIS_CLASS = CCCreatorUtil.class;
69     private static final AutoDoc DOC = new AutoDoc( THIS_CLASS );
70
71     
72     
73     public static AnalysisModuleSet createAnalysisModuleSet( int count )
74     {
75         return createAnalysisModuleSet( createAnalysisModules( count ) );
76     }
77     
78     
79     public static AnalysisModuleSet createAnalysisModuleSet(
80             IAnalysisModule[] amL )
81     {
82         Assert.assertNotNull( amL );
83         AnalysisModuleSet ams = new AnalysisModuleSet( amL );
84         return ams;
85     }
86
87
88     public static IAnalysisModule[] createAnalysisModules( int count )
89     {
90         IAnalysisModule amL[] = new IAnalysisModule[ count ];
91         for (int i = 0; i < count; ++i)
92         {
93             amL[i] = createIAnalysisModule( 0 );
94         };
95         return amL;
96     }
97     
98     
99     private static int moduleIndex = 0;
100     public static IAnalysisModule createIAnalysisModule( int type )
101     {
102         IAnalysisModule am;
103         switch (type)
104         {
105             case 0:
106                 am = createIAnalysisModule( "n"+(moduleIndex++),
107                     "u", "text/plain" );
108                 break;
109             case 1:
110                 am = createIAnalysisModule( "a", "b", "text/html" );
111                 break;
112             case 2:
113                 am = new LineCountMeasure();
114                 break;
115             case 3:
116                 am = new BytecodeCountMeasure();
117                 break;
118             case 4:
119                 am = new BranchCountMeasure();
120                 break;
121             default:
122                 am = null;
123         }
124         return am;
125     }
126     
127     
128     public static IAnalysisModule createIAnalysisModule( String JavaDoc n, String JavaDoc u,
129             String JavaDoc m )
130     {
131         IAnalysisModule am = DatastoreCreatorUtil.createAnalysisModule(
132             n, u, m );
133         return am;
134     }
135     
136     
137     public static ClassRecord createClassRecord( Class JavaDoc c,
138             ModifiedMethod mm, AnalysisModuleSet ams )
139     {
140         Assert.assertNotNull(
141             "Null method.",
142             mm );
143         Assert.assertNotNull( "Null class.", c );
144         ClassRecord cr = new ClassRecord( c.getName(),
145             0, "Source.java", new String JavaDoc[] { mm.getMethodName() }, ams );
146         return cr;
147     }
148     
149     
150     public static ModifiedClass createModifiedClass( Class JavaDoc c )
151             throws IOException JavaDoc
152     {
153         Assert.assertNotNull( c );
154         String JavaDoc filename = BytecodeLoaderUtil.getClassFilename( c );
155         ModifiedClass mc = new ModifiedClass( filename,
156             BytecodeLoaderUtil.loadBytecode( filename ) );
157         return mc;
158     }
159     
160     
161     public static ModifiedClass createModifiedClass( ParseCoverageLogger pcl,
162             Class JavaDoc c )
163             throws IOException JavaDoc
164     {
165         Assert.assertNotNull( c );
166         String JavaDoc filename = BytecodeLoaderUtil.getClassFilename( c );
167         ModifiedClass mc = createModifiedClass( pcl, filename,
168             BytecodeLoaderUtil.loadBytecode( filename ) );
169         return mc;
170     }
171     
172     
173     public static ModifiedClass createModifiedClass( ParseCoverageLogger pcl,
174             String JavaDoc classFilename, byte[] bytecode )
175             throws IOException JavaDoc
176     {
177         Assert.assertNotNull( bytecode );
178         ModifiedClass mc = new ModifiedClass( pcl, classFilename, bytecode );
179         return mc;
180     }
181     
182     
183     public static ModifiedMethod getMainModifiedMethod( ModifiedClass mc )
184     {
185         ModifiedMethod mm[] = mc.getMethods();
186         for (int i = 0; i < mm.length; ++i)
187         {
188             if ("main([Ljava/lang/String;)V".equals( mm[i].getMethodName() ))
189             {
190                 return mm[i];
191             }
192         }
193         Assert.fail( "Class "+mc.getClassName()+
194             " does not have a main method." );
195         // needs a return
196
throw new IllegalStateException JavaDoc("Unreachable statement.");
197     }
198
199     
200     public static ModifiedMethod createModifiedMethod( JavaClass jc,
201             int methodIndex, Method m, MethodGen mg )
202     {
203         return CompilerCreatorUtil.createModifiedMethod( jc, methodIndex,
204             m, mg );
205     }
206
207     
208     public static ModifiedMethod createModifiedMethod( Class JavaDoc c,
209             int methodIndex )
210             throws IOException JavaDoc
211     {
212         JavaClass jc = BCELCreatorUtil.createJavaClass( c );
213         Method m = BCELCreatorUtil.getMethod( jc, methodIndex );
214         MethodGen mg = BCELCreatorUtil.createMethodGen( jc, methodIndex );
215         ModifiedMethod mm = createModifiedMethod( jc, methodIndex, m, mg );
216         return mm;
217     }
218     
219     
220     public static IMethodCode createIMethodCode( Class JavaDoc c, int methodIndex,
221             AnalysisModuleSet ams, int measureIndex )
222             throws IOException JavaDoc
223     {
224         ModifiedMethod mm = createModifiedMethod( c, methodIndex );
225         return createIMethodCode( c, mm, ams, measureIndex );
226     }
227     
228     
229     public static IMethodCode createIMethodCode( Class JavaDoc c, ModifiedMethod mm,
230             AnalysisModuleSet ams, int measureIndex )
231     {
232         IMethodCode mc = CompilerCreatorUtil.createIMethodCode(
233             (short)measureIndex, mm, createClassRecord( c, mm, ams ) );
234         return mc;
235     }
236             
237     
238     
239     public static IAnalysisMetaData createIAnalysisMetaData( String JavaDoc c,
240             String JavaDoc nc, byte w )
241     {
242         DefaultAnalysisMetaData damd = new DefaultAnalysisMetaData( c, nc, w );
243         return damd;
244     }
245     
246     
247     public static AnalysisModuleData createAnalysisModuleData(
248             IAnalysisModule module, IMetaDataReader mdr,
249             IChannelLogReader clr )
250             throws IOException JavaDoc
251     {
252         AnalysisModuleData amd = new AnalysisModuleData( module, mdr, clr );
253         return amd;
254     }
255     
256     
257     public synchronized static File JavaDoc createNewDirectory()
258     {
259         File JavaDoc f = null;
260         while (f == null || f.exists())
261         {
262             f = new File JavaDoc( ".", Long.toString( System.currentTimeMillis() ) );
263         }
264         f.mkdirs();
265         Assert.assertTrue( "Did not generate a directory.",
266             f.isDirectory() );
267         return f;
268     }
269     
270     
271     public static class SimpleClassLogData {
272         public String JavaDoc classSig;
273         public int methods[];
274         public int marks[];
275         
276         public SimpleClassLogData( String JavaDoc cs, int me[], int ma[] )
277         {
278             this.classSig = cs;
279             this.methods = me;
280             this.marks = ma;
281             validate();
282         }
283         
284         
285         public void validate()
286         {
287             Assert.assertNotNull( this.classSig );
288             Assert.assertNotNull( this.methods );
289             Assert.assertNotNull( this.marks );
290             Assert.assertEquals( this.methods.length, this.marks.length );
291         }
292     }
293     
294     
295     public static void populateLogger( IChannelLogger cl,
296             SimpleClassLogData inputData[] )
297     {
298         Assert.assertNotNull( cl );
299         Assert.assertNotNull( inputData );
300         for (int i = 0; i < inputData.length; ++i)
301         {
302             inputData[i].validate();
303             DOC.getLog().info( "Logging class "+inputData[i].classSig+"." );
304             for (int j = 0; j < inputData[i].methods.length; ++j)
305             {
306                 DOC.getLog().debug( "-- mark "+inputData[i].methods[j]+"-"+
307                     inputData[i].marks[j]+"." );
308                 cl.cover( inputData[i].classSig,
309                     (short)inputData[i].methods[j],
310                     (short)inputData[i].marks[j] );
311             }
312         }
313     }
314     
315     
316     /**
317      * Creates a DirChannelLogReader suitable for testing. You need to
318      * pass in an array of channel datas (classes above) to create the
319      * base data.
320      */

321     public static DirectoryChannelLogReader createDirectoryChannelLogReader(
322             File JavaDoc basedir, SimpleClassLogData inputData[], int channel )
323     {
324         short sc = (short)channel;
325         File JavaDoc channelDir = new File JavaDoc( basedir, Short.toString( sc ) );
326         DirectoryChannelLogger dcl = new DirectoryChannelLogger( channelDir );
327         DOC.getLog().info( "Putting log data into dir '"+channelDir+"'." );
328         populateLogger( dcl, inputData );
329         DirectoryChannelLogReader dclr = new DirectoryChannelLogReader(
330             basedir, sc );
331         return dclr;
332     }
333     
334     
335     public static void populateMetaData( IMetaDataWriter mdw, Class JavaDoc set[],
336             IAnalysisModule modules[] )
337             throws IOException JavaDoc
338     {
339         Assert.assertNotNull( mdw );
340         Assert.assertNotNull( set );
341         Assert.assertNotNull( modules );
342         AnalysisModuleSet ams = createAnalysisModuleSet( modules );
343         for (int i = 0; i < modules.length; ++i)
344         {
345             IClassMetaDataWriter cmdw = mdw.getClassWriter( modules[i] );
346             try
347             {
348                 for (int j = 0; j < set.length; ++j)
349                 {
350                     ModifiedClass mc = createModifiedClass( set[j] );
351                     cmdw.writeClassRecord( mc.createClassRecord( ams ) );
352                 }
353             }
354             finally
355             {
356                 cmdw.close();
357             }
358         }
359     }
360     
361     
362     public static DirMetaDataReader createDirMetaDataReader( File JavaDoc basedir,
363             Class JavaDoc populateSet[], IAnalysisModule modules[] )
364             throws IOException JavaDoc
365     {
366         IMetaDataWriter mdw = new DirMetaDataWriter( basedir );
367         try
368         {
369             populateMetaData( mdw, populateSet, modules );
370         }
371         finally
372         {
373             mdw.close();
374         }
375         
376         DirMetaDataReader dmdr = new DirMetaDataReader( basedir );
377         return dmdr;
378     }
379 }
380
381
Popular Tags