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