001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestFileModelAssembler.java,v 1.7 2008/01/02 12:05:55 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.test;
008:
009: import java.io.File;
010:
011: import com.hp.hpl.jena.assembler.*;
012: import com.hp.hpl.jena.assembler.assemblers.FileModelAssembler;
013: import com.hp.hpl.jena.graph.impl.FileGraph;
014: import com.hp.hpl.jena.rdf.model.*;
015: import com.hp.hpl.jena.shared.*;
016: import com.hp.hpl.jena.util.FileUtils;
017:
018: public class TestFileModelAssembler extends ModelAssemblerTestBase {
019: public TestFileModelAssembler(String name) {
020: super (name);
021: }
022:
023: protected Class getAssemblerClass() {
024: return FileModelAssembler.class;
025: }
026:
027: public void testFileModelAssemblerType() {
028: testDemandsMinimalType(new FileModelAssembler(), JA.FileModel);
029: }
030:
031: public void testFileAssemblerExists() {
032: assertInstanceOf(Assembler.class, Assembler.fileModel);
033: assertInstanceOf(FileModelAssembler.class, Assembler.fileModel);
034: }
035:
036: public void testFileAssemblerVocabulary() {
037: assertSubclassOf(JA.FileModel, JA.NamedModel);
038: assertDomain(JA.FileModel, JA.fileEncoding);
039: assertDomain(JA.FileModel, JA.directory);
040: assertDomain(JA.FileModel, JA.mapName);
041: }
042:
043: public void testFileModelAssemblerCreatesFileModels() {
044: FileModelAssembler a = new FileModelAssembler();
045: File x = FileUtils.tempFileName("fileModelAssembler", ".n3");
046: Model m = a.createFileModel(x, "N3", true, false,
047: ReificationStyle.Convenient);
048: assertInstanceOf(FileGraph.class, m.getGraph());
049: FileGraph fg = (FileGraph) m.getGraph();
050: assertEquals(x, fg.name);
051: assertSame(ReificationStyle.Convenient, fg.getReifier()
052: .getStyle());
053: }
054:
055: public void testFileModelAssemblerUsesSpecialisedMethod() {
056: final Model model = ModelFactory.createDefaultModel();
057: FileModelAssembler a = new FileModelAssembler() {
058: public Model createFileModel(File fullName, String lang,
059: boolean create, boolean strict,
060: ReificationStyle style) {
061: return model;
062: }
063: };
064: Resource root = resourceInModel("x rdf:type ja:FileModel; x ja:modelName 'junk'; x ja:directory file:");
065: Model m = a.openModel(root);
066: assertSame(model, m);
067: }
068:
069: public void testFileModelAssemblerUsesLanguage() {
070: final Model model = ModelFactory.createDefaultModel();
071: Resource root = resourceInModel("x rdf:type ja:FileModel; x ja:modelName 'junk'; x ja:directory file:; x ja:fileEncoding 'LANG'");
072: FileModelAssembler a = new FileModelAssembler() {
073: public Model createFileModel(File fullName, String lang,
074: boolean create, boolean strict,
075: ReificationStyle style) {
076: assertEquals("LANG", lang);
077: return model;
078: }
079: };
080: Model m = a.openModel(root);
081: assertSame(model, m);
082: }
083:
084: public void testFileModelAssemblerTrapsBadLanguage() {
085: testTrapsBadLanguage("badLanguage");
086: testTrapsBadLanguage("17");
087: testTrapsBadLanguage("'invalid'xsd:rhubarb");
088: }
089:
090: private void testTrapsBadLanguage(String lang) {
091: final Model model = ModelFactory.createDefaultModel();
092: Resource root = resourceInModel("x rdf:type ja:FileModel; x ja:modelName 'junk'; x ja:directory file:; x ja:fileEncoding <lang>"
093: .replaceAll("<lang>", lang));
094: FileModelAssembler a = new FileModelAssembler() {
095: public Model createFileModel(File fullName, String lang,
096: boolean create, boolean strict,
097: ReificationStyle style) {
098: return model;
099: }
100: };
101: try {
102: a.openModel(root);
103: fail("should trap bad fileEncoding object");
104: } catch (BadObjectException e) {
105: Model m = e.getRoot().getModel();
106: assertEquals(resource("x"), e.getRoot());
107: assertEquals(rdfNode(m, lang), e.getObject());
108: }
109: }
110:
111: public void testFileModelAssemblerUsesStyle() {
112: testUsesStyle("ja:minimal", ReificationStyle.Minimal);
113: testUsesStyle("ja:standard", ReificationStyle.Standard);
114: testUsesStyle("ja:convenient", ReificationStyle.Convenient);
115: }
116:
117: private void testUsesStyle(String styleString,
118: final ReificationStyle style) {
119: final Model model = ModelFactory.createDefaultModel();
120: FileModelAssembler a = new FileModelAssembler() {
121: public Model createFileModel(File fullName, String lang,
122: boolean create, boolean strict, ReificationStyle s) {
123: assertSame(style, s);
124: return model;
125: }
126: };
127: Resource root = resourceInModel("x rdf:type ja:FileModel; x ja:modelName 'junk'; x ja:directory file:"
128: + "; x ja:reificationMode " + styleString);
129: Model m = a.openModel(root);
130: assertSame(model, m);
131: }
132:
133: public void testFileModelAssemblerUsesMode() {
134: testMode(true, true);
135: testMode(false, true);
136: testMode(true, false);
137: try {
138: testMode(false, false);
139: fail("should trap, can nver create");
140: } catch (JenaException e) {
141: pass();
142: }
143: }
144:
145: private void testMode(final boolean mayCreate,
146: final boolean mayReuse) {
147: final Model model = ModelFactory.createDefaultModel();
148: Mode mode = new Mode(mayCreate, mayReuse);
149: FileModelAssembler a = new FileModelAssembler() {
150: public Model createFileModel(File fullName, String lang,
151: boolean create, boolean strict, ReificationStyle s) {
152: if (mayCreate && mayReuse) {
153: assertEquals(
154: "mayCreate && mayReuse implies non-strict",
155: false, strict);
156: }
157: if (mayCreate && !mayReuse) {
158: assertEquals(true, create);
159: assertEquals(true, strict);
160: }
161: if (!mayCreate && mayReuse) {
162: assertEquals(false, create);
163: assertEquals(true, strict);
164: }
165: if (!mayCreate && !mayReuse)
166: throw new JenaException("cannot create");
167: return model;
168: }
169: };
170: Resource root = resourceInModel("x rdf:type ja:FileModel; x ja:modelName 'junk'; x ja:directory file:");
171: Model m = a.openModel(root, mode);
172: assertSame(model, m);
173: }
174:
175: public void testCorrectSimpleModelName() {
176: testCorrectModelName("root/spoo", "root", "spoo", empty);
177: testCorrectModelName("root/branch/spoo", "root/branch", "spoo",
178: empty);
179: testCorrectModelName("/root/spoo", "/root", "spoo", empty);
180: testCorrectModelName("root/spoo", "root", "spoo", empty);
181: }
182:
183: public void testCorrectURIModelName() {
184: testCorrectModelName("root/name/subname", "root",
185: "name/subname", empty);
186: testCorrectModelName("root/name_Ssubname", "root",
187: "name/subname", model("x ja:mapName ja:true"));
188: testCorrectModelName("root/name_Usubname", "root",
189: "name_subname", model("x ja:mapName ja:true"));
190: testCorrectModelName("root/name_Csubname", "root",
191: "name:subname", model("x ja:mapName ja:true"));
192: testCorrectModelName("root/http_C_S_Sdomain_Sdir_Sname",
193: "root", "http://domain/dir/name",
194: model("x ja:mapName ja:true"));
195: }
196:
197: private void testCorrectModelName(String expectedName,
198: String directoryName, String modelName, Model extras) {
199: final Model model = ModelFactory.createDefaultModel();
200: final File wantedFullName = new File(expectedName);
201: final ReificationStyle wantedStyle = ReificationStyle.Standard;
202: final boolean wantedCreate = Mode.DEFAULT.permitCreateNew(null,
203: null);
204: final boolean wantedStrict = Mode.DEFAULT.permitUseExisting(
205: null, null);
206: Resource root = resourceInModel("x rdf:type ja:FileModel; x ja:modelName '"
207: + modelName + "'; x ja:directory file:" + directoryName);
208: root.getModel().add(extras);
209: FileModelAssembler a = new FileModelAssembler() {
210: public Model createFileModel(File fullName, String lang,
211: boolean create, boolean strict,
212: ReificationStyle style) {
213: assertEquals(wantedFullName, fullName);
214: assertEquals(wantedStyle, style);
215: assertEquals(wantedCreate, create);
216: assertEquals(wantedStrict, strict);
217: return model;
218: }
219: };
220: Model m = a.openModel(root);
221: assertSame(model, m);
222: }
223: }
224:
225: /*
226: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
227: * All rights reserved.
228: *
229: * Redistribution and use in source and binary forms, with or without
230: * modification, are permitted provided that the following conditions
231: * are met:
232: * 1. Redistributions of source code must retain the above copyright
233: * notice, this list of conditions and the following disclaimer.
234: * 2. Redistributions in binary form must reproduce the above copyright
235: * notice, this list of conditions and the following disclaimer in the
236: * documentation and/or other materials provided with the distribution.
237: * 3. The name of the author may not be used to endorse or promote products
238: * derived from this software without specific prior written permission.
239: *
240: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
241: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
242: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
243: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
244: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
245: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
249: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
250: */
|