1 21 package net.sourceforge.cobertura.reporting; 22 23 import net.sourceforge.cobertura.coveragedata.ClassData; 24 import net.sourceforge.cobertura.coveragedata.PackageData; 25 import net.sourceforge.cobertura.coveragedata.ProjectData; 26 import net.sourceforge.cobertura.coveragedata.SourceFileData; 27 import net.sourceforge.cobertura.util.FileFinder; 28 import net.sourceforge.cobertura.util.FileFixture; 29 import junit.framework.TestCase; 30 31 public class ComplexityCalculatorTest extends TestCase { 32 private FileFixture fileFixture; 33 private FileFinder fileFinder; 34 private ComplexityCalculator complexity; 35 36 public void testGetCCNForSourceFile() { 37 double ccn1 = complexity.getCCNForSourceFile( new SourceFileData("com/example/Sample1.java")); 38 assertTrue( ccn1!=0.0); 39 double ccn2 = complexity.getCCNForSourceFile( new SourceFileData("com/example/Sample2.java")); 40 assertTrue( ccn2!=0.0); 41 assertTrue( ccn1!=ccn2); 42 43 ccn1 = complexity.getCCNForSourceFile( new SourceFileData("com/example/Sample5.java")); 44 assertTrue( ccn1!=0.0); 45 ccn2 = complexity.getCCNForSourceFile( new SourceFileData("com/example/Sample6.java")); 46 assertTrue( ccn2!=0.0); 47 assertTrue( ccn1!=ccn2); 48 49 double ccn0 = complexity.getCCNForSourceFile( new SourceFileData("com/example/Sample8.java")); 50 assertTrue( ccn0==0.0); 51 52 ccn0 = complexity.getCCNForSourceFile( new SourceFileData("Foo.java")); 53 assertTrue( ccn0==0.0); 54 } 55 56 public void testGetCCNForClass() { 57 double ccn1 = complexity.getCCNForClass( new ClassData("com.example.Sample3")); 58 assertTrue( ccn1!=0.0); 59 double ccn2 = complexity.getCCNForClass( new ClassData("com.example.Sample4")); 60 assertTrue( ccn2!=0.0); 61 assertTrue( ccn1!=ccn2); 62 63 ccn1 = complexity.getCCNForClass( new ClassData("com.example.Sample5")); 64 assertTrue( ccn1!=0.0); 65 ccn2 = complexity.getCCNForClass( new ClassData("com.example.Sample6")); 66 assertTrue( ccn2!=0.0); 67 assertTrue( ccn1!=ccn2); 68 69 double ccn0 = complexity.getCCNForClass( new ClassData("com.example.Sample8")); 70 assertEquals( 0.0, ccn0, 0.0); 71 72 ccn0 = complexity.getCCNForClass( new ClassData("Foo")); 73 assertEquals( 0.0, ccn0, 0.0); 74 } 75 76 public void testGetCCNForPackage() { 77 PackageData pd = new PackageData("com.example"); 78 pd.addClassData( new ClassData("com.example.Sample3")); 79 double ccn1 = complexity.getCCNForPackage( pd); 80 assertTrue( ccn1!=0.0); 81 82 ComplexityCalculator complexity2 = new ComplexityCalculator( fileFinder); 83 pd.addClassData( new ClassData("com.example.Sample4")); 84 double ccn2 = complexity2.getCCNForPackage( pd); 85 double ccn3 = complexity2.getCCNForPackage( pd); 86 assertTrue( ccn2!=0.0); 87 assertTrue( ccn1!=ccn2); 88 assertEquals( ccn2, ccn3, 0e-9); 89 90 PackageData empty = new PackageData( "com.example2"); 91 ComplexityCalculator complexity3 = new ComplexityCalculator( fileFinder); 92 assertEquals( 0.0, complexity3.getCCNForPackage( empty), 0.0); 93 } 94 95 public void testGetCCNForProject() { 96 ProjectData project = new ProjectData(); 97 project.addClassData( new ClassData("com.example.Sample5")); 98 double ccn1 = complexity.getCCNForProject( project); 99 assertTrue( ccn1!=0.0); 100 101 ComplexityCalculator complexity2 = new ComplexityCalculator( fileFinder); 102 project.addClassData( new ClassData("com.example.Sample4")); 103 double ccn2 = complexity2.getCCNForProject( project); 104 assertTrue( ccn2!=0.0); 105 assertTrue( ccn1!=ccn2); 106 107 ComplexityCalculator complexity3 = new ComplexityCalculator( fileFinder); 108 project.addClassData( new ClassData("com.example.Sample8")); 109 double ccn3 = complexity3.getCCNForProject( project); 110 assertEquals( ccn2, ccn3, 0e-9); 111 112 ComplexityCalculator complexity4 = new ComplexityCalculator( fileFinder); 113 double ccn0 = complexity4.getCCNForProject( new ProjectData()); 114 assertEquals( 0.0, ccn0, 0.0); 115 } 116 117 public void testGetCCNForSourceFile_null() { 118 try { 119 complexity.getCCNForSourceFile(null); 120 fail( "NullPointerException expected"); 121 } catch( NullPointerException ex) {} 122 } 123 124 public void testGetCCNForPackage_null() { 125 try { 126 complexity.getCCNForPackage(null); 127 fail( "NullPointerException expected"); 128 } catch( NullPointerException ex) {} 129 } 130 131 public void testGetCCNForProject_null() { 132 try { 133 complexity.getCCNForProject(null); 134 fail( "NullPointerException expected"); 135 } catch( NullPointerException ex) {} 136 } 137 138 public void testConstructor_null() { 139 try { 140 new ComplexityCalculator(null); 141 fail( "NullPointerException expected"); 142 } catch( NullPointerException ex) {} 143 } 144 145 146 protected void setUp() throws Exception { 147 super.setUp(); 148 fileFixture = new FileFixture(); 149 fileFixture.setUp(); 150 151 fileFinder = new FileFinder(); 152 fileFinder.addSourceDirectory(fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[0]).toString()); 153 fileFinder.addSourceDirectory(fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[1]).toString()); 154 fileFinder.addSourceFile( fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[2]).toString(), "com/example\\Sample5.java"); 155 fileFinder.addSourceFile( fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[2]).toString(), "com/example/Sample6.java"); 156 fileFinder.addSourceFile( fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[3]).toString(), "com/example/Sample7.java"); 157 158 161 complexity = new ComplexityCalculator( fileFinder); 162 } 163 164 protected void tearDown() throws Exception { 165 super.tearDown(); 166 fileFixture.tearDown(); 167 } 168 } 169 | Popular Tags |