KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > report > ClassMarkSet


1 /*
2  * @(#)ClassMarkSet.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.report;
28
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisMetaData;
37 import net.sourceforge.groboutils.codecoverage.v2.IChannelLogRecord;
38 import net.sourceforge.groboutils.codecoverage.v2.datastore.MarkRecord;
39
40 /**
41  * Contains all the marks (sorted by covered and not covered) per method.
42  *
43  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
44  * @version $Date: 2004/04/15 05:48:26 $
45  * @since December 17, 2002
46  * @see IAnalysisMetaData
47  */

48 public class ClassMarkSet
49 {
50     private static final org.apache.log4j.Logger LOG =
51         org.apache.log4j.Logger.getLogger( ClassMarkSet.class );
52     
53     private String JavaDoc[] methodNames;
54     private Map JavaDoc methodToCoveredMarks;
55     private Map JavaDoc methodToNotCoveredMarks;
56     
57     
58     //------------------------------------------------------------------------
59

60     /**
61      * Container data for a mark and whether it were covered.
62      * Instances of this class are only used in the ClassMarkSet constructor.
63      */

64     private static class DidCover
65     {
66         public MarkRecord mr;
67         public boolean wasCovered;
68         
69         public DidCover( MarkRecord mr )
70         {
71             if (mr == null)
72             {
73                 throw new IllegalArgumentException JavaDoc("no null args.");
74             }
75             this.mr = mr;
76         }
77         
78         
79         public void cover()
80         {
81             this.wasCovered = true;
82         }
83         
84         public String JavaDoc toString()
85         {
86             return this.mr.toString();
87         }
88     }
89     
90     
91     //------------------------------------------------------------------------
92

93     /**
94      * Convienient way to store marks and their mark index relationship.
95      * Instances of this class are only used in the ClassMarkSet constructor.
96      */

97     private static class MarkSet
98     {
99         private short methodIndex;
100         private String JavaDoc methodName;
101         private Map JavaDoc markSet = new HashMap JavaDoc();
102         
103         public MarkSet( String JavaDoc name, short methIndex )
104         {
105             this.methodName = name;
106             this.methodIndex = methIndex;
107         }
108         
109         
110         public String JavaDoc getMethodName()
111         {
112             return this.methodName;
113         }
114         
115         
116         public short getMethodIndex()
117         {
118             return this.methodIndex;
119         }
120         
121         
122         public void addMark( MarkRecord mr )
123         {
124             if (this.methodIndex != mr.getMethodIndex())
125             {
126                 throw new IllegalStateException JavaDoc(
127                     "Put mark in wrong method bucket.");
128             }
129             Short JavaDoc index = new Short JavaDoc( mr.getMarkIndex() );
130             if (!this.markSet.containsKey( index ))
131             {
132                 this.markSet.put( index, new DidCover( mr ) );
133             }
134             else
135             {
136                 throw new IllegalArgumentException JavaDoc( "Mark index "+index+
137                     " is in the MarkRecord list mroe than once." );
138             }
139         }
140         
141         
142         public void coverMark( short index )
143         {
144             DidCover dc = getMark( index );
145             if (dc != null)
146             {
147                 dc.cover();
148             }
149             else
150             {
151                 // the marks and the logs are out-of-sync
152
/*
153                 LOG.fatal( "Channel log record "+
154                     "has mark "+index+" in method "+getMethodName()+
155                     " [index "+getMethodIndex()+
156                     "] which is not known by the class records." );
157                 */

158                 throw new IllegalArgumentException JavaDoc( "Channel log record "+
159                     "has mark "+index+" in method "+getMethodName()+
160                     " [index "+getMethodIndex()+
161                     "] which is not known by the class records." );
162             }
163         }
164         
165         
166         public DidCover getMark( short index )
167         {
168             return (DidCover)this.markSet.get( new Short JavaDoc( index ) );
169         }
170         
171         
172         public MarkRecord[] getMarksOfType( boolean type )
173         {
174             List JavaDoc out = new ArrayList JavaDoc();
175             Iterator JavaDoc iter = this.markSet.values().iterator();
176             while (iter.hasNext())
177             {
178                 DidCover dc = (DidCover)iter.next();
179                 if (dc.wasCovered == type)
180                 {
181                     out.add( dc.mr );
182                 }
183             }
184             return (MarkRecord[])out.toArray( new MarkRecord[ out.size() ] );
185         }
186     }
187     
188     
189     //------------------------------------------------------------------------
190

191     /**
192      * Convienient way to store the sorted methods and their data.
193      * Instances of this class are only used in the ClassMarkSet constructor.
194      */

195     private static class MethodSet
196     {
197         private String JavaDoc methodNames[];
198         private Map JavaDoc perMethodItems = new HashMap JavaDoc();
199         
200         public MethodSet( String JavaDoc methNames[], MarkRecord mrL[] )
201         {
202             this.methodNames = copyStringArray( methNames );
203             for (int i = 0; i < methNames.length; ++i)
204             {
205                 if (methNames[i] == null)
206                 {
207                     throw new IllegalArgumentException JavaDoc( "No null args." );
208                 }
209                 this.perMethodItems.put( methNames[i],
210                     new MarkSet( methNames[i], (short)i ) );
211             }
212             
213             int len = mrL.length;
214             for (int i = 0; i < len; ++i)
215             {
216                 if (mrL[i] == null)
217                 {
218                     throw new IllegalArgumentException JavaDoc( "No null args." );
219                 }
220                 String JavaDoc sig = mrL[i].getMethodSignature();
221                 MarkSet ms = get( sig );
222                 if (ms == null)
223                 {
224                     throw new IllegalArgumentException JavaDoc(
225                         "Found mark for method "+sig+
226                         " which was not in the method list." );
227                 }
228                 
229                 // assurance
230
if (ms.getMethodIndex() != mrL[i].getMethodIndex())
231                 {
232                     throw new IllegalArgumentException JavaDoc(
233                         "The signature order from ClassRecord ["+
234                         ms.getMethodIndex()+
235                         " does not match the method index ["+
236                         mrL[i].getMethodIndex()+"] for the mark." );
237                 }
238                 
239                 ms.addMark( mrL[i] );
240             }
241         }
242         
243         public int getMethodCount()
244         {
245             return this.methodNames.length;
246         }
247         
248         public String JavaDoc[] getMethodNames()
249         {
250             return copyStringArray( this.methodNames );
251         }
252         
253         public short getMethodIndex( String JavaDoc methName )
254         {
255             MarkSet ms = get( methName );
256             if (ms == null)
257             {
258                 return (short)-1;
259             }
260             return ms.getMethodIndex();
261         }
262         
263         public String JavaDoc getMethodByIndex( short index )
264         {
265             int iindex = (int)index;
266             String JavaDoc names[] = getMethodNames();
267             if (iindex < 0 || iindex >= names.length)
268             {
269                 return null;
270             }
271             return names[ iindex ];
272         }
273         
274         
275         public Iterator JavaDoc nameIterator()
276         {
277             return this.perMethodItems.keySet().iterator();
278         }
279         
280         
281         public MarkSet get( String JavaDoc methName )
282         {
283             return (MarkSet)this.perMethodItems.get( methName );
284         }
285         
286         
287         public MarkSet get( short methodIndex )
288         {
289             return get( getMethodByIndex( methodIndex ) );
290         }
291     }
292     
293     
294     //------------------------------------------------------------------------
295

296     
297     /**
298      *
299      */

300     ClassMarkSet( String JavaDoc className, String JavaDoc methodSigs[],
301             MarkRecord[] marks, IChannelLogRecord[] classLogs )
302     {
303         if (marks == null || classLogs == null || methodSigs == null)
304         {
305             throw new IllegalArgumentException JavaDoc( "No null args." );
306         }
307         
308         this.methodNames = copyStringArray( methodSigs );
309         MethodSet set = new MethodSet( methodSigs, marks );
310         coverMarks( set, classLogs );
311         this.methodToCoveredMarks = getMarksOfType( set, true );
312         this.methodToNotCoveredMarks = getMarksOfType( set, false );
313     }
314     
315     
316     /**
317      * Return all the methods known by the list of marks and class logs.
318      */

319     public String JavaDoc[] getMethodSignatures()
320     {
321         String JavaDoc s[] = this.methodNames;
322         List JavaDoc list = new ArrayList JavaDoc( s.length );
323         for (int i = 0; i < s.length; ++i)
324         {
325             if (s[i] != null)
326             {
327                 list.add( s[i] );
328             }
329         }
330         return (String JavaDoc[])list.toArray( new String JavaDoc[ list.size() ] );
331     }
332     
333     
334     /**
335      * Return all the marks that were covered during the execution of the
336      * given method. Guaranteed to never return null.
337      */

338     public MarkRecord[] getCoveredMarksForMethod( String JavaDoc methodSig )
339     {
340         MarkRecord[] mrL = (MarkRecord[])this.methodToCoveredMarks.get(
341             methodSig );
342         if (mrL == null)
343         {
344             throw new IllegalArgumentException JavaDoc( "Unknown method: "+methodSig );
345         }
346         return mrL;
347     }
348     
349     
350     /**
351      * Return all the marks that were not executed during the runtime
352      * of the given method. Guaranteed to never return null.
353      */

354     public MarkRecord[] getNotCoveredMarksForMethod( String JavaDoc methodSig )
355     {
356         MarkRecord[] mrL = (MarkRecord[])this.methodToNotCoveredMarks.get(
357             methodSig );
358         if (mrL == null)
359         {
360             throw new IllegalArgumentException JavaDoc( "Unknown method: "+methodSig );
361         }
362         return mrL;
363     }
364     
365     
366     
367     //------------------------------------------------------------------------
368
// Examine the input data and organize it
369

370     
371     private void coverMarks( MethodSet methods,
372             IChannelLogRecord[] classLogs )
373     {
374         for (int i = 0; i < classLogs.length; ++i)
375         {
376             IChannelLogRecord clr = classLogs[i];
377             if (clr == null)
378             {
379                 throw new IllegalArgumentException JavaDoc( "no null args" );
380             }
381             short markIndex = clr.getMarkIndex();
382             short methIndex = clr.getMethodIndex();
383             //LOG.debug( "Covering method "+mi+", mark "+markIndex );
384

385             MarkSet ms = methods.get( methIndex );
386             if (ms != null)
387             {
388                 ms.coverMark( markIndex );
389             }
390             else
391             {
392                 // the marks and the logs are out-of-sync
393
/*
394                 // let's assume that it's ok.
395                 LOG.fatal( "Channel log record "+
396                     "refers to a method index ("+methIndex+
397                     ") that is not known by the class records (mark = "+
398                     markIndex+")." );
399                 */

400                 throw new IllegalArgumentException JavaDoc( "Channel log record "+
401                     "refers to a method index ("+methIndex+
402                     ") that is not known by the class records (mark = "+
403                     markIndex+")." );
404             }
405         }
406     }
407     
408     
409     private Map JavaDoc getMarksOfType( MethodSet methods,
410             boolean covered )
411     {
412         Map JavaDoc map = new HashMap JavaDoc();
413         Iterator JavaDoc iter = methods.nameIterator();
414         while (iter.hasNext())
415         {
416             String JavaDoc methName = (String JavaDoc)iter.next();
417             MarkSet ms = methods.get( methName );
418             map.put( methName, ms.getMarksOfType( covered ) );
419         }
420         return map;
421     }
422     
423     
424     private static String JavaDoc[] copyStringArray( String JavaDoc in[] )
425     {
426         int len = in.length;
427         String JavaDoc out[] = new String JavaDoc[ len ];
428         System.arraycopy( in, 0, out, 0, len );
429         return out;
430     }
431 }
432
433
Popular Tags