KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > shared > test > AbstractTestPrefixMapping


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: AbstractTestPrefixMapping.java,v 1.20 2005/03/18 13:56:44 chris-dollin Exp $
5 */

6
7 package com.hp.hpl.jena.shared.test;
8
9 import com.hp.hpl.jena.shared.*;
10 import com.hp.hpl.jena.shared.impl.PrefixMappingImpl;
11 import com.hp.hpl.jena.graph.test.*;
12
13 import java.util.*;
14
15 /**
16     Test prefix mappings - subclass this test and override getMapping() to
17     deliver the prefixMapping to be tested.
18     
19     @author kers
20 */

21
22 public abstract class AbstractTestPrefixMapping extends GraphTestBase
23     {
24     public AbstractTestPrefixMapping( String JavaDoc name )
25          { super( name ); }
26
27     abstract protected PrefixMapping getMapping();
28         
29     static final String JavaDoc crispURI = "http://crisp.nosuch.net/";
30     static final String JavaDoc ropeURI = "scheme:rope/string#";
31     static final String JavaDoc butterURI = "ftp://ftp.nowhere.at.all/cream#";
32         
33     /**
34         The empty prefix is specifically allowed [for the default namespace].
35     */

36     public void testEmptyPrefix()
37         {
38         PrefixMapping pm = getMapping();
39         pm.setNsPrefix( "", crispURI );
40         assertEquals( crispURI, pm.getNsPrefixURI( "" ) );
41         }
42
43     static final String JavaDoc [] badNames =
44         {
45         "<hello>",
46         "foo:bar",
47         "with a space",
48         "-argument"
49         };
50     
51     /**
52         Test that various illegal names are trapped.
53     */

54     public void testCheckNames()
55         {
56         PrefixMapping ns = getMapping();
57         for (int i = 0; i < badNames.length; i += 1)
58             {
59             String JavaDoc bad = badNames[i];
60             try
61                 {
62                 ns.setNsPrefix( bad, crispURI );
63                 fail( "'" + bad + "' is an illegal prefix and should be trapped" );
64                 }
65             catch (PrefixMapping.IllegalPrefixException e) { pass(); }
66             }
67         }
68                  
69     /**
70         test that a PrefixMapping maps names to URIs. The names and URIs are
71         all fully distinct - overlapping names/uris are dealt with in other tests.
72     */

73     public void testPrefixMappingMapping()
74         {
75         String JavaDoc toast = "ftp://ftp.nowhere.not/";
76         assertDiffer( "crisp and toast must differ", crispURI, toast );
77     /* */
78         PrefixMapping ns = getMapping();
79         assertEquals( "crisp should be unset", null, ns.getNsPrefixURI( "crisp" ) );
80         assertEquals( "toast should be unset", null, ns.getNsPrefixURI( "toast" ) );
81         assertEquals( "butter should be unset", null, ns.getNsPrefixURI( "butter" ) );
82     /* */
83         ns.setNsPrefix( "crisp", crispURI );
84         assertEquals( "crisp should be set", crispURI, ns.getNsPrefixURI( "crisp" ) );
85         assertEquals( "toast should still be unset", null, ns.getNsPrefixURI( "toast" ) );
86         assertEquals( "butter should still be unset", null, ns.getNsPrefixURI( "butter" ) );
87     /* */
88         ns.setNsPrefix( "toast", toast );
89         assertEquals( "crisp should be set", crispURI, ns.getNsPrefixURI( "crisp" ) );
90         assertEquals( "toast should be set", toast, ns.getNsPrefixURI( "toast" ) );
91         assertEquals( "butter should still be unset", null, ns.getNsPrefixURI( "butter" ) );
92         }
93         
94     /**
95         Test that we can run the prefix mapping in reverse - from URIs to prefixes.
96         uriB is a prefix of uriA to try and ensure that the ordering of the map doesn't matter.
97     */

98     public void testReversePrefixMapping()
99         {
100         PrefixMapping ns = getMapping();
101         String JavaDoc uriA = "http://jena.hpl.hp.com/A#", uriB = "http://jena.hpl.hp.com/";
102         String JavaDoc uriC = "http://jena.hpl.hp.com/Csharp/";
103         String JavaDoc prefixA = "aa", prefixB = "bb";
104         ns.setNsPrefix( prefixA, uriA ).setNsPrefix( prefixB, uriB );
105         assertEquals( null, ns.getNsURIPrefix( uriC) );
106         assertEquals( prefixA, ns.getNsURIPrefix( uriA ) );
107         assertEquals( prefixB, ns.getNsURIPrefix( uriB ) );
108         }
109     
110     /**
111        test that we can extract a proper Map from a PrefixMapping
112     */

113     public void testPrefixMappingMap()
114         {
115         PrefixMapping ns = getCrispyRope();
116         Map map = ns.getNsPrefixMap();
117         assertEquals( "map should have two elements", 2, map.size() );
118         assertEquals( crispURI, map.get( "crisp" ) );
119         assertEquals( "scheme:rope/string#", map.get( "rope" ) );
120         }
121     
122     /**
123        test that the Map returned by getNsPrefixMap does not alias (parts of)
124        the secret internal map of the PrefixMapping
125     */

126     public void testPrefixMappingSecret()
127         {
128         PrefixMapping ns = getCrispyRope();
129         Map map = ns.getNsPrefixMap();
130     /* */
131         map.put( "crisp", "with/onions" );
132         map.put( "sandwich", "with/cheese" );
133         assertEquals( crispURI, ns.getNsPrefixURI( "crisp" ) );
134         assertEquals( ropeURI, ns.getNsPrefixURI( "rope" ) );
135         assertEquals( null, ns.getNsPrefixURI( "sandwich" ) );
136         }
137         
138     private PrefixMapping getCrispyRope()
139         {
140         PrefixMapping ns = getMapping();
141         ns.setNsPrefix( "crisp", crispURI);
142         ns.setNsPrefix( "rope", ropeURI );
143         return ns;
144         }
145     
146     /**
147        these are strings that should not change when they are prefix-expanded
148        with crisp and rope as legal prefixes.
149    */

150    static final String JavaDoc [] dontChange =
151        {
152        "",
153        "http://www.somedomain.something/whatever#",
154        "crispy:cabbage",
155        "cris:isOnInfiniteEarths",
156        "rop:tangled/web",
157        "roped:abseiling"
158        };
159     
160     /**
161        these are the required mappings which the test cases below should
162        satisfy: an array of 2-arrays, where element 0 is the string to expand
163        and element 1 is the string it should expand to.
164    */

165    static final String JavaDoc [][] expansions =
166        {
167            { "crisp:pathPart", crispURI + "pathPart" },
168            { "rope:partPath", ropeURI + "partPath" },
169            { "crisp:path:part", crispURI + "path:part" },
170        };
171        
172    public void testExpandPrefix()
173        {
174        PrefixMapping ns = getMapping();
175        ns.setNsPrefix( "crisp", crispURI );
176        ns.setNsPrefix( "rope", ropeURI );
177    /* */
178        for (int i = 0; i < dontChange.length; i += 1)
179            assertEquals
180                (
181                "should be unchanged",
182                dontChange[i],
183                ns.expandPrefix( dontChange[i] )
184                );
185    /* */
186        for (int i = 0; i < expansions.length; i += 1)
187            assertEquals
188                (
189                "should expand correctly",
190                expansions[i][1],
191                ns.expandPrefix( expansions[i][0] )
192                );
193        }
194     
195     public void testUseEasyPrefix()
196        {
197        testUseEasyPrefix( "prefix mapping impl", getMapping() );
198        testShortForm( "prefix mapping impl", getMapping() );
199        }
200     
201     public static void testUseEasyPrefix( String JavaDoc title, PrefixMapping ns )
202         {
203         testUsePrefix( title, ns );
204         testShortForm( title, ns );
205         }
206     
207     public static void testUsePrefix( String JavaDoc title, PrefixMapping ns )
208         {
209         ns.setNsPrefix( "crisp", crispURI );
210         ns.setNsPrefix( "butter", butterURI );
211         assertEquals( title, "", ns.usePrefix( "" ) );
212         assertEquals( title, ropeURI, ns.usePrefix( ropeURI ) );
213         assertEquals( title, "crisp:tail", ns.usePrefix( crispURI + "tail" ) );
214         assertEquals( title, "butter:here:we:are", ns.usePrefix( butterURI + "here:we:are" ) );
215         }
216             
217     public static void testShortForm( String JavaDoc title, PrefixMapping ns )
218         {
219         ns.setNsPrefix( "crisp", crispURI );
220         ns.setNsPrefix( "butter", butterURI );
221         assertEquals( title, "", ns.shortForm( "" ) );
222         assertEquals( title, ropeURI, ns.shortForm( ropeURI ) );
223         assertEquals( title, "crisp:tail", ns.shortForm( crispURI + "tail" ) );
224         assertEquals( title, "butter:here:we:are", ns.shortForm( butterURI + "here:we:are" ) );
225         }
226     
227     public void testEasyQName()
228         {
229         PrefixMapping ns = getMapping();
230         String JavaDoc alphaURI = "http://seasonal.song/preamble/";
231         ns.setNsPrefix( "alpha", alphaURI );
232         assertEquals( "alpha:rowboat", ns.qnameFor( alphaURI + "rowboat" ) );
233         }
234     
235     public void testNoQNameNoPrefix()
236         {
237         PrefixMapping ns = getMapping();
238         String JavaDoc alphaURI = "http://seasonal.song/preamble/";
239         ns.setNsPrefix( "alpha", alphaURI );
240         assertEquals( null, ns.qnameFor( "eg:rowboat" ) );
241         }
242     
243     public void testNoQNameBadLocal()
244         {
245         PrefixMapping ns = getMapping();
246         String JavaDoc alphaURI = "http://seasonal.song/preamble/";
247         ns.setNsPrefix( "alpha", alphaURI );
248         assertEquals( null, ns.qnameFor( alphaURI + "12345" ) );
249         }
250     
251     /**
252         The tests implied by the email where Chris suggested adding qnameFor;
253         shortForm generates illegal qnames but qnameFor does not.
254     */

255     public void testQnameFromEmail()
256         {
257         String JavaDoc uri = "http://some.long.uri/for/a/namespace#";
258         PrefixMapping ns = getMapping();
259         ns.setNsPrefix( "x", uri );
260         assertEquals( null, ns.qnameFor( uri ) );
261         assertEquals( null, ns.qnameFor( uri + "non/fiction" ) );
262         }
263
264         
265     /**
266         test that we can add the maplets from another PrefixMapping without
267         losing our own.
268     */

269     public void testAddOtherPrefixMapping()
270         {
271         PrefixMapping a = getMapping();
272         PrefixMapping b = getMapping();
273         assertFalse( "must have two diffferent maps", a == b );
274         a.setNsPrefix( "crisp", crispURI );
275         a.setNsPrefix( "rope", ropeURI );
276         b.setNsPrefix( "butter", butterURI );
277         assertEquals( null, b.getNsPrefixURI( "crisp") );
278         assertEquals( null, b.getNsPrefixURI( "rope") );
279         b.setNsPrefixes( a );
280         checkContainsMapping( b );
281         }
282         
283     private void checkContainsMapping( PrefixMapping b )
284         {
285         assertEquals( crispURI, b.getNsPrefixURI( "crisp") );
286         assertEquals( ropeURI, b.getNsPrefixURI( "rope") );
287         assertEquals( butterURI, b.getNsPrefixURI( "butter") );
288         }
289         
290     /**
291         as for testAddOtherPrefixMapping, except that it's a plain Map
292         we're adding.
293     */

294     public void testAddMap()
295         {
296         PrefixMapping b = getMapping();
297         Map map = new HashMap();
298         map.put( "crisp", crispURI );
299         map.put( "rope", ropeURI );
300         b.setNsPrefix( "butter", butterURI );
301         b.setNsPrefixes( map );
302         checkContainsMapping( b );
303         }
304     
305     public void testAddDefaultMap()
306         {
307         PrefixMapping pm = getMapping();
308         PrefixMapping root = new PrefixMappingImpl();
309         pm.setNsPrefix( "a", "aPrefix:" );
310         pm.setNsPrefix( "b", "bPrefix:" );
311         root.setNsPrefix( "a", "pootle:" );
312         root.setNsPrefix( "z", "bPrefix:" );
313         root.setNsPrefix( "c", "cootle:" );
314         assertSame( pm, pm.withDefaultMappings( root ) );
315         assertEquals( "aPrefix:", pm.getNsPrefixURI( "a" ) );
316         assertEquals( null, pm.getNsPrefixURI( "z" ) );
317         assertEquals( "bPrefix:", pm.getNsPrefixURI( "b" ) );
318         assertEquals( "cootle:", pm.getNsPrefixURI( "c" ) );
319         }
320     
321     
322     public void testSecondPrefixRetainsExistingMap()
323         {
324         PrefixMapping A = getMapping();
325         A.setNsPrefix( "a", crispURI );
326         A.setNsPrefix( "b", crispURI );
327         assertEquals( crispURI, A.getNsPrefixURI( "a" ) );
328         assertEquals( crispURI, A.getNsPrefixURI( "b" ) );
329         }
330     
331     public void testSecondPrefixReplacesReverseMap()
332         {
333         PrefixMapping A = getMapping();
334         A.setNsPrefix( "a", crispURI );
335         A.setNsPrefix( "b", crispURI );
336         assertEquals( "b", A.getNsURIPrefix( crispURI ) );
337         }
338     
339     public void testSecondPrefixDeletedUncoversPreviousMap()
340         {
341         PrefixMapping A = getMapping();
342         A.setNsPrefix( "x", crispURI );
343         A.setNsPrefix( "y", crispURI );
344         A.removeNsPrefix( "y" );
345         assertEquals( "x", A.getNsURIPrefix( crispURI ) );
346         }
347         
348     /**
349         Test that the empty prefix does not wipe an existing prefix for the same URI.
350     */

351     public void testEmptyDoesNotWipeURI()
352         {
353         PrefixMapping pm = getMapping();
354         pm.setNsPrefix( "frodo", ropeURI );
355         pm.setNsPrefix( "", ropeURI );
356         assertEquals( ropeURI, pm.getNsPrefixURI( "frodo" ) );
357         }
358                 
359     /**
360         Test that adding a new prefix mapping for U does not throw away a default
361         mapping for U.
362     */

363     public void testSameURIKeepsDefault()
364         {
365         PrefixMapping A = getMapping();
366         A.setNsPrefix( "", crispURI );
367         A.setNsPrefix( "crisp", crispURI );
368         assertEquals( crispURI, A.getNsPrefixURI( "" ) );
369         }
370         
371     public void testReturnsSelf()
372         {
373         PrefixMapping A = getMapping();
374         assertSame( A, A.setNsPrefix( "crisp", crispURI ) );
375         assertSame( A, A.setNsPrefixes( A ) );
376         assertSame( A, A.setNsPrefixes( new HashMap() ) );
377         assertSame( A, A.removeNsPrefix( "rhubarb" ) );
378         }
379     
380     public void testRemovePrefix()
381         {
382         String JavaDoc hURI = "http://test.remove.prefixes/prefix#";
383         String JavaDoc bURI = "http://other.test.remove.prefixes/prefix#";
384         PrefixMapping A = getMapping();
385         A.setNsPrefix( "hr", hURI );
386         A.setNsPrefix( "br", bURI );
387         A.removeNsPrefix( "hr" );
388         assertEquals( null, A.getNsPrefixURI( "hr" ) );
389         assertEquals( bURI, A.getNsPrefixURI( "br" ) );
390         }
391     
392     public void testAllowNastyNamespace()
393         { // we now allow namespaces to end with non-punctuational characters
394
getMapping().setNsPrefix( "abc", "def" );
395         }
396         
397     public void testLock()
398         {
399         PrefixMapping A = getMapping();
400         assertSame( A, A.lock() );
401     /* */
402         try { A.setNsPrefix( "crisp", crispURI ); fail( "mapping should be frozen" ); }
403         catch (PrefixMapping.JenaLockedException e) { pass(); }
404     /* */
405         try { A.setNsPrefixes( A ); fail( "mapping should be frozen" ); }
406         catch (PrefixMapping.JenaLockedException e) { pass(); }
407     /* */
408         try { A.setNsPrefixes( new HashMap() ); fail( "mapping should be frozen" ); }
409         catch (PrefixMapping.JenaLockedException e) { pass(); }
410     /* */
411         try { A.removeNsPrefix( "toast" ); fail( "mapping should be frozen" ); }
412         catch (PrefixMapping.JenaLockedException e) { pass(); }
413         }
414     }
415
416 /*
417     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
418     All rights reserved.
419
420     Redistribution and use in source and binary forms, with or without
421     modification, are permitted provided that the following conditions
422     are met:
423
424     1. Redistributions of source code must retain the above copyright
425        notice, this list of conditions and the following disclaimer.
426
427     2. Redistributions in binary form must reproduce the above copyright
428        notice, this list of conditions and the following disclaimer in the
429        documentation and/or other materials provided with the distribution.
430
431     3. The name of the author may not be used to endorse or promote products
432        derived from this software without specific prior written permission.
433
434     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
435     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
436     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
437     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
438     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
439     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
440     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
441     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
442     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
443     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
444 */
Popular Tags