1 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 22 public class TestNamespace extends ModelTestBase 23 { 24 public TestNamespace( String name ) 25 { super( name ); } 26 27 public static TestSuite suite() 28 { return new TestSuite( TestNamespace.class ); } 29 30 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 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 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 76 private Map makePrefixes( String prefixes ) 77 { 78 Map result = new HashMap(); 79 StringTokenizer st = new StringTokenizer( prefixes, ";" ); 80 while (st.hasMoreTokens()) 81 { 82 String def = st.nextToken(); 83 int eq = def.indexOf( '=' ); 85 result.put( def.substring( 0, eq ), set( def.substring( eq + 1 ) ) ); 86 } 87 return result; 89 } 90 91 96 private Set set( String 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 | Popular Tags |