KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > TestMetricsFactory


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.metrics;
34
35 import junit.framework.*;
36
37 import java.io.*;
38
39 public class TestMetricsFactory extends TestCase {
40     MetricsConfiguration configuration;
41     MetricsFactory factory;
42     
43     protected void setUp() throws Exception JavaDoc {
44         configuration = new MetricsConfigurationLoader(Boolean.getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE")).load("etc" + File.separator + "MetricsConfig.xml");
45         factory = new MetricsFactory("test", configuration);
46     }
47     
48     public void testCreateProjectMetrics() {
49         Metrics m1 = factory.createProjectMetrics("foo");
50         assertNotNull(m1);
51         assertEquals("New metrics name", "foo", m1.getName());
52
53         Metrics m2 = factory.createProjectMetrics("foo");
54         assertSame(m1, m2);
55         assertEquals("project measurements", configuration.getProjectMeasurements().size(), m1.getMeasurementNames().size());
56     }
57             
58     public void testIncludeProjectMetrics() {
59         Metrics m1 = factory.createProjectMetrics("foo");
60
61         assertFalse("ProjectMetrics() contains external metrics", factory.getProjectMetrics().contains(m1));
62
63         factory.includeProjectMetrics(m1);
64
65         assertTrue("ProjectMetrics() does not contain internal metrics", factory.getProjectMetrics().contains(m1));
66     }
67     
68     public void testCreateGroupMetrics() {
69         Metrics m1 = factory.createGroupMetrics("foo");
70         assertNotNull(m1);
71         assertEquals("New metrics name", "foo", m1.getName());
72
73         Metrics m2 = factory.createGroupMetrics("foo");
74         assertSame(m1, m2);
75         assertEquals("group measurements", configuration.getGroupMeasurements().size(), m1.getMeasurementNames().size());
76     }
77             
78     public void testIncludeGroupMetrics() {
79         Metrics m1 = factory.createGroupMetrics("foo");
80
81         assertFalse("GroupMetrics() contains external metrics", factory.getGroupMetrics().contains(m1));
82
83         factory.includeGroupMetrics(m1);
84
85         assertTrue("GroupMetrics() does not contain internal metrics", factory.getGroupMetrics().contains(m1));
86     }
87
88     public void testCreateClassMetrics() {
89         Metrics m1 = factory.createClassMetrics("foo");
90         assertNotNull(m1);
91         assertEquals("New metrics name", "foo", m1.getName());
92
93         Metrics m2 = factory.createClassMetrics("foo");
94         assertSame(m1, m2);
95         assertEquals("class measurements", configuration.getClassMeasurements().size(), m1.getMeasurementNames().size());
96     }
97         
98     public void testIncludeClassMetrics() {
99         Metrics m1 = factory.createClassMetrics("foo");
100
101         assertFalse("ClassMetrics() contains external metrics", factory.getClassMetrics().contains(m1));
102
103         factory.includeClassMetrics(m1);
104
105         assertTrue("ClassMetrics() does not contain internal metrics", factory.getClassMetrics().contains(m1));
106     }
107
108     public void testCreateMethodMetrics() {
109         Metrics m1 = factory.createMethodMetrics("foo");
110         assertNotNull(m1);
111         assertEquals("New metrics name", "foo", m1.getName());
112
113         Metrics m2 = factory.createMethodMetrics("foo");
114         assertSame(m1, m2);
115         assertEquals("method measurements", configuration.getMethodMeasurements().size(), m1.getMeasurementNames().size());
116     }
117     
118     public void testIncludeMethodMetrics() {
119         Metrics m1 = factory.createMethodMetrics("foo");
120
121         assertFalse("MethodMetrics() contains external metrics", factory.getMethodMetrics().contains(m1));
122
123         factory.includeMethodMetrics(m1);
124
125         assertTrue("MethodMetrics() does not contain internal metrics", factory.getMethodMetrics().contains(m1));
126     }
127     
128     public void testCreateStaticInitializerMetrics() {
129         Metrics m = factory.createMethodMetrics("foo.static {}");
130
131         assertEquals("class name", "foo", m.getParent().getName());
132     }
133
134     public void testCreateStructure() {
135         Metrics methodMetrics = factory.createMethodMetrics("a.A.a()");
136         Metrics classMetrics = factory.createClassMetrics("a.A");
137         Metrics packageMetrics = factory.createGroupMetrics("a");
138         Metrics projectMetrics = factory.createProjectMetrics();
139
140         factory.includeMethodMetrics(methodMetrics);
141         
142         assertTrue(projectMetrics.getSubMetrics().contains(packageMetrics));
143         assertTrue(packageMetrics.getSubMetrics().contains(classMetrics));
144         assertTrue(classMetrics.getSubMetrics().contains(methodMetrics));
145     }
146
147     public void testGroupDefinitionsWithInternal() {
148         configuration.addGroupDefinition("foo", "/foo/");
149         configuration.addGroupDefinition("bar", "/bar/");
150         configuration.addGroupDefinition("baz", "/baz/");
151
152         Metrics metrics = factory.createClassMetrics("com.foobar.Foobar");
153         factory.includeClassMetrics(metrics);
154
155         assertEquals("Number of groups", 3, factory.getGroupMetrics().size());
156         assertEquals("Number of all groups", 3, factory.getAllGroupMetrics().size());
157
158         assertTrue("Group foo missing", factory.getAllGroupNames().contains("foo"));
159         assertTrue("Group bar missing", factory.getAllGroupNames().contains("bar"));
160         assertFalse("Group baz present", factory.getAllGroupNames().contains("baz"));
161         assertTrue("Group com.foobar missing", factory.getAllGroupNames().contains("com.foobar"));
162
163         assertTrue("Group foo missing", factory.getGroupNames().contains("foo"));
164         assertTrue("Group bar missing", factory.getGroupNames().contains("bar"));
165         assertFalse("Group baz present", factory.getGroupNames().contains("baz"));
166         assertTrue("Group com.foobar missing", factory.getGroupNames().contains("com.foobar"));
167
168         assertTrue("Not in foo", factory.createGroupMetrics("foo").getSubMetrics().contains(metrics));
169         assertTrue("Not in bar", factory.createGroupMetrics("bar").getSubMetrics().contains(metrics));
170         assertFalse("In baz", factory.createGroupMetrics("baz").getSubMetrics().contains(metrics));
171         assertTrue("Not in com.foobar", factory.createGroupMetrics("com.foobar").getSubMetrics().contains(metrics));
172
173         assertEquals("Wrong parent", factory.createGroupMetrics("com.foobar"), metrics.getParent());
174     }
175
176     public void testGroupDefinitionsWithExternal() {
177         configuration.addGroupDefinition("foo", "/foo/");
178         configuration.addGroupDefinition("bar", "/bar/");
179         configuration.addGroupDefinition("baz", "/baz/");
180
181         Metrics metrics = factory.createClassMetrics("com.foobar.Foobar");
182
183         assertEquals("Number of groups", 0, factory.getGroupMetrics().size());
184         assertEquals("Number of all groups", 1, factory.getAllGroupMetrics().size());
185
186         assertFalse("Group foo present", factory.getAllGroupNames().contains("foo"));
187         assertFalse("Group bar present", factory.getAllGroupNames().contains("bar"));
188         assertFalse("Group baz present", factory.getAllGroupNames().contains("baz"));
189         assertTrue("Group com.foobar missing", factory.getAllGroupNames().contains("com.foobar"));
190
191         assertFalse("In foo", factory.createGroupMetrics("foo").getSubMetrics().contains(metrics));
192         assertFalse("In bar", factory.createGroupMetrics("bar").getSubMetrics().contains(metrics));
193         assertFalse("In baz", factory.createGroupMetrics("baz").getSubMetrics().contains(metrics));
194         assertFalse("In com.foobar", factory.createGroupMetrics("com.foobar").getSubMetrics().contains(metrics));
195
196         assertEquals("Wrong parent", factory.createGroupMetrics("com.foobar"), metrics.getParent());
197     }
198
199     public void testGroupDefinitionsWithBoth() {
200         configuration.addGroupDefinition("foo", "/foo/");
201         configuration.addGroupDefinition("baz", "/baz/");
202
203         Metrics fooMetrics = factory.createClassMetrics("com.foo.Foo");
204         Metrics foobazMetrics = factory.createClassMetrics("com.baz.Foobaz");
205
206         factory.includeClassMetrics(fooMetrics);
207
208         assertEquals("Number of groups", 2, factory.getGroupMetrics().size());
209
210         assertTrue("Group foo missing", factory.getGroupNames().contains("foo"));
211         assertFalse("Group baz present", factory.getGroupNames().contains("baz"));
212         assertTrue("Group com.foo missing", factory.getGroupNames().contains("com.foo"));
213         assertFalse("Group com.baz missing", factory.getGroupNames().contains("com.baz"));
214
215         assertTrue("Not in foo", factory.createGroupMetrics("foo").getSubMetrics().contains(fooMetrics));
216         assertTrue("Not in com.foo", factory.createGroupMetrics("com.foo").getSubMetrics().contains(fooMetrics));
217
218         assertEquals("foo.size()", 1, factory.createGroupMetrics("foo").getSubMetrics().size());
219         assertEquals("com.foo.size()", 1, factory.createGroupMetrics("com.foo").getSubMetrics().size());
220
221         assertEquals("Number of all groups", 3, factory.getAllGroupMetrics().size());
222
223         assertTrue("Group foo missing", factory.getAllGroupNames().contains("foo"));
224         assertFalse("Group baz present", factory.getAllGroupNames().contains("baz"));
225         assertTrue("Group com.foo missing", factory.getAllGroupNames().contains("com.foo"));
226         assertTrue("Group com.baz missing", factory.getAllGroupNames().contains("com.baz"));
227
228         assertFalse("In com.baz", factory.createGroupMetrics("com.baz").getSubMetrics().contains(foobazMetrics));
229         
230         assertEquals("com.baz.size()", 0, factory.createGroupMetrics("com.baz").getSubMetrics().size());
231
232         assertEquals("Wrong parent", factory.createGroupMetrics("com.foo"), fooMetrics.getParent());
233         assertEquals("Wrong parent", factory.createGroupMetrics("com.baz"), foobazMetrics.getParent());
234     }
235 }
236
Popular Tags