001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestNamespace.java,v 1.14 2008/01/02 12:04:43 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.test;
008:
009: import com.hp.hpl.jena.rdf.model.*;
010: import com.hp.hpl.jena.rdf.model.impl.*;
011: import com.hp.hpl.jena.shared.test.*;
012: import com.hp.hpl.jena.util.CollectionFactory;
013:
014: import java.util.*;
015: import java.io.*;
016:
017: import junit.framework.*;
018:
019: /**
020: @author kers
021: */
022: public class TestNamespace extends ModelTestBase {
023: public TestNamespace(String name) {
024: super (name);
025: }
026:
027: public static TestSuite suite() {
028: return new TestSuite(TestNamespace.class);
029: }
030:
031: /**
032: a simple test of the prefix reader on a known file. test0014.rdf is known to
033: have a namespace definition for eg and rdf, and not for spoo so we see if we
034: can extract them (or not, for spoo).
035: */
036: public void testReadPrefixes() {
037: Model m = ModelFactory.createDefaultModel();
038: m.read("file:testing/wg/rdf-ns-prefix-confusion/test0014.rdf");
039: Map ns = m.getNsPrefixMap();
040: // System.err.println( ">> " + ns );
041: assertEquals("namespace eg", "http://example.org/", ns
042: .get("eg"));
043: assertEquals("namespace rdf",
044: "http://www.w3.org/1999/02/22-rdf-syntax-ns#", ns
045: .get("rdf"));
046: assertEquals("not present", null, ns.get("spoo"));
047: }
048:
049: /**
050: a horridly written test to write out a model with some known namespace
051: prefixes and see if they can be read back in again.
052:
053: TODO tidy and abstract this - we want some more tests.
054:
055: TODO there's a problem: namespaces that aren't used on properties
056: don't reliably get used. Maybe they shouldn't be - but it seems odd.
057: */
058: public void testWritePrefixes() throws IOException {
059: Model m = ModelFactory.createDefaultModel();
060: ModelCom
061: .addNamespaces(
062: m,
063: makePrefixes("fred=ftp://net.fred.org/;spoo=http://spoo.net/"));
064: File f = File.createTempFile("hedgehog", ".rdf");
065: m
066: .add(statement(m,
067: "http://spoo.net/S http://spoo.net/P http://spoo.net/O"));
068: m
069: .add(statement(m,
070: "http://spoo.net/S ftp://net.fred.org/P http://spoo.net/O"));
071: m.write(new FileOutputStream(f));
072: /* */
073: Model m2 = ModelFactory.createDefaultModel();
074: m2.read("file:" + f.getAbsolutePath());
075: Map ns = m2.getNsPrefixMap();
076: assertEquals("namespace spoo", "http://spoo.net/", ns
077: .get("spoo"));
078: assertEquals("namespace fred", "ftp://net.fred.org/", ns
079: .get("fred"));
080: /* */
081: f.deleteOnExit();
082: }
083:
084: /**
085: turn a semi-separated set of P=U definitions into a namespace map.
086: */
087: private Map makePrefixes(String prefixes) {
088: Map result = new HashMap();
089: StringTokenizer st = new StringTokenizer(prefixes, ";");
090: while (st.hasMoreTokens()) {
091: String def = st.nextToken();
092: // System.err.println( "| def is " + def );
093: int eq = def.indexOf('=');
094: result
095: .put(def.substring(0, eq), set(def
096: .substring(eq + 1)));
097: }
098: // result.put( "spoo", set( "http://spoo.net/" ) );
099: return result;
100: }
101:
102: /**
103: make a single-element set.
104: @param element the single element to contain
105: @return a set whose only element == element
106: */
107: private Set set(String element) {
108: Set s = CollectionFactory.createHashedSet();
109: s.add(element);
110: return s;
111: }
112:
113: public void testUseEasyPrefix() {
114: TestPrefixMapping.testUseEasyPrefix("default model",
115: ModelFactory.createDefaultModel());
116: }
117:
118: }
119:
120: /*
121: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
122: All rights reserved.
123:
124: Redistribution and use in source and binary forms, with or without
125: modification, are permitted provided that the following conditions
126: are met:
127:
128: 1. Redistributions of source code must retain the above copyright
129: notice, this list of conditions and the following disclaimer.
130:
131: 2. Redistributions in binary form must reproduce the above copyright
132: notice, this list of conditions and the following disclaimer in the
133: documentation and/or other materials provided with the distribution.
134:
135: 3. The name of the author may not be used to endorse or promote products
136: derived from this software without specific prior written permission.
137:
138: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
139: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
140: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
141: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
142: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
143: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
144: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
145: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
146: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
147: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
148: */
|