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: public class TestTransitiveClosureSlice extends TestCase {
040: private NodeFactory factory;
041:
042: private Node in3;
043: private Node in2;
044: private Node in1;
045: private Node base;
046: private Node out1;
047: private Node out2;
048: private Node out3;
049:
050: private TransitiveClosure selector;
051:
052: protected void setUp() throws Exception {
053: factory = new NodeFactory();
054:
055: in3 = factory.createPackage("in3");
056: in2 = factory.createPackage("in2");
057: in1 = factory.createPackage("in1");
058: base = factory.createPackage("base");
059: out1 = factory.createPackage("out1");
060: out2 = factory.createPackage("out2");
061: out3 = factory.createPackage("out3");
062:
063: in3.addDependency(in2);
064: in2.addDependency(in1);
065: in1.addDependency(base);
066: base.addDependency(out1);
067: out1.addDependency(out2);
068: out2.addDependency(out3);
069:
070: selector = new TransitiveClosure(
071: new RegularExpressionSelectionCriteria("//"),
072: new NullSelectionCriteria());
073: }
074:
075: public void testDefaultDepth() {
076: selector.traverseNodes(Collections.singleton(base));
077:
078: assertEquals("number of packages", 4, selector.getFactory()
079: .getPackages().size());
080: assertEquals("base.Inbound()", 0, ((Node) selector.getFactory()
081: .getPackages().get("base")).getInboundDependencies()
082: .size());
083: assertEquals("base.Outbound()", 1, ((Node) selector
084: .getFactory().getPackages().get("base"))
085: .getOutboundDependencies().size());
086: assertEquals("out1.Inbound()", 1, ((Node) selector.getFactory()
087: .getPackages().get("out1")).getInboundDependencies()
088: .size());
089: assertEquals("out1.Outbound()", 1, ((Node) selector
090: .getFactory().getPackages().get("out1"))
091: .getOutboundDependencies().size());
092: assertEquals("out2.Inbound()", 1, ((Node) selector.getFactory()
093: .getPackages().get("out2")).getInboundDependencies()
094: .size());
095: assertEquals("out2.Outbound()", 1, ((Node) selector
096: .getFactory().getPackages().get("out2"))
097: .getOutboundDependencies().size());
098: assertEquals("out3.Inbound()", 1, ((Node) selector.getFactory()
099: .getPackages().get("out3")).getInboundDependencies()
100: .size());
101: assertEquals("out3.Outbound()", 0, ((Node) selector
102: .getFactory().getPackages().get("out3"))
103: .getOutboundDependencies().size());
104: }
105:
106: public void testUnboundedDepthInboundOutbound() {
107: selector
108: .setMaximumInboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
109: selector
110: .setMaximumOutboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
111:
112: selector.traverseNodes(Collections.singleton(base));
113:
114: assertEquals("number of packages", 7, selector.getFactory()
115: .getPackages().size());
116: assertEquals("in3.Inbound()", 0, ((Node) selector.getFactory()
117: .getPackages().get("in3")).getInboundDependencies()
118: .size());
119: assertEquals("in3.Outbound()", 1, ((Node) selector.getFactory()
120: .getPackages().get("in3")).getOutboundDependencies()
121: .size());
122: assertEquals("in2.Inbound()", 1, ((Node) selector.getFactory()
123: .getPackages().get("in2")).getInboundDependencies()
124: .size());
125: assertEquals("in2.Outbound()", 1, ((Node) selector.getFactory()
126: .getPackages().get("in2")).getOutboundDependencies()
127: .size());
128: assertEquals("in1.Inbound()", 1, ((Node) selector.getFactory()
129: .getPackages().get("in1")).getInboundDependencies()
130: .size());
131: assertEquals("in1.Outbound()", 1, ((Node) selector.getFactory()
132: .getPackages().get("in1")).getOutboundDependencies()
133: .size());
134: assertEquals("base.Inbound()", 1, ((Node) selector.getFactory()
135: .getPackages().get("base")).getInboundDependencies()
136: .size());
137: assertEquals("base.Outbound()", 1, ((Node) selector
138: .getFactory().getPackages().get("base"))
139: .getOutboundDependencies().size());
140: assertEquals("out1.Inbound()", 1, ((Node) selector.getFactory()
141: .getPackages().get("out1")).getInboundDependencies()
142: .size());
143: assertEquals("out1.Outbound()", 1, ((Node) selector
144: .getFactory().getPackages().get("out1"))
145: .getOutboundDependencies().size());
146: assertEquals("out2.Inbound()", 1, ((Node) selector.getFactory()
147: .getPackages().get("out2")).getInboundDependencies()
148: .size());
149: assertEquals("out2.Outbound()", 1, ((Node) selector
150: .getFactory().getPackages().get("out2"))
151: .getOutboundDependencies().size());
152: assertEquals("out3.Inbound()", 1, ((Node) selector.getFactory()
153: .getPackages().get("out3")).getInboundDependencies()
154: .size());
155: assertEquals("out3.Outbound()", 0, ((Node) selector
156: .getFactory().getPackages().get("out3"))
157: .getOutboundDependencies().size());
158: }
159:
160: public void testUnboundedDepthInbound() {
161: selector
162: .setMaximumInboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
163: selector
164: .setMaximumOutboundDepth(TransitiveClosure.DO_NOT_FOLLOW);
165:
166: selector.traverseNodes(Collections.singleton(base));
167:
168: assertEquals("number of packages", 4, selector.getFactory()
169: .getPackages().size());
170: assertEquals("in3.Inbound()", 0, ((Node) selector.getFactory()
171: .getPackages().get("in3")).getInboundDependencies()
172: .size());
173: assertEquals("in3.Outbound()", 1, ((Node) selector.getFactory()
174: .getPackages().get("in3")).getOutboundDependencies()
175: .size());
176: assertEquals("in2.Inbound()", 1, ((Node) selector.getFactory()
177: .getPackages().get("in2")).getInboundDependencies()
178: .size());
179: assertEquals("in2.Outbound()", 1, ((Node) selector.getFactory()
180: .getPackages().get("in2")).getOutboundDependencies()
181: .size());
182: assertEquals("in1.Inbound()", 1, ((Node) selector.getFactory()
183: .getPackages().get("in1")).getInboundDependencies()
184: .size());
185: assertEquals("in1.Outbound()", 1, ((Node) selector.getFactory()
186: .getPackages().get("in1")).getOutboundDependencies()
187: .size());
188: assertEquals("base.Inbound()", 1, ((Node) selector.getFactory()
189: .getPackages().get("base")).getInboundDependencies()
190: .size());
191: assertEquals("base.Outbound()", 0, ((Node) selector
192: .getFactory().getPackages().get("base"))
193: .getOutboundDependencies().size());
194: }
195:
196: public void testUnboundedDepthOutbound() {
197: selector
198: .setMaximumInboundDepth(TransitiveClosure.DO_NOT_FOLLOW);
199: selector
200: .setMaximumOutboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
201:
202: selector.traverseNodes(Collections.singleton(base));
203:
204: assertEquals("number of packages", 4, selector.getFactory()
205: .getPackages().size());
206: assertEquals("base.Inbound()", 0, ((Node) selector.getFactory()
207: .getPackages().get("base")).getInboundDependencies()
208: .size());
209: assertEquals("base.Outbound()", 1, ((Node) selector
210: .getFactory().getPackages().get("base"))
211: .getOutboundDependencies().size());
212: assertEquals("out1.Inbound()", 1, ((Node) selector.getFactory()
213: .getPackages().get("out1")).getInboundDependencies()
214: .size());
215: assertEquals("out1.Outbound()", 1, ((Node) selector
216: .getFactory().getPackages().get("out1"))
217: .getOutboundDependencies().size());
218: assertEquals("out2.Inbound()", 1, ((Node) selector.getFactory()
219: .getPackages().get("out2")).getInboundDependencies()
220: .size());
221: assertEquals("out2.Outbound()", 1, ((Node) selector
222: .getFactory().getPackages().get("out2"))
223: .getOutboundDependencies().size());
224: assertEquals("out3.Inbound()", 1, ((Node) selector.getFactory()
225: .getPackages().get("out3")).getInboundDependencies()
226: .size());
227: assertEquals("out3.Outbound()", 0, ((Node) selector
228: .getFactory().getPackages().get("out3"))
229: .getOutboundDependencies().size());
230: }
231:
232: public void testZeroDepthInboundOutbound() {
233: selector.setMaximumInboundDepth(0);
234: selector.setMaximumOutboundDepth(0);
235:
236: selector.traverseNodes(Collections.singleton(base));
237:
238: assertEquals("number of packages", 1, selector.getFactory()
239: .getPackages().size());
240: assertEquals("base.Inbound()", 0, ((Node) selector.getFactory()
241: .getPackages().get("base")).getInboundDependencies()
242: .size());
243: assertEquals("base.Outbound()", 0, ((Node) selector
244: .getFactory().getPackages().get("base"))
245: .getOutboundDependencies().size());
246: }
247:
248: public void testSingleDepthInboundOutbound() {
249: selector.setMaximumInboundDepth(1);
250: selector.setMaximumOutboundDepth(1);
251:
252: selector.traverseNodes(Collections.singleton(base));
253:
254: assertEquals("number of packages", 3, selector.getFactory()
255: .getPackages().size());
256: assertEquals("in1.Inbound()", 0, ((Node) selector.getFactory()
257: .getPackages().get("in1")).getInboundDependencies()
258: .size());
259: assertEquals("in1.Outbound()", 1, ((Node) selector.getFactory()
260: .getPackages().get("in1")).getOutboundDependencies()
261: .size());
262: assertEquals("base.Inbound()", 1, ((Node) selector.getFactory()
263: .getPackages().get("base")).getInboundDependencies()
264: .size());
265: assertEquals("base.Outbound()", 1, ((Node) selector
266: .getFactory().getPackages().get("base"))
267: .getOutboundDependencies().size());
268: assertEquals("out1.Inbound()", 1, ((Node) selector.getFactory()
269: .getPackages().get("out1")).getInboundDependencies()
270: .size());
271: assertEquals("out1.Outbound()", 0, ((Node) selector
272: .getFactory().getPackages().get("out1"))
273: .getOutboundDependencies().size());
274: }
275:
276: public void testDoubleDepthInboundOutbound() {
277: selector.setMaximumInboundDepth(2);
278: selector.setMaximumOutboundDepth(2);
279:
280: selector.traverseNodes(Collections.singleton(base));
281:
282: assertEquals("number of packages", 5, selector.getFactory()
283: .getPackages().size());
284: assertEquals("in2.Inbound()", 0, ((Node) selector.getFactory()
285: .getPackages().get("in2")).getInboundDependencies()
286: .size());
287: assertEquals("in2.Outbound()", 1, ((Node) selector.getFactory()
288: .getPackages().get("in2")).getOutboundDependencies()
289: .size());
290: assertEquals("in1.Inbound()", 1, ((Node) selector.getFactory()
291: .getPackages().get("in1")).getInboundDependencies()
292: .size());
293: assertEquals("in1.Outbound()", 1, ((Node) selector.getFactory()
294: .getPackages().get("in1")).getOutboundDependencies()
295: .size());
296: assertEquals("base.Inbound()", 1, ((Node) selector.getFactory()
297: .getPackages().get("base")).getInboundDependencies()
298: .size());
299: assertEquals("base.Outbound()", 1, ((Node) selector
300: .getFactory().getPackages().get("base"))
301: .getOutboundDependencies().size());
302: assertEquals("out1.Inbound()", 1, ((Node) selector.getFactory()
303: .getPackages().get("out1")).getInboundDependencies()
304: .size());
305: assertEquals("out1.Outbound()", 1, ((Node) selector
306: .getFactory().getPackages().get("out1"))
307: .getOutboundDependencies().size());
308: assertEquals("out2.Inbound()", 1, ((Node) selector.getFactory()
309: .getPackages().get("out2")).getInboundDependencies()
310: .size());
311: assertEquals("out2.Outbound()", 0, ((Node) selector
312: .getFactory().getPackages().get("out2"))
313: .getOutboundDependencies().size());
314: }
315:
316: public void testExactDepthInboundOutbound() {
317: selector.setMaximumInboundDepth(3);
318: selector.setMaximumOutboundDepth(3);
319:
320: selector.traverseNodes(Collections.singleton(base));
321:
322: assertEquals("number of packages", 7, selector.getFactory()
323: .getPackages().size());
324: assertEquals("in3.Inbound()", 0, ((Node) selector.getFactory()
325: .getPackages().get("in3")).getInboundDependencies()
326: .size());
327: assertEquals("in3.Outbound()", 1, ((Node) selector.getFactory()
328: .getPackages().get("in3")).getOutboundDependencies()
329: .size());
330: assertEquals("in2.Inbound()", 1, ((Node) selector.getFactory()
331: .getPackages().get("in2")).getInboundDependencies()
332: .size());
333: assertEquals("in2.Outbound()", 1, ((Node) selector.getFactory()
334: .getPackages().get("in2")).getOutboundDependencies()
335: .size());
336: assertEquals("in1.Inbound()", 1, ((Node) selector.getFactory()
337: .getPackages().get("in1")).getInboundDependencies()
338: .size());
339: assertEquals("in1.Outbound()", 1, ((Node) selector.getFactory()
340: .getPackages().get("in1")).getOutboundDependencies()
341: .size());
342: assertEquals("base.Inbound()", 1, ((Node) selector.getFactory()
343: .getPackages().get("base")).getInboundDependencies()
344: .size());
345: assertEquals("base.Outbound()", 1, ((Node) selector
346: .getFactory().getPackages().get("base"))
347: .getOutboundDependencies().size());
348: assertEquals("out1.Inbound()", 1, ((Node) selector.getFactory()
349: .getPackages().get("out1")).getInboundDependencies()
350: .size());
351: assertEquals("out1.Outbound()", 1, ((Node) selector
352: .getFactory().getPackages().get("out1"))
353: .getOutboundDependencies().size());
354: assertEquals("out2.Inbound()", 1, ((Node) selector.getFactory()
355: .getPackages().get("out2")).getInboundDependencies()
356: .size());
357: assertEquals("out2.Outbound()", 1, ((Node) selector
358: .getFactory().getPackages().get("out2"))
359: .getOutboundDependencies().size());
360: assertEquals("out3.Inbound()", 1, ((Node) selector.getFactory()
361: .getPackages().get("out3")).getInboundDependencies()
362: .size());
363: assertEquals("out3.Outbound()", 0, ((Node) selector
364: .getFactory().getPackages().get("out3"))
365: .getOutboundDependencies().size());
366: }
367:
368: public void testOverDepthInboundOutbound() {
369: selector.setMaximumInboundDepth(4);
370: selector.setMaximumOutboundDepth(4);
371:
372: selector.traverseNodes(Collections.singleton(base));
373:
374: assertEquals("number of packages", 7, selector.getFactory()
375: .getPackages().size());
376: assertEquals("in3.Inbound()", 0, ((Node) selector.getFactory()
377: .getPackages().get("in3")).getInboundDependencies()
378: .size());
379: assertEquals("in3.Outbound()", 1, ((Node) selector.getFactory()
380: .getPackages().get("in3")).getOutboundDependencies()
381: .size());
382: assertEquals("in2.Inbound()", 1, ((Node) selector.getFactory()
383: .getPackages().get("in2")).getInboundDependencies()
384: .size());
385: assertEquals("in2.Outbound()", 1, ((Node) selector.getFactory()
386: .getPackages().get("in2")).getOutboundDependencies()
387: .size());
388: assertEquals("in1.Inbound()", 1, ((Node) selector.getFactory()
389: .getPackages().get("in1")).getInboundDependencies()
390: .size());
391: assertEquals("in1.Outbound()", 1, ((Node) selector.getFactory()
392: .getPackages().get("in1")).getOutboundDependencies()
393: .size());
394: assertEquals("base.Inbound()", 1, ((Node) selector.getFactory()
395: .getPackages().get("base")).getInboundDependencies()
396: .size());
397: assertEquals("base.Outbound()", 1, ((Node) selector
398: .getFactory().getPackages().get("base"))
399: .getOutboundDependencies().size());
400: assertEquals("out1.Inbound()", 1, ((Node) selector.getFactory()
401: .getPackages().get("out1")).getInboundDependencies()
402: .size());
403: assertEquals("out1.Outbound()", 1, ((Node) selector
404: .getFactory().getPackages().get("out1"))
405: .getOutboundDependencies().size());
406: assertEquals("out2.Inbound()", 1, ((Node) selector.getFactory()
407: .getPackages().get("out2")).getInboundDependencies()
408: .size());
409: assertEquals("out2.Outbound()", 1, ((Node) selector
410: .getFactory().getPackages().get("out2"))
411: .getOutboundDependencies().size());
412: assertEquals("out3.Inbound()", 1, ((Node) selector.getFactory()
413: .getPackages().get("out3")).getInboundDependencies()
414: .size());
415: assertEquals("out3.Outbound()", 0, ((Node) selector
416: .getFactory().getPackages().get("out3"))
417: .getOutboundDependencies().size());
418: }
419: }
|