KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: ModelTestBase.java,v 1.33 2005/03/08 15:40:23 chris-dollin Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model.test;
8
9 import com.hp.hpl.jena.graph.*;
10 import com.hp.hpl.jena.graph.test.*;
11 import com.hp.hpl.jena.rdf.model.*;
12 import com.hp.hpl.jena.rdf.model.impl.*;
13 import com.hp.hpl.jena.shared.*;
14 import com.hp.hpl.jena.util.CollectionFactory;
15
16 import java.util.*;
17
18 /**
19     provides useful functionality for testing models, eg building small models
20     from strings, testing equality, etc.
21     
22     @author kers
23 */

24
25 public class ModelTestBase extends GraphTestBase
26     {
27     public ModelTestBase(String JavaDoc name)
28         { super(name); }
29      
30     protected static Model aModel = extendedModel();
31     
32     protected static final Model empty = ModelFactory.createDefaultModel();
33     
34     protected static Model extendedModel()
35         {
36         Model result = ModelFactory.createDefaultModel();
37         result.setNsPrefixes( PrefixMapping.Extended );
38         return result;
39         }
40     
41     protected static String JavaDoc nice( RDFNode n )
42         { return nice( n.asNode() ); }
43     
44      /**
45         create a Statement in a given Model with (S, P, O) extracted by parsing a string.
46         
47         @param m the model the statement is attached to
48         @param an "S P O" string.
49         @return m.createStatement(S, P, O)
50      */

51     public static Statement statement( Model m, String JavaDoc fact )
52          {
53          StringTokenizer st = new StringTokenizer( fact );
54          Resource sub = resource( m, st.nextToken() );
55          Property pred = property( m, st.nextToken() );
56          RDFNode obj = rdfNode( m, st.nextToken() );
57          return m.createStatement( sub, pred, obj );
58          }
59     
60     public static Statement statement( String JavaDoc fact )
61         { return statement( aModel, fact ); }
62          
63      public static RDFNode rdfNode( Model m, String JavaDoc s )
64         {
65         Node n = Node.create( m, s );
66         return (RDFNode) ((ModelCom) m).getNodeAs( n, (n.isLiteral() ? Literal.class : Resource.class) );
67         }
68
69      protected static Resource resource()
70          { return ResourceFactory.createResource(); }
71          
72     public static Resource resource( String JavaDoc s )
73         { return resource( aModel, s ); }
74     
75     public static Resource resource( Model m, String JavaDoc s )
76         { return (Resource) rdfNode( m, s ); }
77         
78     public static Property property( String JavaDoc s )
79         { return property( aModel, s ); }
80     
81     public static Property property( Model m, String JavaDoc s )
82         { return (Property) rdfNode( m, s, Property.class ); }
83         
84     public static Literal literal( Model m, String JavaDoc s )
85         { return (Literal) rdfNode( m, s, Literal.class ); }
86         
87     public static RDFNode rdfNode( Model m, String JavaDoc s, Class JavaDoc c )
88         {
89         Node n = Node.create( m, s );
90         return (RDFNode) ((ModelCom) m).getNodeAs( n, c );
91         }
92          
93      /**
94         Create an array of Statements parsed from a semi-separated string.
95         
96         @param m a model to serve as a statement factory
97         @param facts a sequence of semicolon-separated "S P O" facts
98         @return a Statement[] of the (S P O) statements from the string
99      */

100      public static Statement [] statements( Model m, String JavaDoc facts )
101         {
102         ArrayList sl = new ArrayList();
103         StringTokenizer st = new StringTokenizer( facts, ";" );
104         while (st.hasMoreTokens()) sl.add( statement( m, st.nextToken() ) );
105         return (Statement []) sl.toArray( new Statement[sl.size()] );
106         }
107         
108     /**
109         Create an array of Resources from a whitespace-separated string
110         
111         @param m a model to serve as a resource factory
112         @param items a whitespace-separated sequence to feed to resource
113         @return a RDFNode[] of the parsed resources
114     */

115     public static Resource [] resources( Model m, String JavaDoc items )
116         {
117         ArrayList rl = new ArrayList();
118         StringTokenizer st = new StringTokenizer( items );
119         while (st.hasMoreTokens()) rl.add( resource( m, st.nextToken() ) );
120         return (Resource []) rl.toArray( new Resource[rl.size()] );
121         }
122         
123     /**
124         add to a model all the statements expressed by a string.
125         
126         @param m the model to be updated
127         @param facts a sequence of semicolon-separated "S P O" facts
128         @return the updated model
129     */

130     public static Model modelAdd( Model m, String JavaDoc facts )
131         {
132         StringTokenizer semis = new StringTokenizer( facts, ";" );
133         while (semis.hasMoreTokens()) m.add( statement( m, semis.nextToken() ) );
134         return m;
135         }
136     
137     /**
138         makes a model initialised with statements parsed from a string.
139         
140         @param facts a string in semicolon-separated "S P O" format
141         @return a model containing those facts
142     */

143     public static Model modelWithStatements( String JavaDoc facts )
144         { return modelWithStatements( ReificationStyle.Standard, facts ); }
145
146     /**
147         makes a model with a given reiifcation style, initialised with statements parsed
148         from a string.
149         
150         @param style the required reification style
151         @param facts a string in semicolon-separated "S P O" format
152         @return a model containing those facts
153     */

154     public static Model modelWithStatements( ReificationStyle style, String JavaDoc facts )
155         { return modelAdd( createModel( style ), facts ); }
156         
157     /**
158         make a model with a given reification style, give it Extended prefixes
159     */

160     public static Model createModel( ReificationStyle style )
161         {
162         Model result = ModelFactory.createDefaultModel( style );
163         result.setNsPrefixes( PrefixMapping.Extended );
164         return result;
165         }
166     
167     /**
168         Answer a default model; it exists merely to abbreviate the rather long explicit
169         invocation.
170         
171         @return a new default [aka memory-based] model
172     */

173     public static Model createMemModel()
174         { return ModelFactory.createDefaultModel(); }
175         
176      /**
177         test that two models are isomorphic and fail if they are not.
178         
179         @param title a String appearing at the beginning of the failure message
180         @param wanted the model value that is expected
181         @param got the model value to check
182         @exception if the models are not isomorphic
183      */

184     public void assertIsoModels( String JavaDoc title, Model wanted, Model got )
185         {
186         if (wanted.isIsomorphicWith( got ) == false)
187             {
188             Map map = CollectionFactory.createHashedMap();
189             fail( title + ": expected " + nice( wanted.getGraph(), map ) + "\n but had " + nice( got.getGraph(), map ) );
190             }
191         }
192
193     /**
194         Fail if the two models are not isomorphic. See assertIsoModels(String,Model,Model).
195     */

196     public void assertIsoModels( Model wanted, Model got )
197         { assertIsoModels( "models must be isomorphic", wanted, got ); }
198         
199     }
200
201
202 /*
203     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
204     All rights reserved.
205
206     Redistribution and use in source and binary forms, with or without
207     modification, are permitted provided that the following conditions
208     are met:
209
210     1. Redistributions of source code must retain the above copyright
211        notice, this list of conditions and the following disclaimer.
212
213     2. Redistributions in binary form must reproduce the above copyright
214        notice, this list of conditions and the following disclaimer in the
215        documentation and/or other materials provided with the distribution.
216
217     3. The name of the author may not be used to endorse or promote products
218        derived from this software without specific prior written permission.
219
220     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
221     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
222     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
223     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
224     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
225     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
228     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
229     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230 */
Popular Tags