001: /*
002: * Cobertura - http://cobertura.sourceforge.net/
003: *
004: * Copyright (C) 2005 Grzegorz Lukasik
005: *
006: * Cobertura is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published
008: * by the Free Software Foundation; either version 2 of the License,
009: * or (at your option) any later version.
010: *
011: * Cobertura is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with Cobertura; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: */
021: package net.sourceforge.cobertura.reporting;
022:
023: import net.sourceforge.cobertura.coveragedata.ClassData;
024: import net.sourceforge.cobertura.coveragedata.PackageData;
025: import net.sourceforge.cobertura.coveragedata.ProjectData;
026: import net.sourceforge.cobertura.coveragedata.SourceFileData;
027: import net.sourceforge.cobertura.util.FileFinder;
028: import net.sourceforge.cobertura.util.FileFixture;
029: import junit.framework.TestCase;
030:
031: public class ComplexityCalculatorTest extends TestCase {
032: private FileFixture fileFixture;
033: private FileFinder fileFinder;
034: private ComplexityCalculator complexity;
035:
036: public void testGetCCNForSourceFile() {
037: double ccn1 = complexity
038: .getCCNForSourceFile(new SourceFileData(
039: "com/example/Sample1.java"));
040: assertTrue(ccn1 != 0.0);
041: double ccn2 = complexity
042: .getCCNForSourceFile(new SourceFileData(
043: "com/example/Sample2.java"));
044: assertTrue(ccn2 != 0.0);
045: assertTrue(ccn1 != ccn2);
046:
047: ccn1 = complexity.getCCNForSourceFile(new SourceFileData(
048: "com/example/Sample5.java"));
049: assertTrue(ccn1 != 0.0);
050: ccn2 = complexity.getCCNForSourceFile(new SourceFileData(
051: "com/example/Sample6.java"));
052: assertTrue(ccn2 != 0.0);
053: assertTrue(ccn1 != ccn2);
054:
055: double ccn0 = complexity
056: .getCCNForSourceFile(new SourceFileData(
057: "com/example/Sample8.java"));
058: assertTrue(ccn0 == 0.0);
059:
060: ccn0 = complexity.getCCNForSourceFile(new SourceFileData(
061: "Foo.java"));
062: assertTrue(ccn0 == 0.0);
063: }
064:
065: public void testGetCCNForClass() {
066: double ccn1 = complexity.getCCNForClass(new ClassData(
067: "com.example.Sample3"));
068: assertTrue(ccn1 != 0.0);
069: double ccn2 = complexity.getCCNForClass(new ClassData(
070: "com.example.Sample4"));
071: assertTrue(ccn2 != 0.0);
072: assertTrue(ccn1 != ccn2);
073:
074: ccn1 = complexity.getCCNForClass(new ClassData(
075: "com.example.Sample5"));
076: assertTrue(ccn1 != 0.0);
077: ccn2 = complexity.getCCNForClass(new ClassData(
078: "com.example.Sample6"));
079: assertTrue(ccn2 != 0.0);
080: assertTrue(ccn1 != ccn2);
081:
082: double ccn0 = complexity.getCCNForClass(new ClassData(
083: "com.example.Sample8"));
084: assertEquals(0.0, ccn0, 0.0);
085:
086: ccn0 = complexity.getCCNForClass(new ClassData("Foo"));
087: assertEquals(0.0, ccn0, 0.0);
088: }
089:
090: public void testGetCCNForPackage() {
091: PackageData pd = new PackageData("com.example");
092: pd.addClassData(new ClassData("com.example.Sample3"));
093: double ccn1 = complexity.getCCNForPackage(pd);
094: assertTrue(ccn1 != 0.0);
095:
096: ComplexityCalculator complexity2 = new ComplexityCalculator(
097: fileFinder);
098: pd.addClassData(new ClassData("com.example.Sample4"));
099: double ccn2 = complexity2.getCCNForPackage(pd);
100: double ccn3 = complexity2.getCCNForPackage(pd);
101: assertTrue(ccn2 != 0.0);
102: assertTrue(ccn1 != ccn2);
103: assertEquals(ccn2, ccn3, 0e-9);
104:
105: PackageData empty = new PackageData("com.example2");
106: ComplexityCalculator complexity3 = new ComplexityCalculator(
107: fileFinder);
108: assertEquals(0.0, complexity3.getCCNForPackage(empty), 0.0);
109: }
110:
111: public void testGetCCNForProject() {
112: ProjectData project = new ProjectData();
113: project.addClassData(new ClassData("com.example.Sample5"));
114: double ccn1 = complexity.getCCNForProject(project);
115: assertTrue(ccn1 != 0.0);
116:
117: ComplexityCalculator complexity2 = new ComplexityCalculator(
118: fileFinder);
119: project.addClassData(new ClassData("com.example.Sample4"));
120: double ccn2 = complexity2.getCCNForProject(project);
121: assertTrue(ccn2 != 0.0);
122: assertTrue(ccn1 != ccn2);
123:
124: ComplexityCalculator complexity3 = new ComplexityCalculator(
125: fileFinder);
126: project.addClassData(new ClassData("com.example.Sample8"));
127: double ccn3 = complexity3.getCCNForProject(project);
128: assertEquals(ccn2, ccn3, 0e-9);
129:
130: ComplexityCalculator complexity4 = new ComplexityCalculator(
131: fileFinder);
132: double ccn0 = complexity4.getCCNForProject(new ProjectData());
133: assertEquals(0.0, ccn0, 0.0);
134: }
135:
136: public void testGetCCNForSourceFile_null() {
137: try {
138: complexity.getCCNForSourceFile(null);
139: fail("NullPointerException expected");
140: } catch (NullPointerException ex) {
141: }
142: }
143:
144: public void testGetCCNForPackage_null() {
145: try {
146: complexity.getCCNForPackage(null);
147: fail("NullPointerException expected");
148: } catch (NullPointerException ex) {
149: }
150: }
151:
152: public void testGetCCNForProject_null() {
153: try {
154: complexity.getCCNForProject(null);
155: fail("NullPointerException expected");
156: } catch (NullPointerException ex) {
157: }
158: }
159:
160: public void testConstructor_null() {
161: try {
162: new ComplexityCalculator(null);
163: fail("NullPointerException expected");
164: } catch (NullPointerException ex) {
165: }
166: }
167:
168: protected void setUp() throws Exception {
169: super .setUp();
170: fileFixture = new FileFixture();
171: fileFixture.setUp();
172:
173: fileFinder = new FileFinder();
174: fileFinder.addSourceDirectory(fileFixture.sourceDirectory(
175: FileFixture.SOURCE_DIRECTORY_IDENTIFIER[0]).toString());
176: fileFinder.addSourceDirectory(fileFixture.sourceDirectory(
177: FileFixture.SOURCE_DIRECTORY_IDENTIFIER[1]).toString());
178: fileFinder.addSourceFile(fileFixture.sourceDirectory(
179: FileFixture.SOURCE_DIRECTORY_IDENTIFIER[2]).toString(),
180: "com/example\\Sample5.java");
181: fileFinder.addSourceFile(fileFixture.sourceDirectory(
182: FileFixture.SOURCE_DIRECTORY_IDENTIFIER[2]).toString(),
183: "com/example/Sample6.java");
184: fileFinder.addSourceFile(fileFixture.sourceDirectory(
185: FileFixture.SOURCE_DIRECTORY_IDENTIFIER[3]).toString(),
186: "com/example/Sample7.java");
187:
188: // Do not add com/example/Sample8.java
189: // fileFinder.addSourceFile( fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[3]).toString(), "com/example/Sample8.java");
190:
191: complexity = new ComplexityCalculator(fileFinder);
192: }
193:
194: protected void tearDown() throws Exception {
195: super.tearDown();
196: fileFixture.tearDown();
197: }
198: }
|