KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > test > AbstractTestGraphMaker


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: AbstractTestGraphMaker.java,v 1.17 2005/03/16 14:46:35 chris-dollin Exp $
5 */

6
7 package com.hp.hpl.jena.graph.test;
8
9 import java.util.Set JavaDoc;
10
11 import com.hp.hpl.jena.graph.*;
12 import com.hp.hpl.jena.shared.*;
13
14 /**
15     Abstract base class for testing graph factories. Subclasses define the
16     method <code>getGraphFactory()</code> which supplies a new graph
17     factory to be tested: ATGF invokes that during <code>setUp</code>
18     and closes it in <code>tearDown</code>.
19 <p>
20     This bunch of tests is not remotely exhaustive, but it was sufficent to
21     drive the development of the first full graph factory. (Although at the time
22     it wasn't abstract.)
23     
24     @author hedgehog
25 */

26
27 public abstract class AbstractTestGraphMaker extends GraphTestBase
28     {
29     public AbstractTestGraphMaker( String JavaDoc name )
30         { super( name ); }
31             
32     public abstract GraphMaker getGraphMaker();
33     
34     private GraphMaker gf;
35     
36     public void setUp()
37         { gf = getGraphMaker(); }
38         
39     public void tearDown()
40         { gf.close(); }
41
42     /**
43         A trivial test that getGraph delivers a proper graph, not cheating with null, and that
44         getGraph() "always" delivers the same Graph.
45     */

46     public void testGetGraph()
47         {
48         Graph g1 = gf.getGraph();
49         assertFalse( "should deliver a Graph", g1 == null );
50         assertSame( g1, gf.getGraph() );
51         g1.close();
52         }
53         
54     public void testCreateGraph()
55         {
56         assertDiffer( "each created graph must differ", gf.createGraph(), gf.createGraph() );
57         }
58     
59     public void testAnyName()
60         {
61         gf.createGraph( "plain" ).close();
62         gf.createGraph( "with.dot" ).close();
63         gf.createGraph( "http://electric-hedgehog.net/topic#marker" ).close();
64         }
65         
66     /**
67         Test that we can't create a graph with the same name twice.
68     */

69     public void testCannotCreateTwice()
70         {
71         String JavaDoc name = jName( "bonsai" );
72         gf.createGraph( name, true );
73         try
74             {
75             gf.createGraph( name, true );
76             fail( "should not be able to create " + name + " twice" );
77             }
78         catch (AlreadyExistsException e)
79             {}
80         }
81         
82     private String JavaDoc jName( String JavaDoc name )
83         { return "jena-test-AbstractTestGraphMaker-" + name; }
84         
85     public void testCanCreateTwice()
86         {
87         String JavaDoc name = jName( "bridge" );
88         Graph g1 = gf.createGraph( name, true );
89         Graph g2 = gf.createGraph( name, false );
90         assertTrue( "graphs should be the same", sameGraph( g1, g2 ) );
91         Graph g3 = gf.createGraph( name );
92         assertTrue( "graphs should be the same", sameGraph( g1, g3 ) );
93         }
94     
95     /**
96         Test that we cannot open a graph that does not exist.
97     */

98     public void testCannotOpenUncreated()
99         {
100         String JavaDoc name = jName( "noSuchGraph" );
101         try { gf.openGraph( name, true ); fail( name + " should not exist" ); }
102         catch (DoesNotExistException e) { }
103         }
104         
105     /**
106         Test that we *can* open a graph that hasn't been created
107     */

108     public void testCanOpenUncreated()
109         {
110         String JavaDoc name = jName( "willBeCreated" );
111         Graph g1 = gf.openGraph( name );
112         g1.close();
113         gf.openGraph( name, true );
114         }
115     
116     /**
117         Utility - test that a graph with the given name exists.
118      */

119     private void testExists( String JavaDoc name )
120         { assertTrue( name + " should exist", gf.hasGraph( name ) ); }
121      
122         
123     /**
124         Utility - test that no graph with the given name exists.
125      */

126     private void testDoesNotExist( String JavaDoc name )
127         { assertFalse( name + " should exist", gf.hasGraph( name ) ); }
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         {
138         String JavaDoc alpha = jName( "alpha" ), beta = jName( "beta" );
139         Graph g1 = gf.createGraph( alpha, true );
140         Graph h1 = gf.createGraph( beta, true );
141         Graph g2 = gf.openGraph( alpha, true );
142         Graph h2 = gf.openGraph( beta, true );
143         assertTrue( "should find alpha", sameGraph( g1, g2 ) );
144         assertTrue( "should find beta", sameGraph( h1, h2 ) );
145         }
146         
147     /**
148         Weak test for "same graph": adding this to one is visible in t'other.
149         Stopgap for use in testCanFindCreatedGraph.
150         TODO: clean that test up (need help from DB group)
151     */

152     private boolean sameGraph( Graph g1, Graph g2 )
153         {
154         Node S = node( "S" ), P = node( "P" ), O = node( "O" );
155         g1.add( Triple.create( S, P, O ) );
156         g2.add( Triple.create( O, P, S ) );
157         return g2.contains( S, P, O ) && g1.contains( O, P, S );
158         }
159         
160     /**
161         Test that we can remove a graph from the factory without disturbing
162         another graph's binding.
163      */

164     public void testCanRemoveGraph()
165         {
166         String JavaDoc alpha = jName( "bingo" ), beta = jName( "brillo" );
167         gf.createGraph( alpha, true );
168         gf.createGraph( beta, true );
169         testExists( alpha );
170         testExists( beta );
171         gf.removeGraph( alpha );
172         testExists( beta );
173         testDoesNotExist( alpha );
174         }
175         
176     public void testHasnt()
177         {
178         assertFalse( "no such graph", gf.hasGraph( "john" ) );
179         assertFalse( "no such graph", gf.hasGraph( "paul" ) );
180         assertFalse( "no such graph", gf.hasGraph( "george" ) );
181     /* */
182         gf.createGraph( "john", true );
183         assertTrue( "john now exists", gf.hasGraph( "john" ) );
184         assertFalse( "no such graph", gf.hasGraph( "paul" ) );
185         assertFalse( "no such graph", gf.hasGraph( "george" ) );
186     /* */
187         gf.createGraph( "paul", true );
188         assertTrue( "john still exists", gf.hasGraph( "john" ) );
189         assertTrue( "paul now exists", gf.hasGraph( "paul" ) );
190         assertFalse( "no such graph", gf.hasGraph( "george" ) );
191     /* */
192         gf.removeGraph( "john" );
193         assertFalse( "john has been removed", gf.hasGraph( "john" ) );
194         assertTrue( "paul still exists", gf.hasGraph( "paul" ) );
195         assertFalse( "no such graph", gf.hasGraph( "george" ) );
196         }
197         
198     public void testCarefulClose()
199         {
200         Graph x = gf.createGraph( "x" );
201         Graph y = gf.openGraph( "x" );
202         x.add( triple( "a BB c" ) );
203         x.close();
204         y.add( triple( "p RR q" ) );
205         y.close();
206         }
207         
208     /**
209         Test that a maker with no graphs lists no names.
210     */

211     public void testListNoGraphs()
212         {
213         Set JavaDoc s = iteratorToSet( gf.listGraphs() );
214         if (s.size() > 0)
215             fail( "found names from 'empty' graph maker: " + s );
216         }
217     
218     /**
219         Test that a maker with three graphs inserted lists those three grapsh; we don't
220         mind what order they appear in. We also use funny names to ensure that the spelling
221         that goes in is the one that comes out [should really be in a separate test].
222     */

223     public void testListThreeGraphs()
224         { String JavaDoc x = "x", y = "y/sub", z = "z:boo";
225         gf.createGraph( x ).close();
226         gf.createGraph( y ).close();
227         gf.createGraph( z ).close();
228         Set JavaDoc s = iteratorToSet( gf.listGraphs() );
229         assertEquals( 3, s.size() );
230         assertTrue( s.contains( x ) );
231         assertTrue( s.contains( y ) );
232         assertTrue( s.contains( z ) ); }
233         
234     /**
235         Test that a maker with some things put in and then some removed gets the right
236         things listed.
237     */

238     public void testListAfterDelete()
239         { String JavaDoc x = "x_y", y = "y//zub", z = "a:b/c";
240         gf.createGraph( x ).close();
241         gf.createGraph( y ).close();
242         gf.createGraph( z ).close();
243         gf.removeGraph( x );
244         Set JavaDoc s = iteratorToSet( gf.listGraphs() );
245         assertEquals( 2, s.size() );
246         assertTrue( s.contains( y ) );
247         assertTrue( s.contains( z ) ); }
248         
249     }
250
251 /*
252     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
253     All rights reserved.
254
255     Redistribution and use in source and binary forms, with or without
256     modification, are permitted provided that the following conditions
257     are met:
258
259     1. Redistributions of source code must retain the above copyright
260        notice, this list of conditions and the following disclaimer.
261
262     2. Redistributions in binary form must reproduce the above copyright
263        notice, this list of conditions and the following disclaimer in the
264        documentation and/or other materials provided with the distribution.
265
266     3. The name of the author may not be used to endorse or promote products
267        derived from this software without specific prior written permission.
268
269     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
270     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
271     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
272     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
273     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
274     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
275     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
276     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
277     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279 */
Popular Tags