KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > test > TestNamespace


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestNamespace.java,v 1.10 2005/02/21 12:15:16 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model.test;
8
9 import com.hp.hpl.jena.rdf.model.*;
10 import com.hp.hpl.jena.rdf.model.impl.*;
11 import com.hp.hpl.jena.shared.test.*;
12 import com.hp.hpl.jena.util.CollectionFactory;
13
14 import java.util.*;
15 import java.io.*;
16
17 import junit.framework.*;
18
19 /**
20     @author kers
21 */

22 public class TestNamespace extends ModelTestBase
23     {
24     public TestNamespace( String JavaDoc name )
25         { super( name ); }
26
27     public static TestSuite suite()
28          { return new TestSuite( TestNamespace.class ); }
29
30     /**
31         a simple test of the prefix reader on a known file. test0014.rdf is known to
32         have a namespace definition for eg and rdf, and not for spoo so we see if we
33         can extract them (or not, for spoo).
34     */

35     public void testReadPrefixes()
36         {
37         Model m = ModelFactory.createDefaultModel();
38         m.read( "file:testing/wg/rdf-ns-prefix-confusion/test0014.rdf" );
39         Map ns = m.getNsPrefixMap();
40         // System.err.println( ">> " + ns );
41
assertEquals( "namespace eg", "http://example.org/", ns.get( "eg" ) );
42         assertEquals( "namespace rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#", ns.get( "rdf" ) );
43         assertEquals( "not present", null, ns.get( "spoo" ) );
44         }
45         
46     /**
47         a horridly written test to write out a model with some known namespace
48         prefixes and see if they can be read back in again.
49         
50         TODO tidy and abstract this - we want some more tests.
51         
52         TODO there's a problem: namespaces that aren't used on properties
53         don't reliably get used. Maybe they shouldn't be - but it seems odd.
54     */

55     public void testWritePrefixes() throws IOException
56         {
57         Model m = ModelFactory.createDefaultModel();
58         ModelCom.addNamespaces( m, makePrefixes( "fred=ftp://net.fred.org/;spoo=http://spoo.net/" ) );
59         File f = File.createTempFile( "hedgehog", ".rdf" );
60         m.add( statement( m, "http://spoo.net/S http://spoo.net/P http://spoo.net/O" ) );
61         m.add( statement( m, "http://spoo.net/S ftp://net.fred.org/P http://spoo.net/O" ) );
62         m.write( new FileWriter( f ) );
63     /* */
64         Model m2 = ModelFactory.createDefaultModel();
65         m2.read( "file:" + f.getAbsolutePath() );
66         Map ns = m2.getNsPrefixMap();
67         assertEquals( "namespace spoo", "http://spoo.net/", ns.get( "spoo" ) );
68         assertEquals( "namespace fred", "ftp://net.fred.org/", ns.get( "fred" ) );
69     /* */
70         f.deleteOnExit();
71         }
72     
73     /**
74         turn a semi-separated set of P=U definitions into a namespace map.
75     */

76     private Map makePrefixes( String JavaDoc prefixes )
77         {
78         Map result = new HashMap();
79         StringTokenizer st = new StringTokenizer( prefixes, ";" );
80         while (st.hasMoreTokens())
81             {
82             String JavaDoc def = st.nextToken();
83             // System.err.println( "| def is " + def );
84
int eq = def.indexOf( '=' );
85             result.put( def.substring( 0, eq ), set( def.substring( eq + 1 ) ) );
86             }
87         // result.put( "spoo", set( "http://spoo.net/" ) );
88
return result;
89         }
90         
91     /**
92         make a single-element set.
93         @param element the single element to contain
94         @return a set whose only element == element
95     */

96     private Set set( String JavaDoc element )
97         {
98         Set s = CollectionFactory.createHashedSet();
99         s.add( element );
100         return s;
101         }
102         
103     public void testUseEasyPrefix()
104         {
105         TestPrefixMapping.testUseEasyPrefix
106             ( "default model", ModelFactory.createDefaultModel() );
107         }
108
109     }
110
111
112 /*
113     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
114     All rights reserved.
115
116     Redistribution and use in source and binary forms, with or without
117     modification, are permitted provided that the following conditions
118     are met:
119
120     1. Redistributions of source code must retain the above copyright
121        notice, this list of conditions and the following disclaimer.
122
123     2. Redistributions in binary form must reproduce the above copyright
124        notice, this list of conditions and the following disclaimer in the
125        documentation and/or other materials provided with the distribution.
126
127     3. The name of the author may not be used to endorse or promote products
128        derived from this software without specific prior written permission.
129
130     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
131     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
132     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
133     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
134     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
135     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
136     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
137     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
138     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
139     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
140 */
Popular Tags