001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: AbstractTestGraphMaker.java,v 1.21 2008/01/02 12:05:32 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.test;
008:
009: import java.util.Set;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.shared.*;
013:
014: /**
015: Abstract base class for testing graph factories. Subclasses define the
016: method <code>getGraphFactory()</code> which supplies a new graph
017: factory to be tested: ATGF invokes that during <code>setUp</code>
018: and closes it in <code>tearDown</code>.
019: <p>
020: This bunch of tests is not remotely exhaustive, but it was sufficent to
021: drive the development of the first full graph factory. (Although at the time
022: it wasn't abstract.)
023:
024: @author hedgehog
025: */
026:
027: public abstract class AbstractTestGraphMaker extends GraphTestBase {
028: public AbstractTestGraphMaker(String name) {
029: super (name);
030: }
031:
032: public abstract GraphMaker getGraphMaker();
033:
034: private GraphMaker gf;
035:
036: public void setUp() {
037: gf = getGraphMaker();
038: }
039:
040: public void tearDown() {
041: gf.close();
042: }
043:
044: /**
045: A trivial test that getGraph delivers a proper graph, not cheating with null, and that
046: getGraph() "always" delivers the same Graph.
047: */
048: public void testGetGraph() {
049: Graph g1 = gf.getGraph();
050: assertFalse("should deliver a Graph", g1 == null);
051: assertSame(g1, gf.getGraph());
052: g1.close();
053: }
054:
055: public void testCreateGraph() {
056: assertDiffer("each created graph must differ",
057: gf.createGraph(), gf.createGraph());
058: }
059:
060: public void testAnyName() {
061: gf.createGraph("plain").close();
062: gf.createGraph("with.dot").close();
063: gf.createGraph("http://electric-hedgehog.net/topic#marker")
064: .close();
065: }
066:
067: /**
068: Test that we can't create a graph with the same name twice.
069: */
070: public void testCannotCreateTwice() {
071: String name = jName("bonsai");
072: gf.createGraph(name, true);
073: try {
074: gf.createGraph(name, true);
075: fail("should not be able to create " + name + " twice");
076: } catch (AlreadyExistsException e) {
077: }
078: }
079:
080: private String jName(String name) {
081: return "jena-test-AbstractTestGraphMaker-" + name;
082: }
083:
084: public void testCanCreateTwice() {
085: String name = jName("bridge");
086: Graph g1 = gf.createGraph(name, true);
087: Graph g2 = gf.createGraph(name, false);
088: assertTrue("graphs should be the same", sameGraph(g1, g2));
089: Graph g3 = gf.createGraph(name);
090: assertTrue("graphs should be the same", sameGraph(g1, g3));
091: }
092:
093: /**
094: Test that we cannot open a graph that does not exist.
095: */
096: public void testCannotOpenUncreated() {
097: String name = jName("noSuchGraph");
098: try {
099: gf.openGraph(name, true);
100: fail(name + " should not exist");
101: } catch (DoesNotExistException e) {
102: }
103: }
104:
105: /**
106: Test that we *can* open a graph that hasn't been created
107: */
108: public void testCanOpenUncreated() {
109: String name = jName("willBeCreated");
110: Graph g1 = gf.openGraph(name);
111: g1.close();
112: gf.openGraph(name, true);
113: }
114:
115: /**
116: Utility - test that a graph with the given name exists.
117: */
118: private void testExists(String name) {
119: assertTrue(name + " should exist", gf.hasGraph(name));
120: }
121:
122: /**
123: Utility - test that no graph with the given name exists.
124: */
125: private void testDoesNotExist(String name) {
126: assertFalse(name + " should exist", gf.hasGraph(name));
127: }
128:
129: /**
130: Test that we can find a graph once its been created. We need to know
131: if two graphs are "the same" here, which is tricky, because the RDB
132: factory produces non-== graphs that are "the same": we have a temporary
133: work-around but it is not sound.
134: *
135: */
136: public void testCanFindCreatedGraph() {
137: String alpha = jName("alpha"), beta = jName("beta");
138: Graph g1 = gf.createGraph(alpha, true);
139: Graph h1 = gf.createGraph(beta, true);
140: Graph g2 = gf.openGraph(alpha, true);
141: Graph h2 = gf.openGraph(beta, true);
142: assertTrue("should find alpha", sameGraph(g1, g2));
143: assertTrue("should find beta", sameGraph(h1, h2));
144: }
145:
146: /**
147: Weak test for "same graph": adding this to one is visible in t'other.
148: Stopgap for use in testCanFindCreatedGraph.
149: TODO: clean that test up (need help from DB group)
150: */
151: private boolean sameGraph(Graph g1, Graph g2) {
152: Node S = node("S"), P = node("P"), O = node("O");
153: g1.add(Triple.create(S, P, O));
154: g2.add(Triple.create(O, P, S));
155: return g2.contains(S, P, O) && g1.contains(O, P, S);
156: }
157:
158: /**
159: Test that we can remove a graph from the factory without disturbing
160: another graph's binding.
161: */
162: public void testCanRemoveGraph() {
163: String alpha = jName("bingo"), beta = jName("brillo");
164: gf.createGraph(alpha, true);
165: gf.createGraph(beta, true);
166: testExists(alpha);
167: testExists(beta);
168: gf.removeGraph(alpha);
169: testExists(beta);
170: testDoesNotExist(alpha);
171: }
172:
173: public void testHasnt() {
174: assertFalse("no such graph", gf.hasGraph("john"));
175: assertFalse("no such graph", gf.hasGraph("paul"));
176: assertFalse("no such graph", gf.hasGraph("george"));
177: /* */
178: gf.createGraph("john", true);
179: assertTrue("john now exists", gf.hasGraph("john"));
180: assertFalse("no such graph", gf.hasGraph("paul"));
181: assertFalse("no such graph", gf.hasGraph("george"));
182: /* */
183: gf.createGraph("paul", true);
184: assertTrue("john still exists", gf.hasGraph("john"));
185: assertTrue("paul now exists", gf.hasGraph("paul"));
186: assertFalse("no such graph", gf.hasGraph("george"));
187: /* */
188: gf.removeGraph("john");
189: assertFalse("john has been removed", gf.hasGraph("john"));
190: assertTrue("paul still exists", gf.hasGraph("paul"));
191: assertFalse("no such graph", gf.hasGraph("george"));
192: }
193:
194: public void testCarefulClose() {
195: Graph x = gf.createGraph("x");
196: Graph y = gf.openGraph("x");
197: x.add(triple("a BB c"));
198: x.close();
199: y.add(triple("p RR q"));
200: y.close();
201: }
202:
203: /**
204: Test that a maker with no graphs lists no names.
205: */
206: public void testListNoGraphs() {
207: Set s = iteratorToSet(gf.listGraphs());
208: if (s.size() > 0)
209: fail("found names from 'empty' graph maker: " + s);
210: }
211:
212: /**
213: Test that a maker with three graphs inserted lists those three grapsh; we don't
214: mind what order they appear in. We also use funny names to ensure that the spelling
215: that goes in is the one that comes out [should really be in a separate test].
216: */
217: public void testListThreeGraphs() {
218: String x = "x", y = "y/sub", z = "z:boo";
219: Graph X = gf.createGraph(x);
220: Graph Y = gf.createGraph(y);
221: Graph Z = gf.createGraph(z);
222: Set wanted = setOfStrings(x + " " + y + " " + z);
223: assertEquals(wanted, iteratorToSet(gf.listGraphs()));
224: X.close();
225: Y.close();
226: Z.close();
227: }
228:
229: /**
230: Test that a maker with some things put in and then some removed gets the right
231: things listed.
232: */
233: public void testListAfterDelete() {
234: String x = "x_y", y = "y//zub", z = "a:b/c";
235: Graph X = gf.createGraph(x);
236: Graph Y = gf.createGraph(y);
237: Graph Z = gf.createGraph(z);
238: gf.removeGraph(x);
239: Set s = iteratorToSet(gf.listGraphs());
240: assertEquals(setOfStrings(y + " " + z), s);
241: X.close();
242: Y.close();
243: Z.close();
244: }
245:
246: }
247:
248: /*
249: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
250: All rights reserved.
251:
252: Redistribution and use in source and binary forms, with or without
253: modification, are permitted provided that the following conditions
254: are met:
255:
256: 1. Redistributions of source code must retain the above copyright
257: notice, this list of conditions and the following disclaimer.
258:
259: 2. Redistributions in binary form must reproduce the above copyright
260: notice, this list of conditions and the following disclaimer in the
261: documentation and/or other materials provided with the distribution.
262:
263: 3. The name of the author may not be used to endorse or promote products
264: derived from this software without specific prior written permission.
265:
266: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
267: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
268: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
269: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
270: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
271: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
272: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
273: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
274: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
275: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276: */
|