KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > test > TestModelEvents


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestModelEvents.java,v 1.18 2005/02/21 12:15:02 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model.test;
8
9 import com.hp.hpl.jena.rdf.listeners.*;
10 import com.hp.hpl.jena.rdf.model.*;
11 import com.hp.hpl.jena.rdf.model.impl.*;
12
13 import java.util.*;
14 import junit.framework.*;
15
16 /**
17     Tests for model events and listeners.
18     @author kers
19 */

20 public class TestModelEvents extends ModelTestBase
21     {
22     public TestModelEvents(String JavaDoc name)
23         { super(name); }
24
25     public static TestSuite suite()
26         { return new TestSuite( TestModelEvents.class ); }
27         
28     protected Model model;
29     protected RecordingModelListener SL;
30     
31     public void setUp()
32         {
33         model = ModelFactory.createDefaultModel();
34         SL = new RecordingModelListener();
35         }
36         
37     public void testRegistrationCompiles()
38         {
39         assertSame( model, model.register( new RecordingModelListener() ) );
40         }
41         
42     public void testUnregistrationCompiles()
43         {
44         model.unregister( new RecordingModelListener() );
45         }
46         
47     public void testAddSingleStatements()
48         {
49         Statement S1 = statement( model, "S P O" );
50         Statement S2 = statement( model, "A B C" );
51         assertFalse( SL.has( new Object JavaDoc [] { "add", S1 } ) );
52         model.register( SL );
53         model.add( S1 );
54         SL.assertHas( new Object JavaDoc[] { "add", S1 } );
55         model.add( S2 );
56         SL.assertHas( new Object JavaDoc[] { "add", S1, "add", S2 } );
57         model.add( S1 );
58         SL.assertHas( new Object JavaDoc[] { "add", S1, "add", S2, "add", S1 } );
59         }
60         
61     public void testTwoListeners()
62         {
63         Statement S = statement( model, "S P O" );
64         RecordingModelListener SL1 = new RecordingModelListener();
65         RecordingModelListener SL2 = new RecordingModelListener();
66         model.register( SL1 ).register( SL2 );
67         model.add( S );
68         SL2.assertHas( new Object JavaDoc[] { "add", S } );
69         SL1.assertHas( new Object JavaDoc[] { "add", S } );
70         }
71         
72     public void testUnregisterWorks()
73         {
74         model.register( SL );
75         model.unregister( SL );
76         model.add( statement( model, "X R Y" ) );
77         SL.assertHas( new Object JavaDoc[] {} );
78         }
79         
80     public void testRemoveSingleStatements()
81         {
82         Statement S = statement( model, "D E F" );
83         model.register( SL );
84         model.add( S );
85         model.remove( S );
86         SL.assertHas( new Object JavaDoc[] { "add", S, "remove", S } );
87         }
88         
89     public void testAddInPieces()
90         {
91         model.register( SL );
92         model.add( resource( model, "S" ), property( model, "P" ), resource( model, "O" ) );
93         SL.assertHas( new Object JavaDoc[] { "add", statement( model, "S P O") } );
94         }
95
96     public void testAddStatementArray()
97         {
98         model.register( SL );
99         Statement [] s = statements( model, "a P b; c Q d" );
100         model.add( s );
101         SL.assertHas( new Object JavaDoc[] {"add[]", Arrays.asList( s )} );
102         }
103         
104     public void testDeleteStatementArray()
105         {
106         model.register( SL );
107         Statement [] s = statements( model, "a P b; c Q d" );
108         model.remove( s );
109         SL.assertHas( new Object JavaDoc[] {"remove[]", Arrays.asList( s )} );
110         }
111         
112     public void testAddStatementList()
113         {
114         model.register( SL );
115         List L = Arrays.asList( statements( model, "b I g; m U g" ) );
116         model.add( L );
117         SL.assertHas( new Object JavaDoc[] {"addList", L} );
118         }
119         
120     public void testDeleteStatementList()
121         {
122         model.register( SL );
123         List L = Arrays.asList( statements( model, "b I g; m U g" ) );
124         model.remove( L );
125         SL.assertHas( new Object JavaDoc[] {"removeList", L} );
126         }
127     
128     public void testAddStatementIterator()
129         {
130         model.register( SL );
131         Statement [] sa = statements( model, "x R y; a P b; x R y" );
132         StmtIterator it = asIterator( sa );
133         model.add( it );
134         SL.assertHas( new Object JavaDoc[] {"addIterator", Arrays.asList( sa )} );
135         }
136         
137     public void testDeleteStatementIterator()
138         {
139         model.register( SL );
140         Statement [] sa = statements( model, "x R y; a P b; x R y" );
141         StmtIterator it = asIterator( sa );
142         model.remove( it );
143         SL.assertHas( new Object JavaDoc[] {"removeIterator", Arrays.asList( sa )} );
144         }
145                     
146     protected StmtIterator asIterator( Statement [] statements )
147         { return new StmtIteratorImpl( Arrays.asList( statements ).iterator() ); }
148         
149     public void testAddModel()
150         {
151         model.register( SL );
152         Model m = modelWithStatements( "NT beats S; S beats H; H beats D" );
153         model.add( m );
154         SL.assertHas( new Object JavaDoc[] {"addModel", m} );
155         }
156         
157     public void testDeleteModel()
158         {
159         model.register( SL );
160         Model m = modelWithStatements( "NT beats S; S beats H; H beats D" );
161         model.remove( m );
162         SL.assertHas( new Object JavaDoc[] {"removeModel", m} );
163         }
164         
165     /**
166         Test that the null listener doesn't appear to do anything. Or at least
167         doesn't crash ....
168     */

169     public void testNullListener()
170         {
171         ModelChangedListener NL = new NullListener();
172         model.register( NL );
173         model.add( statement( model, "S P O " ) );
174         model.remove( statement( model, "X Y Z" ) );
175         model.add( statements( model, "a B c; d E f" ) );
176         model.remove( statements( model, "g H i; j K l" ) );
177         model.add( asIterator( statements( model, "m N o; p Q r" ) ) );
178         model.remove( asIterator( statements( model, "s T u; v W x" ) ) );
179         model.add( modelWithStatements( "leaves fall softly" ) );
180         model.remove( modelWithStatements( "water drips endlessly" ) );
181         model.add( Arrays.asList( statements( model, "xx RR yy" ) ) );
182         model.remove( Arrays.asList( statements( model, "aa VV rr" ) ) );
183         }
184         
185     public void testChangedListener()
186         {
187         ChangedListener CL = new ChangedListener();
188         model.register( CL );
189         assertFalse( CL.hasChanged() );
190         model.add( statement( model, "S P O" ) );
191         assertTrue( CL.hasChanged() );
192         assertFalse( CL.hasChanged() );
193         model.remove( statement( model, "ab CD ef" ) );
194         assertTrue( CL.hasChanged() );
195         model.add( statements( model, "gh IJ kl" ) );
196         assertTrue( CL.hasChanged() );
197         model.remove( statements( model, "mn OP qr" ) );
198         assertTrue( CL.hasChanged() );
199         model.add( asIterator( statements( model, "st UV wx" ) ) );
200         assertTrue( CL.hasChanged() );
201         assertFalse( CL.hasChanged() );
202         model.remove( asIterator( statements( model, "yz AB cd" ) ) );
203         assertTrue( CL.hasChanged() );
204         model.add( modelWithStatements( "ef GH ij" ) );
205         assertTrue( CL.hasChanged() );
206         model.remove( modelWithStatements( "kl MN op" ) );
207         assertTrue( CL.hasChanged() );
208         model.add( Arrays.asList( statements( model, "rs TU vw" ) ) );
209         assertTrue( CL.hasChanged() );
210         model.remove( Arrays.asList( statements( model, "xy wh q" ) ) );
211         assertTrue( CL.hasChanged() );
212         }
213     
214     public void testGeneralEvent()
215         {
216         model.register( SL );
217         Object JavaDoc e = new int [] {};
218         model.notifyEvent( e );
219         SL.assertHas( new Object JavaDoc[] {"someEvent", model, e} );
220         }
221
222     /**
223         Local test class to see that a StatementListener funnels all the changes through
224         add/remove a single statement
225     */

226     public static class WatchStatementListener extends StatementListener
227         {
228         List statements = new ArrayList();
229         String JavaDoc addOrRem = "<unset>";
230         
231         public List contents()
232             { try { return statements; } finally { statements = new ArrayList(); } }
233         
234         public String JavaDoc getAddOrRem()
235             { return addOrRem; }
236                 
237         public void addedStatement( Statement s )
238             { statements.add( s ); addOrRem = "add"; }
239             
240         public void removedStatement( Statement s )
241             { statements.add( s ); addOrRem = "rem"; }
242         }
243         
244     public void another( Map m, Object JavaDoc x )
245         {
246         Integer JavaDoc n = (Integer JavaDoc) m.get( x );
247         if (n == null) n = new Integer JavaDoc(0);
248         m.put( x, new Integer JavaDoc( n.intValue() + 1 ) );
249         }
250     
251     public Map asBag( List l )
252         {
253         Map result = new HashMap();
254         for (int i = 0; i < l.size(); i += 1) another( result, l.get(i) );
255         return result;
256         }
257         
258     public void assertSameBag( List wanted, List got )
259         { assertEquals( asBag( wanted ), asBag( got ) ); }
260         
261     public void testGot( WatchStatementListener sl, String JavaDoc how, String JavaDoc template )
262         {
263         assertSameBag( Arrays.asList( statements( model, template ) ), sl.contents() );
264         assertEquals( how, sl.getAddOrRem() );
265         assertTrue( sl.contents().size() == 0 );
266         }
267         
268     public void testTripleListener()
269         {
270         WatchStatementListener sl = new WatchStatementListener();
271         model.register( sl );
272         model.add( statement( model, "b C d" ) );
273         testGot( sl, "add", "b C d" );
274         model.remove( statement( model, "e F g" ) );
275         testGot( sl, "rem", "e F g" );
276     /* */
277         model.add( statements( model, "h I j; k L m" ) );
278         testGot( sl, "add", "h I j; k L m" );
279         model.remove( statements( model, "n O p; q R s" ) );
280         testGot( sl, "rem", "n O p; q R s" );
281     /* */
282         model.add( Arrays.asList( statements( model, "t U v; w X y" ) ) );
283         testGot( sl, "add", "t U v; w X y" );
284         model.remove( Arrays.asList( statements( model, "z A b; c D e" ) ) );
285         testGot( sl, "rem", "z A b; c D e" );
286     /* */
287         model.add( asIterator( statements( model, "f G h; i J k" ) ) );
288         testGot( sl, "add", "f G h; i J k" );
289         model.remove( asIterator( statements( model, "l M n; o P q" ) ) );
290         testGot( sl, "rem", "l M n; o P q" );
291     /* */
292         model.add( modelWithStatements( "r S t; u V w; x Y z" ) );
293         testGot( sl, "add", "r S t; u V w; x Y z" );
294         model.remove( modelWithStatements( "a E i; o U y" ) );
295         testGot( sl, "rem", "a E i; o U y" );
296         }
297         
298
299     static class OL extends ObjectListener
300         {
301         private Object JavaDoc recorded;
302         private String JavaDoc how;
303         
304         public void added( Object JavaDoc x )
305             { recorded = x; how = "add"; }
306             
307         public void removed( Object JavaDoc x )
308             { recorded = x; how = "rem"; }
309         
310         private Object JavaDoc comparable( Object JavaDoc x )
311             {
312             if (x instanceof Statement []) return Arrays.asList( (Statement []) x );
313             if (x instanceof Iterator) return iteratorToList( (Iterator) x );
314             return x;
315             }
316             
317         public void recent( String JavaDoc wantHow, Object JavaDoc value )
318             {
319             assertEquals( comparable( value ), comparable( recorded ) );
320             assertEquals( wantHow, how );
321             recorded = how = null;
322             }
323         }
324         
325     public void testObjectListener()
326         {
327         OL ll = new OL();
328         model.register( ll );
329         Statement s = statement( model, "aa BB cc" ), s2 = statement( model, "dd EE ff" );
330         model.add( s );
331         ll.recent( "add", s );
332         model.remove( s2 );
333         ll.recent( "rem", s2 );
334     /* */
335         List sList = Arrays.asList( statements( model, "gg HH ii; jj KK ll" ) );
336         model.add( sList );
337         ll.recent( "add", sList );
338         List sList2 = Arrays.asList( statements( model, "mm NN oo; pp QQ rr; ss TT uu" ) );
339         model.remove( sList2 );
340         ll.recent( "rem", sList2 );
341     /* */
342         Model m1 = modelWithStatements( "vv WW xx; yy ZZ aa" );
343         model.add( m1 );
344         ll.recent( "add", m1 );
345         Model m2 = modelWithStatements( "a B g; d E z" );
346         model.remove( m2 );
347         ll.recent( "rem", m2 );
348     /* */
349         Statement [] sa1 = statements( model, "th i k; l m n" );
350         model.add( sa1 );
351         ll.recent( "add", sa1 );
352         Statement [] sa2 = statements( model, "x o p; r u ch" );
353         model.remove( sa2 );
354         ll.recent( "rem", sa2 );
355     /* */
356         Statement [] si1 = statements( model, "u ph ch; psi om eh" );
357         model.add( asIterator( si1 ) );
358         ll.recent( "add", asIterator( si1 ) );
359         Statement [] si2 = statements( model, "at last the; end of these; tests ok guv" );
360         model.remove( asIterator( si2 ) );
361         ll.recent( "rem", asIterator( si2 ) );
362         }
363     }
364
365
366 /*
367     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
368     All rights reserved.
369
370     Redistribution and use in source and binary forms, with or without
371     modification, are permitted provided that the following conditions
372     are met:
373
374     1. Redistributions of source code must retain the above copyright
375        notice, this list of conditions and the following disclaimer.
376
377     2. Redistributions in binary form must reproduce the above copyright
378        notice, this list of conditions and the following disclaimer in the
379        documentation and/or other materials provided with the distribution.
380
381     3. The name of the author may not be used to endorse or promote products
382        derived from this software without specific prior written permission.
383
384     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
385     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
386     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
387     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
388     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
389     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
390     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
391     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
392     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
393     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
394 */
Popular Tags