KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: AbstractTestGraph.java,v 1.57 2005/02/21 11:52:34 andy_seaborne Exp $i
5 */

6
7 package com.hp.hpl.jena.graph.test;
8
9 import com.hp.hpl.jena.util.CollectionFactory;
10 import com.hp.hpl.jena.util.iterator.*;
11 import com.hp.hpl.jena.graph.*;
12 import com.hp.hpl.jena.graph.query.*;
13 import com.hp.hpl.jena.mem.GraphMem;
14 import com.hp.hpl.jena.shared.*;
15
16 import java.util.*;
17
18 /**
19     AbstractTestGraph provides a bunch of basic tests for something that
20     purports to be a Graph. The abstract method getGraph must be overridden
21     in subclasses to deliver a Graph of interest.
22     
23     @author kers
24 */

25 public /* abstract */ class AbstractTestGraph extends GraphTestBase
26     {
27     public AbstractTestGraph( String JavaDoc name )
28         { super( name ); }
29         
30     /**
31         Returns a Graph to take part in the test. Must be overridden in
32         a subclass.
33     */

34     // public abstract Graph getGraph();
35

36     public Graph getGraph() { return new GraphMem(); }
37     
38     public Graph getGraphWith( String JavaDoc facts )
39         {
40         Graph g = getGraph();
41         graphAdd( g, facts );
42         return g;
43         }
44         
45     /**
46         This test case was generated by Ian and was caused by GraphMem
47         not keeping up with changes to the find interface.
48     */

49     public void testFindAndContains()
50         {
51         Graph g = getGraph();
52         Node r = Node.create( "r" ), s = Node.create( "s" ), p = Node.create( "P" );
53         g.add( Triple.create( r, p, s ) );
54         assertTrue( g.contains( r, p, Node.ANY ) );
55         assertTrue( g.find( r, p, Node.ANY ).hasNext() );
56         }
57         
58     public void testFindByFluidTriple()
59         {
60         Graph g = getGraphWith( "x y z " );
61         assertTrue( g.find( triple( "?? y z" ) ).hasNext() );
62         assertTrue( g.find( triple( "x ?? z" ) ).hasNext() );
63         assertTrue( g.find( triple( "x y ??" ) ).hasNext() );
64         }
65         
66     public void testContainsConcrete()
67         {
68         Graph g = getGraphWith( "s P o; _x _R _y; x S 0" );
69         assertTrue( g.contains( triple( "s P o" ) ) );
70         assertTrue( g.contains( triple( "_x _R _y" ) ) );
71         assertTrue( g.contains( triple( "x S 0" ) ) );
72     /* */
73         assertFalse( g.contains( triple( "s P Oh" ) ) );
74         assertFalse( g.contains( triple( "S P O" ) ) );
75         assertFalse( g.contains( triple( "s p o" ) ) );
76         assertFalse( g.contains( triple( "_x _r _y" ) ) );
77         assertFalse( g.contains( triple( "x S 1" ) ) );
78         }
79         
80     public void testContainsFluid()
81         {
82         Graph g = getGraphWith( "x R y; a P b" );
83         assertTrue( g.contains( triple( "?? R y" ) ) );
84         assertTrue( g.contains( triple( "x ?? y" ) ) );
85         assertTrue( g.contains( triple( "x R ??" ) ) );
86         assertTrue( g.contains( triple( "?? P b" ) ) );
87         assertTrue( g.contains( triple( "a ?? b" ) ) );
88         assertTrue( g.contains( triple( "a P ??" ) ) );
89         assertTrue( g.contains( triple( "?? R y" ) ) );
90     /* */
91         assertFalse( g.contains( triple( "?? R b" ) ) );
92         assertFalse( g.contains( triple( "a ?? y" ) ) );
93         assertFalse( g.contains( triple( "x P ??" ) ) );
94         assertFalse( g.contains( triple( "?? R x" ) ) );
95         assertFalse( g.contains( triple( "x ?? R" ) ) );
96         assertFalse( g.contains( triple( "a S ??" ) ) );
97         }
98         
99     /**
100         test isEmpty - moved from the QueryHandler code.
101     */

102     public void testIsEmpty()
103         {
104         Graph g = getGraph();
105         if (canBeEmpty( g ))
106             {
107             assertTrue( g.isEmpty() );
108             g.add( Triple.create( "S P O" ) );
109             assertFalse( g.isEmpty() );
110             g.add( Triple.create( "A B C" ) );
111             assertFalse( g.isEmpty() );
112             g.add( Triple.create( "S P O" ) );
113             assertFalse( g.isEmpty() );
114             g.delete( Triple.create( "S P O" ) );
115             assertFalse( g.isEmpty() );
116             g.delete( Triple.create( "A B C" ) );
117             assertTrue( g.isEmpty() );
118             }
119         }
120         
121
122     public void testAGraph()
123         {
124         String JavaDoc title = this.getClass().getName();
125         Graph g = getGraph();
126         int baseSize = g.size();
127         graphAdd( g, "x R y; p S q; a T b" );
128     /* */
129         assertContainsAll( title + ": simple graph", g, "x R y; p S q; a T b" );
130         assertEquals( title + ": size", baseSize + 3, g.size() );
131         graphAdd( g, "spindizzies lift cities; Diracs communicate instantaneously" );
132         assertEquals( title + ": size after adding", baseSize + 5, g.size() );
133         g.delete( triple( "x R y" ) );
134         g.delete( triple( "a T b" ) );
135         assertEquals( title + ": size after deleting", baseSize + 3, g.size() );
136         assertContainsAll( title + ": modified simple graph", g, "p S q; spindizzies lift cities; Diracs communicate instantaneously" );
137         assertOmitsAll( title + ": modified simple graph", g, "x R y; a T b" );
138     /* */
139         ClosableIterator it = g.find( Node.ANY, node("lift"), Node.ANY );
140         assertTrue( title + ": finds some triple(s)", it.hasNext() );
141         assertEquals( title + ": finds a 'lift' triple", triple("spindizzies lift cities"), it.next() );
142         assertFalse( title + ": finds exactly one triple", it.hasNext() );
143         }
144
145 // public void testStuff()
146
// {
147
//// testAGraph( "StoreMem", new GraphMem() );
148
//// testAGraph( "StoreMemBySubject", new GraphMem() );
149
//// String [] empty = new String [] {};
150
//// Graph g = graphWith( "x R y; p S q; a T b" );
151
//// /* */
152
//// assertContainsAll( "simple graph", g, "x R y; p S q; a T b" );
153
//// graphAdd( g, "spindizzies lift cities; Diracs communicate instantaneously" );
154
//// g.delete( triple( "x R y" ) );
155
//// g.delete( triple( "a T b" ) );
156
//// assertContainsAll( "modified simple graph", g, "p S q; spindizzies lift cities; Diracs communicate instantaneously" );
157
//// assertOmitsAll( "modified simple graph", g, "x R y; a T b" );
158
// }
159

160     /**
161         Test that Graphs have transaction support methods, and that if they fail
162         on some g they fail because they do not support the operation.
163     */

164     public void testHasTransactions()
165         {
166         Graph g = getGraph();
167         TransactionHandler th = g.getTransactionHandler();
168         th.transactionsSupported();
169         try { th.begin(); } catch (UnsupportedOperationException JavaDoc x) {}
170         try { th.abort(); } catch (UnsupportedOperationException JavaDoc x) {}
171         try { th.commit(); } catch (UnsupportedOperationException JavaDoc x) {}
172     /* */
173         Command cmd = new Command()
174             { public Object JavaDoc execute() { return null; } };
175         try { th.executeInTransaction( cmd ); }
176         catch (UnsupportedOperationException JavaDoc x) {}
177         }
178
179     static final Triple [] tripleArray = tripleArray( "S P O; A R B; X Q Y" );
180
181     static final List tripleList = Arrays.asList( tripleArray( "i lt j; p equals q" ) );
182         
183     static final Triple [] setTriples = tripleArray
184         ( "scissors cut paper; paper wraps stone; stone breaks scissors" );
185         
186     static final Set tripleSet = CollectionFactory.createHashedSet( Arrays.asList( setTriples ) );
187                 
188     public void testBulkUpdate()
189         {
190         Graph g = getGraph();
191         BulkUpdateHandler bu = g.getBulkUpdateHandler();
192         Graph items = graphWith( "pigs might fly; dead can dance" );
193         int initialSize = g.size();
194     /* */
195         bu.add( tripleArray );
196         testContains( g, tripleArray );
197         testOmits( g, tripleList );
198     /* */
199         bu.add( tripleList );
200         testContains( g, tripleList );
201         testContains( g, tripleArray );
202     /* */
203         bu.add( tripleSet.iterator() );
204         testContains( g, tripleSet.iterator() );
205         testContains( g, tripleList );
206         testContains( g, tripleArray );
207     /* */
208         bu.add( items );
209         testContains( g, items );
210         testContains( g, tripleSet.iterator() );
211         testContains( g, tripleArray );
212         testContains( g, tripleList );
213     /* */
214         bu.delete( tripleArray );
215         testOmits( g, tripleArray );
216         testContains( g, tripleList );
217         testContains( g, tripleSet.iterator() );
218         testContains( g, items );
219     /* */
220         bu.delete( tripleSet.iterator() );
221         testOmits( g, tripleSet.iterator() );
222         testOmits( g, tripleArray );
223         testContains( g, tripleList );
224         testContains( g, items );
225     /* */
226         bu.delete( items );
227         testOmits( g, tripleSet.iterator() );
228         testOmits( g, tripleArray );
229         testContains( g, tripleList );
230         testOmits( g, items );
231     /* */
232         bu.delete( tripleList );
233         assertEquals( "graph has original size", initialSize, g.size() );
234         }
235         
236     public void testBulkAddWithReification()
237         {
238         testBulkAddWithReification( false );
239         testBulkAddWithReification( true );
240         }
241         
242     public void testBulkAddWithReificationPreamble()
243         {
244         Graph g = getGraph();
245         xSPO( g.getReifier() );
246         assertFalse( getReificationTriples( g.getReifier() ).isEmpty() );
247         }
248         
249     public void testBulkAddWithReification( boolean withReifications )
250         {
251         Graph graphToUpdate = getGraph();
252         BulkUpdateHandler bu = graphToUpdate.getBulkUpdateHandler();
253         Graph graphToAdd = graphWith( "pigs might fly; dead can dance" );
254         Reifier updatedReifier = graphToUpdate.getReifier();
255         Reifier addedReifier = graphToAdd.getReifier();
256         xSPOyXYZ( addedReifier );
257         bu.add( graphToAdd, withReifications );
258         assertIsomorphic
259             (
260             withReifications ? getReificationTriples( addedReifier ) : graphWith( "" ),
261             getReificationTriples( updatedReifier )
262             );
263         }
264         
265     protected void xSPOyXYZ( Reifier r )
266         {
267         xSPO( r );
268         r.reifyAs( Node.create( "y" ), Triple.create( "X Y Z" ) );
269         }
270
271     protected void aABC( Reifier r )
272         { r.reifyAs( Node.create( "a" ), Triple.create( "A B C" ) ); }
273         
274     protected void xSPO( Reifier r )
275         { r.reifyAs( Node.create( "x" ), Triple.create( "S P O" ) ); }
276         
277     public void testRemove()
278         {
279         testRemove( "S ?? ??", "S ?? ??" );
280         testRemove( "S ?? ??", "?? P ??" );
281         testRemove( "S ?? ??", "?? ?? O" );
282         testRemove( "?? P ??", "S ?? ??" );
283         testRemove( "?? P ??", "?? P ??" );
284         testRemove( "?? P ??", "?? ?? O" );
285         testRemove( "?? ?? O", "S ?? ??" );
286         testRemove( "?? ?? O", "?? P ??" );
287         testRemove( "?? ?? O", "?? ?? O" );
288         }
289     
290     public void testRemove( String JavaDoc findRemove, String JavaDoc findCheck )
291         {
292         Graph g = getGraphWith( "S P O" );
293         ExtendedIterator it = g.find( Triple.create( findRemove ) );
294         try
295             {
296             it.next(); it.remove(); it.close();
297             assertFalse( g.contains( Triple.create( findCheck ) ) );
298             }
299         catch (UnsupportedOperationException JavaDoc e)
300             { assertFalse( g.getCapabilities().iteratorRemoveAllowed() ); }
301         }
302     
303     public void testBulkRemoveWithReification()
304         {
305         testBulkUpdateRemoveWithReification( true );
306         testBulkUpdateRemoveWithReification( false );
307         }
308         
309     public void testBulkUpdateRemoveWithReification( boolean withReifications )
310         {
311         Graph g = getGraph();
312         BulkUpdateHandler bu = g.getBulkUpdateHandler();
313         Graph items = graphWith( "pigs might fly; dead can dance" );
314         Reifier gr = g.getReifier(), ir = items.getReifier();
315         xSPOyXYZ( ir );
316         xSPO( gr ); aABC( gr );
317         bu.delete( items, withReifications );
318         Graph answer = graphWith( "" );
319         Reifier ar = answer.getReifier();
320         if (withReifications)
321             aABC( ar );
322         else
323             {
324             xSPO( ar );
325             aABC( ar );
326             }
327         assertIsomorphic( getReificationTriples( ar ), getReificationTriples( gr ) );
328         }
329                                         
330     public void testHasCapabilities()
331         {
332         Graph g = getGraph();
333         Capabilities c = g.getCapabilities();
334         boolean sa = c.sizeAccurate();
335         boolean aaSome = c.addAllowed();
336         boolean aaAll = c.addAllowed( true );
337         boolean daSome = c.deleteAllowed();
338         boolean daAll = c.deleteAllowed( true );
339         boolean cbe = c.canBeEmpty();
340         }
341         
342     public void testFind()
343         {
344         Graph g = getGraph();
345         graphAdd( g, "S P O" );
346         assertTrue( g.find( Node.ANY, Node.ANY, Node.ANY ).hasNext() );
347         assertTrue( g.find( Triple.ANY ).hasNext() );
348         }
349         
350     public void testFind2()
351         {
352         Graph g = getGraphWith( "S P O" );
353         TripleIterator waitingForABigRefactoringHere = null;
354         ExtendedIterator it = g.find( Triple.ANY );
355         }
356
357     protected boolean canBeEmpty( Graph g )
358         { return g.isEmpty(); }
359         
360     public void testEventRegister()
361         {
362         Graph g = getGraph();
363         GraphEventManager gem = g.getEventManager();
364         assertSame( gem, gem.register( new RecordingListener() ) );
365         }
366         
367     /**
368         Test that we can safely unregister a listener that isn't registered.
369     */

370     public void testEventUnregister()
371         {
372         getGraph().getEventManager().unregister( L );
373         }
374         
375     /**
376         Handy triple for test purposes.
377     */

378     protected Triple SPO = Triple.create( "S P O" );
379     protected RecordingListener L = new RecordingListener();
380     
381     /**
382         Utility: get a graph, register L with its manager, return the graph.
383     */

384     protected Graph getAndRegister( GraphListener gl )
385         {
386         Graph g = getGraph();
387         g.getEventManager().register( gl );
388         return g;
389         }
390         
391     public void testAddTriple()
392         {
393         Graph g = getAndRegister( L );
394         g.add( SPO );
395         L.assertHas( new Object JavaDoc[] {"add", g, SPO} );
396         }
397         
398     public void testDeleteTriple()
399         {
400         Graph g = getAndRegister( L );
401         g.delete( SPO );
402         L.assertHas( new Object JavaDoc[] { "delete", g, SPO} );
403         }
404         
405     /**
406          Ensure that triples removed by calling .remove() on the iterator returned by
407          a find() will generate deletion notifications.
408     */

409     public void testEventDeleteByFind()
410         {
411         Graph g = getAndRegister( L );
412         if (g.getCapabilities().iteratorRemoveAllowed())
413             {
414             Triple toRemove = triple( "remove this triple" );
415             g.add( toRemove );
416             ExtendedIterator rtr = g.find( toRemove );
417             assertTrue( "ensure a(t least) one triple", rtr.hasNext() );
418             rtr.next();
419             rtr.remove();
420             L.assertHas( new Object JavaDoc[] { "add", g, toRemove, "delete", g, toRemove} );
421             }
422         }
423     
424     public void testTwoListeners()
425         {
426         RecordingListener L1 = new RecordingListener();
427         RecordingListener L2 = new RecordingListener();
428         Graph g = getGraph();
429         GraphEventManager gem = g.getEventManager();
430         gem.register( L1 ).register( L2 );
431         g.add( SPO );
432         L2.assertHas( new Object JavaDoc[] {"add", g, SPO} );
433         L1.assertHas( new Object JavaDoc[] {"add", g, SPO} );
434         }
435         
436     public void testUnregisterWorks()
437         {
438         Graph g = getGraph();
439         GraphEventManager gem = g.getEventManager();
440         gem.register( L ).unregister( L );
441         g.add( SPO );
442         L.assertHas( new Object JavaDoc[] {} );
443         }
444         
445     public void testRegisterTwice()
446         {
447         Graph g = getAndRegister( L );
448         g.getEventManager().register( L );
449         g.add( SPO );
450         L.assertHas( new Object JavaDoc[] {"add", g, SPO, "add", g, SPO} );
451         }
452         
453     public void testUnregisterOnce()
454         {
455         Graph g = getAndRegister( L );
456         g.getEventManager().register( L ).unregister( L );
457         g.delete( SPO );
458         L.assertHas( new Object JavaDoc[] {"delete", g, SPO} );
459         }
460         
461     public void testBulkAddArrayEvent()
462         {
463         Graph g = getAndRegister( L );
464         Triple [] triples = tripleArray( "x R y; a P b" );
465         g.getBulkUpdateHandler().add( triples );
466         L.assertHas( new Object JavaDoc[] {"add[]", g, triples} );
467         }
468       
469     public void testBulkAddList()
470         {
471         Graph g = getAndRegister( L );
472         List elems = Arrays.asList( tripleArray( "bells ring loudly; pigs might fly" ) );
473         g.getBulkUpdateHandler().add( elems );
474         L.assertHas( new Object JavaDoc[] {"addList", g, elems} );
475         }
476     
477     public void testBulkDeleteArray()
478         {
479         Graph g = getAndRegister( L );
480         Triple [] triples = tripleArray( "x R y; a P b" );
481         g.getBulkUpdateHandler().delete( triples );
482         L.assertHas( new Object JavaDoc[] {"delete[]", g, triples} );
483         }
484         
485     public void testBulkDeleteList()
486         {
487         Graph g = getAndRegister( L );
488         List elems = Arrays.asList( tripleArray( "bells ring loudly; pigs might fly" ) );
489         g.getBulkUpdateHandler().delete( elems );
490         L.assertHas( new Object JavaDoc[] {"deleteList", g, elems} );
491         }
492         
493     public void testBulkAddIterator()
494         {
495         Graph g = getAndRegister( L );
496         Triple [] triples = tripleArray( "I wrote this; you read that; I wrote this" );
497         g.getBulkUpdateHandler().add( asIterator( triples ) );
498         L.assertHas( new Object JavaDoc[] {"addIterator", g, Arrays.asList( triples )} );
499         }
500         
501     public void testBulkDeleteIterator()
502         {
503         Graph g = getAndRegister( L );
504         Triple [] triples = tripleArray( "I wrote this; you read that; I wrote this" );
505         g.getBulkUpdateHandler().delete( asIterator( triples ) );
506         L.assertHas( new Object JavaDoc[] {"deleteIterator", g, Arrays.asList( triples )} );
507         }
508         
509     public Iterator asIterator( Triple [] triples )
510         { return Arrays.asList( triples ).iterator(); }
511     
512     public void testBulkAddGraph()
513         {
514         Graph g = getAndRegister( L );
515         Graph triples = graphWith( "this type graph; I type slowly" );
516         g.getBulkUpdateHandler().add( triples );
517         L.assertHas( new Object JavaDoc[] {"addGraph", g, triples} );
518         }
519         
520     public void testBulkDeleteGraph()
521         {
522         Graph g = getAndRegister( L );
523         Graph triples = graphWith( "this type graph; I type slowly" );
524         g.getBulkUpdateHandler().delete( triples );
525         L.assertHas( new Object JavaDoc[] {"deleteGraph", g, triples} );
526         }
527     
528     public void testGeneralEvent()
529         {
530         Graph g = getAndRegister( L );
531         Object JavaDoc value = new int[]{};
532         g.getEventManager().notifyEvent( g, value );
533         L.assertHas( new Object JavaDoc[] { "someEvent", g, value } );
534         }
535     
536     public void testRemoveAllEvent()
537         {
538         Graph g = getAndRegister( L );
539         g.getBulkUpdateHandler().removeAll();
540         L.assertHas( new Object JavaDoc[] { "someEvent", g, GraphEvents.removeAll } );
541         }
542     
543     public void testRemoveSomeEvent()
544         {
545         Graph g = getAndRegister( L );
546         Node S = node( "S" ), P = node( "?P" ), O = node( "??" );
547         g.getBulkUpdateHandler().remove( S, P, O );
548         Object JavaDoc event = GraphEvents.remove( S, P, O );
549         L.assertHas( new Object JavaDoc[] { "someEvent", g, event } );
550         }
551     
552     /**
553      * Test that nodes can be found in all triple positions.
554      * However, testing for literals in subject positions is suppressed
555      * at present to avoid problems with InfGraphs which try to prevent
556      * such constructs leaking out to the RDF layer.
557      */

558     public void testContainsNode()
559         {
560         Graph g = getGraph();
561         graphAdd( g, "a P b; _c _Q _d; a 11 12" );
562         QueryHandler qh = g.queryHandler();
563         assertTrue( qh.containsNode( node( "a" ) ) );
564         assertTrue( qh.containsNode( node( "P" ) ) );
565         assertTrue( qh.containsNode( node( "b" ) ) );
566         assertTrue( qh.containsNode( node( "_c" ) ) );
567         assertTrue( qh.containsNode( node( "_Q" ) ) );
568         assertTrue( qh.containsNode( node( "_d" ) ) );
569 // assertTrue( qh.containsNode( node( "10" ) ) );
570
assertTrue( qh.containsNode( node( "11" ) ) );
571         assertTrue( qh.containsNode( node( "12" ) ) );
572     /* */
573         assertFalse( qh.containsNode( node( "x" ) ) );
574         assertFalse( qh.containsNode( node( "_y" ) ) );
575         assertFalse( qh.containsNode( node( "99" ) ) );
576         }
577     
578     public void testSubjectsFor()
579         {
580         Graph g = getGraphWith( "a P b; a Q c; a P d; b P x; c Q y" );
581         testSameSubjects( g, Node.ANY, Node.ANY );
582         testSameSubjects( g, node( "P" ), Node.ANY );
583         testSameSubjects( g, node( "Q" ), node( "c" ) );
584         }
585     
586     protected void testSameSubjects( Graph g, Node p, Node o )
587         {
588         Set bis = iteratorToSet( SimpleQueryHandler.subjectsFor( g, p, o ) );
589         Set qhs = iteratorToSet( g.queryHandler().subjectsFor( p, o ) );
590         assertEquals( bis, qhs );
591         }
592     
593     public void testListSubjectsNoRemove()
594         {
595         Graph g = getGraphWith( "a P b; b Q c; c R a" );
596         Iterator it = g.queryHandler().subjectsFor( Node.ANY, Node.ANY );
597         it.next();
598         try { it.remove(); fail( "listSubjects for " + g.getClass() + " should not support .remove()" ); }
599         catch (UnsupportedOperationException JavaDoc e) { pass(); }
600         }
601     
602     public void testObjectsFor()
603         {
604         Graph g = getGraphWith( "b P a; c Q a; d P a; x P b; y Q c" );
605         testSameObjects( g, Node.ANY, Node.ANY );
606         testSameObjects( g, node( "P" ), Node.ANY );
607         testSameObjects( g, node( "Q" ), node( "c" ) );
608         }
609     
610     protected void testSameObjects( Graph g, Node s, Node p )
611         {
612         Set bis = iteratorToSet( SimpleQueryHandler.objectsFor( g, s, p ) );
613         Set qhs = iteratorToSet( g.queryHandler().objectsFor( s, p ) );
614         assertEquals( bis, qhs );
615         }
616
617     public void testListObjectsNoRemove()
618         {
619         Graph g = getGraphWith( "a P b; b Q c; c R a" );
620         Iterator it = g.queryHandler().objectsFor( Node.ANY, Node.ANY );
621         it.next();
622         try { it.remove(); fail( "listObjects for " + g.getClass() + " should not support .remove()" ); }
623         catch (UnsupportedOperationException JavaDoc e) { pass(); }
624         }
625     
626     public void testPredicatesFor()
627         {
628         Graph g = getGraphWith( "a P b; c Q d; e R f; g P b; h Q i" );
629         testSamePredicates( g, Node.ANY, Node.ANY );
630         testSamePredicates( g, Node.ANY, node( "b" ) );
631         testSamePredicates( g, node( "g" ), Node.ANY );
632         testSamePredicates( g, node( "e" ), node( "f" ) );
633         }
634     
635     protected void testSamePredicates( Graph g, Node s, Node o )
636         {
637         Set bis = iteratorToSet( SimpleQueryHandler.predicatesFor( g, s, o ) );
638         Set qhs = iteratorToSet( g.queryHandler().predicatesFor( s, o ) );
639         assertEquals( bis, qhs );
640         }
641
642     public void testListPredicatesNoRemove()
643         {
644         Graph g = getGraphWith( "a P b; b Q c; c R a" );
645         Iterator it = g.queryHandler().predicatesFor( Node.ANY, Node.ANY );
646         it.next();
647         try { it.remove(); fail( "listPredicates for " + g.getClass() + " should not support .remove()" ); }
648         catch (UnsupportedOperationException JavaDoc e) { pass(); }
649         }
650         
651     public void testRemoveAll()
652         {
653         testRemoveAll( "" );
654         testRemoveAll( "a R b" );
655         testRemoveAll( "c S d; e:ff GGG hhhh; _i J 27; Ell Em 'en'" );
656         }
657     
658     public void testRemoveAll( String JavaDoc triples )
659         {
660         Graph g = getGraph();
661         graphAdd( g, triples );
662         g.getBulkUpdateHandler().removeAll();
663         assertTrue( g.isEmpty() );
664         }
665     
666     /**
667         Test cases for RemoveSPO(); each entry is a triple (add, remove, result).
668         <ul>
669         <li>add - the triples to add to the graph to start with
670         <li>remove - the pattern to use in the removal
671         <li>result - the triples that should remain in the graph
672         </ul>
673     */

674     protected String JavaDoc[][] cases =
675         {
676                 { "x R y", "x R y", "" },
677                 { "x R y; a P b", "x R y", "a P b" },
678                 { "x R y; a P b", "?? R y", "a P b" },
679                 { "x R y; a P b", "x R ??", "a P b" },
680                 { "x R y; a P b", "x ?? y", "a P b" },
681                 { "x R y; a P b", "?? ?? ??", "" },
682                 { "x R y; a P b; c P d", "?? P ??", "x R y" },
683                 { "x R y; a P b; x S y", "x ?? ??", "a P b" },
684         };
685     
686     /**
687         Test that remove(s, p, o) works, in the presence of inferencing graphs that
688         mean emptyness isn't available. This is why we go round the houses and
689         test that expected ~= initialContent + addedStuff - removed - initialContent.
690     */

691     public void testRemoveSPO()
692         {
693         for (int i = 0; i < cases.length; i += 1)
694             for (int j = 0; j < 3; j += 1)
695                 {
696                 Graph content = getGraph();
697                 Graph baseContent = copy( content );
698                 graphAdd( content, cases[i][0] );
699                 Triple remove = triple( cases[i][1] );
700                 Graph expected = graphWith( cases[i][2] );
701                 content.getBulkUpdateHandler().remove( remove.getSubject(), remove.getPredicate(), remove.getObject() );
702                 Graph finalContent = remove( copy( content ), baseContent );
703                 assertIsomorphic( cases[i][1], expected, finalContent );
704                 }
705         }
706     
707     protected void add( Graph toUpdate, Graph toAdd )
708         {
709         toUpdate.getBulkUpdateHandler().add( toAdd );
710         }
711     
712     protected Graph remove( Graph toUpdate, Graph toRemove )
713         {
714         toUpdate.getBulkUpdateHandler().delete( toRemove );
715         return toUpdate;
716         }
717     
718     protected Graph copy( Graph g )
719         {
720         Graph result = Factory.createDefaultGraph();
721         result.getBulkUpdateHandler().add( g );
722         return result;
723         }
724     
725     protected Graph getClosed()
726         {
727         Graph result = getGraph();
728         result.close();
729         return result;
730         }
731         
732 // public void testClosedDelete()
733
// {
734
// try { getClosed().delete( triple( "x R y" ) ); fail( "delete when closed" ); }
735
// catch (ClosedException c) { /* as required */ }
736
// }
737
//
738
// public void testClosedAdd()
739
// {
740
// try { getClosed().add( triple( "x R y" ) ); fail( "add when closed" ); }
741
// catch (ClosedException c) { /* as required */ }
742
// }
743
//
744
// public void testClosedContainsTriple()
745
// {
746
// try { getClosed().contains( triple( "x R y" ) ); fail( "contains[triple] when closed" ); }
747
// catch (ClosedException c) { /* as required */ }
748
// }
749
//
750
// public void testClosedContainsSPO()
751
// {
752
// Node a = Node.ANY;
753
// try { getClosed().contains( a, a, a ); fail( "contains[SPO] when closed" ); }
754
// catch (ClosedException c) { /* as required */ }
755
// }
756
//
757
// public void testClosedFindTriple()
758
// {
759
// try { getClosed().find( triple( "x R y" ) ); fail( "find [triple] when closed" ); }
760
// catch (ClosedException c) { /* as required */ }
761
// }
762
//
763
// public void testClosedFindSPO()
764
// {
765
// Node a = Node.ANY;
766
// try { getClosed().find( a, a, a ); fail( "find[SPO] when closed" ); }
767
// catch (ClosedException c) { /* as required */ }
768
// }
769
//
770
// public void testClosedSize()
771
// {
772
// try { getClosed().size(); fail( "size when closed (" + this.getClass() + ")" ); }
773
// catch (ClosedException c) { /* as required */ }
774
// }
775

776     }
777
778
779 /*
780     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
781     All rights reserved.
782
783     Redistribution and use in source and binary forms, with or without
784     modification, are permitted provided that the following conditions
785     are met:
786
787     1. Redistributions of source code must retain the above copyright
788        notice, this list of conditions and the following disclaimer.
789
790     2. Redistributions in binary form must reproduce the above copyright
791        notice, this list of conditions and the following disclaimer in the
792        documentation and/or other materials provided with the distribution.
793
794     3. The name of the author may not be used to endorse or promote products
795        derived from this software without specific prior written permission.
796
797     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
798     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
799     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
800     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
801     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
802     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
803     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
804     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
805     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
806     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
807 */
Popular Tags