KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > TestSelectiveTraversalStrategy


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependency;
34
35 import java.io.*;
36 import java.util.*;
37
38 import junit.framework.*;
39
40 import org.apache.oro.text.perl.*;
41
42 public class TestSelectiveTraversalStrategy extends TestCase {
43     private RegularExpressionSelectionCriteria scopeCriteria;
44     private RegularExpressionSelectionCriteria filterCriteria;
45     private SelectiveTraversalStrategy strategy;
46     private NodeFactory factory;
47
48     private PackageNode a;
49     private ClassNode a_A;
50     private FeatureNode a_A_a;
51     
52     private PackageNode b;
53     private ClassNode b_B;
54     private FeatureNode b_B_b;
55     
56     private PackageNode c;
57     private ClassNode c_C;
58     private FeatureNode c_C_c;
59
60     private List include;
61     private List exclude;
62
63     protected void setUp() throws Exception JavaDoc {
64         scopeCriteria = new RegularExpressionSelectionCriteria();
65         filterCriteria = new RegularExpressionSelectionCriteria();
66         strategy = new SelectiveTraversalStrategy(scopeCriteria, filterCriteria);
67         factory = new NodeFactory();
68
69         a = factory.createPackage("a");
70         a_A = factory.createClass("a.A");
71         a_A_a = factory.createFeature("a.A.a");
72         
73         b = factory.createPackage("b");
74         b_B = factory.createClass("b.B");
75         b_B_b = factory.createFeature("b.B.b");
76         
77         c = factory.createPackage("c");
78         c_C = factory.createClass("c.C");
79         c_C_c = factory.createFeature("c.C.c");
80         
81         include = new LinkedList();
82         include.add("/^b/");
83         
84         exclude = new LinkedList();
85         exclude.add("/^c/");
86     }
87     
88     public void testScope() {
89         scopeCriteria.setMatchingPackages(true);
90         scopeCriteria.setMatchingClasses(false);
91         scopeCriteria.setMatchingFeatures(false);
92
93         assertTrue("a not in package scope", strategy.isInScope(a));
94         assertTrue("a.A in package scope", !strategy.isInScope(a_A));
95         assertTrue("a.A.a in package scope", !strategy.isInScope(a_A_a));
96         assertTrue("b not in package scope", strategy.isInScope(b));
97         assertTrue("b.B in package scope", !strategy.isInScope(b_B));
98         assertTrue("b.B.b in package scope", !strategy.isInScope(b_B_b));
99         assertTrue("c not in package scope", strategy.isInScope(c));
100         assertTrue("c.C in package scope", !strategy.isInScope(c_C));
101         assertTrue("c.C.c in package scope", !strategy.isInScope(c_C_c));
102
103         scopeCriteria.setMatchingPackages(false);
104         scopeCriteria.setMatchingClasses(true);
105         scopeCriteria.setMatchingFeatures(false);
106
107         assertTrue("a in package scope", !strategy.isInScope(a));
108         assertTrue("a.A not in package scope", strategy.isInScope(a_A));
109         assertTrue("a.A.a in package scope", !strategy.isInScope(a_A_a));
110         assertTrue("b not in package scope", !strategy.isInScope(b));
111         assertTrue("b.B in package scope", strategy.isInScope(b_B));
112         assertTrue("b.B.b in package scope", !strategy.isInScope(b_B_b));
113         assertTrue("c not in package scope", !strategy.isInScope(c));
114         assertTrue("c.C in package scope", strategy.isInScope(c_C));
115         assertTrue("c.C.c in package scope", !strategy.isInScope(c_C_c));
116
117         scopeCriteria.setMatchingPackages(false);
118         scopeCriteria.setMatchingClasses(false);
119         scopeCriteria.setMatchingFeatures(true);
120
121         assertTrue("a in package scope", !strategy.isInScope(a));
122         assertTrue("a.A in package scope", !strategy.isInScope(a_A));
123         assertTrue("a.A.a not in package scope", strategy.isInScope(a_A_a));
124         assertTrue("b not in package scope", !strategy.isInScope(b));
125         assertTrue("b.B in package scope", !strategy.isInScope(b_B));
126         assertTrue("b.B.b in package scope", strategy.isInScope(b_B_b));
127         assertTrue("c not in package scope", !strategy.isInScope(c));
128         assertTrue("c.C in package scope", !strategy.isInScope(c_C));
129         assertTrue("c.C.c in package scope", strategy.isInScope(c_C_c));
130     }
131
132     public void testScopeIncludes() {
133         scopeCriteria.setGlobalIncludes(include);
134
135         scopeCriteria.setMatchingPackages(true);
136         scopeCriteria.setMatchingClasses(false);
137         scopeCriteria.setMatchingFeatures(false);
138
139         assertTrue("a in package scope", !strategy.isInScope(a));
140         assertTrue("a.A in package scope", !strategy.isInScope(a_A));
141         assertTrue("a.A.a in package scope", !strategy.isInScope(a_A_a));
142         assertTrue("b not in package scope", strategy.isInScope(b));
143         assertTrue("b.B in package scope", !strategy.isInScope(b_B));
144         assertTrue("b.B.b in package scope", !strategy.isInScope(b_B_b));
145         assertTrue("c in package scope", !strategy.isInScope(c));
146         assertTrue("c.C in package scope", !strategy.isInScope(c_C));
147         assertTrue("c.C.c in package scope", !strategy.isInScope(c_C_c));
148
149         scopeCriteria.setMatchingPackages(false);
150         scopeCriteria.setMatchingClasses(true);
151         scopeCriteria.setMatchingFeatures(false);
152
153         assertTrue("a in package scope", !strategy.isInScope(a));
154         assertTrue("a.A in package scope", !strategy.isInScope(a_A));
155         assertTrue("a.A.a in package scope", !strategy.isInScope(a_A_a));
156         assertTrue("b in package scope", !strategy.isInScope(b));
157         assertTrue("b.B not in package scope", strategy.isInScope(b_B));
158         assertTrue("b.B.b in package scope", !strategy.isInScope(b_B_b));
159         assertTrue("c in package scope", !strategy.isInScope(c));
160         assertTrue("c.C in package scope", !strategy.isInScope(c_C));
161         assertTrue("c.C.c in package scope", !strategy.isInScope(c_C_c));
162
163         scopeCriteria.setMatchingPackages(false);
164         scopeCriteria.setMatchingClasses(false);
165         scopeCriteria.setMatchingFeatures(true);
166
167         assertTrue("a in package scope", !strategy.isInScope(a));
168         assertTrue("a.A in package scope", !strategy.isInScope(a_A));
169         assertTrue("a.A.a in package scope", !strategy.isInScope(a_A_a));
170         assertTrue("b in package scope", !strategy.isInScope(b));
171         assertTrue("b.B in package scope", !strategy.isInScope(b_B));
172         assertTrue("b.B.b not in package scope", strategy.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.isInScope(a_A_a));
216         assertTrue("b in package scope", !strategy.isInScope(b));
217         assertTrue("b.B in package scope", !strategy.isInScope(b_B));
218         assertTrue("b.B.b not in package scope", strategy.isInScope(b_B_b));
219         assertTrue("c not in package scope", !strategy.isInScope(c));
220         assertTrue("c.C in package scope", !strategy.isInScope(c_C));
221         assertTrue("c.C.c in package scope", !strategy.isInScope(c_C_c));
222     }
223
224     public void testFilter() {
225         filterCriteria.setMatchingPackages(true);
226         filterCriteria.setMatchingClasses(false);
227         filterCriteria.setMatchingFeatures(false);
228
229         assertTrue("a not in package filter", strategy.isInFilter(a));
230         assertTrue("a.A in package filter", !strategy.isInFilter(a_A));
231         assertTrue("a.A.a in package filter", !strategy.isInFilter(a_A_a));
232         assertTrue("b not in package filter", strategy.isInFilter(b));
233         assertTrue("b.B in package filter", !strategy.isInFilter(b_B));
234         assertTrue("b.B.b in package filter", !strategy.isInFilter(b_B_b));
235         assertTrue("c not in package filter", strategy.isInFilter(c));
236         assertTrue("c.C in package filter", !strategy.isInFilter(c_C));
237         assertTrue("c.C.c in package filter", !strategy.isInFilter(c_C_c));
238
239         filterCriteria.setMatchingPackages(false);
240         filterCriteria.setMatchingClasses(true);
241         filterCriteria.setMatchingFeatures(false);
242
243         assertTrue("a in package filter", !strategy.isInFilter(a));
244         assertTrue("a.A not in package filter", strategy.isInFilter(a_A));
245         assertTrue("a.A.a in package filter", !strategy.isInFilter(a_A_a));
246         assertTrue("b not in package filter", !strategy.isInFilter(b));
247         assertTrue("b.B in package filter", strategy.isInFilter(b_B));
248         assertTrue("b.B.b in package filter", !strategy.isInFilter(b_B_b));
249         assertTrue("c not in package filter", !strategy.isInFilter(c));
250         assertTrue("c.C in package filter", strategy.isInFilter(c_C));
251         assertTrue("c.C.c in package filter", !strategy.isInFilter(c_C_c));
252
253         filterCriteria.setMatchingPackages(false);
254         filterCriteria.setMatchingClasses(false);
255         filterCriteria.setMatchingFeatures(true);
256
257         assertTrue("a in package filter", !strategy.isInFilter(a));
258         assertTrue("a.A in package filter", !strategy.isInFilter(a_A));
259         assertTrue("a.A.a not in package filter", strategy.isInFilter(a_A_a));
260         assertTrue("b not in package filter", !strategy.isInFilter(b));
261         assertTrue("b.B in package filter", !strategy.isInFilter(b_B));
262         assertTrue("b.B.b in package filter", strategy.isInFilter(b_B_b));
263         assertTrue("c not in package filter", !strategy.isInFilter(c));
264         assertTrue("c.C in package filter", !strategy.isInFilter(c_C));
265         assertTrue("c.C.c in package filter", strategy.isInFilter(c_C_c));
266     }
267
268     public void testFilterIncludes() {
269         filterCriteria.setGlobalIncludes(include);
270
271         filterCriteria.setMatchingPackages(true);
272         filterCriteria.setMatchingClasses(false);
273         filterCriteria.setMatchingFeatures(false);
274
275         assertTrue("a in package filter", !strategy.isInFilter(a));
276         assertTrue("a.A in package filter", !strategy.isInFilter(a_A));
277         assertTrue("a.A.a in package filter", !strategy.isInFilter(a_A_a));
278         assertTrue("b not in package filter", strategy.isInFilter(b));
279         assertTrue("b.B in package filter", !strategy.isInFilter(b_B));
280         assertTrue("b.B.b in package filter", !strategy.isInFilter(b_B_b));
281         assertTrue("c in package filter", !strategy.isInFilter(c));
282         assertTrue("c.C in package filter", !strategy.isInFilter(c_C));
283         assertTrue("c.C.c in package filter", !strategy.isInFilter(c_C_c));
284
285         filterCriteria.setMatchingPackages(false);
286         filterCriteria.setMatchingClasses(true);
287         filterCriteria.setMatchingFeatures(false);
288
289         assertTrue("a in package filter", !strategy.isInFilter(a));
290         assertTrue("a.A in package filter", !strategy.isInFilter(a_A));
291         assertTrue("a.A.a in package filter", !strategy.isInFilter(a_A_a));
292         assertTrue("b in package filter", !strategy.isInFilter(b));
293         assertTrue("b.B not in package filter", strategy.isInFilter(b_B));
294         assertTrue("b.B.b in package filter", !strategy.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.isInFilter(c_C_c));
298
299         filterCriteria.setMatchingPackages(false);
300         filterCriteria.setMatchingClasses(false);
301         filterCriteria.setMatchingFeatures(true);
302
303         assertTrue("a in package filter", !strategy.isInFilter(a));
304         assertTrue("a.A in package filter", !strategy.isInFilter(a_A));
305         assertTrue("a.A.a in package filter", !strategy.isInFilter(a_A_a));
306         assertTrue("b in package filter", !strategy.isInFilter(b));
307         assertTrue("b.B in package filter", !strategy.isInFilter(b_B));
308         assertTrue("b.B.b not in package filter", strategy.isInFilter(b_B_b));
309         assertTrue("c in package filter", !strategy.isInFilter(c));
310         assertTrue("c.C in package filter", !strategy.isInFilter(c_C));
311         assertTrue("c.C.c in package filter", !strategy.isInFilter(c_C_c));
312     }
313
314     public void testFilterExcludes() {
315         filterCriteria.setGlobalExcludes(exclude);
316
317         filterCriteria.setMatchingPackages(true);
318         filterCriteria.setMatchingClasses(false);
319         filterCriteria.setMatchingFeatures(false);
320
321         assertTrue("a not in package filter", strategy.isInFilter(a));
322         assertTrue("a.A in package filter", !strategy.isInFilter(a_A));
323         assertTrue("a.A.a in package filter", !strategy.isInFilter(a_A_a));
324         assertTrue("b not in package filter", strategy.isInFilter(b));
325         assertTrue("b.B in package filter", !strategy.isInFilter(b_B));
326         assertTrue("b.B.b in package filter", !strategy.isInFilter(b_B_b));
327         assertTrue("c in package filter", !strategy.isInFilter(c));
328         assertTrue("c.C in package filter", !strategy.isInFilter(c_C));
329         assertTrue("c.C.c in package filter", !strategy.isInFilter(c_C_c));
330
331         filterCriteria.setMatchingPackages(false);
332         filterCriteria.setMatchingClasses(true);
333         filterCriteria.setMatchingFeatures(false);
334
335         assertTrue("a in package filter", !strategy.isInFilter(a));
336         assertTrue("a.A not in package filter", strategy.isInFilter(a_A));
337         assertTrue("a.A.a in package filter", !strategy.isInFilter(a_A_a));
338         assertTrue("b in package filter", !strategy.isInFilter(b));
339         assertTrue("b.B not in package filter", strategy.isInFilter(b_B));
340         assertTrue("b.B.b in package filter", !strategy.isInFilter(b_B_b));
341         assertTrue("c in package filter", !strategy.isInFilter(c));
342         assertTrue("c.C in package filter", !strategy.isInFilter(c_C));
343         assertTrue("c.C.c in package filter", !strategy.isInFilter(c_C_c));
344
345         filterCriteria.setMatchingPackages(false);
346         filterCriteria.setMatchingClasses(false);
347         filterCriteria.setMatchingFeatures(true);
348
349         assertTrue("a in package filter", !strategy.isInFilter(a));
350         assertTrue("a.A in package filter", !strategy.isInFilter(a_A));
351         assertTrue("a.A.a not in package filter", strategy.isInFilter(a_A_a));
352         assertTrue("b in package filter", !strategy.isInFilter(b));
353         assertTrue("b.B in package filter", !strategy.isInFilter(b_B));
354         assertTrue("b.B.b not in package filter", strategy.isInFilter(b_B_b));
355         assertTrue("c in package filter", !strategy.isInFilter(c));
356         assertTrue("c.C in package filter", !strategy.isInFilter(c_C));
357         assertTrue("c.C.c in package filter", !strategy.isInFilter(c_C_c));
358     }
359 }
360
Popular Tags