1 22 23 package net.sourceforge.cobertura.coveragedata; 24 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import java.util.SortedSet ; 33 import java.util.TreeSet ; 34 35 49 public class ClassData extends CoverageDataContainer 50 implements Comparable , HasBeenInstrumented 51 { 52 53 private static final long serialVersionUID = 5; 54 55 59 private Map branches = new HashMap (); 60 61 private boolean containsInstrumentationInfo = false; 62 63 private Set methodNamesAndDescriptors = new HashSet (); 64 65 private String name = null; 66 67 private String sourceFileName = null; 68 69 72 public ClassData(String name) 73 { 74 if (name == null) 75 throw new IllegalArgumentException ( 76 "Class name must be specified."); 77 this.name = name; 78 } 79 80 public LineData addLine(int lineNumber, String methodName, 81 String methodDescriptor) 82 { 83 LineData lineData = getLineData(lineNumber); 84 if (lineData == null) 85 { 86 lineData = new LineData(lineNumber); 87 children.put(new Integer (lineNumber), lineData); 90 } 91 lineData.setMethodNameAndDescriptor(methodName, methodDescriptor); 92 93 if( methodName!=null && methodDescriptor!=null) 96 methodNamesAndDescriptors.add(methodName + methodDescriptor); 97 return lineData; 98 } 99 100 103 public int compareTo(Object o) 104 { 105 if (!o.getClass().equals(ClassData.class)) 106 return Integer.MAX_VALUE; 107 return this.name.compareTo(((ClassData)o).name); 108 } 109 110 public boolean containsInstrumentationInfo() 111 { 112 return this.containsInstrumentationInfo; 113 } 114 115 120 public boolean equals(Object obj) 121 { 122 if (this == obj) 123 return true; 124 if ((obj == null) || !(obj.getClass().equals(this.getClass()))) 125 return false; 126 127 ClassData classData = (ClassData)obj; 128 return super.equals(obj) 129 && this.branches.equals(classData.branches) 130 && this.methodNamesAndDescriptors 131 .equals(classData.methodNamesAndDescriptors) 132 && this.name.equals(classData.name) 133 && this.sourceFileName.equals(classData.sourceFileName); 134 } 135 136 public String getBaseName() 137 { 138 int lastDot = this.name.lastIndexOf('.'); 139 if (lastDot == -1) 140 { 141 return this.name; 142 } 143 return this.name.substring(lastDot + 1); 144 } 145 146 149 public double getBranchCoverageRate(String methodNameAndDescriptor) 150 { 151 int total = 0; 152 int hits = 0; 153 154 Iterator iter = branches.values().iterator(); 155 while (iter.hasNext()) 156 { 157 LineData next = (LineData)iter.next(); 158 if (methodNameAndDescriptor.equals(next.getMethodName() 159 + next.getMethodDescriptor())) 160 { 161 total++; 162 if (next.getHits() > 0) 163 { 164 hits++; 165 } 166 } 167 } 168 if (total == 0) 169 return 1d; 170 return (double)hits / total; 171 } 172 173 public Collection getBranches() 174 { 175 return Collections.unmodifiableCollection(branches.keySet()); 176 } 177 178 182 public long getHitCount(int lineNumber) 183 { 184 Integer lineObject = new Integer (lineNumber); 185 if (!children.containsKey(lineObject)) 186 { 187 return 0; 188 } 189 190 return ((LineData)children.get(lineObject)).getHits(); 191 } 192 193 196 public double getLineCoverageRate(String methodNameAndDescriptor) 197 { 198 int total = 0; 199 int hits = 0; 200 201 Iterator iter = children.values().iterator(); 202 while (iter.hasNext()) 203 { 204 LineData next = (LineData)iter.next(); 205 if (methodNameAndDescriptor.equals(next.getMethodName() 206 + next.getMethodDescriptor())) 207 { 208 total++; 209 if (next.getHits() > 0) 210 { 211 hits++; 212 } 213 } 214 } 215 if (total == 0) 216 return 1d; 217 return (double)hits / total; 218 } 219 220 private LineData getLineData(int lineNumber) 221 { 222 return (LineData)children.get(new Integer (lineNumber)); 223 } 224 225 public SortedSet getLines() 226 { 227 return new TreeSet (this.children.values()); 228 } 229 230 public Collection getLines(String methodNameAndDescriptor) 231 { 232 Collection lines = new HashSet (); 233 Iterator iter = children.values().iterator(); 234 while (iter.hasNext()) 235 { 236 LineData next = (LineData)iter.next(); 237 if (methodNameAndDescriptor.equals(next.getMethodName() 238 + next.getMethodDescriptor())) 239 { 240 lines.add(next); 241 } 242 } 243 return lines; 244 } 245 246 250 public Set getMethodNamesAndDescriptors() 251 { 252 return methodNamesAndDescriptors; 253 } 254 255 public String getName() 256 { 257 return name; 258 } 259 260 263 public int getNumberOfValidBranches() 264 { 265 return branches.size(); 266 } 267 268 public String getPackageName() 269 { 270 int lastDot = this.name.lastIndexOf('.'); 271 if (lastDot == -1) 272 { 273 return ""; 274 } 275 return this.name.substring(0, lastDot); 276 } 277 278 287 public String getSourceFileName() 288 { 289 String baseName; 290 if (sourceFileName != null) 291 baseName = sourceFileName; 292 else 293 { 294 baseName = getBaseName(); 295 int firstDollarSign = baseName.indexOf('$'); 296 if (firstDollarSign == -1 || firstDollarSign == 0) 297 baseName += ".java"; 298 else 299 baseName = baseName.substring(0, firstDollarSign) 300 + ".java"; 301 } 302 303 String packageName = getPackageName(); 304 if (packageName.equals("")) 305 return baseName; 306 return packageName.replace('.', '/') + '/' + baseName; 307 } 308 309 public int hashCode() 310 { 311 return this.name.hashCode(); 312 } 313 314 317 public boolean isBranch(int lineNumber) 318 { 319 return branches.containsKey(new Integer (lineNumber)); 320 } 321 322 328 public boolean isValidSourceLineNumber(int lineNumber) 329 { 330 return children.containsKey(new Integer (lineNumber)); 331 } 332 333 public void markLineAsBranch(int lineNumber) 334 { 335 LineData lineData = getLineData(lineNumber); 336 if (lineData != null) 337 { 338 lineData.setBranch(true); 339 this.branches.put(new Integer (lineNumber), lineData); 340 } 341 } 342 343 348 public void merge(CoverageData coverageData) 349 { 350 ClassData classData = (ClassData)coverageData; 351 352 if (!this.getName().equals(classData.getName())) 354 return; 355 356 super.merge(coverageData); 357 358 for (Iterator iter = classData.branches.keySet().iterator(); iter.hasNext();) 367 { 368 Object key = iter.next(); 369 if (!this.branches.containsKey(key)) 370 { 371 this.branches.put(key, classData.branches.get(key)); 372 } 373 } 374 375 this.containsInstrumentationInfo |= classData.containsInstrumentationInfo; 376 this.methodNamesAndDescriptors.addAll(classData 377 .getMethodNamesAndDescriptors()); 378 if (classData.sourceFileName != null) 379 this.sourceFileName = classData.sourceFileName; 380 } 381 382 public void removeLine(int lineNumber) 383 { 384 Integer lineObject = new Integer (lineNumber); 385 children.remove(lineObject); 386 branches.remove(lineObject); 387 } 388 389 public void setContainsInstrumentationInfo() 390 { 391 this.containsInstrumentationInfo = true; 392 } 393 394 public void setSourceFileName(String sourceFileName) 395 { 396 this.sourceFileName = sourceFileName; 397 } 398 399 404 public void touch(int lineNumber) 405 { 406 LineData lineData = getLineData(lineNumber); 407 if (lineData == null) 408 lineData = addLine(lineNumber, null, null); 409 lineData.touch(); 410 } 411 412 } 413 | Popular Tags |