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.dependency;
034:
035: import java.util.*;
036:
037: import junit.framework.*;
038:
039: import org.apache.log4j.*;
040:
041: public class TestTransitiveClosureNonMaximized extends TestCase {
042: private NodeFactory factory;
043:
044: private FeatureNode in2;
045: private FeatureNode in1;
046: private FeatureNode base;
047: private FeatureNode out1;
048: private FeatureNode out2;
049:
050: private RegularExpressionSelectionCriteria startCriteria;
051: private RegularExpressionSelectionCriteria stopCriteria;
052:
053: private NodeFactory resultFactory;
054:
055: protected void setUp() {
056: factory = new NodeFactory();
057:
058: in2 = factory.createFeature("in2.In2.In2()");
059: in1 = factory.createFeature("in1.In1.In1()");
060: base = factory.createFeature("base.Base.Base()");
061: out1 = factory.createFeature("out1.Out1.Out1()");
062: out2 = factory.createFeature("out2.Out2.Out2()");
063:
064: in2.addDependency(in1);
065: in1.addDependency(base);
066: base.addDependency(out1);
067: out1.addDependency(out2);
068:
069: List scopeIncludes = new ArrayList(1);
070: scopeIncludes.add("/^base/");
071: List filderIncludes = Collections.EMPTY_LIST;
072:
073: startCriteria = new RegularExpressionSelectionCriteria();
074: startCriteria.setMatchingPackages(false);
075: startCriteria.setMatchingClasses(false);
076: startCriteria.setMatchingFeatures(false);
077: startCriteria.setGlobalIncludes(scopeIncludes);
078:
079: stopCriteria = new RegularExpressionSelectionCriteria();
080: stopCriteria.setMatchingPackages(false);
081: stopCriteria.setMatchingClasses(false);
082: stopCriteria.setMatchingFeatures(false);
083: stopCriteria.setGlobalIncludes(filderIncludes);
084: }
085:
086: public void testFeatureToFeatureFromFeature() {
087: startCriteria.setMatchingFeatures(true);
088: stopCriteria.setMatchingFeatures(true);
089:
090: Logger.getLogger(getClass()).info(
091: "Start f2f test from feature ...");
092: compute(Collections.singleton(base));
093: Logger.getLogger(getClass()).info(
094: "Stop f2f test from feature ...");
095:
096: assertEquals(5, resultFactory.getFeatures().size());
097: assertTrue(resultFactory.getFeatures().values().contains(in2));
098: assertTrue(resultFactory.getFeatures().values().contains(in1));
099: assertTrue(resultFactory.getFeatures().values().contains(base));
100: assertTrue(resultFactory.getFeatures().values().contains(out1));
101: assertTrue(resultFactory.getFeatures().values().contains(out2));
102:
103: assertEquals(0, resultFactory.createFeature("in2.In2.In2()")
104: .getInboundDependencies().size());
105: assertEquals(1, resultFactory.createFeature("in2.In2.In2()")
106: .getOutboundDependencies().size());
107: assertEquals(1, resultFactory.createFeature("in1.In1.In1()")
108: .getInboundDependencies().size());
109: assertEquals(1, resultFactory.createFeature("in1.In1.In1()")
110: .getOutboundDependencies().size());
111: assertEquals(1, resultFactory.createFeature("base.Base.Base()")
112: .getInboundDependencies().size());
113: assertEquals(1, resultFactory.createFeature("base.Base.Base()")
114: .getOutboundDependencies().size());
115: assertEquals(1, resultFactory.createFeature("out1.Out1.Out1()")
116: .getInboundDependencies().size());
117: assertEquals(1, resultFactory.createFeature("out1.Out1.Out1()")
118: .getOutboundDependencies().size());
119: assertEquals(1, resultFactory.createFeature("out2.Out2.Out2()")
120: .getInboundDependencies().size());
121: assertEquals(0, resultFactory.createFeature("out2.Out2.Out2()")
122: .getOutboundDependencies().size());
123:
124: assertEquals(0, resultFactory.createClass("in2.In2")
125: .getInboundDependencies().size());
126: assertEquals(0, resultFactory.createClass("in2.In2")
127: .getOutboundDependencies().size());
128: assertEquals(0, resultFactory.createClass("in1.In1")
129: .getInboundDependencies().size());
130: assertEquals(0, resultFactory.createClass("in1.In1")
131: .getOutboundDependencies().size());
132: assertEquals(0, resultFactory.createClass("base.Base")
133: .getInboundDependencies().size());
134: assertEquals(0, resultFactory.createClass("base.Base")
135: .getOutboundDependencies().size());
136: assertEquals(0, resultFactory.createClass("out1.Out1")
137: .getInboundDependencies().size());
138: assertEquals(0, resultFactory.createClass("out1.Out1")
139: .getOutboundDependencies().size());
140: assertEquals(0, resultFactory.createClass("out2.Out2")
141: .getInboundDependencies().size());
142: assertEquals(0, resultFactory.createClass("out2.Out2")
143: .getOutboundDependencies().size());
144:
145: assertEquals(0, resultFactory.createPackage("in2")
146: .getInboundDependencies().size());
147: assertEquals(0, resultFactory.createPackage("in2")
148: .getOutboundDependencies().size());
149: assertEquals(0, resultFactory.createPackage("in1")
150: .getInboundDependencies().size());
151: assertEquals(0, resultFactory.createPackage("in1")
152: .getOutboundDependencies().size());
153: assertEquals(0, resultFactory.createPackage("base")
154: .getInboundDependencies().size());
155: assertEquals(0, resultFactory.createPackage("base")
156: .getOutboundDependencies().size());
157: assertEquals(0, resultFactory.createPackage("out1")
158: .getInboundDependencies().size());
159: assertEquals(0, resultFactory.createPackage("out1")
160: .getOutboundDependencies().size());
161: assertEquals(0, resultFactory.createPackage("out2")
162: .getInboundDependencies().size());
163: assertEquals(0, resultFactory.createPackage("out2")
164: .getOutboundDependencies().size());
165: }
166:
167: public void testFeatureToFeatureFromPackages() {
168: startCriteria.setMatchingFeatures(true);
169: stopCriteria.setMatchingFeatures(true);
170:
171: Logger.getLogger(getClass()).info(
172: "Start f2f test from package list ...");
173: compute(factory.getPackages().values());
174: Logger.getLogger(getClass()).info(
175: "Stop f2f test from package list ...");
176:
177: assertEquals(5, resultFactory.getFeatures().size());
178: assertTrue(resultFactory.getFeatures().values().contains(in2));
179: assertTrue(resultFactory.getFeatures().values().contains(in1));
180: assertTrue(resultFactory.getFeatures().values().contains(base));
181: assertTrue(resultFactory.getFeatures().values().contains(out1));
182: assertTrue(resultFactory.getFeatures().values().contains(out2));
183:
184: assertEquals(0, resultFactory.createFeature("in2.In2.In2()")
185: .getInboundDependencies().size());
186: assertEquals(1, resultFactory.createFeature("in2.In2.In2()")
187: .getOutboundDependencies().size());
188: assertEquals(1, resultFactory.createFeature("in1.In1.In1()")
189: .getInboundDependencies().size());
190: assertEquals(1, resultFactory.createFeature("in1.In1.In1()")
191: .getOutboundDependencies().size());
192: assertEquals(1, resultFactory.createFeature("base.Base.Base()")
193: .getInboundDependencies().size());
194: assertEquals(1, resultFactory.createFeature("base.Base.Base()")
195: .getOutboundDependencies().size());
196: assertEquals(1, resultFactory.createFeature("out1.Out1.Out1()")
197: .getInboundDependencies().size());
198: assertEquals(1, resultFactory.createFeature("out1.Out1.Out1()")
199: .getOutboundDependencies().size());
200: assertEquals(1, resultFactory.createFeature("out2.Out2.Out2()")
201: .getInboundDependencies().size());
202: assertEquals(0, resultFactory.createFeature("out2.Out2.Out2()")
203: .getOutboundDependencies().size());
204:
205: assertEquals(0, resultFactory.createClass("in2.In2")
206: .getInboundDependencies().size());
207: assertEquals(0, resultFactory.createClass("in2.In2")
208: .getOutboundDependencies().size());
209: assertEquals(0, resultFactory.createClass("in1.In1")
210: .getInboundDependencies().size());
211: assertEquals(0, resultFactory.createClass("in1.In1")
212: .getOutboundDependencies().size());
213: assertEquals(0, resultFactory.createClass("base.Base")
214: .getInboundDependencies().size());
215: assertEquals(0, resultFactory.createClass("base.Base")
216: .getOutboundDependencies().size());
217: assertEquals(0, resultFactory.createClass("out1.Out1")
218: .getInboundDependencies().size());
219: assertEquals(0, resultFactory.createClass("out1.Out1")
220: .getOutboundDependencies().size());
221: assertEquals(0, resultFactory.createClass("out2.Out2")
222: .getInboundDependencies().size());
223: assertEquals(0, resultFactory.createClass("out2.Out2")
224: .getOutboundDependencies().size());
225:
226: assertEquals(0, resultFactory.createPackage("in2")
227: .getInboundDependencies().size());
228: assertEquals(0, resultFactory.createPackage("in2")
229: .getOutboundDependencies().size());
230: assertEquals(0, resultFactory.createPackage("in1")
231: .getInboundDependencies().size());
232: assertEquals(0, resultFactory.createPackage("in1")
233: .getOutboundDependencies().size());
234: assertEquals(0, resultFactory.createPackage("base")
235: .getInboundDependencies().size());
236: assertEquals(0, resultFactory.createPackage("base")
237: .getOutboundDependencies().size());
238: assertEquals(0, resultFactory.createPackage("out1")
239: .getInboundDependencies().size());
240: assertEquals(0, resultFactory.createPackage("out1")
241: .getOutboundDependencies().size());
242: assertEquals(0, resultFactory.createPackage("out2")
243: .getInboundDependencies().size());
244: assertEquals(0, resultFactory.createPackage("out2")
245: .getOutboundDependencies().size());
246: }
247:
248: public void testClassToClassFromClass() {
249: startCriteria.setMatchingClasses(true);
250: stopCriteria.setMatchingClasses(true);
251:
252: Logger.getLogger(getClass()).info(
253: "Start c2c test from class ...");
254: compute(Collections.singleton(base.getClassNode()));
255: Logger.getLogger(getClass()).info(
256: "Stop c2c test from class ...");
257:
258: assertEquals(0, resultFactory.getFeatures().size());
259:
260: assertEquals(5, resultFactory.getClasses().size());
261: assertTrue(resultFactory.getClasses().values().contains(
262: in2.getClassNode()));
263: assertTrue(resultFactory.getClasses().values().contains(
264: in1.getClassNode()));
265: assertTrue(resultFactory.getClasses().values().contains(
266: base.getClassNode()));
267: assertTrue(resultFactory.getClasses().values().contains(
268: out1.getClassNode()));
269: assertTrue(resultFactory.getClasses().values().contains(
270: out2.getClassNode()));
271:
272: assertEquals(0, resultFactory.createClass("in2.In2")
273: .getInboundDependencies().size());
274: assertEquals(1, resultFactory.createClass("in2.In2")
275: .getOutboundDependencies().size());
276: assertEquals(1, resultFactory.createClass("in1.In1")
277: .getInboundDependencies().size());
278: assertEquals(1, resultFactory.createClass("in1.In1")
279: .getOutboundDependencies().size());
280: assertEquals(1, resultFactory.createClass("base.Base")
281: .getInboundDependencies().size());
282: assertEquals(1, resultFactory.createClass("base.Base")
283: .getOutboundDependencies().size());
284: assertEquals(1, resultFactory.createClass("out1.Out1")
285: .getInboundDependencies().size());
286: assertEquals(1, resultFactory.createClass("out1.Out1")
287: .getOutboundDependencies().size());
288: assertEquals(1, resultFactory.createClass("out2.Out2")
289: .getInboundDependencies().size());
290: assertEquals(0, resultFactory.createClass("out2.Out2")
291: .getOutboundDependencies().size());
292:
293: assertEquals(0, resultFactory.createPackage("in2")
294: .getInboundDependencies().size());
295: assertEquals(0, resultFactory.createPackage("in2")
296: .getOutboundDependencies().size());
297: assertEquals(0, resultFactory.createPackage("in1")
298: .getInboundDependencies().size());
299: assertEquals(0, resultFactory.createPackage("in1")
300: .getOutboundDependencies().size());
301: assertEquals(0, resultFactory.createPackage("base")
302: .getInboundDependencies().size());
303: assertEquals(0, resultFactory.createPackage("base")
304: .getOutboundDependencies().size());
305: assertEquals(0, resultFactory.createPackage("out1")
306: .getInboundDependencies().size());
307: assertEquals(0, resultFactory.createPackage("out1")
308: .getOutboundDependencies().size());
309: assertEquals(0, resultFactory.createPackage("out2")
310: .getInboundDependencies().size());
311: assertEquals(0, resultFactory.createPackage("out2")
312: .getOutboundDependencies().size());
313: }
314:
315: public void testClassToClassFromPackageList() {
316: startCriteria.setMatchingClasses(true);
317: stopCriteria.setMatchingClasses(true);
318:
319: Logger.getLogger(getClass()).info(
320: "Start c2c test from package list ...");
321: compute(factory.getPackages().values());
322: Logger.getLogger(getClass()).info(
323: "Stop c2c test from package list ...");
324:
325: assertEquals(0, resultFactory.getFeatures().size());
326:
327: assertEquals(5, resultFactory.getClasses().size());
328: assertTrue(resultFactory.getClasses().values().contains(
329: in2.getClassNode()));
330: assertTrue(resultFactory.getClasses().values().contains(
331: in1.getClassNode()));
332: assertTrue(resultFactory.getClasses().values().contains(
333: base.getClassNode()));
334: assertTrue(resultFactory.getClasses().values().contains(
335: out1.getClassNode()));
336: assertTrue(resultFactory.getClasses().values().contains(
337: out2.getClassNode()));
338:
339: assertEquals(0, resultFactory.createClass("in2.In2")
340: .getInboundDependencies().size());
341: assertEquals(1, resultFactory.createClass("in2.In2")
342: .getOutboundDependencies().size());
343: assertEquals(1, resultFactory.createClass("in1.In1")
344: .getInboundDependencies().size());
345: assertEquals(1, resultFactory.createClass("in1.In1")
346: .getOutboundDependencies().size());
347: assertEquals(1, resultFactory.createClass("base.Base")
348: .getInboundDependencies().size());
349: assertEquals(1, resultFactory.createClass("base.Base")
350: .getOutboundDependencies().size());
351: assertEquals(1, resultFactory.createClass("out1.Out1")
352: .getInboundDependencies().size());
353: assertEquals(1, resultFactory.createClass("out1.Out1")
354: .getOutboundDependencies().size());
355: assertEquals(1, resultFactory.createClass("out2.Out2")
356: .getInboundDependencies().size());
357: assertEquals(0, resultFactory.createClass("out2.Out2")
358: .getOutboundDependencies().size());
359:
360: assertEquals(0, resultFactory.createPackage("in2")
361: .getInboundDependencies().size());
362: assertEquals(0, resultFactory.createPackage("in2")
363: .getOutboundDependencies().size());
364: assertEquals(0, resultFactory.createPackage("in1")
365: .getInboundDependencies().size());
366: assertEquals(0, resultFactory.createPackage("in1")
367: .getOutboundDependencies().size());
368: assertEquals(0, resultFactory.createPackage("base")
369: .getInboundDependencies().size());
370: assertEquals(0, resultFactory.createPackage("base")
371: .getOutboundDependencies().size());
372: assertEquals(0, resultFactory.createPackage("out1")
373: .getInboundDependencies().size());
374: assertEquals(0, resultFactory.createPackage("out1")
375: .getOutboundDependencies().size());
376: assertEquals(0, resultFactory.createPackage("out2")
377: .getInboundDependencies().size());
378: assertEquals(0, resultFactory.createPackage("out2")
379: .getOutboundDependencies().size());
380: }
381:
382: public void testPackageToPackageFromPackage() {
383: startCriteria.setMatchingPackages(true);
384: stopCriteria.setMatchingPackages(true);
385:
386: Logger.getLogger(getClass()).info(
387: "Start p2p test from package ...");
388: compute(Collections.singleton(base.getClassNode()
389: .getPackageNode()));
390: Logger.getLogger(getClass()).info(
391: "Stop p2p test from package ...");
392:
393: assertEquals(0, resultFactory.getFeatures().size());
394:
395: assertEquals(0, resultFactory.getClasses().size());
396:
397: assertEquals(5, resultFactory.getPackages().size());
398: assertTrue(resultFactory.getPackages().values().contains(
399: in2.getClassNode().getPackageNode()));
400: assertTrue(resultFactory.getPackages().values().contains(
401: in1.getClassNode().getPackageNode()));
402: assertTrue(resultFactory.getPackages().values().contains(
403: base.getClassNode().getPackageNode()));
404: assertTrue(resultFactory.getPackages().values().contains(
405: out1.getClassNode().getPackageNode()));
406: assertTrue(resultFactory.getPackages().values().contains(
407: out2.getClassNode().getPackageNode()));
408:
409: assertEquals(0, resultFactory.createPackage("in2")
410: .getInboundDependencies().size());
411: assertEquals(1, resultFactory.createPackage("in2")
412: .getOutboundDependencies().size());
413: assertEquals(1, resultFactory.createPackage("in1")
414: .getInboundDependencies().size());
415: assertEquals(1, resultFactory.createPackage("in1")
416: .getOutboundDependencies().size());
417: assertEquals(1, resultFactory.createPackage("base")
418: .getInboundDependencies().size());
419: assertEquals(1, resultFactory.createPackage("base")
420: .getOutboundDependencies().size());
421: assertEquals(1, resultFactory.createPackage("out1")
422: .getInboundDependencies().size());
423: assertEquals(1, resultFactory.createPackage("out1")
424: .getOutboundDependencies().size());
425: assertEquals(1, resultFactory.createPackage("out2")
426: .getInboundDependencies().size());
427: assertEquals(0, resultFactory.createPackage("out2")
428: .getOutboundDependencies().size());
429: }
430:
431: public void testPackageToPackageFromPackageList() {
432: startCriteria.setMatchingPackages(true);
433: stopCriteria.setMatchingPackages(true);
434:
435: Logger.getLogger(getClass()).info(
436: "Start p2p test from package list ...");
437: compute(factory.getPackages().values());
438: Logger.getLogger(getClass()).info(
439: "Stop p2p test from package list ...");
440:
441: assertEquals(0, resultFactory.getFeatures().size());
442:
443: assertEquals(0, resultFactory.getClasses().size());
444:
445: assertEquals(5, resultFactory.getPackages().size());
446: assertTrue(resultFactory.getPackages().values().contains(
447: in2.getClassNode().getPackageNode()));
448: assertTrue(resultFactory.getPackages().values().contains(
449: in1.getClassNode().getPackageNode()));
450: assertTrue(resultFactory.getPackages().values().contains(
451: base.getClassNode().getPackageNode()));
452: assertTrue(resultFactory.getPackages().values().contains(
453: out1.getClassNode().getPackageNode()));
454: assertTrue(resultFactory.getPackages().values().contains(
455: out2.getClassNode().getPackageNode()));
456:
457: assertEquals(0, resultFactory.createPackage("in2")
458: .getInboundDependencies().size());
459: assertEquals(1, resultFactory.createPackage("in2")
460: .getOutboundDependencies().size());
461: assertEquals(1, resultFactory.createPackage("in1")
462: .getInboundDependencies().size());
463: assertEquals(1, resultFactory.createPackage("in1")
464: .getOutboundDependencies().size());
465: assertEquals(1, resultFactory.createPackage("base")
466: .getInboundDependencies().size());
467: assertEquals(1, resultFactory.createPackage("base")
468: .getOutboundDependencies().size());
469: assertEquals(1, resultFactory.createPackage("out1")
470: .getInboundDependencies().size());
471: assertEquals(1, resultFactory.createPackage("out1")
472: .getOutboundDependencies().size());
473: assertEquals(1, resultFactory.createPackage("out2")
474: .getInboundDependencies().size());
475: assertEquals(0, resultFactory.createPackage("out2")
476: .getOutboundDependencies().size());
477: }
478:
479: private void compute(Collection nodes) {
480: RegularExpressionSelectionCriteria localStartCriteria = new RegularExpressionSelectionCriteria();
481: localStartCriteria.setGlobalIncludes(startCriteria
482: .getGlobalIncludes());
483: RegularExpressionSelectionCriteria localStopCriteria = new RegularExpressionSelectionCriteria();
484: localStopCriteria.setGlobalIncludes(stopCriteria
485: .getGlobalIncludes());
486:
487: TransitiveClosure closure = new TransitiveClosure(
488: localStartCriteria, localStopCriteria);
489: closure
490: .setMaximumInboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
491: closure
492: .setMaximumOutboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
493: closure.traverseNodes(nodes);
494:
495: RegularExpressionSelectionCriteria localScopeCriteria = new RegularExpressionSelectionCriteria();
496: localScopeCriteria.setMatchingPackages(startCriteria
497: .isMatchingPackages());
498: localScopeCriteria.setMatchingClasses(startCriteria
499: .isMatchingClasses());
500: localScopeCriteria.setMatchingFeatures(startCriteria
501: .isMatchingFeatures());
502: localScopeCriteria.setGlobalIncludes("//");
503: RegularExpressionSelectionCriteria localFilterCriteria = new RegularExpressionSelectionCriteria();
504: localFilterCriteria.setMatchingPackages(stopCriteria
505: .isMatchingPackages());
506: localFilterCriteria.setMatchingClasses(stopCriteria
507: .isMatchingClasses());
508: localFilterCriteria.setMatchingFeatures(stopCriteria
509: .isMatchingFeatures());
510: localFilterCriteria.setGlobalIncludes("//");
511:
512: GraphSummarizer summarizer = new GraphSummarizer(
513: localScopeCriteria, localFilterCriteria);
514: summarizer.traverseNodes(closure.getFactory().getPackages()
515: .values());
516:
517: resultFactory = summarizer.getScopeFactory();
518: }
519: }
|