001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.metrics;
034:
035: import junit.framework.*;
036:
037: import java.io.*;
038:
039: public class TestMetricsFactory extends TestCase {
040: MetricsConfiguration configuration;
041: MetricsFactory factory;
042:
043: protected void setUp() throws Exception {
044: configuration = new MetricsConfigurationLoader(Boolean
045: .getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE"))
046: .load("etc" + File.separator + "MetricsConfig.xml");
047: factory = new MetricsFactory("test", configuration);
048: }
049:
050: public void testCreateProjectMetrics() {
051: Metrics m1 = factory.createProjectMetrics("foo");
052: assertNotNull(m1);
053: assertEquals("New metrics name", "foo", m1.getName());
054:
055: Metrics m2 = factory.createProjectMetrics("foo");
056: assertSame(m1, m2);
057: assertEquals("project measurements", configuration
058: .getProjectMeasurements().size(), m1
059: .getMeasurementNames().size());
060: }
061:
062: public void testIncludeProjectMetrics() {
063: Metrics m1 = factory.createProjectMetrics("foo");
064:
065: assertFalse("ProjectMetrics() contains external metrics",
066: factory.getProjectMetrics().contains(m1));
067:
068: factory.includeProjectMetrics(m1);
069:
070: assertTrue(
071: "ProjectMetrics() does not contain internal metrics",
072: factory.getProjectMetrics().contains(m1));
073: }
074:
075: public void testCreateGroupMetrics() {
076: Metrics m1 = factory.createGroupMetrics("foo");
077: assertNotNull(m1);
078: assertEquals("New metrics name", "foo", m1.getName());
079:
080: Metrics m2 = factory.createGroupMetrics("foo");
081: assertSame(m1, m2);
082: assertEquals("group measurements", configuration
083: .getGroupMeasurements().size(), m1
084: .getMeasurementNames().size());
085: }
086:
087: public void testIncludeGroupMetrics() {
088: Metrics m1 = factory.createGroupMetrics("foo");
089:
090: assertFalse("GroupMetrics() contains external metrics", factory
091: .getGroupMetrics().contains(m1));
092:
093: factory.includeGroupMetrics(m1);
094:
095: assertTrue("GroupMetrics() does not contain internal metrics",
096: factory.getGroupMetrics().contains(m1));
097: }
098:
099: public void testCreateClassMetrics() {
100: Metrics m1 = factory.createClassMetrics("foo");
101: assertNotNull(m1);
102: assertEquals("New metrics name", "foo", m1.getName());
103:
104: Metrics m2 = factory.createClassMetrics("foo");
105: assertSame(m1, m2);
106: assertEquals("class measurements", configuration
107: .getClassMeasurements().size(), m1
108: .getMeasurementNames().size());
109: }
110:
111: public void testIncludeClassMetrics() {
112: Metrics m1 = factory.createClassMetrics("foo");
113:
114: assertFalse("ClassMetrics() contains external metrics", factory
115: .getClassMetrics().contains(m1));
116:
117: factory.includeClassMetrics(m1);
118:
119: assertTrue("ClassMetrics() does not contain internal metrics",
120: factory.getClassMetrics().contains(m1));
121: }
122:
123: public void testCreateMethodMetrics() {
124: Metrics m1 = factory.createMethodMetrics("foo");
125: assertNotNull(m1);
126: assertEquals("New metrics name", "foo", m1.getName());
127:
128: Metrics m2 = factory.createMethodMetrics("foo");
129: assertSame(m1, m2);
130: assertEquals("method measurements", configuration
131: .getMethodMeasurements().size(), m1
132: .getMeasurementNames().size());
133: }
134:
135: public void testIncludeMethodMetrics() {
136: Metrics m1 = factory.createMethodMetrics("foo");
137:
138: assertFalse("MethodMetrics() contains external metrics",
139: factory.getMethodMetrics().contains(m1));
140:
141: factory.includeMethodMetrics(m1);
142:
143: assertTrue("MethodMetrics() does not contain internal metrics",
144: factory.getMethodMetrics().contains(m1));
145: }
146:
147: public void testCreateStaticInitializerMetrics() {
148: Metrics m = factory.createMethodMetrics("foo.static {}");
149:
150: assertEquals("class name", "foo", m.getParent().getName());
151: }
152:
153: public void testCreateStructure() {
154: Metrics methodMetrics = factory.createMethodMetrics("a.A.a()");
155: Metrics classMetrics = factory.createClassMetrics("a.A");
156: Metrics packageMetrics = factory.createGroupMetrics("a");
157: Metrics projectMetrics = factory.createProjectMetrics();
158:
159: factory.includeMethodMetrics(methodMetrics);
160:
161: assertTrue(projectMetrics.getSubMetrics().contains(
162: packageMetrics));
163: assertTrue(packageMetrics.getSubMetrics()
164: .contains(classMetrics));
165: assertTrue(classMetrics.getSubMetrics().contains(methodMetrics));
166: }
167:
168: public void testGroupDefinitionsWithInternal() {
169: configuration.addGroupDefinition("foo", "/foo/");
170: configuration.addGroupDefinition("bar", "/bar/");
171: configuration.addGroupDefinition("baz", "/baz/");
172:
173: Metrics metrics = factory
174: .createClassMetrics("com.foobar.Foobar");
175: factory.includeClassMetrics(metrics);
176:
177: assertEquals("Number of groups", 3, factory.getGroupMetrics()
178: .size());
179: assertEquals("Number of all groups", 3, factory
180: .getAllGroupMetrics().size());
181:
182: assertTrue("Group foo missing", factory.getAllGroupNames()
183: .contains("foo"));
184: assertTrue("Group bar missing", factory.getAllGroupNames()
185: .contains("bar"));
186: assertFalse("Group baz present", factory.getAllGroupNames()
187: .contains("baz"));
188: assertTrue("Group com.foobar missing", factory
189: .getAllGroupNames().contains("com.foobar"));
190:
191: assertTrue("Group foo missing", factory.getGroupNames()
192: .contains("foo"));
193: assertTrue("Group bar missing", factory.getGroupNames()
194: .contains("bar"));
195: assertFalse("Group baz present", factory.getGroupNames()
196: .contains("baz"));
197: assertTrue("Group com.foobar missing", factory.getGroupNames()
198: .contains("com.foobar"));
199:
200: assertTrue("Not in foo", factory.createGroupMetrics("foo")
201: .getSubMetrics().contains(metrics));
202: assertTrue("Not in bar", factory.createGroupMetrics("bar")
203: .getSubMetrics().contains(metrics));
204: assertFalse("In baz", factory.createGroupMetrics("baz")
205: .getSubMetrics().contains(metrics));
206: assertTrue("Not in com.foobar", factory.createGroupMetrics(
207: "com.foobar").getSubMetrics().contains(metrics));
208:
209: assertEquals("Wrong parent", factory
210: .createGroupMetrics("com.foobar"), metrics.getParent());
211: }
212:
213: public void testGroupDefinitionsWithExternal() {
214: configuration.addGroupDefinition("foo", "/foo/");
215: configuration.addGroupDefinition("bar", "/bar/");
216: configuration.addGroupDefinition("baz", "/baz/");
217:
218: Metrics metrics = factory
219: .createClassMetrics("com.foobar.Foobar");
220:
221: assertEquals("Number of groups", 0, factory.getGroupMetrics()
222: .size());
223: assertEquals("Number of all groups", 1, factory
224: .getAllGroupMetrics().size());
225:
226: assertFalse("Group foo present", factory.getAllGroupNames()
227: .contains("foo"));
228: assertFalse("Group bar present", factory.getAllGroupNames()
229: .contains("bar"));
230: assertFalse("Group baz present", factory.getAllGroupNames()
231: .contains("baz"));
232: assertTrue("Group com.foobar missing", factory
233: .getAllGroupNames().contains("com.foobar"));
234:
235: assertFalse("In foo", factory.createGroupMetrics("foo")
236: .getSubMetrics().contains(metrics));
237: assertFalse("In bar", factory.createGroupMetrics("bar")
238: .getSubMetrics().contains(metrics));
239: assertFalse("In baz", factory.createGroupMetrics("baz")
240: .getSubMetrics().contains(metrics));
241: assertFalse("In com.foobar", factory.createGroupMetrics(
242: "com.foobar").getSubMetrics().contains(metrics));
243:
244: assertEquals("Wrong parent", factory
245: .createGroupMetrics("com.foobar"), metrics.getParent());
246: }
247:
248: public void testGroupDefinitionsWithBoth() {
249: configuration.addGroupDefinition("foo", "/foo/");
250: configuration.addGroupDefinition("baz", "/baz/");
251:
252: Metrics fooMetrics = factory.createClassMetrics("com.foo.Foo");
253: Metrics foobazMetrics = factory
254: .createClassMetrics("com.baz.Foobaz");
255:
256: factory.includeClassMetrics(fooMetrics);
257:
258: assertEquals("Number of groups", 2, factory.getGroupMetrics()
259: .size());
260:
261: assertTrue("Group foo missing", factory.getGroupNames()
262: .contains("foo"));
263: assertFalse("Group baz present", factory.getGroupNames()
264: .contains("baz"));
265: assertTrue("Group com.foo missing", factory.getGroupNames()
266: .contains("com.foo"));
267: assertFalse("Group com.baz missing", factory.getGroupNames()
268: .contains("com.baz"));
269:
270: assertTrue("Not in foo", factory.createGroupMetrics("foo")
271: .getSubMetrics().contains(fooMetrics));
272: assertTrue("Not in com.foo", factory.createGroupMetrics(
273: "com.foo").getSubMetrics().contains(fooMetrics));
274:
275: assertEquals("foo.size()", 1, factory.createGroupMetrics("foo")
276: .getSubMetrics().size());
277: assertEquals("com.foo.size()", 1, factory.createGroupMetrics(
278: "com.foo").getSubMetrics().size());
279:
280: assertEquals("Number of all groups", 3, factory
281: .getAllGroupMetrics().size());
282:
283: assertTrue("Group foo missing", factory.getAllGroupNames()
284: .contains("foo"));
285: assertFalse("Group baz present", factory.getAllGroupNames()
286: .contains("baz"));
287: assertTrue("Group com.foo missing", factory.getAllGroupNames()
288: .contains("com.foo"));
289: assertTrue("Group com.baz missing", factory.getAllGroupNames()
290: .contains("com.baz"));
291:
292: assertFalse("In com.baz", factory.createGroupMetrics("com.baz")
293: .getSubMetrics().contains(foobazMetrics));
294:
295: assertEquals("com.baz.size()", 0, factory.createGroupMetrics(
296: "com.baz").getSubMetrics().size());
297:
298: assertEquals("Wrong parent", factory
299: .createGroupMetrics("com.foo"), fooMetrics.getParent());
300: assertEquals("Wrong parent", factory
301: .createGroupMetrics("com.baz"), foobazMetrics
302: .getParent());
303: }
304: }
|