1 26 27 package net.sourceforge.groboutils.codecoverage.v2.report; 28 29 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 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 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 [] methodNames; 54 private Map methodToCoveredMarks; 55 private Map methodToNotCoveredMarks; 56 57 58 60 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 ("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 toString() 85 { 86 return this.mr.toString(); 87 } 88 } 89 90 91 93 97 private static class MarkSet 98 { 99 private short methodIndex; 100 private String methodName; 101 private Map markSet = new HashMap (); 102 103 public MarkSet( String name, short methIndex ) 104 { 105 this.methodName = name; 106 this.methodIndex = methIndex; 107 } 108 109 110 public String 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 ( 127 "Put mark in wrong method bucket."); 128 } 129 Short index = new Short ( 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 ( "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 158 throw new IllegalArgumentException ( "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 ( index ) ); 169 } 170 171 172 public MarkRecord[] getMarksOfType( boolean type ) 173 { 174 List out = new ArrayList (); 175 Iterator 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 191 195 private static class MethodSet 196 { 197 private String methodNames[]; 198 private Map perMethodItems = new HashMap (); 199 200 public MethodSet( String 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 ( "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 ( "No null args." ); 219 } 220 String sig = mrL[i].getMethodSignature(); 221 MarkSet ms = get( sig ); 222 if (ms == null) 223 { 224 throw new IllegalArgumentException ( 225 "Found mark for method "+sig+ 226 " which was not in the method list." ); 227 } 228 229 if (ms.getMethodIndex() != mrL[i].getMethodIndex()) 231 { 232 throw new IllegalArgumentException ( 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 [] getMethodNames() 249 { 250 return copyStringArray( this.methodNames ); 251 } 252 253 public short getMethodIndex( String 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 getMethodByIndex( short index ) 264 { 265 int iindex = (int)index; 266 String names[] = getMethodNames(); 267 if (iindex < 0 || iindex >= names.length) 268 { 269 return null; 270 } 271 return names[ iindex ]; 272 } 273 274 275 public Iterator nameIterator() 276 { 277 return this.perMethodItems.keySet().iterator(); 278 } 279 280 281 public MarkSet get( String 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 296 297 300 ClassMarkSet( String className, String methodSigs[], 301 MarkRecord[] marks, IChannelLogRecord[] classLogs ) 302 { 303 if (marks == null || classLogs == null || methodSigs == null) 304 { 305 throw new IllegalArgumentException ( "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 319 public String [] getMethodSignatures() 320 { 321 String s[] = this.methodNames; 322 List list = new ArrayList ( 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 [])list.toArray( new String [ list.size() ] ); 331 } 332 333 334 338 public MarkRecord[] getCoveredMarksForMethod( String methodSig ) 339 { 340 MarkRecord[] mrL = (MarkRecord[])this.methodToCoveredMarks.get( 341 methodSig ); 342 if (mrL == null) 343 { 344 throw new IllegalArgumentException ( "Unknown method: "+methodSig ); 345 } 346 return mrL; 347 } 348 349 350 354 public MarkRecord[] getNotCoveredMarksForMethod( String methodSig ) 355 { 356 MarkRecord[] mrL = (MarkRecord[])this.methodToNotCoveredMarks.get( 357 methodSig ); 358 if (mrL == null) 359 { 360 throw new IllegalArgumentException ( "Unknown method: "+methodSig ); 361 } 362 return mrL; 363 } 364 365 366 367 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 ( "no null args" ); 380 } 381 short markIndex = clr.getMarkIndex(); 382 short methIndex = clr.getMethodIndex(); 383 385 MarkSet ms = methods.get( methIndex ); 386 if (ms != null) 387 { 388 ms.coverMark( markIndex ); 389 } 390 else 391 { 392 400 throw new IllegalArgumentException ( "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 getMarksOfType( MethodSet methods, 410 boolean covered ) 411 { 412 Map map = new HashMap (); 413 Iterator iter = methods.nameIterator(); 414 while (iter.hasNext()) 415 { 416 String methName = (String )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 [] copyStringArray( String in[] ) 425 { 426 int len = in.length; 427 String out[] = new String [ len ]; 428 System.arraycopy( in, 0, out, 0, len ); 429 return out; 430 } 431 } 432 433 | Popular Tags |