001: /*
002: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestNode.java,v 1.48 2008/01/02 12:05:34 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.test;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.graph.impl.*;
011: import com.hp.hpl.jena.rdf.model.AnonId;
012: import com.hp.hpl.jena.rdf.model.impl.Util;
013: import com.hp.hpl.jena.shared.*;
014: import com.hp.hpl.jena.datatypes.*;
015: import com.hp.hpl.jena.vocabulary.*;
016:
017: import junit.framework.*;
018:
019: /**
020: @author bwm out of kers
021: Exercise nodes. Make sure that the different node types do not overlap
022: and that the test predicates work properly on the different node kinds.
023: */
024:
025: public class TestNode extends GraphTestBase {
026: public TestNode(String name) {
027: super (name);
028: }
029:
030: public static TestSuite suite() {
031: return new TestSuite(TestNode.class);
032: }
033:
034: private static final String U = "http://some.domain.name/magic/spells.incant";
035: private static final String N = "Alice";
036: private static final LiteralLabel L = new LiteralLabel(
037: "ashes are burning", "en", false);
038: private static final AnonId A = AnonId.create();
039:
040: public void testBlanks() {
041: assertTrue("anonymous nodes are blank", Node.createAnon()
042: .isBlank());
043: assertFalse("anonymous nodes aren't literal", Node.createAnon()
044: .isLiteral());
045: assertFalse("anonymous nodes aren't URIs", Node.createAnon()
046: .isURI());
047: assertFalse("anonymous nodes aren't variables", Node
048: .createAnon().isVariable());
049: assertEquals("anonymous nodes have the right id", Node
050: .createAnon(A).getBlankNodeId(), A);
051: }
052:
053: public void testLiterals() {
054: assertFalse("literal nodes aren't blank", Node.createLiteral(L)
055: .isBlank());
056: assertTrue("literal nodes are literal", Node.createLiteral(L)
057: .isLiteral());
058: assertFalse("literal nodes aren't variables", Node
059: .createLiteral(L).isVariable());
060: assertFalse("literal nodes aren't URIs", Node.createLiteral(L)
061: .isURI());
062: assertEquals("literal nodes preserve value", Node
063: .createLiteral(L).getLiteral(), L);
064: }
065:
066: public void testURIs() {
067: assertFalse("URI nodes aren't blank", Node.createURI(U)
068: .isBlank());
069: assertFalse("URI nodes aren't literal", Node.createURI(U)
070: .isLiteral());
071: assertFalse("URI nodes aren't variables", Node.createURI(U)
072: .isVariable());
073: assertTrue("URI nodes are URIs", Node.createURI(U).isURI());
074: assertEquals("URI nodes preserve URI", Node.createURI(U)
075: .getURI(), U);
076: }
077:
078: public void testVariables() {
079: assertFalse("variable nodes aren't blank", Node.createVariable(
080: N).isBlank());
081: assertFalse("variable nodes aren't literal", Node
082: .createVariable(N).isLiteral());
083: assertFalse("variable nodes aren't URIs", Node
084: .createVariable(N).isURI());
085: assertTrue("variable nodes are variable", Node
086: .createVariable(N).isVariable());
087: assertEquals("variable nodes keep their name", N, Node
088: .createVariable(N).getName());
089: assertEquals("variable nodes keep their name", N + "x", Node
090: .createVariable(N + "x").getName());
091: }
092:
093: public void testANY() {
094: assertFalse("ANY nodes aren't blank", Node.ANY.isBlank());
095: assertFalse("ANY nodes aren't literals", Node.ANY.isLiteral());
096: assertFalse("ANY nodes aren't URIs", Node.ANY.isURI());
097: assertFalse("ANY nodes aren't variables", Node.ANY.isVariable());
098: assertFalse("ANY nodes aren't blank", Node.ANY.isBlank());
099: assertFalse("ANY nodes aren't blank", Node.ANY.isBlank());
100: }
101:
102: public void testNodeVariableConstructor() {
103: assertEquals(Node.createVariable("hello"), new Node_Variable(
104: "hello"));
105: assertEquals(Node.createVariable("world"), new Node_Variable(
106: "world"));
107: assertDiffer(Node.createVariable("hello"), new Node_Variable(
108: "world"));
109: assertEquals("myName", new Node_Variable("myName").getName());
110: }
111:
112: /**
113: test cases for equality: an array of (Node, String) pairs. [It's not worth
114: making a special class for these pairs.] The nodes are created with caching
115: off, to make sure that caching effects don't hide the effect of using .equals().
116: The strings are "equality groups": the nodes should test equal iff their
117: associated strings test equal.
118: */
119: private Object[][] eqTestCases() {
120: try {
121: Node.cache(false);
122: AnonId id = AnonId.create();
123: LiteralLabel L2 = new LiteralLabel(id.toString(), "", false);
124:
125: LiteralLabel LLang1 = new LiteralLabel("xyz", "en", null);
126: LiteralLabel LLang2 = new LiteralLabel("xyz", "EN", null);
127:
128: String U2 = id.toString();
129: String N2 = id.toString();
130: return new Object[][] { { Node.ANY, "0" },
131: { Node.createAnon(id), "1" },
132: { Node.createAnon(), "2" },
133: { Node.createAnon(id), "1" },
134: { Node.createLiteral(L), "3" },
135:
136: { Node.createLiteral(L2), "4" },
137: { Node.createLiteral(L), "3" },
138: { Node.createURI(U), "5" },
139: { Node.createURI(U2), "6" },
140: { Node.createURI(U), "5" },
141: { Node.createVariable(N), "7" },
142: { Node.createVariable(N2), "8" },
143: { Node.createVariable(N), "7" },
144:
145: { Node.createLiteral(LLang1), "9" },
146: { Node.createLiteral(LLang2), "10" }, };
147: } finally {
148: Node.cache(true);
149: }
150: }
151:
152: public void testNodeEquals() {
153: Object[][] tests = eqTestCases();
154: for (int i = 0; i < tests.length; i += 1) {
155: Object[] I = tests[i];
156: assertFalse(I[0] + " should not equal null", I[0]
157: .equals(null));
158: assertFalse(I[0] + "should not equal 'String'", I[0]
159: .equals("String"));
160: for (int j = 0; j < tests.length; j += 1) {
161: Object[] J = tests[j];
162: testEquality(I[1].equals(J[1]), I[0], J[0]);
163: }
164: }
165: }
166:
167: private void testEquality(boolean testEq, Object x, Object y) {
168: String testName = getType(x) + " " + x + " and " + getType(y)
169: + " " + y;
170: if (testEq)
171: assertEquals(testName + "should be equal", x, y);
172: else
173: assertDiffer(testName + " should differ", x, y);
174: }
175:
176: private String getType(Object x) {
177: String fullName = x.getClass().getName();
178: return fullName.substring(fullName.lastIndexOf('.') + 1);
179: }
180:
181: public void testEquals() {
182: try {
183: Node.cache(false);
184: assertDiffer("different variables", Node
185: .createVariable("xx"), Node.createVariable("yy"));
186: assertEquals("same vars", Node.createVariable("aa"), Node
187: .createVariable("aa"));
188: assertEquals("same URI", Node.createURI(U), Node
189: .createURI(U));
190: assertEquals("same anon", Node.createAnon(A), Node
191: .createAnon(A));
192: assertEquals("same literal", Node.createLiteral(L), Node
193: .createLiteral(L));
194: assertFalse("distinct URIs", Node.createURI(U) == Node
195: .createURI(U));
196: assertFalse("distinct hyphens", Node.createAnon(A) == Node
197: .createAnon(A));
198: assertFalse("distinct literals",
199: Node.createLiteral(L) == Node.createLiteral(L));
200: assertFalse("distinct vars",
201: Node.createVariable("aa") == Node
202: .createVariable("aa"));
203: } finally {
204: Node.cache(true);
205: }
206: }
207:
208: /**
209: test that the label of a Node can be retrieved from that Node in
210: a way appropriate to that Node.
211: */
212: public void testLabels() {
213: AnonId id = AnonId.create();
214: assertEquals("get URI value", U, Node.createURI(U).getURI());
215: assertEquals("get blank value", id, Node.createAnon(id)
216: .getBlankNodeId());
217: assertEquals("get literal value", L, Node.createLiteral(L)
218: .getLiteral());
219: assertEquals("get variable name", N, Node.createVariable(N)
220: .getName());
221: }
222:
223: /**
224: this is where we test that using the wrong accessor on a Node gets you
225: an exception.
226: */
227: public void testFailingLabels() {
228: Node u = Node.createURI(U), b = Node.createAnon();
229: Node l = Node.createLiteral(L), v = Node.createVariable(N);
230: Node a = Node.ANY;
231: /* */
232: testGetURIFails(a);
233: testGetURIFails(b);
234: testGetURIFails(l);
235: testGetURIFails(v);
236: /* */
237: testGetLiteralFails(a);
238: testGetLiteralFails(u);
239: testGetLiteralFails(b);
240: testGetLiteralFails(v);
241: /* */
242: testGetNameFails(a);
243: testGetNameFails(u);
244: testGetNameFails(b);
245: testGetNameFails(l);
246: /* */
247: testGetBlankNodeIdFails(a);
248: testGetBlankNodeIdFails(u);
249: testGetBlankNodeIdFails(l);
250: testGetBlankNodeIdFails(v);
251: }
252:
253: public void testGetBlankNodeIdFails(Node n) {
254: try {
255: n.getBlankNodeId();
256: fail(n.getClass() + " should fail getName()");
257: } catch (UnsupportedOperationException e) {
258: }
259: }
260:
261: public void testGetURIFails(Node n) {
262: try {
263: n.getURI();
264: fail(n.getClass() + " should fail getURI()");
265: } catch (UnsupportedOperationException e) {
266: }
267: }
268:
269: public void testGetNameFails(Node n) {
270: try {
271: n.getName();
272: fail(n.getClass() + " should fail getName()");
273: } catch (UnsupportedOperationException e) {
274: }
275: }
276:
277: public void testGetLiteralFails(Node n) {
278: try {
279: n.getLiteral();
280: fail(n.getClass() + " should fail getLiteral()");
281: } catch (UnsupportedOperationException e) {
282: }
283: }
284:
285: public void testGetBlankNodeLabelString() {
286: Node n = Node.createAnon();
287: assertEquals(n.getBlankNodeId().getLabelString(), n
288: .getBlankNodeLabel());
289: }
290:
291: public void testVariableSupport() {
292: assertEquals(Node_Variable.variable("xxx"), Node_Variable
293: .variable("xxx"));
294: assertDiffer(Node_Variable.variable("xxx"), Node_Variable
295: .variable("yyy"));
296: assertEquals(Node_Variable.variable("aaa"), Node_Variable
297: .variable("aaa"));
298: assertDiffer(Node_Variable.variable("aaa"), Node_Variable
299: .variable("yyy"));
300: }
301:
302: public void testCache() {
303: assertEquals(Node_Variable.variable("xxx"), Node_Variable
304: .variable("xxx"));
305: assertTrue("remembers URI", Node.createURI(U) == Node
306: .createURI(U));
307: assertTrue("remembers literal", Node.createLiteral(L) == Node
308: .createLiteral(L));
309: assertTrue("remembers hyphens", Node.createAnon(A) == Node
310: .createAnon(A));
311: assertTrue("remembers variables",
312: Node.createVariable(N) == Node.createVariable(N));
313: assertFalse("is not confused", Node.createVariable(N) == Node
314: .createURI(N));
315: }
316:
317: /**
318: Test that the create method does sensible things on null and ""
319: */
320: public void testCreateBadString() {
321: try {
322: NodeCreateUtils.create(null);
323: fail("must catch null argument");
324: } catch (NullPointerException e) {
325: } catch (JenaException e) {
326: }
327: try {
328: NodeCreateUtils.create("");
329: fail("must catch empty argument");
330: } catch (JenaException e) {
331: }
332: }
333:
334: /**
335: Test that anonymous nodes are created with the correct labels
336: */
337: public void testCreateAnon() {
338: String idA = "_xxx", idB = "_yyy";
339: Node a = NodeCreateUtils.create(idA), b = NodeCreateUtils
340: .create(idB);
341: assertTrue("both must be bnodes", a.isBlank() && b.isBlank());
342: assertEquals(new AnonId(idA), a.getBlankNodeId());
343: assertEquals(new AnonId(idB), b.getBlankNodeId());
344: }
345:
346: public void testCreateVariable() {
347: String V = "wobbly";
348: Node v = NodeCreateUtils.create("?" + V);
349: assertTrue("must be a variable", v.isVariable());
350: assertEquals("name must be correct", V, v.getName());
351: }
352:
353: public void testCreateANY() {
354: assertEquals("?? must denote ANY", Node.ANY, NodeCreateUtils
355: .create("??"));
356: }
357:
358: public void testCreatePlainLiteralSingleQuotes() {
359: Node n = NodeCreateUtils.create("'xxx'");
360: assertEquals("xxx", n.getLiteralLexicalForm());
361: assertEquals("", n.getLiteralLanguage());
362: assertEquals(null, n.getLiteralDatatypeURI());
363: }
364:
365: public void testCreatePlainLiteralDoubleQuotes() {
366: Node n = NodeCreateUtils.create("\"xxx\"");
367: assertEquals("xxx", n.getLiteralLexicalForm());
368: assertEquals("", n.getLiteralLanguage());
369: assertEquals(null, n.getLiteralDatatypeURI());
370: }
371:
372: public void testCreateLiteralBackslashEscape() {
373: testStringConversion("xx\\x", "'xx\\\\x'");
374: testStringConversion("xx\\x\\y", "'xx\\\\x\\\\y'");
375: testStringConversion("\\xyz\\", "'\\\\xyz\\\\'");
376: }
377:
378: public void testCreateLiteralQuoteEscapes() {
379: testStringConversion("x\'y", "'x\\'y'");
380: testStringConversion("x\"y", "'x\\\"y'");
381: testStringConversion("x\'y\"z", "'x\\\'y\\\"z'");
382: }
383:
384: public void testCreateLiteralOtherEscapes() {
385: testStringConversion(" ", "'\\s'");
386: testStringConversion("\t", "'\\t'");
387: testStringConversion("\n", "'\\n'");
388: }
389:
390: protected void testStringConversion(String wanted, String template) {
391: Node n = NodeCreateUtils.create(template);
392: assertEquals(wanted, n.getLiteralLexicalForm());
393: assertEquals("", n.getLiteralLanguage());
394: assertEquals(null, n.getLiteralDatatypeURI());
395: }
396:
397: public void testCreateLanguagedLiteralEN1() {
398: Node n = NodeCreateUtils.create("'chat'en-UK");
399: assertEquals("chat", n.getLiteralLexicalForm());
400: assertEquals("en-UK", n.getLiteralLanguage());
401: assertEquals(null, n.getLiteralDatatypeURI());
402: }
403:
404: public void testCreateLanguagedLiteralEN2() {
405: Node n1 = NodeCreateUtils.create("'chat'en-UK");
406: Node n2 = NodeCreateUtils.create("'chat'EN-UK");
407: assertTrue(n1.sameValueAs(n2));
408: assertFalse(n1.equals(n2));
409: }
410:
411: public void testCreateLanguagedLiteralXY() {
412: Node n = NodeCreateUtils.create("\"chat\"xy-AB");
413: assertEquals("chat", n.getLiteralLexicalForm());
414: assertEquals("xy-AB", n.getLiteralLanguage());
415: assertEquals(null, n.getLiteralDatatypeURI());
416: }
417:
418: public void testCreateTypedLiteralInteger() {
419: Node n = NodeCreateUtils.create("'42'xsd:integer");
420: assertEquals("42", n.getLiteralLexicalForm());
421: assertEquals("", n.getLiteralLanguage());
422: assertEquals(expand("xsd:integer"), n.getLiteralDatatypeURI());
423: }
424:
425: public void testCreateTypedLiteralBoolean() {
426: Node n = NodeCreateUtils.create("\"true\"xsd:boolean");
427: assertEquals("true", n.getLiteralLexicalForm());
428: assertEquals("", n.getLiteralLanguage());
429: assertEquals(expand("xsd:boolean"), n.getLiteralDatatypeURI());
430: }
431:
432: public void testGetPlainLiteralLexicalForm() {
433: Node n = NodeCreateUtils.create("'stuff'");
434: assertEquals("stuff", n.getLiteralLexicalForm());
435: }
436:
437: public void testGetNumericLiteralLexicalForm() {
438: Node n = NodeCreateUtils.create("17");
439: assertEquals("17", n.getLiteralLexicalForm());
440: }
441:
442: public void testTypesExpandPrefix() {
443: testTypeExpandsPrefix("rdf:spoo");
444: testTypeExpandsPrefix("rdfs:bar");
445: testTypeExpandsPrefix("owl:henry");
446: testTypeExpandsPrefix("xsd:bool");
447: testTypeExpandsPrefix("unknown:spoo");
448: }
449:
450: private void testTypeExpandsPrefix(String type) {
451: Node n = NodeCreateUtils.create("'stuff'" + type);
452: String wanted = PrefixMapping.Extended.expandPrefix(type);
453: assertEquals(wanted, n.getLiteralDatatypeURI());
454: }
455:
456: public void testCreateURI() {
457: String uri = "http://www.electric-hedgehog.net/";
458: testCreateURI(uri);
459: testCreateURI("rdf:trinket",
460: "http://www.w3.org/1999/02/22-rdf-syntax-ns#trinket");
461: testCreateURI("rdfs:device",
462: "http://www.w3.org/2000/01/rdf-schema#device");
463: testCreateURI("dc:creator", DC.getURI() + "creator");
464: testCreateURI("rss:something", RSS.getURI() + "something");
465: testCreateURI("vcard:TITLE", VCARD.getURI() + "TITLE");
466: testCreateURI("owl:wol", OWL.NAMESPACE + "wol");
467: }
468:
469: public void testCreateURIOtherMap() {
470: String myNS = "eh:foo/bar#", suffix = "something";
471: PrefixMapping mine = PrefixMapping.Factory.create()
472: .setNsPrefix("mine", myNS);
473: Node n = NodeCreateUtils.create(mine, "mine:" + suffix);
474: assertEquals(myNS + suffix, n.getURI());
475: }
476:
477: private void testCreateURI(String inOut) {
478: testCreateURI(inOut, inOut);
479: }
480:
481: private void testCreateURI(String in, String wanted) {
482: String got = NodeCreateUtils.create(in).getURI();
483: if (!wanted.equals(got)) {
484: if (in.equals(wanted))
485: fail("should preserve " + in);
486: else
487: fail("should translate " + in + " to " + wanted
488: + " not " + got);
489: }
490: }
491:
492: public void testCreatePrefixed() {
493: PrefixMapping pm = PrefixMapping.Factory.create();
494: /* TODO Node n = */NodeCreateUtils.create(pm, "xyz");
495: }
496:
497: public void testToStringWithPrefixMapping() {
498: PrefixMapping pm = PrefixMapping.Factory.create();
499: String prefix = "spoo", ns = "abc:def/ghi#";
500: pm.setNsPrefix(prefix, ns);
501: String suffix = "bamboozle";
502: assertEquals(prefix + ":" + suffix, NodeCreateUtils.create(
503: ns + suffix).toString(pm));
504: }
505:
506: public void testNodeHelp() {
507: assertTrue("node() making URIs", node("hello").isURI());
508: assertTrue("node() making literals", node("123").isLiteral());
509: assertTrue("node() making literals", node("'hello'")
510: .isLiteral());
511: assertTrue("node() making hyphens", node("_x").isBlank());
512: assertTrue("node() making variables", node("?x").isVariable());
513: }
514:
515: public void testVisitorPatternNode() {
516: NodeVisitor returnNode = new NodeVisitor() {
517: public Object visitAny(Node_ANY it) {
518: return it;
519: }
520:
521: public Object visitBlank(Node_Blank it, AnonId id) {
522: return it;
523: }
524:
525: public Object visitLiteral(Node_Literal it, LiteralLabel lit) {
526: return it;
527: }
528:
529: public Object visitURI(Node_URI it, String uri) {
530: return it;
531: }
532:
533: public Object visitVariable(Node_Variable it, String name) {
534: return it;
535: }
536: };
537: testVisitorPatternNode("sortOfURI", returnNode);
538: testVisitorPatternNode("?variable", returnNode);
539: testVisitorPatternNode("_anon", returnNode);
540: testVisitorPatternNode("11", returnNode);
541: testVisitorPatternNode("??", returnNode);
542: }
543:
544: private void testVisitorPatternNode(String ns, NodeVisitor v) {
545: Node n = node(ns);
546: assertEquals(n, n.visitWith(v));
547: }
548:
549: private void visitExamples(NodeVisitor nv) {
550: node("sortOfURI").visitWith(nv);
551: node("?variableI").visitWith(nv);
552: node("_anon").visitWith(nv);
553: node("11").visitWith(nv);
554: node("??").visitWith(nv);
555: }
556:
557: public void testVisitorPatternValue() {
558: NodeVisitor checkValue = new NodeVisitor() {
559: public Object visitAny(Node_ANY it) {
560: return null;
561: }
562:
563: public Object visitBlank(Node_Blank it, AnonId id) {
564: assertTrue(it.getBlankNodeId() == id);
565: return null;
566: }
567:
568: public Object visitLiteral(Node_Literal it, LiteralLabel lit) {
569: assertTrue(it.getLiteral() == lit);
570: return null;
571: }
572:
573: public Object visitURI(Node_URI it, String uri) {
574: assertTrue(it.getURI() == uri);
575: return null;
576: }
577:
578: public Object visitVariable(Node_Variable it, String name) {
579: assertEquals(it.getName(), name);
580: return null;
581: }
582: };
583: visitExamples(checkValue);
584: }
585:
586: /**
587: Test that the appropriate elements of the visitor are called exactly once;
588: this relies on the order of the visits in visitExamples.
589: */
590: public void testVisitorPatternCalled() {
591: final String[] strings = new String[] { "" };
592: NodeVisitor checkCalled = new NodeVisitor() {
593: public Object visitAny(Node_ANY it) {
594: strings[0] += " any";
595: return null;
596: }
597:
598: public Object visitBlank(Node_Blank it, AnonId id) {
599: strings[0] += " blank";
600: return null;
601: }
602:
603: public Object visitLiteral(Node_Literal it, LiteralLabel lit) {
604: strings[0] += " literal";
605: return null;
606: }
607:
608: public Object visitURI(Node_URI it, String uri) {
609: strings[0] += " uri";
610: return null;
611: }
612:
613: public Object visitVariable(Node_Variable it, String name) {
614: strings[0] += " variable";
615: return null;
616: }
617: };
618: String desired = " uri variable blank literal any";
619: visitExamples(checkCalled);
620: assertEquals("all vists must have been made", desired,
621: strings[0]);
622: }
623:
624: public void testSimpleMatches() {
625: assertTrue(NodeCreateUtils.create("S").matches(
626: NodeCreateUtils.create("S")));
627: assertFalse("", NodeCreateUtils.create("S").matches(
628: NodeCreateUtils.create("T")));
629: assertFalse("", NodeCreateUtils.create("S").matches(null));
630: assertTrue(NodeCreateUtils.create("_X").matches(
631: NodeCreateUtils.create("_X")));
632: assertFalse("", NodeCreateUtils.create("_X").matches(
633: NodeCreateUtils.create("_Y")));
634: assertFalse("", NodeCreateUtils.create("_X").matches(null));
635: assertTrue(NodeCreateUtils.create("10").matches(
636: NodeCreateUtils.create("10")));
637: assertFalse("", NodeCreateUtils.create("10").matches(
638: NodeCreateUtils.create("11")));
639: assertFalse("", NodeCreateUtils.create("10").matches(null));
640: assertTrue(Node.ANY.matches(NodeCreateUtils.create("S")));
641: assertTrue(Node.ANY.matches(NodeCreateUtils.create("_X")));
642: assertTrue(Node.ANY.matches(NodeCreateUtils.create("10")));
643: assertFalse("", Node.ANY.matches(null));
644: }
645:
646: public void testDataMatches() {
647: TypeMapper tm = TypeMapper.getInstance();
648: RDFDatatype dt1 = tm.getTypeByValue(new Integer(10));
649: RDFDatatype dt2 = tm.getTypeByValue(new Short((short) 10));
650: Node a = Node.createLiteral("10", "", dt1);
651: Node b = Node.createLiteral("10", "", dt2);
652: assertDiffer("types must make a difference", a, b);
653: assertTrue("A and B must express the same value", a
654: .sameValueAs(b));
655: assertTrue("matching literals must respect sameValueAs", a
656: .matches(b));
657: }
658:
659: public void testLiteralToString() {
660: TypeMapper tm = TypeMapper.getInstance();
661: RDFDatatype dtInt = tm.getTypeByValue(new Integer(10));
662: Node plain = Node.createLiteral("rhubarb", "", false);
663: Node english = Node.createLiteral("eccentric", "en_UK", false);
664: Node typed = Node.createLiteral("10", "", dtInt);
665: assertEquals("\"rhubarb\"", plain.toString());
666: assertEquals("rhubarb", plain.toString(false));
667: assertEquals("\"eccentric\"@en_UK", english.toString());
668: assertEquals("10^^http://www.w3.org/2001/XMLSchema#int", typed
669: .toString(false));
670: }
671:
672: public void testGetIndexingValueURI() {
673: Node u = NodeCreateUtils.create("eh:/telephone");
674: assertSame(u, u.getIndexingValue());
675: }
676:
677: public void testGetIndexingValueBlank() {
678: Node b = NodeCreateUtils.create("_television");
679: assertSame(b, b.getIndexingValue());
680: }
681:
682: public void testGetIndexingValuePlainString() {
683: testIndexingValueLiteral(NodeCreateUtils.create("'literally'"));
684: }
685:
686: public void testGetIndexingValueLanguagedString() {
687: testIndexingValueLiteral(NodeCreateUtils.create("'chat'fr"));
688: }
689:
690: public void testGetIndexingValueXSDString() {
691: testIndexingValueLiteral(NodeCreateUtils
692: .create("'string'xsd:string"));
693: }
694:
695: private void testIndexingValueLiteral(Node s) {
696: assertEquals(s.getLiteral().getIndexingValue(), s
697: .getIndexingValue());
698: }
699:
700: // TODO should have more of these
701: public void testGetLiteralValuePlainString() {
702: Node s = NodeCreateUtils.create("'aString'");
703: assertSame(s.getLiteral().getValue(), s.getLiteralValue());
704: }
705:
706: public void testGetLiteralDatatypeNull() {
707: assertEquals(null, NodeCreateUtils.create("'plain'")
708: .getLiteralDatatype());
709: }
710:
711: public void testLiteralIsXML() {
712: assertFalse(NodeCreateUtils.create("'notXML'")
713: .getLiteralIsXML());
714: assertFalse(NodeCreateUtils.create("17").getLiteralIsXML());
715: assertFalse(NodeCreateUtils.create("'joke'xsd:Joke")
716: .getLiteralIsXML());
717: assertTrue(Node.createLiteral("lit", "lang", true)
718: .getLiteralIsXML());
719: assertFalse(Node.createLiteral("lit", "lang", false)
720: .getLiteralIsXML());
721: }
722:
723: public void testConcrete() {
724: assertTrue(NodeCreateUtils.create("S").isConcrete());
725: assertTrue(NodeCreateUtils.create("_P").isConcrete());
726: assertTrue(NodeCreateUtils.create("11").isConcrete());
727: assertTrue(NodeCreateUtils.create("'hello'").isConcrete());
728: assertFalse(NodeCreateUtils.create("??").isConcrete());
729: assertFalse(NodeCreateUtils.create("?x").isConcrete());
730: }
731:
732: static String[] someURIs = new String[] {
733: "http://domainy.thing/stuff/henry",
734: "http://whatever.com/stingy-beast/bee",
735: "ftp://erewhon/12345", "potatoe:rhubarb" };
736:
737: /**
738: test that URI nodes have namespace/localname splits which are consistent
739: with Util.splitNamepace.
740: */
741: public void testNamespace() {
742: for (int i = 0; i < someURIs.length; i += 1) {
743: String uri = someURIs[i];
744: int split = Util.splitNamespace(uri);
745: Node n = NodeCreateUtils.create(uri);
746: assertEquals("check namespace", uri.substring(0, split), n
747: .getNameSpace());
748: assertEquals("check localname", uri.substring(split), n
749: .getLocalName());
750: }
751: }
752:
753: protected static String[] someNodes = { "42", "'hello'", "_anon",
754: "'robotic'tick", "'teriffic'abc:def" };
755:
756: public void testHasURI() {
757: for (int i = 0; i < someURIs.length; i += 1)
758: testHasURI(someURIs[i]);
759: for (int i = 0; i < someNodes.length; i += 1)
760: testHasURI(someNodes[i]);
761: }
762:
763: protected void testHasURI(String uri) {
764: Node n = NodeCreateUtils.create(uri);
765: assertTrue(uri, !n.isURI() || n.hasURI(uri));
766: assertFalse(uri, n.hasURI(uri + "x"));
767: }
768:
769: /**
770: Answer the string <code>s</code> prefix-expanded using the built-in
771: PrefixMapping.Extended.
772: */
773: private String expand(String s) {
774: return PrefixMapping.Extended.expandPrefix(s);
775: }
776: }
777:
778: /*
779: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
780: All rights reserved.
781:
782: Redistribution and use in source and binary forms, with or without
783: modification, are permitted provided that the following conditions
784: are met:
785:
786: 1. Redistributions of source code must retain the above copyright
787: notice, this list of conditions and the following disclaimer.
788:
789: 2. Redistributions in binary form must reproduce the above copyright
790: notice, this list of conditions and the following disclaimer in the
791: documentation and/or other materials provided with the distribution.
792:
793: 3. The name of the author may not be used to endorse or promote products
794: derived from this software without specific prior written permission.
795:
796: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
797: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
798: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
799: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
800: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
801: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
802: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
803: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
804: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
805: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
806: */
|