KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > test > TestRDFNodes


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestRDFNodes.java,v 1.5 2005/02/21 12:15:17 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model.test;
8
9 import com.hp.hpl.jena.rdf.model.*;
10
11 import java.util.*;
12
13 import junit.framework.*;
14
15 /**
16     @author kers
17     This class tests various properties of RDFNodes, to start with the
18     new Visitor stuff.
19 */

20 public class TestRDFNodes extends ModelTestBase
21     {
22     public TestRDFNodes(String JavaDoc name)
23         { super(name); }
24
25     public static TestSuite suite()
26         { return new TestSuite( TestRDFNodes.class ); }
27         
28     public void testRDFVisitor()
29         {
30         final List strings = new ArrayList();
31         Model m = ModelFactory.createDefaultModel();
32         final RDFNode S = m.createResource();
33         final RDFNode P = m.createProperty( "eh:PP" );
34         final RDFNode O = m.createLiteral( "LL" );
35     /* */
36         RDFVisitor rv = new RDFVisitor()
37             {
38             public Object JavaDoc visitBlank( Resource R, AnonId id )
39                 {
40                 strings.add( "blank" );
41                 assertTrue( "must visit correct node", R == S );
42                 assertEquals( "must have correct field", R.getId(), id );
43                 return "blank result";
44                 }
45             public Object JavaDoc visitURI( Resource R, String JavaDoc uri )
46                 {
47                 strings.add( "uri" );
48                 assertTrue( "must visit correct node", R == P );
49                 assertEquals( "must have correct field", R.getURI(), uri );
50                 return "uri result";
51                 }
52             public Object JavaDoc visitLiteral( Literal L )
53                 {
54                 strings.add( "literal" );
55                 assertTrue( "must visit correct node", L == O );
56                 return "literal result";
57                 }
58             };
59     /* */
60         assertEquals( "blank result", S.visitWith( rv ) );
61         assertEquals( "uri result", P.visitWith( rv ) );
62         assertEquals( "literal result", O.visitWith( rv ) );
63     /* */
64         assertEquals( strings.get(0), "blank" );
65         assertEquals( strings.get(1), "uri" );
66         assertEquals( strings.get(2), "literal" );
67         }
68         
69     public void testRemoveAllRemoves()
70         {
71         String JavaDoc ps = "x P a; x P b", rest = "x Q c; y P a; y Q b";
72         Model m = modelWithStatements( ps + "; " + rest );
73         Resource r = resource( m, "x" );
74         Resource r2 = r.removeAll( property( m, "P" ) );
75         assertSame( "removeAll should deliver its receiver", r, r2 );
76         assertIsoModels( "x's P-values should go", modelWithStatements( rest ), m );
77         }
78         
79     public void testRemoveAllBoring()
80         {
81         Model m1 = modelWithStatements( "x P a; y Q b" );
82         Model m2 = modelWithStatements( "x P a; y Q b" );
83         resource( m2, "x" ).removeAll( property( m2, "Z" ) );
84         assertIsoModels( "m2 should be unchanged", m1, m2 );
85         }
86         
87     public void testInModel()
88         {
89         Model m1 = modelWithStatements( "" );
90         Model m2 = modelWithStatements( "" );
91         Resource r1 = resource( m1, "r1" );
92         Resource r2 = resource( m1, "_r2" );
93     /* */
94         assertTrue( r1.getModel() == m1 );
95         assertTrue( r2.getModel() == m1 );
96         assertFalse( r1.isAnon() );
97         assertTrue( r2.isAnon() );
98     /* */
99         assertTrue( ((Resource) r1.inModel( m2 )).getModel() == m2 );
100         assertTrue( ((Resource) r2.inModel( m2 )).getModel() == m2 );
101     /* */
102         assertEquals( r1, r1.inModel( m2 ) );
103         assertEquals( r2, r2.inModel( m2 ) );
104         }
105     }
106
107 /*
108     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
109     All rights reserved.
110
111     Redistribution and use in source and binary forms, with or without
112     modification, are permitted provided that the following conditions
113     are met:
114
115     1. Redistributions of source code must retain the above copyright
116        notice, this list of conditions and the following disclaimer.
117
118     2. Redistributions in binary form must reproduce the above copyright
119        notice, this list of conditions and the following disclaimer in the
120        documentation and/or other materials provided with the distribution.
121
122     3. The name of the author may not be used to endorse or promote products
123        derived from this software without specific prior written permission.
124
125     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
126     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
127     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
128     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
129     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
130     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
131     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
132     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
133     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
134     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135 */
Popular Tags