KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > test > TestNode


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestNode.java,v 1.34 2005/02/21 11:52:47 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.test;
8
9
10 import com.hp.hpl.jena.graph.*;
11 import com.hp.hpl.jena.graph.impl.*;
12 import com.hp.hpl.jena.rdf.model.AnonId;
13 import com.hp.hpl.jena.rdf.model.impl.Util;
14 import com.hp.hpl.jena.shared.*;
15 import com.hp.hpl.jena.datatypes.*;
16 import com.hp.hpl.jena.vocabulary.*;
17
18 import junit.framework.*;
19
20 /**
21     @author bwm out of kers
22     Exercise nodes. Make sure that the different node types do not overlap
23     and that the test predicates work properly on the different node kinds.
24 */

25
26 public class TestNode extends GraphTestBase
27     {
28     public TestNode( String JavaDoc name )
29         { super( name ); }
30     
31     public static TestSuite suite()
32         { return new TestSuite( TestNode.class ); }
33     
34     private static final String JavaDoc U = "http://some.domain.name/magic/spells.incant";
35     private static final String JavaDoc N = "Alice";
36     private static final LiteralLabel L = new LiteralLabel( "ashes are burning", "en", false );
37     private static final AnonId A = new AnonId();
38
39     public void testBlanks()
40         {
41         assertTrue( "anonymous nodes are blank", Node.createAnon().isBlank() );
42         assertFalse( "anonymous nodes aren't literal", Node.createAnon().isLiteral() );
43         assertFalse( "anonymous nodes aren't URIs", Node.createAnon().isURI() );
44         assertFalse( "anonymous nodes aren't variables", Node.createAnon().isVariable() );
45         assertEquals( "anonymous nodes have the right id", Node.createAnon(A).getBlankNodeId(), A );
46         }
47         
48     public void testLiterals()
49         {
50         assertFalse( "literal nodes aren't blank", Node.createLiteral( L ).isBlank() );
51         assertTrue( "literal nodes are literal", Node.createLiteral( L ).isLiteral() );
52         assertFalse( "literal nodes aren't variables", Node.createLiteral( L ).isVariable() );
53         assertFalse( "literal nodes aren't URIs", Node.createLiteral( L ).isURI() );
54         assertEquals( "literal nodes preserve value", Node.createLiteral( L ).getLiteral(), L );
55         }
56         
57     public void testURIs()
58         {
59         assertFalse( "URI nodes aren't blank", Node.createURI( U ).isBlank() );
60         assertFalse( "URI nodes aren't literal", Node.createURI( U ).isLiteral() );
61         assertFalse( "URI nodes aren't variables", Node.createURI( U ).isVariable() );
62         assertTrue( "URI nodes are URIs", Node.createURI( U ).isURI() );
63         assertEquals( "URI nodes preserve URI", Node.createURI( U ).getURI(), U );
64         }
65         
66     public void testVariables()
67         {
68         assertFalse( "variable nodes aren't blank", Node.createVariable( N ).isBlank() );
69         assertFalse( "variable nodes aren't literal", Node.createVariable( N ).isLiteral() );
70         assertFalse( "variable nodes aren't URIs", Node.createVariable( N ).isURI() );
71         assertTrue( "variable nodes are variable", Node.createVariable( N ).isVariable() );
72         assertEquals( "variable nodes keep their name", N, Node.createVariable( N ).getName() );
73         assertEquals( "variable nodes keep their name", N + "x", Node.createVariable( N + "x" ).getName() );
74         }
75         
76     public void testANY()
77         {
78         assertFalse( "ANY nodes aren't blank", Node.ANY.isBlank() );
79         assertFalse( "ANY nodes aren't literals", Node.ANY.isLiteral() );
80         assertFalse( "ANY nodes aren't URIs", Node.ANY.isURI() );
81         assertFalse( "ANY nodes aren't variables", Node.ANY.isVariable() );
82         assertFalse( "ANY nodes aren't blank", Node.ANY.isBlank() );
83         assertFalse( "ANY nodes aren't blank", Node.ANY.isBlank() );
84         }
85         
86     /**
87         test cases for equality: an array of (Node, String) pairs. [It's not worth
88         making a special class for these pairs.] The nodes are created with caching
89         off, to make sure that caching effects don't hide the effect of using .equals().
90         The strings are "equality groups": the nodes should test equal iff their
91         associated strings test equal.
92     */

93     private Object JavaDoc [][] eqTestCases()
94         {
95         try
96             {
97             Node.cache( false );
98             AnonId id = new AnonId();
99             LiteralLabel L2 = new LiteralLabel( id.toString(), "", false );
100             String JavaDoc U2 = id.toString();
101             String JavaDoc N2 = id.toString();
102             return new Object JavaDoc [][]
103                 {
104                     { Node.ANY, "0" },
105                     { Node.createAnon( id ), "1" },
106                     { Node.createAnon(), "2" },
107                     { Node.createAnon( id ), "1" },
108                     { Node.createLiteral( L ), "3" },
109                     { Node.createLiteral( L2 ), "4" },
110                     { Node.createLiteral( L ), "3" },
111                     { Node.createURI( U ), "5" },
112                     { Node.createURI( U2 ), "6" },
113                     { Node.createURI( U ), "5" },
114                     { Node.createVariable( N ), "7" },
115                     { Node.createVariable( N2 ), "8" },
116                     { Node.createVariable( N ), "7" }
117                 };
118             }
119         finally
120             { Node.cache( true ); }
121         }
122         
123     public void testNodeEquals()
124         {
125         Object JavaDoc [][] tests = eqTestCases();
126         for (int i = 0; i < tests.length; i += 1)
127             {
128             Object JavaDoc [] I = tests[i];
129             assertFalse( I[0] + " should not equal null", I[0].equals( null ) );
130             assertFalse( I[0] + "should not equal 'String'", I[0].equals( "String" ) );
131             for (int j = 0; j < tests.length; j += 1)
132                 {
133                 Object JavaDoc [] J = tests[j];
134                 testEquality( I[1].equals( J[1] ), I[0], J[0] );
135                 }
136             }
137         }
138         
139     private void testEquality( boolean testEq, Object JavaDoc x, Object JavaDoc y )
140         {
141         String JavaDoc testName = getType( x ) + " " + x + " and " + getType( y ) + " " + y;
142         if (testEq)
143             assertEquals( testName + "should be equal", x, y );
144         else
145             assertDiffer( testName + " should differ", x, y );
146         }
147         
148     private String JavaDoc getType( Object JavaDoc x )
149         {
150         String JavaDoc fullName = x.getClass().getName();
151         return fullName.substring( fullName.lastIndexOf( '.' ) + 1 );
152         }
153         
154     public void testEquals()
155         {
156         try
157             {
158             Node.cache( false );
159             assertDiffer( "different variables", Node.createVariable( "xx" ), Node.createVariable( "yy" ) );
160             assertEquals( "same vars", Node.createVariable( "aa" ), Node.createVariable( "aa" ) );
161             assertEquals( "same URI", Node.createURI( U ), Node.createURI( U ) );
162             assertEquals( "same anon", Node.createAnon( A ), Node.createAnon( A ) );
163             assertEquals( "same literal", Node.createLiteral( L ), Node.createLiteral( L ) );
164             assertFalse( "distinct URIs", Node.createURI( U ) == Node.createURI( U ) );
165             assertFalse( "distinct blanks", Node.createAnon( A ) == Node.createAnon( A ) );
166             assertFalse( "distinct literals", Node.createLiteral( L ) == Node.createLiteral( L ) );
167             assertFalse( "distinct vars", Node.createVariable( "aa" ) == Node.createVariable( "aa" ) );
168             }
169         finally
170             { Node.cache( true ); }
171         }
172         
173     /**
174         test that the label of a Node can be retrieved from that Node in
175         a way appropriate to that Node.
176     */

177     public void testLabels()
178         {
179         AnonId id = new AnonId();
180         assertEquals( "get URI value", U, Node.createURI( U ).getURI() );
181         assertEquals( "get blank value", id, Node.createAnon( id ).getBlankNodeId() );
182         assertEquals( "get literal value", L, Node.createLiteral( L ).getLiteral() );
183         assertEquals( "get variable name", N, Node.createVariable( N ).getName() );
184         }
185         
186     /**
187         this is where we test that using the wrong accessor on a Node gets you
188         an exception.
189     */

190     public void testFailingLabels()
191         {
192         Node u = Node.createURI( U ), b = Node.createAnon();
193         Node l = Node.createLiteral( L ), v = Node.createVariable( N );
194         Node a = Node.ANY;
195     /* */
196         testGetURIFails( a );
197         testGetURIFails( b );
198         testGetURIFails( l );
199         testGetURIFails( v );
200     /* */
201         testGetLiteralFails( a );
202         testGetLiteralFails( u );
203         testGetLiteralFails( b );
204         testGetLiteralFails( v );
205     /* */
206         testGetNameFails( a );
207         testGetNameFails( u );
208         testGetNameFails( b );
209         testGetNameFails( l );
210     /* */
211         testGetBlankNodeIdFails( a );
212         testGetBlankNodeIdFails( u );
213         testGetBlankNodeIdFails( l );
214         testGetBlankNodeIdFails( v );
215         }
216         
217     public void testGetBlankNodeIdFails( Node n )
218         { try { n.getBlankNodeId(); fail( n.getClass() + " should fail getName()" ); } catch (UnsupportedOperationException JavaDoc e) {} }
219
220     public void testGetURIFails( Node n )
221         { try { n.getURI(); fail( n.getClass() + " should fail getURI()" ); } catch (UnsupportedOperationException JavaDoc e) {} }
222         
223     public void testGetNameFails( Node n )
224         { try { n.getName(); fail( n.getClass() + " should fail getName()" ); } catch (UnsupportedOperationException JavaDoc e) {} }
225     
226     public void testGetLiteralFails( Node n )
227         { try { n.getLiteral(); fail( n.getClass() + " should fail getLiteral()" ); } catch (UnsupportedOperationException JavaDoc e) {} }
228         
229     public void testVariableSupport()
230         {
231         assertEquals( Node_Variable.variable( "xxx" ), Node_Variable.variable( "xxx" ) );
232         assertDiffer( Node_Variable.variable( "xxx" ), Node_Variable.variable( "yyy" ) );
233         assertEquals( Node_Variable.variable( "aaa" ), Node_Variable.variable( "aaa" ) );
234         assertDiffer( Node_Variable.variable( "aaa" ), Node_Variable.variable( "yyy" ) );
235         }
236     
237     public void testCache()
238         {
239         assertEquals( Node_Variable.variable( "xxx" ), Node_Variable.variable( "xxx" ) );
240         assertTrue( "remembers URI", Node.createURI( U ) == Node.createURI( U ) );
241         assertTrue( "remembers literal", Node.createLiteral( L ) == Node.createLiteral( L ) );
242         assertTrue( "remembers blanks", Node.createAnon( A ) == Node.createAnon( A ) );
243         assertTrue( "remembers variables", Node.createVariable( N ) == Node.createVariable( N ) );
244         assertFalse( "is not confused", Node.createVariable( N ) == Node.createURI( N ) );
245         }
246         
247     /**
248         Test that the create method does sensible things on null and ""
249     */

250     public void testCreateBadString()
251         {
252         try { Node.create( null ); fail( "must catch null argument" ); }
253         catch (NullPointerException JavaDoc e) {}
254         catch (JenaException e) {}
255         try { Node.create( "" ); fail("must catch empty argument" ); }
256         catch (JenaException e) {}
257         }
258         
259     /**
260         Test that anonymous nodes are created with the correct labels
261     */

262     public void testCreateAnon()
263         {
264         String JavaDoc idA = "_xxx", idB = "_yyy";
265         Node a = Node.create( idA ), b = Node.create( idB );
266         assertTrue( "both must be bnodes", a.isBlank() && b.isBlank() );
267         assertEquals( new AnonId( idA ), a.getBlankNodeId() );
268         assertEquals( new AnonId( idB ), b.getBlankNodeId() );
269         }
270         
271     public void testCreateVariable()
272         {
273         String JavaDoc V = "wobbly";
274         Node v = Node.create( "?" + V );
275         assertTrue( "must be a variable", v.isVariable() );
276         assertEquals( "name must be correct", V, v.getName() );
277         }
278         
279     public void testCreateANY()
280         {
281         assertEquals( "?? must denote ANY", Node.ANY, Node.create( "??" ) );
282         }
283     
284     public void testCreatePlainLiteralSIngleQuotes()
285         {
286         Node n = Node.create( "'xxx'" );
287         assertEquals( "xxx", n.getLiteral().getLexicalForm() );
288         assertEquals( "", n.getLiteral().language() );
289         assertEquals( null, n.getLiteral().getDatatypeURI() );
290         }
291     
292     public void testCreatePlainLiteralDoubleQuotes()
293         {
294         Node n = Node.create( "\"xxx\"" );
295         assertEquals( "xxx", n.getLiteral().getLexicalForm() );
296         assertEquals( "", n.getLiteral().language() );
297         assertEquals( null, n.getLiteral().getDatatypeURI() );
298         }
299     
300     public void testCreateLiteralBackslashEscape()
301         {
302         testStringConversion( "xx\\x", "'xx\\\\x'" );
303         testStringConversion( "xx\\x\\y", "'xx\\\\x\\\\y'" );
304         testStringConversion( "\\xyz\\", "'\\\\xyz\\\\'" );
305         }
306     
307     public void testCreateLiteralQuoteEscapes()
308         {
309         testStringConversion( "x\'y", "'x\\'y'" );
310         testStringConversion( "x\"y", "'x\\\"y'" );
311         testStringConversion( "x\'y\"z", "'x\\\'y\\\"z'" );
312         }
313     
314     public void testCreateLiteralOtherEscapes()
315         {
316         testStringConversion( " ", "'\\s'" );
317         testStringConversion( "\t", "'\\t'" );
318         testStringConversion( "\n", "'\\n'" );
319         }
320     
321     protected void testStringConversion( String JavaDoc wanted, String JavaDoc template )
322         {
323         Node n = Node.create( template );
324         assertEquals( wanted, n.getLiteral().getLexicalForm() );
325         assertEquals( "", n.getLiteral().language() );
326         assertEquals( null, n.getLiteral().getDatatypeURI() );
327         }
328
329     public void testCreateLanguagedLiteralEN()
330         {
331         Node n = Node.create( "'chat'en-UK" );
332         assertEquals( "chat", n.getLiteral().getLexicalForm() );
333         assertEquals( "en-UK", n.getLiteral().language() );
334         assertEquals( null, n.getLiteral().getDatatypeURI() );
335         }
336     
337     public void testCreateLanguagedLiteralXY()
338         {
339         Node n = Node.create( "\"chat\"xy-AB" );
340         assertEquals( "chat", n.getLiteral().getLexicalForm() );
341         assertEquals( "xy-AB", n.getLiteral().language() );
342         assertEquals( null, n.getLiteral().getDatatypeURI() );
343         }
344     
345     public void testCreateTypedLiteralInteger()
346         {
347         Node n = Node.create( "'42'xsd:integer" );
348         assertEquals( "42", n.getLiteral().getLexicalForm() );
349         assertEquals( "", n.getLiteral().language() );
350         assertEquals( expand( "xsd:integer" ), n.getLiteral().getDatatypeURI() );
351         }
352     
353     public void testCreateTypedLiteralBoolean()
354         {
355         Node n = Node.create( "\"true\"xsd:boolean" );
356         assertEquals( "true", n.getLiteral().getLexicalForm() );
357         assertEquals( "", n.getLiteral().language() );
358         assertEquals( expand( "xsd:boolean" ), n.getLiteral().getDatatypeURI() );
359         }
360         
361     public void testTypesExpandPrefix()
362         {
363         testTypeExpandsPrefix( "rdf:spoo" );
364         testTypeExpandsPrefix( "rdfs:bar" );
365         testTypeExpandsPrefix( "owl:henry" );
366         testTypeExpandsPrefix( "xsd:bool" );
367         testTypeExpandsPrefix( "unknown:spoo" );
368         }
369     
370     private void testTypeExpandsPrefix( String JavaDoc type )
371         {
372         Node n = Node.create( "'stuff'" + type );
373         String JavaDoc wanted = PrefixMapping.Extended.expandPrefix( type );
374         assertEquals( wanted, n.getLiteral().getDatatypeURI() );
375         }
376
377     public void testCreateURI()
378         {
379         String JavaDoc uri = "http://www.electric-hedgehog.net/";
380         testCreateURI( uri );
381         testCreateURI( "rdf:trinket", "http://www.w3.org/1999/02/22-rdf-syntax-ns#trinket" );
382         testCreateURI( "rdfs:device", "http://www.w3.org/2000/01/rdf-schema#device" );
383         testCreateURI( "dc:creator", DC.getURI() + "creator" );
384         testCreateURI( "rss:something", RSS.getURI() + "something" );
385         testCreateURI( "vcard:TITLE", VCARD.getURI() + "TITLE" );
386         testCreateURI( "owl:wol", OWL.NAMESPACE + "wol" );
387         }
388         
389     public void testCreateURIOtherMap()
390         {
391         String JavaDoc myNS = "eh:foo/bar#", suffix = "something";
392         PrefixMapping mine = PrefixMapping.Factory.create().setNsPrefix( "mine", myNS );
393         Node n = Node.create( mine, "mine:" + suffix );
394         assertEquals( myNS + suffix, n.getURI() );
395         }
396         
397     private void testCreateURI( String JavaDoc inOut )
398         { testCreateURI( inOut, inOut ); }
399         
400     private void testCreateURI( String JavaDoc in, String JavaDoc wanted )
401         {
402         String JavaDoc got = Node.create( in ).getURI();
403         if (!wanted.equals( got ))
404             {
405             if (in.equals( wanted )) fail( "should preserve " + in );
406             else fail( "should translate " + in + " to " + wanted + " not " + got );
407             }
408         }
409         
410     public void testCreatePrefixed()
411         {
412         PrefixMapping pm = PrefixMapping.Factory.create();
413         /* TODO Node n = */ Node.create( pm, "xyz" );
414         }
415         
416     public void testToStringWithPrefixMapping()
417         {
418         PrefixMapping pm = PrefixMapping.Factory.create();
419         String JavaDoc prefix = "spoo", ns = "abc:def/ghi#";
420         pm.setNsPrefix( prefix, ns );
421         String JavaDoc suffix = "bamboozle";
422         assertEquals( prefix + ":" + suffix, Node.create( ns + suffix ).toString( pm ) );
423         }
424         
425     public void testNodeHelp()
426         {
427         assertTrue( "node() making URIs", node( "hello" ).isURI() );
428         assertTrue( "node() making literals", node( "123" ).isLiteral() );
429         assertTrue( "node() making literals", node( "'hello'" ).isLiteral() );
430         assertTrue( "node() making blanks", node( "_x" ).isBlank() );
431         assertTrue( "node() making variables", node( "?x" ).isVariable() );
432         }
433         
434     public void testVisitorPatternNode()
435         {
436        NodeVisitor returnNode = new NodeVisitor()
437             {
438             public Object JavaDoc visitAny( Node_ANY it ) { return it; }
439             public Object JavaDoc visitBlank( Node_Blank it, AnonId id ) { return it; }
440             public Object JavaDoc visitLiteral( Node_Literal it, LiteralLabel lit ) { return it; }
441             public Object JavaDoc visitURI( Node_URI it, String JavaDoc uri ) { return it; }
442             public Object JavaDoc visitVariable( Node_Variable it, String JavaDoc name ) { return it; }
443             };
444         testVisitorPatternNode( "sortOfURI", returnNode );
445         testVisitorPatternNode( "?variable", returnNode );
446         testVisitorPatternNode( "_anon", returnNode );
447         testVisitorPatternNode( "11", returnNode );
448         testVisitorPatternNode( "??", returnNode );
449         }
450         
451     private void testVisitorPatternNode( String JavaDoc ns, NodeVisitor v )
452         {
453         Node n = node( ns );
454         assertEquals( n, n.visitWith( v ) );
455         }
456         
457     private void visitExamples( NodeVisitor nv )
458         {
459         node( "sortOfURI" ).visitWith( nv );
460         node( "?variableI" ).visitWith( nv );
461         node( "_anon" ).visitWith( nv );
462         node( "11" ).visitWith( nv );
463         node( "??" ).visitWith( nv );
464         }
465         
466     public void testVisitorPatternValue()
467         {
468         NodeVisitor checkValue = new NodeVisitor()
469             {
470             public Object JavaDoc visitAny( Node_ANY it )
471                 { return null; }
472             public Object JavaDoc visitBlank( Node_Blank it, AnonId id )
473                 { assertTrue( it.getBlankNodeId() == id ); return null; }
474             public Object JavaDoc visitLiteral( Node_Literal it, LiteralLabel lit )
475                 { assertTrue( it.getLiteral() == lit ); return null; }
476             public Object JavaDoc visitURI( Node_URI it, String JavaDoc uri )
477                 { assertTrue( it.getURI() == uri ); return null; }
478             public Object JavaDoc visitVariable( Node_Variable it, String JavaDoc name )
479                 { assertEquals( it.getName(), name ); return null; }
480             };
481         visitExamples( checkValue );
482         }
483         
484     /**
485         Test that the appropriate elements of the visitor are called exactly once;
486         this relies on the order of the visits in visitExamples.
487     */

488     public void testVisitorPatternCalled()
489         {
490         final String JavaDoc [] strings = new String JavaDoc [] { "" };
491         NodeVisitor checkCalled = new NodeVisitor()
492             {
493             public Object JavaDoc visitAny( Node_ANY it )
494                 { strings[0] += " any"; return null; }
495             public Object JavaDoc visitBlank( Node_Blank it, AnonId id )
496                 { strings[0] += " blank"; return null; }
497             public Object JavaDoc visitLiteral( Node_Literal it, LiteralLabel lit )
498                 { strings[0] += " literal"; return null; }
499             public Object JavaDoc visitURI( Node_URI it, String JavaDoc uri )
500                 { strings[0] += " uri"; return null; }
501             public Object JavaDoc visitVariable( Node_Variable it, String JavaDoc name )
502                 { strings[0] += " variable"; return null; }
503             };
504         String JavaDoc desired = " uri variable blank literal any";
505         visitExamples( checkCalled );
506         assertEquals( "all vists must have been made", desired, strings[0] );
507         }
508         
509     public void testSimpleMatches()
510         {
511         assertTrue( Node.create( "S").matches( Node.create( "S" ) ) );
512         assertFalse( "", Node.create( "S").matches( Node.create( "T" ) ) );
513         assertFalse( "", Node.create( "S" ).matches( null ) );
514         assertTrue( Node.create( "_X").matches( Node.create( "_X" ) ) );
515         assertFalse( "", Node.create( "_X").matches( Node.create( "_Y" ) ) );
516         assertFalse( "", Node.create( "_X").matches( null ) );
517         assertTrue( Node.create( "10" ).matches( Node.create( "10" ) ) );
518         assertFalse( "", Node.create( "10" ).matches( Node.create( "11" ) ) );
519         assertFalse( "", Node.create( "10" ).matches( null ) );
520         assertTrue( Node.ANY.matches( Node.create( "S" ) ) );
521         assertTrue( Node.ANY.matches( Node.create( "_X" ) ) );
522         assertTrue( Node.ANY.matches( Node.create( "10" ) ) );
523         assertFalse( "", Node.ANY.matches( null ) );
524         }
525         
526     public void testDataMatches()
527         {
528         TypeMapper tm = TypeMapper.getInstance();
529         RDFDatatype dt1 = tm.getTypeByValue( new Integer JavaDoc( 10 ) );
530         RDFDatatype dt2 = tm.getTypeByValue( new Short JavaDoc( (short) 10 ) );
531         Node a = Node.createLiteral( "10", "", dt1 );
532         Node b = Node.createLiteral( "10", "", dt2 );
533         assertDiffer( "types must make a difference", a, b );
534         assertTrue( "A and B must express the same value", a.sameValueAs( b ) );
535         assertTrue( "matching literals must respect sameValueAs", a.matches( b ) );
536         }
537         
538     public void testLiteralToString()
539         {
540         TypeMapper tm = TypeMapper.getInstance();
541         RDFDatatype dtInt = tm.getTypeByValue( new Integer JavaDoc( 10 ) );
542         Node plain = Node.createLiteral( "rhubarb", "", false );
543         Node english = Node.createLiteral( "eccentric", "en_UK", false );
544         Node typed = Node.createLiteral( "10", "", dtInt );
545         assertEquals( "\"rhubarb\"", plain.toString() );
546         assertEquals( "rhubarb", plain.toString( false ) );
547         assertEquals( "\"eccentric\"@en_UK", english.toString() );
548         assertEquals( "10^^http://www.w3.org/2001/XMLSchema#int", typed.toString( false ) );
549         }
550         
551     public void testConcrete()
552         {
553         assertTrue( Node.create( "S" ).isConcrete() );
554         assertTrue( Node.create( "_P" ).isConcrete() );
555         assertTrue( Node.create( "11" ).isConcrete() );
556         assertTrue( Node.create( "'hello'" ).isConcrete() );
557         assertFalse( Node.create( "??" ).isConcrete() );
558         assertFalse( Node.create( "?x" ).isConcrete() );
559         }
560
561     static String JavaDoc [] someURIs = new String JavaDoc []
562         {
563             "http://domainy.thing/stuff/henry",
564             "http://whatever.com/stingy-beast/bee",
565             "ftp://erewhon/12345",
566             "potatoe:rhubarb"
567         };
568     
569     /**
570         test that URI nodes have namespace/localname splits which are consistent
571         with Util.splitNamepace.
572     */

573     public void testNamespace()
574         {
575         for (int i = 0; i < someURIs.length; i += 1)
576             {
577             String JavaDoc uri = someURIs[i];
578             int split = Util.splitNamespace( uri );
579             Node n = Node.create( uri );
580             assertEquals( "check namespace", uri.substring( 0, split ), n.getNameSpace() );
581             assertEquals( "check localname", uri.substring( split ), n.getLocalName() );
582             }
583         }
584     
585     protected static String JavaDoc [] someNodes =
586         {
587             "42",
588             "'hello'",
589             "_anon",
590             "'robotic'tick",
591             "'teriffic'abc:def"
592         };
593     
594     public void testHasURI()
595         {
596         for (int i = 0; i < someURIs.length; i += 1) testHasURI( someURIs[i] );
597         for (int i = 0; i < someNodes.length; i += 1) testHasURI( someNodes[i] );
598         }
599
600     private void testHasURI( String JavaDoc uri )
601         {
602         Node n = Node.create( uri );
603         assertTrue( uri, !n.isURI() || n.hasURI( uri ) );
604         assertFalse( uri, n.hasURI( uri + "x" ) );
605         }
606
607     /**
608         Answer the string <code>s</code> prefix-expanded using the built-in
609         PrefixMapping.Extended.
610     */

611     private String JavaDoc expand( String JavaDoc s )
612         { return PrefixMapping.Extended.expandPrefix( s ); }
613     }
614
615 /*
616     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
617     All rights reserved.
618
619     Redistribution and use in source and binary forms, with or without
620     modification, are permitted provided that the following conditions
621     are met:
622
623     1. Redistributions of source code must retain the above copyright
624        notice, this list of conditions and the following disclaimer.
625
626     2. Redistributions in binary form must reproduce the above copyright
627        notice, this list of conditions and the following disclaimer in the
628        documentation and/or other materials provided with the distribution.
629
630     3. The name of the author may not be used to endorse or promote products
631        derived from this software without specific prior written permission.
632
633     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
634     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
635     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
636     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
637     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
638     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
639     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
640     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
641     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
642     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
643 */

644
Popular Tags