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