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 TestTransitiveClosureWithTestClass extends TestCase {
040: private NodeFactory factory;
041:
042: private Node _package;
043: private Node test_class;
044: private Node test_main_method;
045: private Node test_Test_method;
046:
047: private Node java_lang_package;
048: private Node java_lang_Object_class;
049: private Node java_lang_Object_Object_method;
050: private Node java_lang_String_class;
051:
052: private Node java_util_package;
053: private Node java_util_Collections_class;
054: private Node java_util_Collections_singleton_method;
055:
056: private List scopeIncludes;
057:
058: private RegularExpressionSelectionCriteria startCriteria;
059: private RegularExpressionSelectionCriteria stopCriteria;
060:
061: private NodeFactory resultFactory;
062:
063: protected void setUp() throws Exception {
064: factory = new NodeFactory();
065:
066: _package = factory.createPackage("");
067: test_class = factory.createClass("test");
068: test_main_method = factory.createFeature("test.main(String[])");
069: test_Test_method = factory.createFeature("test.test()");
070:
071: java_lang_package = factory.createPackage("java.lang");
072: java_lang_Object_class = factory
073: .createClass("java.lang.Object");
074: java_lang_Object_Object_method = factory
075: .createFeature("java.lang.Object.Object()");
076: java_lang_String_class = factory
077: .createClass("java.lang.String");
078:
079: java_util_package = factory.createPackage("java.util");
080: java_util_Collections_class = factory
081: .createClass("java.util.Collections");
082: java_util_Collections_singleton_method = factory
083: .createFeature("java.util.Collections.singleton(java.lang.Object)");
084:
085: test_class.addDependency(java_lang_Object_class);
086: test_main_method.addDependency(java_lang_Object_class);
087: test_main_method.addDependency(java_lang_Object_Object_method);
088: test_main_method.addDependency(java_lang_String_class);
089: test_main_method
090: .addDependency(java_util_Collections_singleton_method);
091: test_Test_method.addDependency(java_lang_Object_Object_method);
092:
093: scopeIncludes = new ArrayList(1);
094: scopeIncludes.add("/test/");
095:
096: startCriteria = new RegularExpressionSelectionCriteria();
097: stopCriteria = new RegularExpressionSelectionCriteria();
098: }
099:
100: public void testCompleteClosure() {
101: startCriteria.setGlobalIncludes(scopeIncludes);
102: stopCriteria.setGlobalIncludes(Collections.EMPTY_LIST);
103:
104: compute(factory.getPackages().values());
105:
106: assertEquals("Different number of packages", factory
107: .getPackages().size(), resultFactory.getPackages()
108: .size());
109: assertEquals("Different number of classes", factory
110: .getClasses().size(), resultFactory.getClasses().size());
111: assertEquals("Different number of features", factory
112: .getFeatures().size(), resultFactory.getFeatures()
113: .size());
114:
115: Iterator i;
116:
117: i = resultFactory.getPackages().keySet().iterator();
118: while (i.hasNext()) {
119: Object key = i.next();
120: assertEquals(factory.getPackages().get(key), resultFactory
121: .getPackages().get(key));
122: assertTrue(factory.getPackages().get(key) != resultFactory
123: .getPackages().get(key));
124: assertEquals("Package " + key
125: + " has different inbound count", ((Node) factory
126: .getPackages().get(key)).getInboundDependencies()
127: .size(), ((Node) resultFactory.getPackages().get(
128: key)).getInboundDependencies().size());
129: assertEquals("Package " + key
130: + " has different outbound count", ((Node) factory
131: .getPackages().get(key)).getOutboundDependencies()
132: .size(), ((Node) resultFactory.getPackages().get(
133: key)).getOutboundDependencies().size());
134: }
135:
136: i = resultFactory.getClasses().keySet().iterator();
137: while (i.hasNext()) {
138: Object key = i.next();
139: assertEquals(factory.getClasses().get(key), resultFactory
140: .getClasses().get(key));
141: assertTrue(factory.getClasses().get(key) != resultFactory
142: .getClasses().get(key));
143: assertEquals("Class " + key
144: + " has different inbound count", ((Node) factory
145: .getClasses().get(key)).getInboundDependencies()
146: .size(), ((Node) resultFactory.getClasses()
147: .get(key)).getInboundDependencies().size());
148: assertEquals("Class " + key
149: + " has different outbound count", ((Node) factory
150: .getClasses().get(key)).getOutboundDependencies()
151: .size(), ((Node) resultFactory.getClasses()
152: .get(key)).getOutboundDependencies().size());
153: }
154:
155: i = resultFactory.getFeatures().keySet().iterator();
156: while (i.hasNext()) {
157: Object key = i.next();
158: assertEquals(factory.getFeatures().get(key), resultFactory
159: .getFeatures().get(key));
160: assertTrue(factory.getFeatures().get(key) != resultFactory
161: .getFeatures().get(key));
162: assertEquals("Feature " + key
163: + " has different inbound count", ((Node) factory
164: .getFeatures().get(key)).getInboundDependencies()
165: .size(), ((Node) resultFactory.getFeatures().get(
166: key)).getInboundDependencies().size());
167: assertEquals("Feature " + key
168: + " has different outbound count", ((Node) factory
169: .getFeatures().get(key)).getOutboundDependencies()
170: .size(), ((Node) resultFactory.getFeatures().get(
171: key)).getOutboundDependencies().size());
172: }
173: }
174:
175: public void testCopyAllNodesOnly() {
176: startCriteria.setGlobalIncludes(scopeIncludes);
177: stopCriteria.setMatchingPackages(false);
178: stopCriteria.setMatchingClasses(false);
179: stopCriteria.setMatchingFeatures(false);
180: stopCriteria.setGlobalIncludes("//");
181:
182: compute(factory.getPackages().values());
183:
184: assertEquals("Different number of packages", 1, resultFactory
185: .getPackages().size());
186: assertEquals("Different number of classes", 1, resultFactory
187: .getClasses().size());
188: assertEquals("Different number of features", 2, resultFactory
189: .getFeatures().size());
190:
191: Iterator i;
192:
193: i = resultFactory.getPackages().keySet().iterator();
194: while (i.hasNext()) {
195: Object key = i.next();
196: assertEquals(factory.getPackages().get(key), resultFactory
197: .getPackages().get(key));
198: assertTrue(factory.getPackages().get(key) != resultFactory
199: .getPackages().get(key));
200: assertTrue(((Node) resultFactory.getPackages().get(key))
201: .getInboundDependencies().isEmpty());
202: assertTrue(((Node) resultFactory.getPackages().get(key))
203: .getOutboundDependencies().isEmpty());
204: }
205:
206: i = resultFactory.getClasses().keySet().iterator();
207: while (i.hasNext()) {
208: Object key = i.next();
209: assertEquals(factory.getClasses().get(key), resultFactory
210: .getClasses().get(key));
211: assertTrue(factory.getClasses().get(key) != resultFactory
212: .getClasses().get(key));
213: assertTrue(((Node) resultFactory.getClasses().get(key))
214: .getInboundDependencies().isEmpty());
215: assertTrue(((Node) resultFactory.getClasses().get(key))
216: .getOutboundDependencies().isEmpty());
217: }
218:
219: i = resultFactory.getFeatures().keySet().iterator();
220: while (i.hasNext()) {
221: Object key = i.next();
222: assertEquals(factory.getFeatures().get(key), resultFactory
223: .getFeatures().get(key));
224: assertTrue(factory.getFeatures().get(key) != resultFactory
225: .getFeatures().get(key));
226: assertTrue(((Node) resultFactory.getFeatures().get(key))
227: .getInboundDependencies().isEmpty());
228: assertTrue(((Node) resultFactory.getFeatures().get(key))
229: .getOutboundDependencies().isEmpty());
230: }
231: }
232:
233: public void testCopyPackageNodesOnly() {
234: startCriteria.setMatchingClasses(false);
235: startCriteria.setMatchingFeatures(false);
236: startCriteria.setGlobalIncludes(scopeIncludes);
237: stopCriteria.setMatchingPackages(false);
238: stopCriteria.setMatchingClasses(false);
239: stopCriteria.setMatchingFeatures(false);
240: stopCriteria.setGlobalIncludes("//");
241:
242: compute(factory.getPackages().values());
243:
244: assertEquals("Different number of packages", 1, resultFactory
245: .getPackages().size());
246: assertTrue(resultFactory.getClasses().isEmpty());
247: assertTrue(resultFactory.getFeatures().isEmpty());
248: }
249:
250: public void testCopyClassNodesOnly() {
251: startCriteria.setMatchingPackages(false);
252: startCriteria.setMatchingFeatures(false);
253: startCriteria.setGlobalIncludes(scopeIncludes);
254: stopCriteria.setMatchingPackages(false);
255: stopCriteria.setMatchingClasses(false);
256: stopCriteria.setMatchingFeatures(false);
257: stopCriteria.setGlobalIncludes("//");
258:
259: compute(factory.getPackages().values());
260:
261: assertEquals("Different number of packages", 1, resultFactory
262: .getPackages().size());
263: assertEquals("Different number of classes", 1, resultFactory
264: .getClasses().size());
265: assertTrue(resultFactory.getFeatures().isEmpty());
266:
267: Iterator i;
268:
269: i = resultFactory.getPackages().keySet().iterator();
270: while (i.hasNext()) {
271: Object key = i.next();
272: assertEquals(factory.getPackages().get(key), resultFactory
273: .getPackages().get(key));
274: assertTrue(factory.getPackages().get(key) != resultFactory
275: .getPackages().get(key));
276: assertTrue(((Node) resultFactory.getPackages().get(key))
277: .getInboundDependencies().isEmpty());
278: assertTrue(((Node) resultFactory.getPackages().get(key))
279: .getOutboundDependencies().isEmpty());
280: }
281:
282: i = resultFactory.getClasses().keySet().iterator();
283: while (i.hasNext()) {
284: Object key = i.next();
285: assertEquals(factory.getClasses().get(key), resultFactory
286: .getClasses().get(key));
287: assertTrue(factory.getClasses().get(key) != resultFactory
288: .getClasses().get(key));
289: assertTrue(((Node) resultFactory.getClasses().get(key))
290: .getInboundDependencies().isEmpty());
291: assertTrue(((Node) resultFactory.getClasses().get(key))
292: .getOutboundDependencies().isEmpty());
293: }
294: }
295:
296: public void testCopyFeatureNodesOnly() {
297: startCriteria.setMatchingPackages(false);
298: startCriteria.setMatchingClasses(false);
299: startCriteria.setGlobalIncludes(scopeIncludes);
300: stopCriteria.setMatchingPackages(false);
301: stopCriteria.setMatchingClasses(false);
302: stopCriteria.setMatchingFeatures(false);
303: stopCriteria.setGlobalIncludes("//");
304:
305: compute(factory.getPackages().values());
306:
307: assertEquals("Different number of packages", 1, resultFactory
308: .getPackages().size());
309: assertEquals("Different number of classes", 1, resultFactory
310: .getClasses().size());
311: assertEquals("Different number of features", 2, resultFactory
312: .getFeatures().size());
313:
314: Iterator i;
315:
316: i = resultFactory.getPackages().keySet().iterator();
317: while (i.hasNext()) {
318: Object key = i.next();
319: assertEquals(factory.getPackages().get(key), resultFactory
320: .getPackages().get(key));
321: assertTrue(factory.getPackages().get(key) != resultFactory
322: .getPackages().get(key));
323: assertTrue(((Node) resultFactory.getPackages().get(key))
324: .getInboundDependencies().isEmpty());
325: assertTrue(((Node) resultFactory.getPackages().get(key))
326: .getOutboundDependencies().isEmpty());
327: }
328:
329: i = resultFactory.getClasses().keySet().iterator();
330: while (i.hasNext()) {
331: Object key = i.next();
332: assertEquals(factory.getClasses().get(key), resultFactory
333: .getClasses().get(key));
334: assertTrue(factory.getClasses().get(key) != resultFactory
335: .getClasses().get(key));
336: assertTrue(((Node) resultFactory.getClasses().get(key))
337: .getInboundDependencies().isEmpty());
338: assertTrue(((Node) resultFactory.getClasses().get(key))
339: .getOutboundDependencies().isEmpty());
340: }
341:
342: i = resultFactory.getFeatures().keySet().iterator();
343: while (i.hasNext()) {
344: Object key = i.next();
345: assertEquals(factory.getFeatures().get(key), resultFactory
346: .getFeatures().get(key));
347: assertTrue(factory.getFeatures().get(key) != resultFactory
348: .getFeatures().get(key));
349: assertTrue(((Node) resultFactory.getFeatures().get(key))
350: .getInboundDependencies().isEmpty());
351: assertTrue(((Node) resultFactory.getFeatures().get(key))
352: .getOutboundDependencies().isEmpty());
353: }
354: }
355:
356: public void testCopyNothing() {
357: startCriteria.setMatchingPackages(false);
358: startCriteria.setMatchingClasses(false);
359: startCriteria.setMatchingFeatures(false);
360:
361: compute(factory.getPackages().values());
362:
363: assertTrue(resultFactory.getPackages().isEmpty());
364: assertTrue(resultFactory.getClasses().isEmpty());
365: assertTrue(resultFactory.getFeatures().isEmpty());
366: }
367:
368: private void compute(Collection nodes) {
369: RegularExpressionSelectionCriteria localStartCriteria = new RegularExpressionSelectionCriteria();
370: localStartCriteria.setGlobalIncludes(startCriteria
371: .getGlobalIncludes());
372: RegularExpressionSelectionCriteria localStopCriteria = new RegularExpressionSelectionCriteria();
373: localStopCriteria.setGlobalIncludes(stopCriteria
374: .getGlobalIncludes());
375:
376: TransitiveClosure closure = new TransitiveClosure(
377: localStartCriteria, localStopCriteria);
378: closure.traverseNodes(nodes);
379:
380: RegularExpressionSelectionCriteria localScopeCriteria = new RegularExpressionSelectionCriteria();
381: localScopeCriteria.setMatchingPackages(startCriteria
382: .isMatchingPackages());
383: localScopeCriteria.setMatchingClasses(startCriteria
384: .isMatchingClasses());
385: localScopeCriteria.setMatchingFeatures(startCriteria
386: .isMatchingFeatures());
387: localScopeCriteria.setGlobalIncludes("//");
388: RegularExpressionSelectionCriteria localFilterCriteria = new RegularExpressionSelectionCriteria();
389: localFilterCriteria.setMatchingPackages(stopCriteria
390: .isMatchingPackages());
391: localFilterCriteria.setMatchingClasses(stopCriteria
392: .isMatchingClasses());
393: localFilterCriteria.setMatchingFeatures(stopCriteria
394: .isMatchingFeatures());
395: localFilterCriteria.setGlobalIncludes("//");
396:
397: GraphSummarizer summarizer = new GraphSummarizer(
398: localScopeCriteria, localFilterCriteria);
399: summarizer.traverseNodes(closure.getFactory().getPackages()
400: .values());
401:
402: resultFactory = summarizer.getScopeFactory();
403: }
404: }
|