KickJava   Java API By Example, From Geeks To Geeks.

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


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 ProjectDataTest extends TestCase
27 {
28
29     private ProjectData coverageData;
30
31     public void setUp()
32     {
33         coverageData = new ProjectData();
34     }
35
36     public void testAddClass()
37     {
38         ClassData classData;
39
40         assertEquals(0, coverageData.getNumberOfChildren());
41         assertEquals(0, coverageData.getClasses().size());
42         assertEquals(null, coverageData.getClassData("gobbleDeeGoop"));
43
44         classData = new ClassData("com.example.HelloWorld");
45         classData.setSourceFileName("com/example/HelloWorld.java");
46         for (int i = 0; i < 10; i++)
47             classData.addLine(i, "test", "(I)B");
48         coverageData.addClassData(classData);
49         assertEquals(1, coverageData.getNumberOfChildren());
50         assertEquals(1, coverageData.getClasses().size());
51         assertEquals(classData, coverageData.getClassData(classData.getName()));
52
53         classData = new ClassData("com.example.HelloWorldHelper");
54         classData.setSourceFileName("com/example/HelloWorldHelper.java");
55         for (int i = 0; i < 14; i++)
56             classData.addLine(i, "test", "(I)B");
57         coverageData.addClassData(classData);
58         assertEquals(1, coverageData.getNumberOfChildren());
59         assertEquals(2, coverageData.getClasses().size());
60         assertEquals(classData, coverageData.getClassData(classData.getName()));
61
62         // See what happens when we try to add the same class twice
63
classData = new ClassData("com.example.HelloWorld");
64         classData.setSourceFileName("com/example/HelloWorld.java");
65         for (int i = 0; i < 19; i++)
66             classData.addLine(i, "test", "(I)B");
67         try
68         {
69             coverageData.addClassData(classData);
70             // removed by Jeremy Thomerson when changing PackageData
71
// fail("Expected an IllegalArgumentException but did not receive one!");
72
}
73         catch (IllegalArgumentException JavaDoc e)
74         {
75             // Good!
76
}
77         assertEquals(1, coverageData.getNumberOfChildren());
78         assertEquals(2, coverageData.getClasses().size());
79     }
80
81     public void testEquals()
82     {
83         ProjectData a = new ProjectData();
84         ProjectData b = new ProjectData();
85         ProjectData c = new ProjectData();
86         ClassData classData1 = new ClassData("com.example.HelloWorld1");
87         ClassData classData2 = new ClassData("com.example.HelloWorld2");
88         ClassData classData3 = new ClassData("com.example.HelloWorld3");
89         ClassData classData4 = new ClassData("com.example.HelloWorld4");
90
91         classData1.setSourceFileName("com/example/HelloWorld1.java");
92         classData2.setSourceFileName("com/example/HelloWorld2.java");
93         classData3.setSourceFileName("com/example/HelloWorld3.java");
94         classData4.setSourceFileName("com/example/HelloWorld4.java");
95
96         a.addClassData(classData1);
97         a.addClassData(classData2);
98         a.addClassData(classData3);
99         b.addClassData(classData1);
100         b.addClassData(classData2);
101         c.addClassData(classData1);
102         c.addClassData(classData2);
103         c.addClassData(classData4);
104
105         assertFalse(a.equals(null));
106         assertFalse(a.equals(classData1));
107
108         assertTrue(a.equals(a));
109         assertFalse(a.equals(b));
110         assertFalse(a.equals(c));
111         assertFalse(b.equals(a));
112         assertTrue(b.equals(b));
113         assertFalse(b.equals(c));
114         assertFalse(c.equals(a));
115         assertFalse(c.equals(b));
116         assertTrue(c.equals(c));
117
118         b.addClassData(classData3);
119         assertTrue(a.equals(b));
120         assertTrue(b.equals(a));
121
122         assertFalse(a.equals(c));
123         assertFalse(c.equals(a));
124     }
125     
126     public void testHashCode()
127     {
128         ProjectData a = new ProjectData();
129         ProjectData b = new ProjectData();
130         ClassData classData1 = new ClassData("com.example.HelloWorld1");
131         ClassData classData2 = new ClassData("com.example.HelloWorld2");
132         ClassData classData3 = new ClassData("com.example.HelloWorld3");
133
134         classData1.setSourceFileName("com/example/HelloWorld1.java");
135         classData2.setSourceFileName("com/example/HelloWorld2.java");
136         classData3.setSourceFileName("com/example/HelloWorld3.java");
137
138         a.addClassData(classData1);
139         a.addClassData(classData2);
140         a.addClassData(classData3);
141         b.addClassData(classData1);
142         b.addClassData(classData2);
143
144         assertEquals(a.hashCode(), a.hashCode());
145         assertEquals(b.hashCode(), b.hashCode());
146
147         b.addClassData(classData3);
148         assertEquals(a.hashCode(), b.hashCode());
149     }
150
151 }
152
Popular Tags