001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: ModelTestBase.java,v 1.40 2008/01/02 12:04:43 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.test;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.graph.test.*;
011: import com.hp.hpl.jena.rdf.model.*;
012: import com.hp.hpl.jena.shared.*;
013: import com.hp.hpl.jena.util.CollectionFactory;
014:
015: import java.util.*;
016:
017: /**
018: provides useful functionality for testing models, eg building small models
019: from strings, testing equality, etc.
020:
021: @author kers
022: */
023:
024: public class ModelTestBase extends GraphTestBase {
025: public ModelTestBase(String name) {
026: super (name);
027: }
028:
029: protected static Model aModel = extendedModel();
030:
031: protected static final Model empty = ModelFactory
032: .createDefaultModel();
033:
034: protected static Model extendedModel() {
035: Model result = ModelFactory.createDefaultModel();
036: result.setNsPrefixes(PrefixMapping.Extended);
037: return result;
038: }
039:
040: protected static String nice(RDFNode n) {
041: return nice(n.asNode());
042: }
043:
044: /**
045: create a Statement in a given Model with (S, P, O) extracted by parsing a string.
046:
047: @param m the model the statement is attached to
048: @param an "S P O" string.
049: @return m.createStatement(S, P, O)
050: */
051: public static Statement statement(Model m, String fact) {
052: StringTokenizer st = new StringTokenizer(fact);
053: Resource sub = resource(m, st.nextToken());
054: Property pred = property(m, st.nextToken());
055: RDFNode obj = rdfNode(m, st.nextToken());
056: return m.createStatement(sub, pred, obj);
057: }
058:
059: public static Statement statement(String fact) {
060: return statement(aModel, fact);
061: }
062:
063: public static RDFNode rdfNode(Model m, String s) {
064: return m.asRDFNode(NodeCreateUtils.create(m, s));
065: }
066:
067: public static RDFNode rdfNode(Model m, String s, Class c) {
068: return rdfNode(m, s).as(c);
069: }
070:
071: protected static Resource resource() {
072: return ResourceFactory.createResource();
073: }
074:
075: public static Resource resource(String s) {
076: return resource(aModel, s);
077: }
078:
079: public static Resource resource(Model m, String s) {
080: return (Resource) rdfNode(m, s);
081: }
082:
083: public static Property property(String s) {
084: return property(aModel, s);
085: }
086:
087: public static Property property(Model m, String s) {
088: return (Property) rdfNode(m, s).as(Property.class);
089: }
090:
091: public static Literal literal(Model m, String s) {
092: return (Literal) rdfNode(m, s).as(Literal.class);
093: }
094:
095: /**
096: Create an array of Statements parsed from a semi-separated string.
097:
098: @param m a model to serve as a statement factory
099: @param facts a sequence of semicolon-separated "S P O" facts
100: @return a Statement[] of the (S P O) statements from the string
101: */
102: public static Statement[] statements(Model m, String facts) {
103: ArrayList sl = new ArrayList();
104: StringTokenizer st = new StringTokenizer(facts, ";");
105: while (st.hasMoreTokens())
106: sl.add(statement(m, st.nextToken()));
107: return (Statement[]) sl.toArray(new Statement[sl.size()]);
108: }
109:
110: /**
111: Create an array of Resources from a whitespace-separated string
112:
113: @param m a model to serve as a resource factory
114: @param items a whitespace-separated sequence to feed to resource
115: @return a RDFNode[] of the parsed resources
116: */
117: public static Resource[] resources(Model m, String items) {
118: ArrayList rl = new ArrayList();
119: StringTokenizer st = new StringTokenizer(items);
120: while (st.hasMoreTokens())
121: rl.add(resource(m, st.nextToken()));
122: return (Resource[]) rl.toArray(new Resource[rl.size()]);
123: }
124:
125: /**
126: Answer the set of resources given by the space-separated
127: <code>items</code> string. Each resource specification is interpreted
128: as per <code>resource</code>.
129: */
130: public static Set resourceSet(String items) {
131: Set result = new HashSet();
132: StringTokenizer st = new StringTokenizer(items);
133: while (st.hasMoreTokens())
134: result.add(resource(st.nextToken()));
135: return result;
136: }
137:
138: /**
139: add to a model all the statements expressed by a string.
140:
141: @param m the model to be updated
142: @param facts a sequence of semicolon-separated "S P O" facts
143: @return the updated model
144: */
145: public static Model modelAdd(Model m, String facts) {
146: StringTokenizer semis = new StringTokenizer(facts, ";");
147: while (semis.hasMoreTokens())
148: m.add(statement(m, semis.nextToken()));
149: return m;
150: }
151:
152: /**
153: makes a model initialised with statements parsed from a string.
154:
155: @param facts a string in semicolon-separated "S P O" format
156: @return a model containing those facts
157: */
158: public static Model modelWithStatements(String facts) {
159: return modelWithStatements(ReificationStyle.Standard, facts);
160: }
161:
162: /**
163: makes a model with a given reiifcation style, initialised with statements parsed
164: from a string.
165:
166: @param style the required reification style
167: @param facts a string in semicolon-separated "S P O" format
168: @return a model containing those facts
169: */
170: public static Model modelWithStatements(ReificationStyle style,
171: String facts) {
172: return modelAdd(createModel(style), facts);
173: }
174:
175: /**
176: make a model with a given reification style, give it Extended prefixes
177: */
178: public static Model createModel(ReificationStyle style) {
179: Model result = ModelFactory.createDefaultModel(style);
180: result.setNsPrefixes(PrefixMapping.Extended);
181: return result;
182: }
183:
184: /**
185: Answer a default model; it exists merely to abbreviate the rather long explicit
186: invocation.
187:
188: @return a new default [aka memory-based] model
189: */
190: public static Model createMemModel() {
191: return ModelFactory.createDefaultModel();
192: }
193:
194: /**
195: test that two models are isomorphic and fail if they are not.
196:
197: @param title a String appearing at the beginning of the failure message
198: @param wanted the model value that is expected
199: @param got the model value to check
200: @exception if the models are not isomorphic
201: */
202: public static void assertIsoModels(String title, Model wanted,
203: Model got) {
204: if (wanted.isIsomorphicWith(got) == false) {
205: Map map = CollectionFactory.createHashedMap();
206: fail(title + ": expected " + nice(wanted.getGraph(), map)
207: + "\n but had " + nice(got.getGraph(), map));
208: }
209: }
210:
211: /**
212: Fail if the two models are not isomorphic. See assertIsoModels(String,Model,Model).
213: */
214: public static void assertIsoModels(Model wanted, Model got) {
215: assertIsoModels("models must be isomorphic", wanted, got);
216: }
217:
218: }
219:
220: /*
221: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
222: All rights reserved.
223:
224: Redistribution and use in source and binary forms, with or without
225: modification, are permitted provided that the following conditions
226: are met:
227:
228: 1. Redistributions of source code must retain the above copyright
229: notice, this list of conditions and the following disclaimer.
230:
231: 2. Redistributions in binary form must reproduce the above copyright
232: notice, this list of conditions and the following disclaimer in the
233: documentation and/or other materials provided with the distribution.
234:
235: 3. The name of the author may not be used to endorse or promote products
236: derived from this software without specific prior written permission.
237:
238: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
239: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
240: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
241: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
242: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
243: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
248: */
|