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: import java.util.*;
039:
040: import org.xml.sax.*;
041:
042: import org.apache.log4j.*;
043:
044: import com.jeantessier.classreader.*;
045:
046: public class TestMetricsGathererDependenciesFilter extends TestCase {
047: public static final String TEST_DIRNAME = "classes"
048: + File.separator + "testpackage";
049: public static final String OTHER_DIRNAME = "classes"
050: + File.separator + "otherpackage";
051:
052: private MetricsFactory factory;
053: private ClassfileLoader loader;
054: private MetricsGatherer gatherer;
055:
056: protected void setUp() throws Exception {
057: Logger.getLogger(getClass()).debug(
058: "Starting " + getName() + " ...");
059:
060: factory = new MetricsFactory("test",
061: new MetricsConfigurationLoader(Boolean
062: .getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE"))
063: .load("etc" + File.separator
064: + "MetricsConfig.xml"));
065:
066: Collection<String> dirs = new ArrayList<String>();
067: dirs.add(TEST_DIRNAME);
068: dirs.add(OTHER_DIRNAME);
069: loader = new AggregatingClassfileLoader();
070: loader.load(dirs);
071:
072: gatherer = new MetricsGatherer("test", factory);
073: }
074:
075: protected void tearDown() throws Exception {
076: Logger.getLogger(getClass()).debug(
077: "Done with " + getName() + " ...");
078: }
079:
080: public void testpackage_TestClass_testMethod_withFilterForMethod() {
081: Collection filterIncludes = new HashSet();
082: filterIncludes.add("testpackage.TestClass.targetMethod()");
083:
084: gatherer.setFilterIncludes(filterIncludes);
085: gatherer.visitClassfiles(loader.getAllClassfiles());
086:
087: Collection dependencies;
088:
089: dependencies = ((CollectionMeasurement) factory
090: .createMethodMetrics(
091: "testpackage.TestClass.testMethod(java.lang.String)")
092: .getMeasurement(
093: Metrics.INBOUND_INTRA_CLASS_METHOD_DEPENDENCIES))
094: .getValues();
095: assertEquals(Metrics.INBOUND_INTRA_CLASS_METHOD_DEPENDENCIES
096: + " " + dependencies, 0, dependencies.size());
097:
098: dependencies = ((CollectionMeasurement) factory
099: .createMethodMetrics(
100: "testpackage.TestClass.testMethod(java.lang.String)")
101: .getMeasurement(
102: Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES))
103: .getValues();
104: assertEquals(Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES
105: + " " + dependencies, 0, dependencies.size());
106:
107: dependencies = ((CollectionMeasurement) factory
108: .createMethodMetrics(
109: "testpackage.TestClass.testMethod(java.lang.String)")
110: .getMeasurement(
111: Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES))
112: .getValues();
113: assertEquals(Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES
114: + " " + dependencies, 0, dependencies.size());
115:
116: dependencies = ((CollectionMeasurement) factory
117: .createMethodMetrics(
118: "testpackage.TestClass.testMethod(java.lang.String)")
119: .getMeasurement(
120: Metrics.OUTBOUND_INTRA_CLASS_FEATURE_DEPENDENCIES))
121: .getValues();
122: assertTrue(
123: Metrics.OUTBOUND_INTRA_CLASS_FEATURE_DEPENDENCIES
124: + " "
125: + dependencies
126: + " missing testpackage.TestClass.targetMethod()",
127: dependencies
128: .contains("testpackage.TestClass.targetMethod()"));
129: assertEquals(Metrics.OUTBOUND_INTRA_CLASS_FEATURE_DEPENDENCIES
130: + " " + dependencies, 1, dependencies.size());
131:
132: dependencies = ((CollectionMeasurement) factory
133: .createMethodMetrics(
134: "testpackage.TestClass.testMethod(java.lang.String)")
135: .getMeasurement(
136: Metrics.OUTBOUND_INTRA_PACKAGE_FEATURE_DEPENDENCIES))
137: .getValues();
138: assertEquals(
139: Metrics.OUTBOUND_INTRA_PACKAGE_FEATURE_DEPENDENCIES
140: + " " + dependencies, 0, dependencies.size());
141:
142: dependencies = ((CollectionMeasurement) factory
143: .createMethodMetrics(
144: "testpackage.TestClass.testMethod(java.lang.String)")
145: .getMeasurement(
146: Metrics.OUTBOUND_INTRA_PACKAGE_CLASS_DEPENDENCIES))
147: .getValues();
148: assertEquals(Metrics.OUTBOUND_INTRA_PACKAGE_CLASS_DEPENDENCIES
149: + " " + dependencies, 0, dependencies.size());
150:
151: dependencies = ((CollectionMeasurement) factory
152: .createMethodMetrics(
153: "testpackage.TestClass.testMethod(java.lang.String)")
154: .getMeasurement(
155: Metrics.OUTBOUND_EXTRA_PACKAGE_FEATURE_DEPENDENCIES))
156: .getValues();
157: assertEquals(
158: Metrics.OUTBOUND_EXTRA_PACKAGE_FEATURE_DEPENDENCIES
159: + " " + dependencies, 0, dependencies.size());
160:
161: dependencies = ((CollectionMeasurement) factory
162: .createMethodMetrics(
163: "testpackage.TestClass.testMethod(java.lang.String)")
164: .getMeasurement(
165: Metrics.OUTBOUND_EXTRA_PACKAGE_CLASS_DEPENDENCIES))
166: .getValues();
167: assertEquals(Metrics.OUTBOUND_EXTRA_PACKAGE_CLASS_DEPENDENCIES
168: + " " + dependencies, 0, dependencies.size());
169: }
170:
171: public void testpackage_TestClass_testMethod_withFilterForClass() {
172: Collection filterIncludes = new HashSet();
173: filterIncludes.add("testpackage.TargetClass");
174:
175: gatherer.setFilterIncludes(filterIncludes);
176: gatherer.visitClassfiles(loader.getAllClassfiles());
177:
178: Collection dependencies;
179:
180: dependencies = ((CollectionMeasurement) factory
181: .createMethodMetrics(
182: "testpackage.TestClass.testMethod(java.lang.String)")
183: .getMeasurement(
184: Metrics.INBOUND_INTRA_CLASS_METHOD_DEPENDENCIES))
185: .getValues();
186: assertEquals(Metrics.INBOUND_INTRA_CLASS_METHOD_DEPENDENCIES
187: + " " + dependencies, 0, dependencies.size());
188:
189: dependencies = ((CollectionMeasurement) factory
190: .createMethodMetrics(
191: "testpackage.TestClass.testMethod(java.lang.String)")
192: .getMeasurement(
193: Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES))
194: .getValues();
195: assertEquals(Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES
196: + " " + dependencies, 0, dependencies.size());
197:
198: dependencies = ((CollectionMeasurement) factory
199: .createMethodMetrics(
200: "testpackage.TestClass.testMethod(java.lang.String)")
201: .getMeasurement(
202: Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES))
203: .getValues();
204: assertEquals(Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES
205: + " " + dependencies, 0, dependencies.size());
206:
207: dependencies = ((CollectionMeasurement) factory
208: .createMethodMetrics(
209: "testpackage.TestClass.testMethod(java.lang.String)")
210: .getMeasurement(
211: Metrics.OUTBOUND_INTRA_CLASS_FEATURE_DEPENDENCIES))
212: .getValues();
213: assertEquals(Metrics.OUTBOUND_INTRA_CLASS_FEATURE_DEPENDENCIES
214: + " " + dependencies, 0, dependencies.size());
215:
216: dependencies = ((CollectionMeasurement) factory
217: .createMethodMetrics(
218: "testpackage.TestClass.testMethod(java.lang.String)")
219: .getMeasurement(
220: Metrics.OUTBOUND_INTRA_PACKAGE_FEATURE_DEPENDENCIES))
221: .getValues();
222: assertEquals(
223: Metrics.OUTBOUND_INTRA_PACKAGE_FEATURE_DEPENDENCIES
224: + " " + dependencies, 0, dependencies.size());
225:
226: dependencies = ((CollectionMeasurement) factory
227: .createMethodMetrics(
228: "testpackage.TestClass.testMethod(java.lang.String)")
229: .getMeasurement(
230: Metrics.OUTBOUND_INTRA_PACKAGE_CLASS_DEPENDENCIES))
231: .getValues();
232: assertTrue(Metrics.OUTBOUND_INTRA_PACKAGE_CLASS_DEPENDENCIES
233: + " " + dependencies
234: + " missing testpackage.TargetClass", dependencies
235: .contains("testpackage.TargetClass"));
236: assertEquals(Metrics.OUTBOUND_INTRA_PACKAGE_CLASS_DEPENDENCIES
237: + " " + dependencies, 1, dependencies.size());
238:
239: dependencies = ((CollectionMeasurement) factory
240: .createMethodMetrics(
241: "testpackage.TestClass.testMethod(java.lang.String)")
242: .getMeasurement(
243: Metrics.OUTBOUND_EXTRA_PACKAGE_FEATURE_DEPENDENCIES))
244: .getValues();
245: assertEquals(
246: Metrics.OUTBOUND_EXTRA_PACKAGE_FEATURE_DEPENDENCIES
247: + " " + dependencies, 0, dependencies.size());
248:
249: dependencies = ((CollectionMeasurement) factory
250: .createMethodMetrics(
251: "testpackage.TestClass.testMethod(java.lang.String)")
252: .getMeasurement(
253: Metrics.OUTBOUND_EXTRA_PACKAGE_CLASS_DEPENDENCIES))
254: .getValues();
255: assertEquals(Metrics.OUTBOUND_EXTRA_PACKAGE_CLASS_DEPENDENCIES
256: + " " + dependencies, 0, dependencies.size());
257: }
258:
259: public void testpackage_TestClass_testMethod_withFilterForPackage() {
260: Collection filterIncludes = new HashSet();
261: filterIncludes.add("java.lang");
262:
263: gatherer.setFilterIncludes(filterIncludes);
264: gatherer.visitClassfiles(loader.getAllClassfiles());
265:
266: Collection dependencies;
267:
268: dependencies = ((CollectionMeasurement) factory
269: .createMethodMetrics(
270: "testpackage.TestClass.testMethod(java.lang.String)")
271: .getMeasurement(
272: Metrics.INBOUND_INTRA_CLASS_METHOD_DEPENDENCIES))
273: .getValues();
274: assertEquals(Metrics.INBOUND_INTRA_CLASS_METHOD_DEPENDENCIES
275: + " " + dependencies, 0, dependencies.size());
276:
277: dependencies = ((CollectionMeasurement) factory
278: .createMethodMetrics(
279: "testpackage.TestClass.testMethod(java.lang.String)")
280: .getMeasurement(
281: Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES))
282: .getValues();
283: assertEquals(Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES
284: + " " + dependencies, 0, dependencies.size());
285:
286: dependencies = ((CollectionMeasurement) factory
287: .createMethodMetrics(
288: "testpackage.TestClass.testMethod(java.lang.String)")
289: .getMeasurement(
290: Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES))
291: .getValues();
292: assertEquals(Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES
293: + " " + dependencies, 0, dependencies.size());
294:
295: dependencies = ((CollectionMeasurement) factory
296: .createMethodMetrics(
297: "testpackage.TestClass.testMethod(java.lang.String)")
298: .getMeasurement(
299: Metrics.OUTBOUND_INTRA_CLASS_FEATURE_DEPENDENCIES))
300: .getValues();
301: assertEquals(Metrics.OUTBOUND_INTRA_CLASS_FEATURE_DEPENDENCIES
302: + " " + dependencies, 0, dependencies.size());
303:
304: dependencies = ((CollectionMeasurement) factory
305: .createMethodMetrics(
306: "testpackage.TestClass.testMethod(java.lang.String)")
307: .getMeasurement(
308: Metrics.OUTBOUND_INTRA_PACKAGE_FEATURE_DEPENDENCIES))
309: .getValues();
310: assertEquals(
311: Metrics.OUTBOUND_INTRA_PACKAGE_FEATURE_DEPENDENCIES
312: + " " + dependencies, 0, dependencies.size());
313:
314: dependencies = ((CollectionMeasurement) factory
315: .createMethodMetrics(
316: "testpackage.TestClass.testMethod(java.lang.String)")
317: .getMeasurement(
318: Metrics.OUTBOUND_INTRA_PACKAGE_CLASS_DEPENDENCIES))
319: .getValues();
320: assertEquals(Metrics.OUTBOUND_INTRA_PACKAGE_CLASS_DEPENDENCIES
321: + " " + dependencies, 0, dependencies.size());
322:
323: dependencies = ((CollectionMeasurement) factory
324: .createMethodMetrics(
325: "testpackage.TestClass.testMethod(java.lang.String)")
326: .getMeasurement(
327: Metrics.OUTBOUND_EXTRA_PACKAGE_FEATURE_DEPENDENCIES))
328: .getValues();
329: assertEquals(
330: Metrics.OUTBOUND_EXTRA_PACKAGE_FEATURE_DEPENDENCIES
331: + " " + dependencies, 0, dependencies.size());
332:
333: dependencies = ((CollectionMeasurement) factory
334: .createMethodMetrics(
335: "testpackage.TestClass.testMethod(java.lang.String)")
336: .getMeasurement(
337: Metrics.OUTBOUND_EXTRA_PACKAGE_CLASS_DEPENDENCIES))
338: .getValues();
339: assertEquals(Metrics.OUTBOUND_EXTRA_PACKAGE_CLASS_DEPENDENCIES
340: + " " + dependencies, 0, dependencies.size());
341: }
342:
343: public void testpackage_TestClass_withFilterForClass() {
344: Collection filterIncludes = new HashSet();
345: filterIncludes.add("testpackage.TargetInterface");
346:
347: gatherer.setFilterIncludes(filterIncludes);
348: gatherer.visitClassfiles(loader.getAllClassfiles());
349:
350: Collection dependencies;
351:
352: dependencies = ((CollectionMeasurement) factory
353: .createClassMetrics("testpackage.TestClass")
354: .getMeasurement(
355: Metrics.INBOUND_INTRA_PACKAGE_DEPENDENCIES))
356: .getValues();
357: assertEquals(Metrics.INBOUND_INTRA_PACKAGE_DEPENDENCIES + " "
358: + dependencies, 0, dependencies.size());
359:
360: dependencies = ((CollectionMeasurement) factory
361: .createClassMetrics("testpackage.TestClass")
362: .getMeasurement(
363: Metrics.INBOUND_EXTRA_PACKAGE_DEPENDENCIES))
364: .getValues();
365: assertEquals(Metrics.INBOUND_EXTRA_PACKAGE_DEPENDENCIES + " "
366: + dependencies, 0, dependencies.size());
367:
368: dependencies = ((CollectionMeasurement) factory
369: .createClassMetrics("testpackage.TestClass")
370: .getMeasurement(
371: Metrics.OUTBOUND_INTRA_PACKAGE_DEPENDENCIES))
372: .getValues();
373: assertTrue(
374: Metrics.OUTBOUND_INTRA_PACKAGE_DEPENDENCIES + " "
375: + dependencies
376: + " missing testpackage.TargetInterface",
377: dependencies.contains("testpackage.TargetInterface"));
378: assertEquals(Metrics.OUTBOUND_INTRA_PACKAGE_DEPENDENCIES + " "
379: + dependencies, 1, dependencies.size());
380:
381: dependencies = ((CollectionMeasurement) factory
382: .createClassMetrics("testpackage.TestClass")
383: .getMeasurement(
384: Metrics.OUTBOUND_EXTRA_PACKAGE_DEPENDENCIES))
385: .getValues();
386: assertEquals(Metrics.OUTBOUND_EXTRA_PACKAGE_DEPENDENCIES + " "
387: + dependencies, 0, dependencies.size());
388:
389: dependencies = ((CollectionMeasurement) factory
390: .createClassMetrics("testpackage.TestClass")
391: .getMeasurement(
392: Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES))
393: .getValues();
394: assertEquals(Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES
395: + " " + dependencies, 0, dependencies.size());
396:
397: dependencies = ((CollectionMeasurement) factory
398: .createClassMetrics("testpackage.TestClass")
399: .getMeasurement(
400: Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES))
401: .getValues();
402: assertEquals(Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES
403: + " " + dependencies, 0, dependencies.size());
404: }
405:
406: public void testpackage_TestClass_withFilterForPackage() {
407: Collection filterIncludes = new HashSet();
408: filterIncludes.add("java.lang");
409:
410: gatherer.setFilterIncludes(filterIncludes);
411: gatherer.visitClassfiles(loader.getAllClassfiles());
412:
413: Collection dependencies;
414:
415: dependencies = ((CollectionMeasurement) factory
416: .createClassMetrics("testpackage.TestClass")
417: .getMeasurement(
418: Metrics.INBOUND_INTRA_PACKAGE_DEPENDENCIES))
419: .getValues();
420: assertEquals(Metrics.INBOUND_INTRA_PACKAGE_DEPENDENCIES + " "
421: + dependencies, 0, dependencies.size());
422:
423: dependencies = ((CollectionMeasurement) factory
424: .createClassMetrics("testpackage.TestClass")
425: .getMeasurement(
426: Metrics.INBOUND_EXTRA_PACKAGE_DEPENDENCIES))
427: .getValues();
428: assertEquals(Metrics.INBOUND_EXTRA_PACKAGE_DEPENDENCIES + " "
429: + dependencies, 0, dependencies.size());
430:
431: dependencies = ((CollectionMeasurement) factory
432: .createClassMetrics("testpackage.TestClass")
433: .getMeasurement(
434: Metrics.OUTBOUND_INTRA_PACKAGE_DEPENDENCIES))
435: .getValues();
436: assertEquals(Metrics.OUTBOUND_INTRA_PACKAGE_DEPENDENCIES + " "
437: + dependencies, 0, dependencies.size());
438:
439: dependencies = ((CollectionMeasurement) factory
440: .createClassMetrics("testpackage.TestClass")
441: .getMeasurement(
442: Metrics.OUTBOUND_EXTRA_PACKAGE_DEPENDENCIES))
443: .getValues();
444: assertEquals(Metrics.OUTBOUND_EXTRA_PACKAGE_DEPENDENCIES + " "
445: + dependencies, 0, dependencies.size());
446:
447: dependencies = ((CollectionMeasurement) factory
448: .createClassMetrics("testpackage.TestClass")
449: .getMeasurement(
450: Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES))
451: .getValues();
452: assertEquals(Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES
453: + " " + dependencies, 0, dependencies.size());
454:
455: dependencies = ((CollectionMeasurement) factory
456: .createClassMetrics("testpackage.TestClass")
457: .getMeasurement(
458: Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES))
459: .getValues();
460: assertEquals(Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES
461: + " " + dependencies, 0, dependencies.size());
462: }
463: }
|