1 22 23 package net.sourceforge.cobertura.coveragedata; 24 25 import java.util.Collection ; 26 27 import junit.framework.TestCase; 28 29 public class ClassDataTest extends TestCase 30 { 31 32 private final ClassData a = new ClassData("com.example.HelloWorld"); 33 private final ClassData b = new ClassData("com.example.HelloWorld"); 34 private final ClassData c = new ClassData("com.example.HelloWorld"); 35 private final ClassData defPckg = new ClassData("DefaultPackageClass"); 36 37 public void setUp() 38 { 39 a.setSourceFileName("com/example/HelloWorld.java"); 40 b.setSourceFileName("com/example/HelloWorld.java"); 41 c.setSourceFileName("com/example/HelloWorld.java"); 42 defPckg.setSourceFileName("DefaultPackageClass.java"); 43 44 for (int i = 1; i <= 5; i++) 45 b.addLine(i, "test", "(I)B"); 46 for (int i = 1; i <= 5; i++) 47 c.addLine(i, "test", "(I)B"); 48 for (int i = 1; i <= 5; i++) 49 defPckg.addLine(i, "test", "(I)B"); 50 51 b.touch(1); 52 b.touch(2); 53 } 54 55 public void testBranch() 56 { 57 assertFalse(a.isBranch(2)); 59 a.markLineAsBranch(2); 60 assertFalse(a.isBranch(2)); 61 62 assertFalse(b.isBranch(2)); 63 b.markLineAsBranch(2); 64 assertTrue(b.isBranch(2)); 65 66 assertFalse(b.isBranch(4)); 67 b.markLineAsBranch(4); 68 assertTrue(b.isBranch(4)); 69 70 Collection branches = b.getBranches(); 71 assertEquals(2, branches.size()); 72 assertEquals(b.getNumberOfValidBranches(), branches.size()); 73 } 76 77 public void testBranchCoverage() 78 { 79 assertEquals(0, a.getNumberOfCoveredBranches()); 80 assertEquals(0, b.getNumberOfCoveredBranches()); 81 assertEquals(0, a.getNumberOfValidBranches()); 82 assertEquals(0, b.getNumberOfValidBranches()); 83 assertEquals(1.00d, a.getBranchCoverageRate(), 0d); 84 assertEquals(1.00d, b.getBranchCoverageRate(), 0d); 85 86 assertEquals(1.00d, a.getBranchCoverageRate("test(I)B"), 0d); 87 assertEquals(1.00d, b.getBranchCoverageRate("test(I)B"), 0d); 88 89 c.markLineAsBranch(1); 90 c.markLineAsBranch(2); 91 c.markLineAsBranch(3); 92 c.markLineAsBranch(4); 93 94 assertEquals(0, c.getNumberOfCoveredBranches()); 95 assertEquals(4, c.getNumberOfValidBranches()); 96 assertEquals(0.00d, c.getBranchCoverageRate(), 0d); 97 assertEquals(0.00d, c.getBranchCoverageRate("test(I)B"), 0d); 98 99 c.touch(1); 100 c.touch(2); 101 102 assertEquals(2, c.getNumberOfCoveredBranches()); 103 assertEquals(4, c.getNumberOfValidBranches()); 104 assertEquals(0.50d, c.getBranchCoverageRate(), 0d); 105 assertEquals(0.50d, c.getBranchCoverageRate("test(I)B"), 0d); 106 } 107 108 public void testConstructor() 109 { 110 try 111 { 112 new ClassData(null); 113 fail("Expected an IllegalArgumentException but did not receive one!"); 114 } 115 catch (IllegalArgumentException e) 116 { 117 } 119 120 assertEquals("HelloWorld", a.getBaseName()); 121 assertEquals("com.example", a.getPackageName()); 122 assertEquals("com.example.HelloWorld", a.getName()); 123 124 assertEquals("DefaultPackageClass", defPckg.getBaseName()); 125 assertEquals("", defPckg.getPackageName()); 126 assertEquals("DefaultPackageClass", defPckg.getName()); 127 } 128 129 public void testEquals() 130 { 131 assertFalse(a.equals(null)); 132 assertFalse(a.equals(new Integer (4))); 133 assertFalse(a.equals(new PackageData("com.example"))); 134 135 assertTrue(a.equals(a)); 136 assertFalse(a.equals(b)); 137 assertFalse(a.equals(c)); 138 assertFalse(a.equals(defPckg)); 139 assertFalse(b.equals(a)); 140 assertTrue(b.equals(b)); 141 assertFalse(b.equals(c)); 142 assertFalse(b.equals(defPckg)); 143 assertFalse(c.equals(a)); 144 assertFalse(c.equals(b)); 145 assertTrue(c.equals(c)); 146 assertFalse(c.equals(defPckg)); 147 assertFalse(defPckg.equals(a)); 148 assertFalse(defPckg.equals(b)); 149 assertFalse(defPckg.equals(c)); 150 assertTrue(defPckg.equals(defPckg)); 151 152 153 c.touch(1); 154 c.touch(2); 155 assertTrue(b.equals(c)); 156 } 157 158 public void testGetHitCount() 159 { 160 assertEquals(0, a.getHitCount(45)); 161 assertEquals(0, b.getHitCount(45)); 162 assertEquals(0, c.getHitCount(-45)); 163 164 assertEquals(1, b.getHitCount(1)); 165 assertEquals(1, b.getHitCount(2)); 166 assertEquals(0, b.getHitCount(3)); 167 168 assertEquals(0, c.getHitCount(1)); 169 assertEquals(0, c.getHitCount(2)); 170 171 assertEquals(0, b.getHitCount(3)); 172 b.touch(3); 173 assertEquals(1, b.getHitCount(3)); 174 for (int i = 0; i < 234; i++) 175 b.touch(3); 176 assertEquals(235, b.getHitCount(3)); 177 } 178 179 public void testLineCoverage() 180 { 181 assertEquals(0, a.getNumberOfCoveredLines()); 182 assertEquals(0, a.getNumberOfValidLines()); 183 assertEquals(2, b.getNumberOfCoveredLines()); 184 assertEquals(5, b.getNumberOfValidLines()); 185 assertEquals(0, c.getNumberOfCoveredLines()); 186 assertEquals(5, c.getNumberOfValidLines()); 187 assertEquals(1d, a.getLineCoverageRate(), 0d); 188 assertEquals(0.4d, b.getLineCoverageRate(), 0d); 189 assertEquals(0d, c.getLineCoverageRate(), 0d); 190 191 assertEquals(1d, a.getLineCoverageRate("test(I)B"), 0d); 192 assertEquals(0.4d, b.getLineCoverageRate("test(I)B"), 0d); 193 assertEquals(0d, c.getLineCoverageRate("test(I)B"), 0d); 194 195 assertEquals(1d, a.getLineCoverageRate("notReal(I)B"), 0d); 196 assertEquals(1d, b.getLineCoverageRate("notReal(I)B"), 0d); 197 assertEquals(1d, c.getLineCoverageRate("notReal(I)B"), 0d); 198 } 199 200 public void testRemoveLine() 201 { 202 assertEquals(0, a.getNumberOfValidBranches()); 203 assertEquals(0, a.getNumberOfValidLines()); 204 a.removeLine(3); 205 assertEquals(0, a.getNumberOfValidBranches()); 206 assertEquals(0, a.getNumberOfValidLines()); 207 208 assertEquals(0, b.getNumberOfValidBranches()); 209 assertEquals(5, b.getNumberOfValidLines()); 210 b.removeLine(3); 211 assertEquals(0, b.getNumberOfValidBranches()); 212 assertEquals(4, b.getNumberOfValidLines()); 213 214 c.markLineAsBranch(2); 215 c.markLineAsBranch(3); 216 c.markLineAsBranch(4); 217 assertEquals(3, c.getNumberOfValidBranches()); 218 assertEquals(5, c.getNumberOfValidLines()); 219 c.removeLine(3); 220 assertEquals(2, c.getNumberOfValidBranches()); 221 assertEquals(4, c.getNumberOfValidLines()); 222 } 223 224 public void testSourceFileName() 225 { 226 a.setSourceFileName(null); 227 assertEquals("com/example/HelloWorld.java", a.getSourceFileName()); 228 a.setSourceFileName("HelloWorld.java"); 229 assertEquals("com/example/HelloWorld.java", a.getSourceFileName()); 230 231 ClassData d = new ClassData("org.jaxen.expr.IdentitySet$IdentityWrapp"); 232 assertEquals("org/jaxen/expr/IdentitySet.java", d.getSourceFileName()); 233 234 ClassData e = new ClassData("org.jaxen.expr.NodeComparator$1"); 235 assertEquals("org/jaxen/expr/NodeComparator.java", e.getSourceFileName()); 236 237 assertEquals( "DefaultPackageClass.java", defPckg.getSourceFileName()); 238 239 ClassData f = new ClassData("$strangeClass"); 240 assertEquals( "$strangeClass.java", f.getSourceFileName()); 241 } 242 243 public void testTouch() 244 { 245 int line = 3; 246 247 assertFalse(a.isValidSourceLineNumber(line)); 248 a.touch(line); 249 assertTrue(a.isValidSourceLineNumber(line)); 250 251 assertTrue(b.isValidSourceLineNumber(line)); 252 assertEquals(0, b.getHitCount(line)); 253 b.touch(line); 254 assertTrue(b.isValidSourceLineNumber(line)); 255 assertEquals(1, b.getHitCount(line)); 256 b.touch(line); 257 assertEquals(2, b.getHitCount(line)); 258 assertTrue(b.isValidSourceLineNumber(line)); 259 } 260 261 } | Popular Tags |