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 TestRegularExpressionSelectionCriteria extends TestCase {
040: private RegularExpressionSelectionCriteria criteria;
041:
042: private PackageNode a;
043: private ClassNode a_A;
044: private FeatureNode a_A_a;
045:
046: private PackageNode b;
047: private ClassNode b_B;
048: private FeatureNode b_B_b;
049:
050: private PackageNode c;
051: private ClassNode c_C;
052: private FeatureNode c_C_c;
053:
054: private List include;
055: private List exclude;
056:
057: protected void setUp() throws Exception {
058: criteria = new RegularExpressionSelectionCriteria();
059:
060: NodeFactory factory = new NodeFactory();
061:
062: a = factory.createPackage("a");
063: a_A = factory.createClass("a.A");
064: a_A_a = factory.createFeature("a.A.a");
065:
066: b = factory.createPackage("b");
067: b_B = factory.createClass("b.B");
068: b_B_b = factory.createFeature("b.B.b");
069:
070: c = factory.createPackage("c");
071: c_C = factory.createClass("c.C");
072: c_C_c = factory.createFeature("c.C.c");
073:
074: include = new LinkedList();
075: include.add("/^b/");
076:
077: exclude = new LinkedList();
078: exclude.add("/^c/");
079: }
080:
081: public void testParseRE() {
082: List expected = new ArrayList();
083: expected.add("/test/");
084:
085: List test = RegularExpressionSelectionCriteria
086: .parseRE("/test/");
087:
088: assertEquals("size", expected.size(), test.size());
089: assertEquals("/test/", expected.get(0), test.get(0));
090: }
091:
092: public void testParseBrokenRE() {
093: List expected = new ArrayList();
094: expected.add("/test");
095:
096: List test = RegularExpressionSelectionCriteria.parseRE("/test");
097:
098: assertEquals("size", expected.size(), test.size());
099: assertEquals("/test", expected.get(0), test.get(0));
100: }
101:
102: public void testParseMultipleREs() {
103: List expected = new ArrayList();
104: expected.add("/test1/");
105: expected.add("/test2/");
106:
107: List test = RegularExpressionSelectionCriteria
108: .parseRE("/test1/,/test2/");
109:
110: assertEquals("size", expected.size(), test.size());
111: assertEquals("/test1/", expected.get(0), test.get(0));
112: assertEquals("/test2/", expected.get(1), test.get(1));
113: }
114:
115: public void testParseComplexREs() {
116: List expected = new ArrayList();
117: expected.add("/test1\\/test2/");
118:
119: List test = RegularExpressionSelectionCriteria
120: .parseRE("/test1\\/test2/");
121:
122: assertEquals("size", expected.size(), test.size());
123: assertEquals("/test1\\/test2/", expected.get(0), test.get(0));
124: }
125:
126: public void testParseReallyComplexREs() {
127: List expected = new ArrayList();
128: expected.add("m=test1\\=test2=i");
129:
130: List test = RegularExpressionSelectionCriteria
131: .parseRE("m=test1\\=test2=i");
132:
133: assertEquals("size", expected.size(), test.size());
134: assertEquals("m=test1\\=test2=i", expected.get(0), test.get(0));
135: }
136:
137: public void testMatch() {
138: criteria.setGlobalIncludes("//");
139: criteria.setMatchingPackages(true);
140: criteria.setMatchingClasses(false);
141: criteria.setMatchingFeatures(false);
142:
143: assertTrue("a not in package scope", criteria.matches(a));
144: assertTrue("a.A in package scope", !criteria.matches(a_A));
145: assertTrue("a.A.a in package scope", !criteria.matches(a_A_a));
146: assertTrue("b not in package scope", criteria.matches(b));
147: assertTrue("b.B in package scope", !criteria.matches(b_B));
148: assertTrue("b.B.b in package scope", !criteria.matches(b_B_b));
149: assertTrue("c not in package scope", criteria.matches(c));
150: assertTrue("c.C in package scope", !criteria.matches(c_C));
151: assertTrue("c.C.c in package scope", !criteria.matches(c_C_c));
152:
153: criteria.setMatchingPackages(false);
154: criteria.setMatchingClasses(true);
155: criteria.setMatchingFeatures(false);
156:
157: assertTrue("a in package scope", !criteria.matches(a));
158: assertTrue("a.A not in package scope", criteria.matches(a_A));
159: assertTrue("a.A.a in package scope", !criteria.matches(a_A_a));
160: assertTrue("b not in package scope", !criteria.matches(b));
161: assertTrue("b.B in package scope", criteria.matches(b_B));
162: assertTrue("b.B.b in package scope", !criteria.matches(b_B_b));
163: assertTrue("c not in package scope", !criteria.matches(c));
164: assertTrue("c.C in package scope", criteria.matches(c_C));
165: assertTrue("c.C.c in package scope", !criteria.matches(c_C_c));
166:
167: criteria.setMatchingPackages(false);
168: criteria.setMatchingClasses(false);
169: criteria.setMatchingFeatures(true);
170:
171: assertTrue("a in package scope", !criteria.matches(a));
172: assertTrue("a.A in package scope", !criteria.matches(a_A));
173: assertTrue("a.A.a not in package scope", criteria
174: .matches(a_A_a));
175: assertTrue("b not in package scope", !criteria.matches(b));
176: assertTrue("b.B in package scope", !criteria.matches(b_B));
177: assertTrue("b.B.b in package scope", criteria.matches(b_B_b));
178: assertTrue("c not in package scope", !criteria.matches(c));
179: assertTrue("c.C in package scope", !criteria.matches(c_C));
180: assertTrue("c.C.c in package scope", criteria.matches(c_C_c));
181: }
182:
183: public void testGlobalIncludes() {
184: criteria.setGlobalIncludes(include);
185:
186: criteria.setMatchingPackages(true);
187: criteria.setMatchingClasses(false);
188: criteria.setMatchingFeatures(false);
189:
190: assertTrue("a in package scope", !criteria.matches(a));
191: assertTrue("a.A in package scope", !criteria.matches(a_A));
192: assertTrue("a.A.a in package scope", !criteria.matches(a_A_a));
193: assertTrue("b not in package scope", criteria.matches(b));
194: assertTrue("b.B in package scope", !criteria.matches(b_B));
195: assertTrue("b.B.b in package scope", !criteria.matches(b_B_b));
196: assertTrue("c in package scope", !criteria.matches(c));
197: assertTrue("c.C in package scope", !criteria.matches(c_C));
198: assertTrue("c.C.c in package scope", !criteria.matches(c_C_c));
199:
200: criteria.setMatchingPackages(false);
201: criteria.setMatchingClasses(true);
202: criteria.setMatchingFeatures(false);
203:
204: assertTrue("a in package scope", !criteria.matches(a));
205: assertTrue("a.A in package scope", !criteria.matches(a_A));
206: assertTrue("a.A.a in package scope", !criteria.matches(a_A_a));
207: assertTrue("b in package scope", !criteria.matches(b));
208: assertTrue("b.B not in package scope", criteria.matches(b_B));
209: assertTrue("b.B.b in package scope", !criteria.matches(b_B_b));
210: assertTrue("c in package scope", !criteria.matches(c));
211: assertTrue("c.C in package scope", !criteria.matches(c_C));
212: assertTrue("c.C.c in package scope", !criteria.matches(c_C_c));
213:
214: criteria.setMatchingPackages(false);
215: criteria.setMatchingClasses(false);
216: criteria.setMatchingFeatures(true);
217:
218: assertTrue("a in package scope", !criteria.matches(a));
219: assertTrue("a.A in package scope", !criteria.matches(a_A));
220: assertTrue("a.A.a in package scope", !criteria.matches(a_A_a));
221: assertTrue("b in package scope", !criteria.matches(b));
222: assertTrue("b.B in package scope", !criteria.matches(b_B));
223: assertTrue("b.B.b not in package scope", criteria
224: .matches(b_B_b));
225: assertTrue("c in package scope", !criteria.matches(c));
226: assertTrue("c.C in package scope", !criteria.matches(c_C));
227: assertTrue("c.C.c in package scope", !criteria.matches(c_C_c));
228: }
229:
230: public void testGlobalExcludes() {
231: criteria.setGlobalIncludes("//");
232: criteria.setGlobalExcludes(exclude);
233:
234: criteria.setMatchingPackages(true);
235: criteria.setMatchingClasses(false);
236: criteria.setMatchingFeatures(false);
237:
238: assertTrue("a not in package scope", criteria.matches(a));
239: assertTrue("a.A in package scope", !criteria.matches(a_A));
240: assertTrue("a.A.a in package scope", !criteria.matches(a_A_a));
241: assertTrue("b not in package scope", criteria.matches(b));
242: assertTrue("b.B in package scope", !criteria.matches(b_B));
243: assertTrue("b.B.b in package scope", !criteria.matches(b_B_b));
244: assertTrue("c in package scope", !criteria.matches(c));
245: assertTrue("c.C in package scope", !criteria.matches(c_C));
246: assertTrue("c.C.c in package scope", !criteria.matches(c_C_c));
247:
248: criteria.setMatchingPackages(false);
249: criteria.setMatchingClasses(true);
250: criteria.setMatchingFeatures(false);
251:
252: assertTrue("a in package scope", !criteria.matches(a));
253: assertTrue("a.A not in package scope", criteria.matches(a_A));
254: assertTrue("a.A.a in package scope", !criteria.matches(a_A_a));
255: assertTrue("b in package scope", !criteria.matches(b));
256: assertTrue("b.B not in package scope", criteria.matches(b_B));
257: assertTrue("b.B.b in package scope", !criteria.matches(b_B_b));
258: assertTrue("c not in package scope", !criteria.matches(c));
259: assertTrue("c.C in package scope", !criteria.matches(c_C));
260: assertTrue("c.C.c in package scope", !criteria.matches(c_C_c));
261:
262: criteria.setMatchingPackages(false);
263: criteria.setMatchingClasses(false);
264: criteria.setMatchingFeatures(true);
265:
266: assertTrue("a in package scope", !criteria.matches(a));
267: assertTrue("a.A in package scope", !criteria.matches(a_A));
268: assertTrue("a.A.a not in package scope", criteria
269: .matches(a_A_a));
270: assertTrue("b in package scope", !criteria.matches(b));
271: assertTrue("b.B in package scope", !criteria.matches(b_B));
272: assertTrue("b.B.b not in package scope", criteria
273: .matches(b_B_b));
274: assertTrue("c not in package scope", !criteria.matches(c));
275: assertTrue("c.C in package scope", !criteria.matches(c_C));
276: assertTrue("c.C.c in package scope", !criteria.matches(c_C_c));
277: }
278: }
|