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