1 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 20 public class TestModelEvents extends ModelTestBase 21 { 22 public TestModelEvents(String 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 [] { "add", S1 } ) ); 52 model.register( SL ); 53 model.add( S1 ); 54 SL.assertHas( new Object [] { "add", S1 } ); 55 model.add( S2 ); 56 SL.assertHas( new Object [] { "add", S1, "add", S2 } ); 57 model.add( S1 ); 58 SL.assertHas( new Object [] { "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 [] { "add", S } ); 69 SL1.assertHas( new Object [] { "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 [] {} ); 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 [] { "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 [] { "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 [] {"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 [] {"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 [] {"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 [] {"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 [] {"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 [] {"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 [] {"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 [] {"removeModel", m} ); 163 } 164 165 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 e = new int [] {}; 218 model.notifyEvent( e ); 219 SL.assertHas( new Object [] {"someEvent", model, e} ); 220 } 221 222 226 public static class WatchStatementListener extends StatementListener 227 { 228 List statements = new ArrayList(); 229 String addOrRem = "<unset>"; 230 231 public List contents() 232 { try { return statements; } finally { statements = new ArrayList(); } } 233 234 public String 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 x ) 245 { 246 Integer n = (Integer ) m.get( x ); 247 if (n == null) n = new Integer (0); 248 m.put( x, new Integer ( 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 how, String 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 recorded; 302 private String how; 303 304 public void added( Object x ) 305 { recorded = x; how = "add"; } 306 307 public void removed( Object x ) 308 { recorded = x; how = "rem"; } 309 310 private Object comparable( Object 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 wantHow, Object 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 | Popular Tags |