KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > datastruct > v1 > PathRegistryUTest


1 /*
2  * @(#)PathRegistryUTest.java 0.9.0 15-MAR-2001 - 18:27
3  *
4  * Copyright (C) 2001,,2003 2002 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.datastruct.v1;
28
29 import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35
36 /**
37  *
38  *
39  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
40  * @since March 15, 2001 (Alpha 0.9.0)
41  * @version $Date: 2003/02/10 22:52:44 $
42  */

43 public class PathRegistryUTest extends TestCase
44 {
45     private static final Class JavaDoc THIS_CLASS = PathRegistryUTest.class;
46     
47     public PathRegistryUTest( String JavaDoc name )
48     {
49         super( name );
50     }
51     
52     public static Test suite()
53     {
54         TestSuite suite = new TestSuite( THIS_CLASS );
55         
56         return suite;
57     }
58     
59     public static void main( String JavaDoc[] args )
60     {
61         String JavaDoc[] name = { THIS_CLASS.getName() };
62         
63         // junit.textui.TestRunner.main( name );
64
// junit.swingui.TestRunner.main( name );
65

66         junit.textui.TestRunner.main( name );
67     }
68     
69     protected void setUp() throws Exception JavaDoc
70     {
71         super.setUp();
72         
73         // set ourself up
74
}
75     
76     
77     protected void tearDown() throws Exception JavaDoc
78     {
79         // tear ourself down
80

81         super.tearDown();
82     }
83     
84     
85     public void testInstantiate()
86     {
87         new PathRegistry( '.', true );
88         new PathRegistry( '.', false );
89     }
90     
91     
92     public void testAddCaseSensitiveNotRecursive()
93             throws PathAlreadyRegisteredException
94     {
95         PathRegistry pr = new PathRegistry( '/', true );
96
97         // now try to get something not added
98
notRegisteredTest( pr, "root" );
99
100         Object JavaDoc o1 = new Object JavaDoc();
101         pr.register( "root", o1, false, true );
102         registeredRightTest( pr, "root", o1 );
103         Object JavaDoc o2;
104         
105         // now try to get the path with wrong case
106
caseNotRegisteredTest( pr, "root", '/' );
107         
108         // now try to get a sub path
109
notRegisteredTest( pr, "root/empty" );
110         
111         // now try to get something not added
112
notRegisteredTest( pr, "unknown" );
113
114         // now try to get something not added
115
notRegisteredTest( pr, "/unknown" );
116         
117         // now add a sub-path
118
notRegisteredTest( pr, "root/deep" );
119         Object JavaDoc o1a = new Object JavaDoc();
120         pr.register( "root/deep", o1a, false, true );
121         registeredRightTest( pr, "root/deep", o1a );
122         
123         // now try to get the path with wrong case
124
caseNotRegisteredTest( pr, "root/deep", '/' );
125         
126         // now try to get a bad sub path
127
notRegisteredTest( pr, "root/empty" );
128         notRegisteredTest( pr, "root/deep/empty" );
129         
130         // now try to get something not added
131
notRegisteredTest( pr, "unknown" );
132     }
133     
134     
135     public void testAddTwiceException()
136             throws PathAlreadyRegisteredException
137     {
138         PathRegistry pr = new PathRegistry( '/', true );
139         Object JavaDoc o1 = new Object JavaDoc();
140         notRegisteredTest( pr, "root" );
141
142         pr.register( "root", o1, false, true );
143         registeredRightTest( pr, "root", o1 );
144         
145         // now try to register the same path again
146
alreadyRegisteredTest( pr, "root" );
147     }
148     
149     
150     public void testAddCaseInsensitiveNotRecursive()
151             throws PathAlreadyRegisteredException
152     {
153         PathRegistry pr = new PathRegistry( '/', true );
154         notRegisteredTest( pr, "root" );
155
156         Object JavaDoc o1 = new Object JavaDoc();
157         pr.register( "root", o1, false, false );
158
159         // now try to get with variations
160
caseRegisteredRightTest( pr, "root", o1, '/' );
161         
162         // now try to get a sub path
163
caseNotRegisteredTest( pr, "root/unknown", '/' );
164     }
165     
166     
167     public void testAddTwiceExceptionCaseInsensitive()
168             throws PathAlreadyRegisteredException
169     {
170         PathRegistry pr = new PathRegistry( '/', true );
171         notRegisteredTest( pr, "root" );
172         Object JavaDoc o1 = new Object JavaDoc();
173         pr.register( "root", o1, false, false );
174         registeredRightTest( pr, "root", o1 );
175         
176         // now try to register the same path again
177
caseAlreadyRegisteredTest( pr, "root", '/' );
178     }
179     
180     
181     public void testAddCaseSensitiveRecursive()
182             throws PathAlreadyRegisteredException
183     {
184         PathRegistry pr = new PathRegistry( '/', true );
185         notRegisteredTest( pr, "root" );
186         Object JavaDoc o1 = new Object JavaDoc();
187         pr.register( "root", o1, true, true );
188         registeredRightTest( pr, "root", o1 );
189         
190         
191         // now try to get the path with wrong case
192
caseNotRegisteredTest( pr, "root", '/' );
193         
194         // now try to get a sub path
195
registeredRightTest( pr, "root/empty", o1 );
196         
197         // now try to get something not added
198
notRegisteredTest( pr, "unknown" );
199         
200         // now try to get something not added
201
notRegisteredTest( pr, "unknown/subpath" );
202     }
203     
204     
205     public void testAddTwiceExceptionRecursive()
206             throws PathAlreadyRegisteredException
207     {
208         PathRegistry pr = new PathRegistry( '/', true );
209         notRegisteredTest( pr, "root" );
210         Object JavaDoc o1 = new Object JavaDoc();
211         pr.register( "root", o1, true, true );
212         registeredRightTest( pr, "root", o1 );
213         
214         alreadyRegisteredTest( pr, "root" );
215         alreadyRegisteredTest( pr, "root/empty" );
216         alreadyRegisteredTest( pr, "root/whatever" );
217         alreadyRegisteredTest( pr, "root/empty/whatever" );
218     }
219
220     
221     public void testRemoveCaseSensitiveNotResursive()
222             throws PathAlreadyRegisteredException,
223             NoRegisteredComponentException
224     {
225         PathRegistry pr = new PathRegistry( '/', true );
226         notRegisteredTest( pr, "root" );
227         Object JavaDoc o1 = new Object JavaDoc();
228         pr.register( "root", o1, true, true );
229         registeredRightTest( pr, "root", o1 );
230         
231         cantRemoveEntryTest( pr, "unknown" );
232         
233         caseCantRemoveEntryTest( pr, "root", '/' );
234         cantRemoveEntryTest( pr, "root/empty" );
235         
236         pr.remove( "root" );
237         
238         cantRemoveEntryTest( pr, "root" );
239     }
240     
241     
242     
243     //----------------------------------------------------------
244
// Protected helper methods
245

246     
247     /**
248      * Assert that the given path has nothing registered to it.
249      */

250     protected void notRegisteredTest( PathRegistry pr, String JavaDoc path )
251     {
252         Object JavaDoc o = pr.get( path );
253         assertTrue( "Must return null on bad path and sub-path '"+path+
254             "', but found ["+o+"].", o == null );
255     }
256     
257     
258     /**
259      * Assert that the given path's case variations are not registered
260      * (however, the original path may be registered).
261      */

262     protected void caseNotRegisteredTest( PathRegistry pr, String JavaDoc path,
263             char separator )
264     {
265         String JavaDoc[] variations = getCaseVariations( path, separator );
266         for (int i = 0; i < variations.length; i++)
267         {
268             if (!path.equals( variations[i] ))
269             {
270                 notRegisteredTest( pr, variations[i] );
271             }
272         }
273     }
274     
275     
276     /**
277      * Assert that the given path is registered to the given object.
278      */

279     protected void registeredRightTest( PathRegistry pr, String JavaDoc path,
280             Object JavaDoc expectedObj )
281     {
282         Object JavaDoc o2 = pr.get( path );
283         assertNotNull( "Path must be registered.", o2 );
284         assertEquals( "What gets put in, needs to equal what gets put out.",
285             expectedObj, o2 );
286     }
287    
288    
289     /**
290      * Assert that the given path and its case variations are registered/
291      */

292     protected void caseRegisteredRightTest( PathRegistry pr, String JavaDoc path,
293             Object JavaDoc expectedObj, char separator )
294     {
295         registeredRightTest( pr, path, expectedObj );
296         String JavaDoc[] variations = getCaseVariations( path, separator );
297         for (int i = 0; i < variations.length; i++)
298         {
299             registeredRightTest( pr, variations[i], expectedObj );
300         }
301     }
302     
303     
304     /**
305      * Attempt to register an object at the given path. The test succeeds
306      * if it is already registered, and fails if it does not throw an
307      * exception.
308      */

309     protected void alreadyRegisteredTest( PathRegistry pr, String JavaDoc path )
310     {
311         try
312         {
313             pr.register( path, new Object JavaDoc(), false, false );
314         }
315         catch (PathAlreadyRegisteredException pare)
316         {
317             // good check!
318
return;
319         }
320         fail( "PathRegistry should have thrown an exception" );
321     }
322     
323     
324     /**
325      * Pass if the correct exception is thrown when trying to remove
326      * an entry that doesn't exist.
327      */

328     protected void cantRemoveEntryTest( PathRegistry pr, String JavaDoc path )
329     {
330         try
331         {
332             pr.remove( path );
333         }
334         catch (NoRegisteredComponentException nrce)
335         {
336             // good check!
337
return;
338         }
339         fail( "PathRegistry should have thrown an exception" );
340     }
341     
342     
343     protected void caseCantRemoveEntryTest( PathRegistry pr, String JavaDoc path,
344             char separator )
345     {
346         String JavaDoc[] variations = getCaseVariations( path, separator );
347         for (int i = 0; i < variations.length; i++)
348         {
349             if (!path.equals( variations[i] ))
350             {
351                 cantRemoveEntryTest( pr, variations[i] );
352             }
353         }
354     }
355     
356     /**
357      * Attempt to register the given path and its case variations.
358      */

359     protected void caseAlreadyRegisteredTest( PathRegistry pr, String JavaDoc path,
360             char separator )
361     {
362         alreadyRegisteredTest( pr, path );
363         String JavaDoc[] variations = getCaseVariations( path, separator );
364         for (int i = 0; i < variations.length; i++)
365         {
366             alreadyRegisteredTest( pr, variations[i] );
367         }
368     }
369     
370     
371     private static java.util.Random JavaDoc s_rand = new java.util.Random JavaDoc();
372     private static synchronized int nextInt( int n )
373     {
374         // almost identical to JDK 1.2's Random.nextInt( n )
375
if (n <= 0) throw new IllegalArgumentException JavaDoc("n must be > 0");
376         
377         if ((n & -n) == n) // n is a power of 2
378
{
379             // mask by 7fff... to force it to be positive
380
return (int)((n * ((long)s_rand.nextInt() & 0x7fffffffL) ) >> 31);
381         }
382         
383         int bits, val;
384         do {
385             // mask by 7fff... to force it to be positive
386
bits = s_rand.nextInt() & 0x7fffffff;
387             val = bits % n;
388         } while(bits - val + (n-1) < 0);
389         return val;
390     }
391     
392     /**
393      * Get a set of variations in case upper / lower of the given
394      * path.
395      */

396     protected String JavaDoc[] getCaseVariations( String JavaDoc orig, char separator )
397     {
398         orig = orig.toLowerCase();
399         char buff[] = orig.toCharArray();
400         int len = buff.length;
401         if (len <= 0)
402         {
403             return new String JavaDoc[0];
404         }
405         if (len == 1)
406         {
407             return new String JavaDoc[] { orig, orig.toUpperCase() };
408         }
409         
410         java.util.Vector JavaDoc var = new java.util.Vector JavaDoc();
411         for (int i = 0; i < len; i++)
412         {
413             int pos1 = nextInt( len );
414             int pos2 = nextInt( len );
415             char origChar1 = buff[ pos1 ];
416             char origChar2 = buff[ pos2 ];
417             if (origChar1 != separator)
418                 buff[ pos1 ] = Character.toUpperCase( origChar1 );
419             String JavaDoc s = new String JavaDoc( buff );
420             if (!s.equals( orig ))
421                 var.addElement( s );
422             if (origChar2 != separator)
423                 buff[ pos2 ] = Character.toUpperCase( origChar2 );
424             if (!s.equals( orig ))
425                 var.addElement( s );
426             if (origChar1 != separator)
427                 buff[ pos1 ] = Character.toLowerCase( origChar1 );
428             if (!s.equals( orig ))
429                 var.addElement( s );
430             if (origChar2 != separator)
431                 buff[ pos2 ] = Character.toLowerCase( origChar2 );
432             if (!s.equals( orig ))
433                 var.addElement( s );
434             buff[ pos1 ] = origChar1;
435             buff[ pos2 ] = origChar2;
436         }
437         String JavaDoc ss[] = new String JavaDoc[ var.size() ];
438         var.copyInto( ss );
439         return ss;
440     }
441 }
442
Popular Tags