001: /*
002: * @(#)PathRegistryUTest.java 0.9.0 15-MAR-2001 - 18:27
003: *
004: * Copyright (C) 2001,,2003 2002 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.datastruct.v1;
028:
029: import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
030:
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: /**
036: *
037: *
038: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
039: * @since March 15, 2001 (Alpha 0.9.0)
040: * @version $Date: 2003/02/10 22:52:44 $
041: */
042: public class PathRegistryUTest extends TestCase {
043: private static final Class THIS_CLASS = PathRegistryUTest.class;
044:
045: public PathRegistryUTest(String name) {
046: super (name);
047: }
048:
049: public static Test suite() {
050: TestSuite suite = new TestSuite(THIS_CLASS);
051:
052: return suite;
053: }
054:
055: public static void main(String[] args) {
056: String[] name = { THIS_CLASS.getName() };
057:
058: // junit.textui.TestRunner.main( name );
059: // junit.swingui.TestRunner.main( name );
060:
061: junit.textui.TestRunner.main(name);
062: }
063:
064: protected void setUp() throws Exception {
065: super .setUp();
066:
067: // set ourself up
068: }
069:
070: protected void tearDown() throws Exception {
071: // tear ourself down
072:
073: super .tearDown();
074: }
075:
076: public void testInstantiate() {
077: new PathRegistry('.', true);
078: new PathRegistry('.', false);
079: }
080:
081: public void testAddCaseSensitiveNotRecursive()
082: throws PathAlreadyRegisteredException {
083: PathRegistry pr = new PathRegistry('/', true);
084:
085: // now try to get something not added
086: notRegisteredTest(pr, "root");
087:
088: Object o1 = new Object();
089: pr.register("root", o1, false, true);
090: registeredRightTest(pr, "root", o1);
091: Object o2;
092:
093: // now try to get the path with wrong case
094: caseNotRegisteredTest(pr, "root", '/');
095:
096: // now try to get a sub path
097: notRegisteredTest(pr, "root/empty");
098:
099: // now try to get something not added
100: notRegisteredTest(pr, "unknown");
101:
102: // now try to get something not added
103: notRegisteredTest(pr, "/unknown");
104:
105: // now add a sub-path
106: notRegisteredTest(pr, "root/deep");
107: Object o1a = new Object();
108: pr.register("root/deep", o1a, false, true);
109: registeredRightTest(pr, "root/deep", o1a);
110:
111: // now try to get the path with wrong case
112: caseNotRegisteredTest(pr, "root/deep", '/');
113:
114: // now try to get a bad sub path
115: notRegisteredTest(pr, "root/empty");
116: notRegisteredTest(pr, "root/deep/empty");
117:
118: // now try to get something not added
119: notRegisteredTest(pr, "unknown");
120: }
121:
122: public void testAddTwiceException()
123: throws PathAlreadyRegisteredException {
124: PathRegistry pr = new PathRegistry('/', true);
125: Object o1 = new Object();
126: notRegisteredTest(pr, "root");
127:
128: pr.register("root", o1, false, true);
129: registeredRightTest(pr, "root", o1);
130:
131: // now try to register the same path again
132: alreadyRegisteredTest(pr, "root");
133: }
134:
135: public void testAddCaseInsensitiveNotRecursive()
136: throws PathAlreadyRegisteredException {
137: PathRegistry pr = new PathRegistry('/', true);
138: notRegisteredTest(pr, "root");
139:
140: Object o1 = new Object();
141: pr.register("root", o1, false, false);
142:
143: // now try to get with variations
144: caseRegisteredRightTest(pr, "root", o1, '/');
145:
146: // now try to get a sub path
147: caseNotRegisteredTest(pr, "root/unknown", '/');
148: }
149:
150: public void testAddTwiceExceptionCaseInsensitive()
151: throws PathAlreadyRegisteredException {
152: PathRegistry pr = new PathRegistry('/', true);
153: notRegisteredTest(pr, "root");
154: Object o1 = new Object();
155: pr.register("root", o1, false, false);
156: registeredRightTest(pr, "root", o1);
157:
158: // now try to register the same path again
159: caseAlreadyRegisteredTest(pr, "root", '/');
160: }
161:
162: public void testAddCaseSensitiveRecursive()
163: throws PathAlreadyRegisteredException {
164: PathRegistry pr = new PathRegistry('/', true);
165: notRegisteredTest(pr, "root");
166: Object o1 = new Object();
167: pr.register("root", o1, true, true);
168: registeredRightTest(pr, "root", o1);
169:
170: // now try to get the path with wrong case
171: caseNotRegisteredTest(pr, "root", '/');
172:
173: // now try to get a sub path
174: registeredRightTest(pr, "root/empty", o1);
175:
176: // now try to get something not added
177: notRegisteredTest(pr, "unknown");
178:
179: // now try to get something not added
180: notRegisteredTest(pr, "unknown/subpath");
181: }
182:
183: public void testAddTwiceExceptionRecursive()
184: throws PathAlreadyRegisteredException {
185: PathRegistry pr = new PathRegistry('/', true);
186: notRegisteredTest(pr, "root");
187: Object o1 = new Object();
188: pr.register("root", o1, true, true);
189: registeredRightTest(pr, "root", o1);
190:
191: alreadyRegisteredTest(pr, "root");
192: alreadyRegisteredTest(pr, "root/empty");
193: alreadyRegisteredTest(pr, "root/whatever");
194: alreadyRegisteredTest(pr, "root/empty/whatever");
195: }
196:
197: public void testRemoveCaseSensitiveNotResursive()
198: throws PathAlreadyRegisteredException,
199: NoRegisteredComponentException {
200: PathRegistry pr = new PathRegistry('/', true);
201: notRegisteredTest(pr, "root");
202: Object o1 = new Object();
203: pr.register("root", o1, true, true);
204: registeredRightTest(pr, "root", o1);
205:
206: cantRemoveEntryTest(pr, "unknown");
207:
208: caseCantRemoveEntryTest(pr, "root", '/');
209: cantRemoveEntryTest(pr, "root/empty");
210:
211: pr.remove("root");
212:
213: cantRemoveEntryTest(pr, "root");
214: }
215:
216: //----------------------------------------------------------
217: // Protected helper methods
218:
219: /**
220: * Assert that the given path has nothing registered to it.
221: */
222: protected void notRegisteredTest(PathRegistry pr, String path) {
223: Object o = pr.get(path);
224: assertTrue("Must return null on bad path and sub-path '" + path
225: + "', but found [" + o + "].", o == null);
226: }
227:
228: /**
229: * Assert that the given path's case variations are not registered
230: * (however, the original path may be registered).
231: */
232: protected void caseNotRegisteredTest(PathRegistry pr, String path,
233: char separator) {
234: String[] variations = getCaseVariations(path, separator);
235: for (int i = 0; i < variations.length; i++) {
236: if (!path.equals(variations[i])) {
237: notRegisteredTest(pr, variations[i]);
238: }
239: }
240: }
241:
242: /**
243: * Assert that the given path is registered to the given object.
244: */
245: protected void registeredRightTest(PathRegistry pr, String path,
246: Object expectedObj) {
247: Object o2 = pr.get(path);
248: assertNotNull("Path must be registered.", o2);
249: assertEquals(
250: "What gets put in, needs to equal what gets put out.",
251: expectedObj, o2);
252: }
253:
254: /**
255: * Assert that the given path and its case variations are registered/
256: */
257: protected void caseRegisteredRightTest(PathRegistry pr,
258: String path, Object expectedObj, char separator) {
259: registeredRightTest(pr, path, expectedObj);
260: String[] variations = getCaseVariations(path, separator);
261: for (int i = 0; i < variations.length; i++) {
262: registeredRightTest(pr, variations[i], expectedObj);
263: }
264: }
265:
266: /**
267: * Attempt to register an object at the given path. The test succeeds
268: * if it is already registered, and fails if it does not throw an
269: * exception.
270: */
271: protected void alreadyRegisteredTest(PathRegistry pr, String path) {
272: try {
273: pr.register(path, new Object(), false, false);
274: } catch (PathAlreadyRegisteredException pare) {
275: // good check!
276: return;
277: }
278: fail("PathRegistry should have thrown an exception");
279: }
280:
281: /**
282: * Pass if the correct exception is thrown when trying to remove
283: * an entry that doesn't exist.
284: */
285: protected void cantRemoveEntryTest(PathRegistry pr, String path) {
286: try {
287: pr.remove(path);
288: } catch (NoRegisteredComponentException nrce) {
289: // good check!
290: return;
291: }
292: fail("PathRegistry should have thrown an exception");
293: }
294:
295: protected void caseCantRemoveEntryTest(PathRegistry pr,
296: String path, char separator) {
297: String[] variations = getCaseVariations(path, separator);
298: for (int i = 0; i < variations.length; i++) {
299: if (!path.equals(variations[i])) {
300: cantRemoveEntryTest(pr, variations[i]);
301: }
302: }
303: }
304:
305: /**
306: * Attempt to register the given path and its case variations.
307: */
308: protected void caseAlreadyRegisteredTest(PathRegistry pr,
309: String path, char separator) {
310: alreadyRegisteredTest(pr, path);
311: String[] variations = getCaseVariations(path, separator);
312: for (int i = 0; i < variations.length; i++) {
313: alreadyRegisteredTest(pr, variations[i]);
314: }
315: }
316:
317: private static java.util.Random s_rand = new java.util.Random();
318:
319: private static synchronized int nextInt(int n) {
320: // almost identical to JDK 1.2's Random.nextInt( n )
321: if (n <= 0)
322: throw new IllegalArgumentException("n must be > 0");
323:
324: if ((n & -n) == n) // n is a power of 2
325: {
326: // mask by 7fff... to force it to be positive
327: return (int) ((n * ((long) s_rand.nextInt() & 0x7fffffffL)) >> 31);
328: }
329:
330: int bits, val;
331: do {
332: // mask by 7fff... to force it to be positive
333: bits = s_rand.nextInt() & 0x7fffffff;
334: val = bits % n;
335: } while (bits - val + (n - 1) < 0);
336: return val;
337: }
338:
339: /**
340: * Get a set of variations in case upper / lower of the given
341: * path.
342: */
343: protected String[] getCaseVariations(String orig, char separator) {
344: orig = orig.toLowerCase();
345: char buff[] = orig.toCharArray();
346: int len = buff.length;
347: if (len <= 0) {
348: return new String[0];
349: }
350: if (len == 1) {
351: return new String[] { orig, orig.toUpperCase() };
352: }
353:
354: java.util.Vector var = new java.util.Vector();
355: for (int i = 0; i < len; i++) {
356: int pos1 = nextInt(len);
357: int pos2 = nextInt(len);
358: char origChar1 = buff[pos1];
359: char origChar2 = buff[pos2];
360: if (origChar1 != separator)
361: buff[pos1] = Character.toUpperCase(origChar1);
362: String s = new String(buff);
363: if (!s.equals(orig))
364: var.addElement(s);
365: if (origChar2 != separator)
366: buff[pos2] = Character.toUpperCase(origChar2);
367: if (!s.equals(orig))
368: var.addElement(s);
369: if (origChar1 != separator)
370: buff[pos1] = Character.toLowerCase(origChar1);
371: if (!s.equals(orig))
372: var.addElement(s);
373: if (origChar2 != separator)
374: buff[pos2] = Character.toLowerCase(origChar2);
375: if (!s.equals(orig))
376: var.addElement(s);
377: buff[pos1] = origChar1;
378: buff[pos2] = origChar2;
379: }
380: String ss[] = new String[var.size()];
381: var.copyInto(ss);
382: return ss;
383: }
384: }
|