KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 package com.hp.hpl.jena.rdf.model.test;
8
9 import com.hp.hpl.jena.rdf.model.*;
10 import com.hp.hpl.jena.rdf.model.impl.ModelCom;
11 import com.hp.hpl.jena.shared.*;
12 import com.hp.hpl.jena.graph.*;
13
14 /**
15     @author kers
16 */

17 public abstract class AbstractTestModel extends ModelTestBase
18     {
19     public AbstractTestModel( String JavaDoc name )
20         { super(name); }
21
22     public abstract Model getModel();
23     
24     private Model model;
25     
26     public void setUp()
27         { model = getModel(); }
28         
29     public void tearDown()
30         { model.close(); }
31        
32     public void testTransactions()
33         {
34         Command cmd = new Command()
35             { public Object JavaDoc execute() { return null; } };
36         if (model.supportsTransactions()) model.executeInTransaction( cmd );
37         }
38         
39     public void testCreateResourceFromNode()
40         {
41         RDFNode S = model.getRDFNode( Node.create( "spoo:S" ) );
42         assertTrue( S instanceof Resource );
43         assertEquals( "spoo:S", ((Resource) S).getURI() );
44         }
45         
46     public void testCreateLiteralFromNode()
47         {
48         RDFNode S = model.getRDFNode( Node.create( "42" ) );
49         assertTrue( S instanceof Literal );
50         assertEquals( "42", ((Literal) S).getLexicalForm() );
51         }
52             
53    public void testCreateBlankFromNode()
54         {
55         RDFNode S = model.getRDFNode( Node.create( "_Blank" ) );
56         assertTrue( S instanceof Resource );
57         assertEquals( new AnonId( "_Blank" ), ((Resource) S).getId() );
58         }
59         
60     public void testIsEmpty()
61         {
62         Statement S1 = statement( model, "model rdf:type nonEmpty" );
63         Statement S2 = statement( model, "pinky rdf:type Pig" );
64         assertTrue( model.isEmpty() );
65         model.add( S1 );
66         assertFalse( model.isEmpty() );
67         model.add( S2 );
68         assertFalse( model.isEmpty() );
69         model.remove( S1 );
70         assertFalse( model.isEmpty() );
71         model.remove( S2 );
72         assertTrue( model.isEmpty() );
73         }
74         
75     public void testContainsResource()
76         {
77         modelAdd( model, "x R y; _a P _b" );
78         assertTrue( model.containsResource( resource( model, "x" ) ) );
79         assertTrue( model.containsResource( resource( model, "R" ) ) );
80         assertTrue( model.containsResource( resource( model, "y" ) ) );
81         assertTrue( model.containsResource( resource( model, "_a" ) ) );
82         assertTrue( model.containsResource( resource( model, "P" ) ) );
83         assertTrue( model.containsResource( resource( model, "_b" ) ) );
84         assertFalse( model.containsResource( resource( model, "i" ) ) );
85         assertFalse( model.containsResource( resource( model, "_j" ) ) );
86         }
87         
88     /**
89         Test the new version of getProperty(), which delivers null for not-found
90         properties.
91     */

92     public void testGetProperty()
93         {
94         modelAdd( model, "x P a; x P b; x R c" );
95         Resource x = resource( model, "x" );
96         assertEquals( resource( model, "c" ), x.getProperty( property( model, "R" ) ).getObject() );
97         RDFNode ob = x.getProperty( property( model, "P" ) ).getObject();
98         assertTrue( ob.equals( resource( model, "a" ) ) || ob.equals( resource( model, "b" ) ) );
99         assertNull( x.getProperty( property( model, "noSuchPropertyHere" ) ) );
100         }
101     
102     public void testToStatement()
103         {
104         Triple t = triple( "a P b" );
105         Statement s = model.asStatement( t );
106         assertEquals( node( "a" ), s.getSubject().asNode() );
107         assertEquals( node( "P" ), s.getPredicate().asNode() );
108         assertEquals( node( "b" ), s.getObject().asNode() );
109         }
110     
111     public void testAsRDF()
112         {
113         RDFNode r = model.asRDFNode( node( "a" ) );
114         }
115     
116     public void testRemoveAll()
117         {
118         testRemoveAll( "" );
119         testRemoveAll( "a RR b" );
120         testRemoveAll( "x P y; a Q b; c R 17; _d S 'e'" );
121         testRemoveAll( "subject Predicate 'object'; http://nowhere/x scheme:cunning not:plan" );
122         }
123     
124     protected void testRemoveAll( String JavaDoc statements )
125         {
126         modelAdd( model, statements );
127         assertSame( model, model.removeAll() );
128         assertEquals( "model should have size 0 following removeAll(): ", 0, model.size() );
129         }
130     
131     /**
132         Test cases for RemoveSPO(); each entry is a triple (add, remove, result).
133         <ul>
134         <li>add - the triples to add to the graph to start with
135         <li>remove - the pattern to use in the removal
136         <li>result - the triples that should remain in the graph
137         </ul>
138     */

139     protected String JavaDoc[][] cases =
140         {
141                 { "x R y", "x R y", "" },
142                 { "x R y; a P b", "x R y", "a P b" },
143                 { "x R y; a P b", "?? R y", "a P b" },
144                 { "x R y; a P b", "x R ??", "a P b" },
145                 { "x R y; a P b", "x ?? y", "a P b" },
146                 { "x R y; a P b", "?? ?? ??", "" },
147                 { "x R y; a P b; c P d", "?? P ??", "x R y" },
148                 { "x R y; a P b; x S y", "x ?? ??", "a P b" },
149         };
150     
151     /**
152     Test that remove(s, p, o) works, in the presence of inferencing graphs that
153     mean emptyness isn't available. This is why we go round the houses and
154     test that expected ~= initialContent + addedStuff - removed - initialContent.
155     */

156     public void testRemoveSPO()
157         {
158         ModelCom mc = (ModelCom) ModelFactory.createDefaultModel();
159         for (int i = 0; i < cases.length; i += 1)
160             for (int j = 0; j < 3; j += 1)
161                 {
162                 Model content = getModel();
163                 Model baseContent = copy( content );
164                 modelAdd( content, cases[i][0] );
165                 Triple remove = triple( cases[i][1] );
166                 Node s = remove.getSubject(), p = remove.getPredicate(), o = remove.getObject();
167                 Resource S = (Resource) (s.equals( Node.ANY ) ? null : mc.getRDFNode( s ));
168                 Property P = (Property)((p.equals( Node.ANY ) ? null : mc.getRDFNode( p ).as( Property.class )));
169                 RDFNode O = o.equals( Node.ANY ) ? null : mc.getRDFNode( o );
170                 Model expected = modelWithStatements( cases[i][2] );
171                 content.removeAll( S, P, O );
172                 Model finalContent = copy( content ).remove( baseContent );
173                 assertIsoModels( cases[i][1], expected, finalContent );
174                 }
175         }
176     
177     protected Model copy( Model m )
178         {
179         return ModelFactory.createDefaultModel().add( m );
180         }
181     }
182
183
184 /*
185     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
186     All rights reserved.
187
188     Redistribution and use in source and binary forms, with or without
189     modification, are permitted provided that the following conditions
190     are met:
191
192     1. Redistributions of source code must retain the above copyright
193        notice, this list of conditions and the following disclaimer.
194
195     2. Redistributions in binary form must reproduce the above copyright
196        notice, this list of conditions and the following disclaimer in the
197        documentation and/or other materials provided with the distribution.
198
199     3. The name of the author may not be used to endorse or promote products
200        derived from this software without specific prior written permission.
201
202     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
203     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
204     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
205     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
206     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
208     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
209     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
211     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
212 */
Popular Tags