1 21 package net.sourceforge.cobertura.merge; 22 23 import java.util.Collection ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 28 import junit.framework.TestCase; 29 import net.sourceforge.cobertura.coveragedata.ClassData; 30 import net.sourceforge.cobertura.coveragedata.LineData; 31 import net.sourceforge.cobertura.coveragedata.PackageData; 32 import net.sourceforge.cobertura.coveragedata.ProjectData; 33 import net.sourceforge.cobertura.coveragedata.SourceFileData; 34 35 38 public class MergeTest extends TestCase { 39 private ClassData firstClass = new ClassData("test.First"); 40 private ClassData secondClass = new ClassData("test.Second"); 41 private ClassData thirdClass = new ClassData("test.Third"); 42 private ClassData firstClassB = new ClassData("test.First"); 43 private ClassData fourthClass = new ClassData("test.me.Fourth"); 44 private ClassData fifthClass = new ClassData("beautiful.Fourth"); 45 private ClassData sixthClass = new ClassData("Fourth"); 46 private ClassData seventhClass = new ClassData("Seventh"); 47 48 private ProjectData greenProject = new ProjectData(); 49 private ProjectData redProject = new ProjectData(); 50 51 public void testMergePackages() { 52 greenProject.addClassData( firstClass); 53 greenProject.addClassData( fourthClass); 54 redProject.addClassData( fifthClass); 55 redProject.addClassData( sixthClass); 56 redProject.addClassData( seventhClass); 57 58 greenProject.merge( redProject); 59 60 Iterator subpackages = greenProject.getSubPackages( "").iterator(); 61 assertEquals( "", ((PackageData)subpackages.next()).getName()); 62 assertEquals( "beautiful", ((PackageData)subpackages.next()).getName()); 63 assertEquals( "test", ((PackageData)subpackages.next()).getName()); 64 assertEquals( "test.me", ((PackageData)subpackages.next()).getName()); 65 assertFalse( subpackages.hasNext()); 66 67 assertEquals(5, greenProject.getClasses().size()); 68 assertEquals(3, redProject.getClasses().size()); 69 70 assertNotNull( greenProject.getClassData("test.First")); 71 assertNotNull( greenProject.getClassData("test.me.Fourth")); 72 assertNotNull( greenProject.getClassData("beautiful.Fourth")); 73 assertNotNull( greenProject.getClassData("Fourth")); 74 assertNotNull( greenProject.getClassData("Seventh")); 75 assertNull( redProject.getClassData( "test.First")); 76 77 Iterator packages = greenProject.getPackages().iterator(); 78 79 PackageData first = (PackageData) packages.next(); 80 assertEquals( "", first.getName()); 81 assertEquals( 2, first.getNumberOfChildren()); 82 assertNotNull( first.getChild("Fourth")); 83 assertNotNull( first.getChild("Seventh")); 84 85 PackageData beautiful = (PackageData) packages.next(); 86 assertEquals( "beautiful", beautiful.getName()); 87 assertEquals( 1, beautiful.getNumberOfChildren()); 88 assertNotNull( beautiful.getChild("Fourth")); 89 90 PackageData test = (PackageData) packages.next(); 91 assertEquals( "test", test.getName()); 92 assertEquals( 1, test.getNumberOfChildren()); 93 assertNotNull( test.getChild("First")); 94 assertNull( test.getChild("test/me/First.java")); 95 assertNull( test.getChild("Fourth.java")); 96 97 PackageData testMe = (PackageData) packages.next(); 98 assertEquals( "test.me", testMe.getName()); 99 assertEquals( 1, testMe.getNumberOfChildren()); 100 assertNull( testMe.getChild("test/First.java")); 101 assertNotNull( testMe.getChild("Fourth")); 102 assertNull( testMe.getChild("Fourth.java")); 103 104 assertFalse( packages.hasNext()); 105 } 106 107 108 public void testMergeDifferentClassData() { 109 greenProject.addClassData( firstClass); 110 111 redProject.addClassData( secondClass); 112 redProject.addClassData( thirdClass); 113 114 greenProject.merge( redProject); 115 116 assertEquals( 1, greenProject.getNumberOfChildren()); 117 assertEquals( 3, greenProject.getClasses().size()); 118 119 assertNotNull( greenProject.getClassData("test.First")); 120 assertNotNull( greenProject.getClassData("test.Second")); 121 assertNotNull( greenProject.getClassData("test.Third")); 122 123 assertNull( redProject.getClassData("test.First")); 124 assertNotNull( redProject.getClassData("test.Second")); 125 assertNotNull( redProject.getClassData("test.Third")); 126 } 127 128 public void testMergeSimillarClassData() { 129 greenProject.addClassData( secondClass); 130 greenProject.addClassData( thirdClass); 131 132 redProject.addClassData( firstClass); 133 redProject.addClassData( thirdClass); 134 135 greenProject.merge( redProject); 136 137 assertEquals( 1, greenProject.getNumberOfChildren()); 138 assertEquals( 3, greenProject.getClasses().size()); 139 140 assertNotNull( greenProject.getClassData("test.First")); 141 assertNotNull( greenProject.getClassData("test.Second")); 142 assertNotNull( greenProject.getClassData("test.Third")); 143 144 assertNotNull( redProject.getClassData("test.First")); 145 assertNull( redProject.getClassData("test.Second")); 146 assertNotNull( redProject.getClassData("test.Third")); 147 } 148 149 public void testMergeDifferentLineNumbers() { 150 firstClass.addLine( 2, "helloWorld","()V"); 151 firstClass.addLine( 3, "helloWorld","()V"); 152 greenProject.addClassData( firstClass); 153 154 firstClassB.addLine( 1, "helloWorld","()V"); 155 firstClassB.addLine( 5, "helloWorld","()V"); 156 redProject.addClassData( firstClassB); 157 158 greenProject.merge( redProject); 159 160 ClassData cd = greenProject.getClassData("test.First"); 161 assertNotNull( cd); 162 assertEquals( 4, cd.getNumberOfValidLines()); 163 assertEquals( 2, redProject.getClassData("test.First").getNumberOfValidLines()); 164 165 Iterator lines = cd.getLines().iterator(); 166 LineData line1 = (LineData) lines.next(); 167 assertEquals( 1, line1.getLineNumber()); 168 LineData line2 = (LineData) lines.next(); 169 assertEquals( 2, line2.getLineNumber()); 170 LineData line3 = (LineData) lines.next(); 171 assertEquals( 3, line3.getLineNumber()); 172 LineData line5 = (LineData) lines.next(); 173 assertEquals( 5, line5.getLineNumber()); 174 assertFalse( lines.hasNext()); 175 } 176 177 public void testMergeSimillarLineNumbers() { 178 firstClass.addLine( 2, "helloWorld","()V"); 179 firstClass.touch(2); 180 firstClass.touch(2); 181 firstClass.addLine( 3, "helloWorld","()V"); 182 greenProject.addClassData( firstClass); 183 184 firstClassB.addLine( 2, "helloWorld","()V"); 185 firstClassB.touch(2); 186 firstClassB.touch(2); 187 firstClassB.touch(2); 188 firstClassB.addLine( 3, "helloWorld","()V"); 189 firstClassB.touch(3); 190 firstClassB.addLine( 7, "helloWorld","()V"); 191 redProject.addClassData( firstClassB); 192 193 greenProject.merge( redProject); 194 195 ClassData cd = greenProject.getClassData("test.First"); 196 assertNotNull( cd); 197 assertEquals( 3, cd.getNumberOfValidLines()); 198 assertEquals( 3, redProject.getClassData("test.First").getNumberOfValidLines()); 199 200 Iterator lines = cd.getLines().iterator(); 201 LineData line2 = (LineData) lines.next(); 202 assertEquals( 2, line2.getLineNumber()); 203 assertEquals( 5, line2.getHits()); 204 LineData line3 = (LineData) lines.next(); 205 assertEquals( 3, line3.getLineNumber()); 206 assertEquals( 1, line3.getHits()); 207 LineData line7 = (LineData) lines.next(); 208 assertEquals( 7, line7.getLineNumber()); 209 assertEquals( 0, line7.getHits()); 210 assertFalse( lines.hasNext()); 211 } 212 213 public void testMergeBranches() { 214 firstClass.addLine( 1, "helloWorld","()V"); 215 firstClass.markLineAsBranch(1); 216 firstClass.addLine( 2, "helloWorld","()V"); 217 firstClass.markLineAsBranch(2); 218 firstClass.addLine( 3, "helloWorld","()V"); 219 firstClass.addLine( 4, "helloWorld","()V"); 220 firstClass.markLineAsBranch(4); 221 firstClass.addLine( 5, "helloWorld","()V"); 222 firstClass.addLine( 8, "helloWorld","()V"); 223 greenProject.addClassData( firstClass); 224 225 firstClassB.addLine( 1, "helloWorld","()V"); 226 firstClassB.markLineAsBranch(1); 227 firstClassB.addLine( 2, "helloWorld","()V"); 228 firstClassB.addLine( 3, "helloWorld","()V"); 229 firstClassB.markLineAsBranch(3); 230 firstClassB.addLine( 6, "helloWorld","()V"); 231 firstClassB.markLineAsBranch(6); 232 firstClassB.addLine( 7, "helloWorld","()V"); 233 firstClassB.addLine( 8, "helloWorld","()V"); 234 redProject.addClassData( firstClassB); 235 236 greenProject.merge( redProject); 237 238 ClassData cd = greenProject.getClassData("test.First"); 239 240 Iterator lines = cd.getLines().iterator(); 241 242 LineData line1 = (LineData) lines.next(); 243 assertTrue( line1.isBranch()); 244 LineData line2 = (LineData) lines.next(); 245 assertTrue( line2.isBranch()); 246 LineData line3 = (LineData) lines.next(); 247 assertTrue( line3.isBranch()); 248 LineData line4 = (LineData) lines.next(); 249 assertTrue( line4.isBranch()); 250 LineData line5 = (LineData) lines.next(); 251 assertFalse( line5.isBranch()); 252 LineData line6 = (LineData) lines.next(); 253 assertTrue( line6.isBranch()); 254 LineData line7 = (LineData) lines.next(); 255 assertFalse( line7.isBranch()); 256 LineData line8 = (LineData) lines.next(); 257 assertFalse( line8.isBranch()); 258 assertFalse( lines.hasNext()); 259 } 260 261 public void testMergeSourceFiles() { 262 greenProject.addClassData( secondClass); 263 greenProject.addClassData( fourthClass); 264 265 redProject.addClassData( firstClass); 266 redProject.addClassData( fifthClass); 267 redProject.addClassData( seventhClass); 268 269 greenProject.merge( redProject); 270 271 Collection sources = greenProject.getSourceFiles(); 272 assertEquals( 5, sources.size()); 273 274 Set sourceNames = new HashSet (); 275 Iterator it = sources.iterator(); 276 while( it.hasNext()) 277 sourceNames.add( ((SourceFileData)it.next()).getName()); 278 279 assertTrue( sourceNames.contains("test/First.java")); 280 assertTrue( sourceNames.contains("test/Second.java")); 281 assertTrue( sourceNames.contains("test/me/Fourth.java")); 282 assertTrue( sourceNames.contains("beautiful/Fourth.java")); 283 assertTrue( sourceNames.contains("Seventh.java")); 284 } 285 } 286 | Popular Tags |