001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestFileGraph.java,v 1.20 2008/01/02 12:05:32 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.test;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.graph.impl.*;
011: import com.hp.hpl.jena.rdf.model.*;
012: import com.hp.hpl.jena.shared.ReificationStyle;
013: import com.hp.hpl.jena.util.FileUtils;
014:
015: import java.io.*;
016: import java.util.*;
017:
018: import junit.framework.*;
019:
020: /**
021: Test FileGraph by seeing if we can make some file graphs and then read
022: them back.
023:
024: @author hedgehog
025: */
026:
027: public class TestFileGraph extends GraphTestBase {
028: public TestFileGraph(String name) {
029: super (name);
030: }
031:
032: // TODO want a wider variety of cases, now we've discovered how to abstract.
033: public static TestSuite suite() {
034: TestSuite result = new TestSuite(TestFileGraph.class);
035: result.addTest(new Case("x /R y", "xxxA", ".rdf"));
036: result.addTest(new Case("x /R y", "xxxB", ".n3"));
037: result.addTest(new Case("x /R y", "xxxC", ".nt"));
038: result.addTest(new Case("x /R y; p /R q", "xxxD", ".rdf"));
039: result.addTest(new Case("x /R y; p /R q", "xxxE", ".n3"));
040: result.addTest(new Case("x /R y; p /R q", "xxxF", ".nt"));
041: result.addTest(new Case("http://domain/S ftp:ftp/P O", "xxxG",
042: ".rdf"));
043: result.addTest(new Case("http://domain/S ftp:ftp/P O", "xxxH",
044: ".nt"));
045: result.addTest(new Case("http://domain/S ftp:ftp/P O", "xxxI",
046: ".n3"));
047: return result;
048: }
049:
050: public void testPlausibleGraphname() {
051: assertTrue(FileGraph.isPlausibleGraphName("agnessi.rdf"));
052: assertTrue(FileGraph.isPlausibleGraphName("parabola.nt"));
053: assertTrue(FileGraph.isPlausibleGraphName("hyperbola.n3"));
054: assertTrue(FileGraph.isPlausibleGraphName("chris.dollin.n3"));
055: assertTrue(FileGraph
056: .isPlausibleGraphName("hedgehog.spine.end.rdf"));
057: }
058:
059: public void testisPlausibleUppercaseGraphname() {
060: assertTrue(FileGraph.isPlausibleGraphName("LOUDER.RDF"));
061: assertTrue(FileGraph.isPlausibleGraphName("BRIDGE.NT"));
062: assertTrue(FileGraph.isPlausibleGraphName("NOTN2.N3"));
063: assertTrue(FileGraph.isPlausibleGraphName("chris.dollin.N3"));
064: assertTrue(FileGraph
065: .isPlausibleGraphName("hedgehog.spine.end.RDF"));
066: }
067:
068: public void testImPlausibleGraphName() {
069: assertFalse(FileGraph.isPlausibleGraphName("undecorated"));
070: assertFalse(FileGraph.isPlausibleGraphName("danger.exe"));
071: assertFalse(FileGraph.isPlausibleGraphName("pretty.jpg"));
072: assertFalse(FileGraph.isPlausibleGraphName("FileGraph.java"));
073: assertFalse(FileGraph.isPlausibleGraphName("infix.rdf.c"));
074: }
075:
076: public void testTransactionCommit() {
077: Graph initial = graphWith("initial hasValue 42; also hasURI hello");
078: Graph extra = graphWith("extra hasValue 17; also hasURI world");
079: File foo = FileUtils.tempFileName("fileGraph", ".nt");
080:
081: Graph g = new FileGraph(foo, true, true);
082: g.getBulkUpdateHandler().add(initial);
083: g.getTransactionHandler().begin();
084: g.getBulkUpdateHandler().add(extra);
085: g.getTransactionHandler().commit();
086: Graph union = graphWith("");
087: union.getBulkUpdateHandler().add(initial);
088: union.getBulkUpdateHandler().add(extra);
089: assertIsomorphic(union, g);
090: Model inFile = ModelFactory.createDefaultModel();
091: inFile.read("file:///" + foo, "N-TRIPLES");
092: assertIsomorphic(union, inFile.getGraph());
093: }
094:
095: public void testTransactionAbort() {
096: Graph initial = graphWith("initial hasValue 42; also hasURI hello");
097: Graph extra = graphWith("extra hasValue 17; also hasURI world");
098: File foo = FileUtils.tempFileName("fileGraph", ".n3");
099: Graph g = new FileGraph(foo, true, true);
100: g.getBulkUpdateHandler().add(initial);
101: g.getTransactionHandler().begin();
102: g.getBulkUpdateHandler().add(extra);
103: g.getTransactionHandler().abort();
104: assertIsomorphic(initial, g);
105: }
106:
107: public void testTransactionCommitThenAbort() {
108: Graph initial = graphWith("A pings B; B pings C");
109: Graph extra = graphWith("C pingedBy B; fileGraph rdf:type Graph");
110: File foo = FileUtils.tempFileName("fileGraph", ".nt");
111: Graph g = new FileGraph(foo, true, true);
112: g.getTransactionHandler().begin();
113: g.getBulkUpdateHandler().add(initial);
114: g.getTransactionHandler().commit();
115: g.getTransactionHandler().begin();
116: g.getBulkUpdateHandler().add(extra);
117: g.getTransactionHandler().abort();
118: assertIsomorphic(initial, g);
119: Model inFile = ModelFactory.createDefaultModel();
120: inFile.read("file:///" + foo, "N-TRIPLES");
121: assertIsomorphic(initial, inFile.getGraph());
122: }
123:
124: public void testClosingNotifys() {
125: final List history = new ArrayList();
126: FileGraph.NotifyOnClose n = new FileGraph.NotifyOnClose() {
127: public void notifyClosed(File f) {
128: history.add(f);
129: }
130: };
131: File file = FileUtils.tempFileName("fileGraph", ".nt");
132: Graph g = new FileGraph(n, file, true, true,
133: ReificationStyle.Minimal);
134: assertEquals(new ArrayList(), history);
135: g.close();
136: assertEquals(oneElementList(file), history);
137: }
138:
139: protected List oneElementList(Object x) {
140: List result = new ArrayList();
141: result.add(x);
142: return result;
143: }
144:
145: /**
146: Test that the graph encoded as the test-string content can be
147: written out to a temporary file generated from the prefix and suffix,
148: and then read back correctly. The temporary files are marked as
149: delete-on-exit to try and avoid cluttering the user's filespace ...
150: */
151: private static class Case extends TestFileGraph {
152: String content;
153: String prefix;
154: String suffix;
155:
156: Case(String content, String prefix, String suffix) {
157: super ("Case: " + content + " in " + prefix + "*" + suffix);
158: this .content = content;
159: this .prefix = prefix;
160: this .suffix = suffix;
161: }
162:
163: public void runTest() {
164: File foo = FileUtils.tempFileName(prefix, suffix);
165: Graph original = graphWith(content);
166: Graph g = new FileGraph(foo, true, true);
167: g.getBulkUpdateHandler().add(original);
168: g.close();
169: Graph g2 = new FileGraph(foo, false, true);
170: assertIsomorphic(original, g2);
171: g2.close();
172: }
173: }
174:
175: }
176:
177: /*
178: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
179: All rights reserved.
180:
181: Redistribution and use in source and binary forms, with or without
182: modification, are permitted provided that the following conditions
183: are met:
184:
185: 1. Redistributions of source code must retain the above copyright
186: notice, this list of conditions and the following disclaimer.
187:
188: 2. Redistributions in binary form must reproduce the above copyright
189: notice, this list of conditions and the following disclaimer in the
190: documentation and/or other materials provided with the distribution.
191:
192: 3. The name of the author may not be used to endorse or promote products
193: derived from this software without specific prior written permission.
194:
195: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
196: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
200: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
202: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
203: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
204: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
205: */
|