KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > coveragedata > ClassDataTest


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

22
23 package net.sourceforge.cobertura.coveragedata;
24
25 import java.util.Collection JavaDoc;
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         // Setting an invalid line as a branch should not make the line valid
58
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 JavaDoc branches = b.getBranches();
71         assertEquals(2, branches.size());
72         assertEquals(b.getNumberOfValidBranches(), branches.size());
73         //assertTrue(branches.contains(new LineData(2, "test", "(I)B")));
74
//assertTrue(branches.contains(new LineData(4, "test", "(I)B")));
75
}
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 JavaDoc e)
116         {
117             // Good!
118
}
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 JavaDoc(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