001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: AbstractTestPrefixMapping.java,v 1.29 2008/01/15 15:45:28 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.shared.test;
008:
009: import com.hp.hpl.jena.shared.*;
010: import com.hp.hpl.jena.graph.test.*;
011:
012: import java.util.*;
013:
014: /**
015: Test prefix mappings - subclass this test and override getMapping() to
016: deliver the prefixMapping to be tested.
017:
018: @author kers
019: */
020:
021: public abstract class AbstractTestPrefixMapping extends GraphTestBase {
022: public AbstractTestPrefixMapping(String name) {
023: super (name);
024: }
025:
026: /**
027: Subclasses implement to return a new, empty prefixMapping of their
028: preferred kind.
029: */
030: abstract protected PrefixMapping getMapping();
031:
032: static final String crispURI = "http://crisp.nosuch.net/";
033: static final String ropeURI = "scheme:rope/string#";
034: static final String butterURI = "ftp://ftp.nowhere.at.all/cream#";
035:
036: /**
037: The empty prefix is specifically allowed [for the default namespace].
038: */
039: public void testEmptyPrefix() {
040: PrefixMapping pm = getMapping();
041: pm.setNsPrefix("", crispURI);
042: assertEquals(crispURI, pm.getNsPrefixURI(""));
043: }
044:
045: static final String[] badNames = { "<hello>", "foo:bar",
046: "with a space", "-argument" };
047:
048: /**
049: Test that various illegal names are trapped.
050: */
051: public void testCheckNames() {
052: PrefixMapping ns = getMapping();
053: for (int i = 0; i < badNames.length; i += 1) {
054: String bad = badNames[i];
055: try {
056: ns.setNsPrefix(bad, crispURI);
057: fail("'"
058: + bad
059: + "' is an illegal prefix and should be trapped");
060: } catch (PrefixMapping.IllegalPrefixException e) {
061: pass();
062: }
063: }
064: }
065:
066: public void testNullURITrapped() {
067: try {
068: getMapping().setNsPrefix("xy", null);
069: fail("shouild trap null URI in setNsPrefix");
070: } catch (NullPointerException e) {
071: pass();
072: }
073: }
074:
075: /**
076: test that a PrefixMapping maps names to URIs. The names and URIs are
077: all fully distinct - overlapping names/uris are dealt with in other tests.
078: */
079: public void testPrefixMappingMapping() {
080: String toast = "ftp://ftp.nowhere.not/";
081: assertDiffer("crisp and toast must differ", crispURI, toast);
082: /* */
083: PrefixMapping ns = getMapping();
084: assertEquals("crisp should be unset", null, ns
085: .getNsPrefixURI("crisp"));
086: assertEquals("toast should be unset", null, ns
087: .getNsPrefixURI("toast"));
088: assertEquals("butter should be unset", null, ns
089: .getNsPrefixURI("butter"));
090: /* */
091: ns.setNsPrefix("crisp", crispURI);
092: assertEquals("crisp should be set", crispURI, ns
093: .getNsPrefixURI("crisp"));
094: assertEquals("toast should still be unset", null, ns
095: .getNsPrefixURI("toast"));
096: assertEquals("butter should still be unset", null, ns
097: .getNsPrefixURI("butter"));
098: /* */
099: ns.setNsPrefix("toast", toast);
100: assertEquals("crisp should be set", crispURI, ns
101: .getNsPrefixURI("crisp"));
102: assertEquals("toast should be set", toast, ns
103: .getNsPrefixURI("toast"));
104: assertEquals("butter should still be unset", null, ns
105: .getNsPrefixURI("butter"));
106: }
107:
108: /**
109: Test that we can run the prefix mapping in reverse - from URIs to prefixes.
110: uriB is a prefix of uriA to try and ensure that the ordering of the map doesn't matter.
111: */
112: public void testReversePrefixMapping() {
113: PrefixMapping ns = getMapping();
114: String uriA = "http://jena.hpl.hp.com/A#", uriB = "http://jena.hpl.hp.com/";
115: String uriC = "http://jena.hpl.hp.com/Csharp/";
116: String prefixA = "aa", prefixB = "bb";
117: ns.setNsPrefix(prefixA, uriA).setNsPrefix(prefixB, uriB);
118: assertEquals(null, ns.getNsURIPrefix(uriC));
119: assertEquals(prefixA, ns.getNsURIPrefix(uriA));
120: assertEquals(prefixB, ns.getNsURIPrefix(uriB));
121: }
122:
123: /**
124: test that we can extract a proper Map from a PrefixMapping
125: */
126: public void testPrefixMappingMap() {
127: PrefixMapping ns = getCrispyRope();
128: Map map = ns.getNsPrefixMap();
129: assertEquals("map should have two elements", 2, map.size());
130: assertEquals(crispURI, map.get("crisp"));
131: assertEquals("scheme:rope/string#", map.get("rope"));
132: }
133:
134: /**
135: test that the Map returned by getNsPrefixMap does not alias (parts of)
136: the secret internal map of the PrefixMapping
137: */
138: public void testPrefixMappingSecret() {
139: PrefixMapping ns = getCrispyRope();
140: Map map = ns.getNsPrefixMap();
141: /* */
142: map.put("crisp", "with/onions");
143: map.put("sandwich", "with/cheese");
144: assertEquals(crispURI, ns.getNsPrefixURI("crisp"));
145: assertEquals(ropeURI, ns.getNsPrefixURI("rope"));
146: assertEquals(null, ns.getNsPrefixURI("sandwich"));
147: }
148:
149: private PrefixMapping getCrispyRope() {
150: PrefixMapping ns = getMapping();
151: ns.setNsPrefix("crisp", crispURI);
152: ns.setNsPrefix("rope", ropeURI);
153: return ns;
154: }
155:
156: /**
157: these are strings that should not change when they are prefix-expanded
158: with crisp and rope as legal prefixes.
159: */
160: static final String[] dontChange = { "",
161: "http://www.somedomain.something/whatever#",
162: "crispy:cabbage", "cris:isOnInfiniteEarths",
163: "rop:tangled/web", "roped:abseiling" };
164:
165: /**
166: these are the required mappings which the test cases below should
167: satisfy: an array of 2-arrays, where element 0 is the string to expand
168: and element 1 is the string it should expand to.
169: */
170: static final String[][] expansions = {
171: { "crisp:pathPart", crispURI + "pathPart" },
172: { "rope:partPath", ropeURI + "partPath" },
173: { "crisp:path:part", crispURI + "path:part" }, };
174:
175: public void testExpandPrefix() {
176: PrefixMapping ns = getMapping();
177: ns.setNsPrefix("crisp", crispURI);
178: ns.setNsPrefix("rope", ropeURI);
179: /* */
180: for (int i = 0; i < dontChange.length; i += 1)
181: assertEquals("should be unchanged", dontChange[i], ns
182: .expandPrefix(dontChange[i]));
183: /* */
184: for (int i = 0; i < expansions.length; i += 1)
185: assertEquals("should expand correctly", expansions[i][1],
186: ns.expandPrefix(expansions[i][0]));
187: }
188:
189: public void testUseEasyPrefix() {
190: testUseEasyPrefix("prefix mapping impl", getMapping());
191: testShortForm("prefix mapping impl", getMapping());
192: }
193:
194: public static void testUseEasyPrefix(String title, PrefixMapping ns) {
195: testShortForm(title, ns);
196: }
197:
198: public static void testShortForm(String title, PrefixMapping ns) {
199: ns.setNsPrefix("crisp", crispURI);
200: ns.setNsPrefix("butter", butterURI);
201: assertEquals(title, "", ns.shortForm(""));
202: assertEquals(title, ropeURI, ns.shortForm(ropeURI));
203: assertEquals(title, "crisp:tail", ns.shortForm(crispURI
204: + "tail"));
205: assertEquals(title, "butter:here:we:are", ns
206: .shortForm(butterURI + "here:we:are"));
207: }
208:
209: public void testEasyQName() {
210: PrefixMapping ns = getMapping();
211: String alphaURI = "http://seasonal.song/preamble/";
212: ns.setNsPrefix("alpha", alphaURI);
213: assertEquals("alpha:rowboat", ns.qnameFor(alphaURI + "rowboat"));
214: }
215:
216: public void testNoQNameNoPrefix() {
217: PrefixMapping ns = getMapping();
218: String alphaURI = "http://seasonal.song/preamble/";
219: ns.setNsPrefix("alpha", alphaURI);
220: assertEquals(null, ns.qnameFor("eg:rowboat"));
221: }
222:
223: public void testNoQNameBadLocal() {
224: PrefixMapping ns = getMapping();
225: String alphaURI = "http://seasonal.song/preamble/";
226: ns.setNsPrefix("alpha", alphaURI);
227: assertEquals(null, ns.qnameFor(alphaURI + "12345"));
228: }
229:
230: /**
231: The tests implied by the email where Chris suggested adding qnameFor;
232: shortForm generates illegal qnames but qnameFor does not.
233: */
234: public void testQnameFromEmail() {
235: String uri = "http://some.long.uri/for/a/namespace#";
236: PrefixMapping ns = getMapping();
237: ns.setNsPrefix("x", uri);
238: assertEquals(null, ns.qnameFor(uri));
239: assertEquals(null, ns.qnameFor(uri + "non/fiction"));
240: }
241:
242: /**
243: test that we can add the maplets from another PrefixMapping without
244: losing our own.
245: */
246: public void testAddOtherPrefixMapping() {
247: PrefixMapping a = getMapping();
248: PrefixMapping b = getMapping();
249: assertFalse("must have two diffferent maps", a == b);
250: a.setNsPrefix("crisp", crispURI);
251: a.setNsPrefix("rope", ropeURI);
252: b.setNsPrefix("butter", butterURI);
253: assertEquals(null, b.getNsPrefixURI("crisp"));
254: assertEquals(null, b.getNsPrefixURI("rope"));
255: b.setNsPrefixes(a);
256: checkContainsMapping(b);
257: }
258:
259: private void checkContainsMapping(PrefixMapping b) {
260: assertEquals(crispURI, b.getNsPrefixURI("crisp"));
261: assertEquals(ropeURI, b.getNsPrefixURI("rope"));
262: assertEquals(butterURI, b.getNsPrefixURI("butter"));
263: }
264:
265: /**
266: as for testAddOtherPrefixMapping, except that it's a plain Map
267: we're adding.
268: */
269: public void testAddMap() {
270: PrefixMapping b = getMapping();
271: Map map = new HashMap();
272: map.put("crisp", crispURI);
273: map.put("rope", ropeURI);
274: b.setNsPrefix("butter", butterURI);
275: b.setNsPrefixes(map);
276: checkContainsMapping(b);
277: }
278:
279: public void testAddDefaultMap() {
280: PrefixMapping pm = getMapping();
281: PrefixMapping root = PrefixMapping.Factory.create();
282: pm.setNsPrefix("a", "aPrefix:");
283: pm.setNsPrefix("b", "bPrefix:");
284: root.setNsPrefix("a", "pootle:");
285: root.setNsPrefix("z", "bPrefix:");
286: root.setNsPrefix("c", "cootle:");
287: assertSame(pm, pm.withDefaultMappings(root));
288: assertEquals("aPrefix:", pm.getNsPrefixURI("a"));
289: assertEquals(null, pm.getNsPrefixURI("z"));
290: assertEquals("bPrefix:", pm.getNsPrefixURI("b"));
291: assertEquals("cootle:", pm.getNsPrefixURI("c"));
292: }
293:
294: public void testSecondPrefixRetainsExistingMap() {
295: PrefixMapping A = getMapping();
296: A.setNsPrefix("a", crispURI);
297: A.setNsPrefix("b", crispURI);
298: assertEquals(crispURI, A.getNsPrefixURI("a"));
299: assertEquals(crispURI, A.getNsPrefixURI("b"));
300: }
301:
302: public void testSecondPrefixReplacesReverseMap() {
303: PrefixMapping A = getMapping();
304: A.setNsPrefix("a", crispURI);
305: A.setNsPrefix("b", crispURI);
306: assertEquals("b", A.getNsURIPrefix(crispURI));
307: }
308:
309: public void testSecondPrefixDeletedUncoversPreviousMap() {
310: PrefixMapping A = getMapping();
311: A.setNsPrefix("x", crispURI);
312: A.setNsPrefix("y", crispURI);
313: A.removeNsPrefix("y");
314: assertEquals("x", A.getNsURIPrefix(crispURI));
315: }
316:
317: /**
318: Test that the empty prefix does not wipe an existing prefix for the same URI.
319: */
320: public void testEmptyDoesNotWipeURI() {
321: PrefixMapping pm = getMapping();
322: pm.setNsPrefix("frodo", ropeURI);
323: pm.setNsPrefix("", ropeURI);
324: assertEquals(ropeURI, pm.getNsPrefixURI("frodo"));
325: }
326:
327: /**
328: Test that adding a new prefix mapping for U does not throw away a default
329: mapping for U.
330: */
331: public void testSameURIKeepsDefault() {
332: PrefixMapping A = getMapping();
333: A.setNsPrefix("", crispURI);
334: A.setNsPrefix("crisp", crispURI);
335: assertEquals(crispURI, A.getNsPrefixURI(""));
336: }
337:
338: public void testReturnsSelf() {
339: PrefixMapping A = getMapping();
340: assertSame(A, A.setNsPrefix("crisp", crispURI));
341: assertSame(A, A.setNsPrefixes(A));
342: assertSame(A, A.setNsPrefixes(new HashMap()));
343: assertSame(A, A.removeNsPrefix("rhubarb"));
344: }
345:
346: public void testRemovePrefix() {
347: String hURI = "http://test.remove.prefixes/prefix#";
348: String bURI = "http://other.test.remove.prefixes/prefix#";
349: PrefixMapping A = getMapping();
350: A.setNsPrefix("hr", hURI);
351: A.setNsPrefix("br", bURI);
352: A.removeNsPrefix("hr");
353: assertEquals(null, A.getNsPrefixURI("hr"));
354: assertEquals(bURI, A.getNsPrefixURI("br"));
355: }
356:
357: public void testEquality() {
358: testEquals("");
359: testEquals("", "x=a", false);
360: testEquals("x=a", "", false);
361: testEquals("x=a");
362: testEquals("x=a y=b", "y=b x=a", true);
363: testEquals("x=a x=b", "x=b x=a", false);
364: }
365:
366: protected void testEquals(String S) {
367: testEquals(S, S, true);
368: }
369:
370: protected void testEquals(String S, String T, boolean expected) {
371: testEqualsBase(S, T, expected);
372: testEqualsBase(T, S, expected);
373: }
374:
375: public void testEqualsBase(String S, String T, boolean expected) {
376: testEquals(S, T, expected, getMapping(), getMapping());
377: testEquals(S, T, expected, PrefixMapping.Factory.create(),
378: getMapping());
379: }
380:
381: protected void testEquals(String S, String T, boolean expected,
382: PrefixMapping A, PrefixMapping B) {
383: fill(A, S);
384: fill(B, T);
385: String title = "usual: '" + S + "', testing: '" + T
386: + "', should be " + (expected ? "equal" : "different");
387: assertEquals(title, expected, A.samePrefixMappingAs(B));
388: assertEquals(title, expected, B.samePrefixMappingAs(A));
389: }
390:
391: protected void fill(PrefixMapping pm, String settings) {
392: List L = listOfStrings(settings);
393: for (int i = 0; i < L.size(); i += 1) {
394: String setting = (String) L.get(i);
395: int eq = setting.indexOf('=');
396: pm.setNsPrefix(setting.substring(0, eq), setting
397: .substring(eq + 1));
398: }
399: }
400:
401: public void testAllowNastyNamespace() { // we now allow namespaces to end with non-punctuational characters
402: getMapping().setNsPrefix("abc", "def");
403: }
404:
405: public void testLock() {
406: PrefixMapping A = getMapping();
407: assertSame(A, A.lock());
408: /* */
409: try {
410: A.setNsPrefix("crisp", crispURI);
411: fail("mapping should be frozen");
412: } catch (PrefixMapping.JenaLockedException e) {
413: pass();
414: }
415: /* */
416: try {
417: A.setNsPrefixes(A);
418: fail("mapping should be frozen");
419: } catch (PrefixMapping.JenaLockedException e) {
420: pass();
421: }
422: /* */
423: try {
424: A.setNsPrefixes(new HashMap());
425: fail("mapping should be frozen");
426: } catch (PrefixMapping.JenaLockedException e) {
427: pass();
428: }
429: /* */
430: try {
431: A.removeNsPrefix("toast");
432: fail("mapping should be frozen");
433: } catch (PrefixMapping.JenaLockedException e) {
434: pass();
435: }
436: }
437: }
438:
439: /*
440: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
441: All rights reserved.
442:
443: Redistribution and use in source and binary forms, with or without
444: modification, are permitted provided that the following conditions
445: are met:
446:
447: 1. Redistributions of source code must retain the above copyright
448: notice, this list of conditions and the following disclaimer.
449:
450: 2. Redistributions in binary form must reproduce the above copyright
451: notice, this list of conditions and the following disclaimer in the
452: documentation and/or other materials provided with the distribution.
453:
454: 3. The name of the author may not be used to endorse or promote products
455: derived from this software without specific prior written permission.
456:
457: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
458: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
459: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
460: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
461: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
462: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
463: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
464: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
465: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
466: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
467: */
|