KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2005 Mark Doliner
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
22 package net.sourceforge.cobertura.coveragedata;
23
24 import junit.framework.TestCase;
25
26 public class PackageDataTest extends TestCase
27 {
28
29     private PackageData packageData;
30
31     public void setUp()
32     {
33         packageData = new PackageData("com.example");
34         assertEquals("com.example", packageData.getName());
35     }
36
37     public void testAddClass()
38     {
39         ClassData classData;
40
41         assertEquals(0, packageData.getNumberOfChildren());
42
43         classData = new ClassData("com.example.HelloWorld");
44         classData.setSourceFileName("com/example/HelloWorld.java");
45         for (int i = 0; i < 10; i++)
46             classData.addLine(i, "test", "(I)B");
47         packageData.addClassData(classData);
48         assertEquals(1, packageData.getNumberOfChildren());
49         assertTrue(packageData.contains(classData.getBaseName()));
50
51         classData = new ClassData("com.example.HelloWorldHelper");
52         classData.setSourceFileName("com/example/HelloWorldHelper.java");
53         for (int i = 0; i < 14; i++)
54             classData.addLine(i, "test", "(I)B");
55         packageData.addClassData(classData);
56         assertEquals(2, packageData.getNumberOfChildren());
57         assertTrue(packageData.contains(classData.getBaseName()));
58
59         // See what happens when we try to add the same class twice
60
classData = new ClassData("com.example.HelloWorld");
61         classData.setSourceFileName("com/example/HelloWorld.java");
62         for (int i = 0; i < 19; i++)
63             classData.addLine(i, "test", "(I)B");
64         try
65         {
66             packageData.addClassData(classData);
67             // removed by Jeremy Thomerson when changing PackageData
68
// fail("Expected an IllegalArgumentException but did not receive one!");
69
}
70         catch (IllegalArgumentException JavaDoc e)
71         {
72             // Good!
73
}
74
75         assertEquals(2, packageData.getNumberOfChildren());
76     }
77
78     public void testBranchCoverage()
79     {
80         assertEquals(0, packageData.getNumberOfCoveredBranches());
81         assertEquals(0, packageData.getNumberOfValidBranches());
82         assertEquals(1.00d, packageData.getBranchCoverageRate(), 0d);
83
84         ClassData classData = new ClassData("com.example.HelloWorld");
85         classData.setSourceFileName("com/example/HelloWorld.java");
86         for (int i = 0; i < 10; i++)
87             classData.addLine(i, "test", "(I)B");
88         packageData.addClassData(classData);
89
90         assertEquals(0, packageData.getNumberOfCoveredBranches());
91         assertEquals(0, packageData.getNumberOfValidBranches());
92         assertEquals(1.00d, packageData.getBranchCoverageRate(), 0d);
93
94         classData.markLineAsBranch(1);
95         classData.markLineAsBranch(2);
96         classData.markLineAsBranch(3);
97         classData.markLineAsBranch(4);
98
99         assertEquals(0, packageData.getNumberOfCoveredBranches());
100         assertEquals(4, packageData.getNumberOfValidBranches());
101         assertEquals(0.00d, packageData.getBranchCoverageRate(), 0d);
102
103         classData.touch(1);
104         classData.touch(2);
105
106         assertEquals(2, packageData.getNumberOfCoveredBranches());
107         assertEquals(4, packageData.getNumberOfValidBranches());
108         assertEquals(0.50d, packageData.getBranchCoverageRate(), 0d);
109     }
110
111     public void testConstructor()
112     {
113         try
114         {
115             new PackageData(null);
116             fail("Expected an IllegalArgumentException but did not receive one!");
117         }
118         catch (IllegalArgumentException JavaDoc e)
119         {
120             // Good!
121
}
122     }
123
124     public void testEquals()
125     {
126         PackageData a = new PackageData("com.example");
127         PackageData b = new PackageData("com.example");
128         PackageData c = new PackageData("com.example");
129         ClassData classData1 = new ClassData("com.example.HelloWorld1");
130         ClassData classData2 = new ClassData("com.example.HelloWorld2");
131         ClassData classData3 = new ClassData("com.example.HelloWorld3");
132         ClassData classData4 = new ClassData("com.example.HelloWorld4");
133
134         classData1.setSourceFileName("com/example/HelloWorld1.java");
135         classData2.setSourceFileName("com/example/HelloWorld2.java");
136         classData3.setSourceFileName("com/example/HelloWorld3.java");
137         classData4.setSourceFileName("com/example/HelloWorld4.java");
138
139         a.addClassData(classData1);
140         a.addClassData(classData2);
141         a.addClassData(classData3);
142         b.addClassData(classData1);
143         b.addClassData(classData2);
144         c.addClassData(classData1);
145         c.addClassData(classData2);
146         c.addClassData(classData4);
147
148         assertFalse(a.equals(null));
149         assertFalse(a.equals(classData1));
150
151         assertTrue(a.equals(a));
152         assertFalse(a.equals(b));
153         assertFalse(a.equals(c));
154         assertFalse(b.equals(a));
155         assertTrue(b.equals(b));
156         assertFalse(b.equals(c));
157         assertFalse(c.equals(a));
158         assertFalse(c.equals(b));
159         assertTrue(c.equals(c));
160
161         b.addClassData(classData3);
162         assertTrue(a.equals(b));
163         assertTrue(b.equals(a));
164
165         assertFalse(a.equals(c));
166         assertFalse(c.equals(a));
167     }
168
169     public void testHashCode()
170     {
171         PackageData a = new PackageData("com.example");
172         PackageData b = new PackageData("com.example");
173         ClassData classData1 = new ClassData("com.example.HelloWorld1");
174         ClassData classData2 = new ClassData("com.example.HelloWorld2");
175         ClassData classData3 = new ClassData("com.example.HelloWorld3");
176
177         classData1.setSourceFileName("com/example/HelloWorld1.java");
178         classData2.setSourceFileName("com/example/HelloWorld2.java");
179         classData3.setSourceFileName("com/example/HelloWorld3.java");
180
181         a.addClassData(classData1);
182         a.addClassData(classData2);
183         a.addClassData(classData3);
184         b.addClassData(classData1);
185         b.addClassData(classData2);
186
187         assertEquals(a.hashCode(), a.hashCode());
188         assertEquals(b.hashCode(), b.hashCode());
189
190         b.addClassData(classData3);
191         assertEquals(a.hashCode(), b.hashCode());
192         assertEquals(b.hashCode(), b.hashCode());
193     }
194 }
195
Popular Tags