001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestContentAssembler.java,v 1.12 2008/01/02 12:05:57 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.test;
008:
009: import com.hp.hpl.jena.assembler.*;
010: import com.hp.hpl.jena.assembler.assemblers.ContentAssembler;
011: import com.hp.hpl.jena.assembler.exceptions.UnknownEncodingException;
012: import com.hp.hpl.jena.rdf.model.*;
013: import com.hp.hpl.jena.util.*;
014:
015: public class TestContentAssembler extends AssemblerTestBase {
016: protected static String Testing = "testing/assemblers";
017:
018: public TestContentAssembler(String name) {
019: super (name);
020: }
021:
022: protected Class getAssemblerClass() {
023: return ContentAssembler.class;
024: }
025:
026: public void testContentAssemblerType() {
027: testDemandsMinimalType(new ContentAssembler(), JA.Content);
028: }
029:
030: public void testContentVocabulary() {
031: assertSubclassOf(JA.Content, JA.HasFileManager);
032: assertSubclassOf(JA.ContentItem, JA.Content);
033: // assertSubclassOf( JA.LiteralContent, JA.Content );
034: }
035:
036: public void testContent() {
037: Assembler a = new ContentAssembler();
038: Content c = (Content) a
039: .open(resourceInModel("x rdf:type ja:Content"));
040: assertNotNull(c);
041: Model m = ModelFactory.createDefaultModel();
042: c.fill(m);
043: assertEquals(0, m.size());
044: }
045:
046: public void testMultipleLiteralsWorks() {
047: Assembler a = new ContentAssembler();
048: String A = "<eh:/A> a <eh:/Type>.".replaceAll(" ", "\\\\s");
049: String B = "<eh:/Type> a rdfs:Class.".replaceAll(" ", "\\\\s");
050: Resource root = resourceInModel("x rdf:type ja:Content; x rdf:type ja:LiteralContent; x ja:literalContent '"
051: + A + "'; x ja:literalContent '" + B + "'");
052: Content C = (Content) a.open(root);
053: assertIsoModels(
054: model("Type rdf:type rdfs:Class; A rdf:type Type"), C
055: .fill(model("")));
056: }
057:
058: public void testN3StringContentSingleTriples() {
059: testStringContent("_x rdf:value '17'xsd:integer",
060: "_:x rdf:value 17 .");
061: testStringContent("_x rdf:value '42'xsd:integer",
062: "_:x rdf:value 42 .");
063: testStringContent("_x rdfs:label 'cosmetic'",
064: "_:x rdfs:label 'cosmetic' .");
065: testStringContent("_x owl:sameAs spoo",
066: "_:x owl:sameAs <eh:/spoo> .");
067: testStringContent("_x rdf:value '17'xsd:something",
068: "_:x rdf:value '17'^^xsd:something .");
069: testStringContent("_x dc:title 'A\\sTitle'",
070: "_:x dc:title 'A Title' .");
071: }
072:
073: public void testN3StringContentMultipleTriples() {
074: testStringContent("x rdf:value 5; y owl:sameAs x",
075: "<eh:/x> rdf:value 5 . <eh:/y> owl:sameAs <eh:/x> .");
076: }
077:
078: public void testRDFXMLContent() {
079: Assembler a = new ContentAssembler();
080: String Stuff = "<owl:Class></owl:Class>".replaceAll(" ",
081: "\\\\s");
082: Resource root = resourceInModel("x rdf:type ja:Content; x rdf:type ja:LiteralContent; x ja:literalContent '"
083: + Stuff + "'; x ja:contentEncoding 'RDF/XML'");
084: Content c = (Content) a.open(root);
085: assertIsoModels(model("_x rdf:type owl:Class"), c
086: .fill(model("")));
087: }
088:
089: public void testSingleExternalContent() {
090: Assembler a = new ContentAssembler();
091: String source = Testing + "/schema.n3";
092: Resource root = resourceInModel("x rdf:type ja:Content; x rdf:type ja:ExternalContent; x ja:externalContent file:"
093: + source);
094: Content c = (Content) a.open(root);
095: assertIsoModels(FileManager.get().loadModel("file:" + source),
096: c.fill(model("")));
097: }
098:
099: public void testMultipleExternalContent() {
100: Assembler a = new ContentAssembler();
101: String sourceA = Testing + "/schema.n3";
102: String sourceB = Testing + "/schema2.n3";
103: Resource root = resourceInModel("x rdf:type ja:Content; x rdf:type ja:ExternalContent"
104: + "; x ja:externalContent file:"
105: + sourceA
106: + "; x ja:externalContent file:" + sourceB);
107: Content c = (Content) a.open(root);
108: Model wanted = FileManager.get().loadModel("file:" + sourceA)
109: .add(FileManager.get().loadModel("file:" + sourceB));
110: assertIsoModels(wanted, c.fill(model("")));
111: }
112:
113: public void testIndirectContent() {
114: Assembler a = new ContentAssembler();
115: Resource root = resourceInModel("x rdf:type ja:Content; x ja:content y"
116: + "; y rdf:type ja:Content; y ja:content z"
117: + "; z rdf:type ja:Content; z ja:quotedContent A; A P B");
118: Content c = (Content) a.open(root);
119: Model wanted = model("A P B");
120: assertIsoModels(wanted, c.fill(model("")));
121: }
122:
123: public void testTrapsBadEncodings() {
124: Assembler a = new ContentAssembler();
125: Resource root = resourceInModel("x rdf:type ja:Content; x ja:contentEncoding 'bogus'; x ja:literalContent 'sham'");
126: try {
127: a.open(root);
128: fail("should trap bad encoding");
129: } catch (UnknownEncodingException e) {
130: assertEquals("bogus", e.getEncoding());
131: assertEquals(resource("x"), e.getRoot());
132: }
133: }
134:
135: public void testContentTrapsBadObjects() {
136: testContentTrapsBadObjects("ja:content", "17");
137: // testContentTrapsBadObjects( "ja:externalContent", "17" );
138: testContentTrapsBadObjects("ja:quotedContent", "17");
139: testContentTrapsBadObjects("ja:literalContent", "aResource");
140: testContentTrapsBadObjects("ja:literalContent", "17");
141: testContentTrapsBadObjects("ja:literalContent", "'plume'fr");
142: }
143:
144: private void testContentTrapsBadObjects(String property,
145: String value) {
146: Assembler a = new ContentAssembler();
147: Resource root = resourceInModel("x rdf:type ja:Content; x <property> <value>"
148: .replaceAll("<property>", property).replaceAll(
149: "<value>", value));
150: try {
151: a.open(root);
152: fail("should trap bad content resource");
153: } catch (BadObjectException e) {
154: assertEquals(resource("x"), e.getRoot());
155: assertEquals(rdfNode(empty, value), e.getObject());
156: }
157: }
158:
159: public void testMixedContent() {
160: Assembler a = new ContentAssembler();
161: String source = Testing + "/schema.n3";
162: Resource root = resourceInModel("x rdf:type ja:Content; x rdf:type ja:LiteralContent; x rdf:type ja:ExternalContent"
163: + "; x ja:literalContent '<eh:/eggs>\\srdf:type\\srdf:Property.'"
164: + "; x ja:externalContent file:" + source);
165: Content c = (Content) a.open(root);
166: Model wanted = FileManager.get().loadModel("file:" + source)
167: .add(model("eggs rdf:type rdf:Property"));
168: assertIsoModels(wanted, c.fill(model("")));
169: }
170:
171: public void testSingleContentQuotation() {
172: Assembler a = new ContentAssembler();
173: Resource root = resourceInModel("c rdf:type ja:Content; c rdf:type ja:QuotedContent; c ja:quotedContent x; x P A; x Q B");
174: Content c = (Content) a.open(root);
175: assertIsoModels(model("x P A; x Q B"), c.fill(model("")));
176: }
177:
178: public void testMultipleContentQuotation() {
179: Assembler a = new ContentAssembler();
180: Resource root = resourceInModel("c rdf:type ja:Content; c rdf:type ja:QuotedContent; c ja:quotedContent x"
181: + "; c ja:quotedContent y; x P A; x Q B; y R C");
182: Content c = (Content) a.open(root);
183: assertIsoModels(model("x P A; x Q B; y R C"), c.fill(model("")));
184: }
185:
186: public void testContentLoadsPrefixMappings() {
187: Assembler a = new ContentAssembler();
188: String content = "@prefix foo: <eh:/foo#>. <eh:/eggs> rdf:type rdf:Property."
189: .replaceAll(" ", "\\\\s");
190: Resource root = resourceInModel("x rdf:type ja:Content; x rdf:type ja:LiteralContent; x ja:literalContent '"
191: + content + "'");
192: Content c = (Content) a.open(root);
193: Model m = ModelFactory.createDefaultModel();
194: c.fill(m);
195: assertEquals("eh:/foo#", m.getNsPrefixURI("foo"));
196: }
197:
198: protected void testStringContent(String expected, String n3) {
199: Assembler a = new ContentAssembler();
200: Resource root = resourceInModel("x rdf:type ja:Content; x rdf:type ja:LiteralContent; x ja:literalContent '"
201: + n3.replaceAll(" ", "\\\\s") + "'");
202: Content c = (Content) a.open(root);
203: Model m = ModelFactory.createDefaultModel();
204: c.fill(m);
205: assertIsoModels(model(expected), m);
206: }
207:
208: /*
209: -- ContentAssembler FileManager tests ----------------------------------
210: */
211:
212: public void testContentAssemblerHasNoDefaultFileManager() {
213: assertNull("by default, ContentAssemblers have no FileManager",
214: new ContentAssembler().getFileManager());
215: }
216:
217: public void testContentAssemblerHasSuppliedFileManager() {
218: FileManager fm = new FileManager();
219: assertSame(fm, new ContentAssembler(fm).getFileManager());
220: }
221:
222: public void testUsesSuppliedFileManager() {
223: final boolean[] used = { false };
224: FileManager fm = new FileManager() {
225: public Model loadModel(String filenameOrURI) {
226: used[0] = true;
227: return FileManager.get().loadModel(filenameOrURI);
228: }
229: };
230: Assembler a = new ContentAssembler(fm);
231: String source = Testing + "/schema.n3";
232: Resource root = resourceInModel("x rdf:type ja:Content; x rdf:type ja:ExternalContent; x ja:externalContent file:"
233: + source);
234: Content c = (Content) a.open(root);
235: assertIsoModels(FileManager.get().loadModel("file:" + source),
236: c.fill(model("")));
237: assertTrue("the supplied file manager must have been used",
238: used[0]);
239: }
240:
241: public void testContentAssemblerUsesFileManagerProperty() {
242: Model expected = model("a P b");
243: String fileName = "file:spoo";
244: FixedFileManager fm = new FixedFileManager(expected, fileName);
245: NamedObjectAssembler noa = new NamedObjectAssembler(
246: resource("F"), fm);
247: Resource root = resourceInModel("x rdf:type ja:Content; x rdf:type ja:ExternalContent; x ja:externalContent <F>; x ja:fileManager F"
248: .replaceAll("<F>", fileName));
249: Assembler a = new ContentAssembler();
250: Content c = (Content) a.open(noa, root);
251: assertTrue(fm.wasUsed());
252: assertIsoModels(expected, c.fill(model()));
253: }
254:
255: private final class FixedFileManager extends FileManager {
256: private final Model expected;
257: private final String fileName;
258: private boolean used;
259:
260: private FixedFileManager(Model expected, String fileName) {
261: this .expected = expected;
262: this .fileName = fileName;
263: }
264:
265: public Model loadModel(String filenameOrURI) {
266: used = true;
267: assertEquals(fileName, filenameOrURI);
268: return expected;
269: }
270:
271: public boolean wasUsed() {
272: return used;
273: }
274: }
275:
276: }
277:
278: /*
279: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
280: * All rights reserved.
281: *
282: * Redistribution and use in source and binary forms, with or without
283: * modification, are permitted provided that the following conditions
284: * are met:
285: * 1. Redistributions of source code must retain the above copyright
286: * notice, this list of conditions and the following disclaimer.
287: * 2. Redistributions in binary form must reproduce the above copyright
288: * notice, this list of conditions and the following disclaimer in the
289: * documentation and/or other materials provided with the distribution.
290: * 3. The name of the author may not be used to endorse or promote products
291: * derived from this software without specific prior written permission.
292: *
293: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
294: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
295: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
296: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
297: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
298: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
299: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
300: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
301: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
302: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
303: */
|