KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > reporting > ComplexityCalculatorTest


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2005 Grzegorz Lukasik
5  *
6  * Cobertura is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License,
9  * or (at your option) any later version.
10  *
11  * Cobertura is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Cobertura; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  */

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 JavaDoc ex) {}
122     }
123     
124     public void testGetCCNForPackage_null() {
125         try {
126             complexity.getCCNForPackage(null);
127             fail( "NullPointerException expected");
128         } catch( NullPointerException JavaDoc ex) {}
129     }
130
131     public void testGetCCNForProject_null() {
132         try {
133             complexity.getCCNForProject(null);
134             fail( "NullPointerException expected");
135         } catch( NullPointerException JavaDoc ex) {}
136     }
137     
138     public void testConstructor_null() {
139         try {
140             new ComplexityCalculator(null);
141             fail( "NullPointerException expected");
142         } catch( NullPointerException JavaDoc ex) {}
143     }
144
145     
146     protected void setUp() throws Exception JavaDoc {
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         // Do not add com/example/Sample8.java
159
// fileFinder.addSourceFile( fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[3]).toString(), "com/example/Sample8.java");
160

161         complexity = new ComplexityCalculator( fileFinder);
162     }
163
164     protected void tearDown() throws Exception JavaDoc {
165         super.tearDown();
166         fileFixture.tearDown();
167     }
168 }
169
Popular Tags