1 6 11 12 package com.hp.hpl.jena.enhanced.test; 13 import com.hp.hpl.jena.mem.*; 14 import com.hp.hpl.jena.graph.*; 15 import com.hp.hpl.jena.graph.test.*; 16 import com.hp.hpl.jena.enhanced.*; 17 18 import junit.framework.*; 19 20 41 public class TestPackage extends GraphTestBase { 42 43 static final private GraphPersonality split = new GraphPersonality(); 44 45 static final private GraphPersonality combo = new GraphPersonality(); 46 47 48 static final private GraphPersonality bitOfBoth = new GraphPersonality(); 49 static final private GraphPersonality broken = new GraphPersonality(); 50 static { 51 split.add( TestObject.class, TestObjectImpl.factory ); 57 split.add( TestSubject.class, TestSubjectImpl.factory ); 58 split.add( TestProperty.class, TestPropertyImpl.factory ); 59 60 combo.add( TestObject.class, TestAllImpl.factory ); 61 combo.add( TestSubject.class, TestAllImpl.factory ); 62 combo.add( TestProperty.class, TestAllImpl.factory ); 63 64 bitOfBoth.add( TestObject.class, TestObjectImpl.factory ); 65 bitOfBoth.add( TestSubject.class, TestSubjectImpl.factory ); 66 bitOfBoth.add( TestProperty.class, TestAllImpl.factory ); 67 68 broken.add(TestObject.class, TestObjectImpl.factory ); 70 broken.add( TestSubject.class, TestSubjectImpl.factory ); 71 broken.add( TestProperty.class, TestObjectImpl.factory ); 72 } 73 74 public TestPackage(String name) 75 { 76 super( name ); 77 } 78 79 public static TestSuite suite() 80 { return new TestSuite( TestPackage.class ); } 81 82 86 public void testEquals() 87 { 88 EnhNode a = new EnhNode( Node.create( "eg:example" ), null ); 89 assertEquals( a, a ); 90 } 91 92 95 private static void miniAsSupports(String title, TestNode n, Class intf, boolean rslt ) { 96 assertTrue(title +":sanity",n instanceof Polymorphic); 97 98 TestNode as1 = (TestNode)((EnhNode)n).viewAs(intf); 100 TestNode as2 = (TestNode)((EnhNode)n).viewAs(intf); 101 102 assertTrue( title + ":idempotency", as1==as2 ); 104 105 assertEquals( title +":support",rslt,((EnhNode) as1).supports( intf ) ); 108 } 109 110 private static void oneNodeAsSupports(String title, TestNode n, boolean rslts[] ) { 111 miniAsSupports(title+"/TestSubject",n,TestSubject.class,rslts[0]); 113 miniAsSupports(title+"/TestProperty",n,TestProperty.class,rslts[1]); 114 miniAsSupports(title+"/TestObject",n,TestObject.class,rslts[2]); 115 } 116 117 private static void manyNodeAsSupports(String title, TestNode n[], boolean rslts[][] ) { 118 for (int i=0;i<n.length;i++){ 120 oneNodeAsSupports(title+"["+i+"]",n[i],rslts[i]); 121 } 122 } 123 124 125 130 private static void basic(String title, Personality p) { 131 Graph g = new GraphMem(); 132 TestModel model = new TestModelImpl(g,p); 133 graphAdd( g, "x R y;" ); 135 136 TestNode nodes[] = new TestNode[]{ 139 model.aSubject(), 140 model.aProperty(), 141 model.anObject() 142 }; 143 144 manyNodeAsSupports(title+"(a)",nodes, 146 new boolean[][]{ 147 new boolean[]{true,false,false}, new boolean[]{false,true,false}, 149 new boolean[]{false,false,true} 150 }); 151 152 graphAdd(g,"y R x;" ); 153 154 manyNodeAsSupports(title+"(b)",nodes, 159 new boolean[][]{ 160 new boolean[]{true,false,true}, new boolean[]{false,true,false}, 162 new boolean[]{true,false,true} 163 }); 164 165 g.delete( triple( "x R y" ) ); 166 167 172 manyNodeAsSupports(title+"(c)",nodes, 173 new boolean[][]{ 174 new boolean[]{false,false,true}, 175 new boolean[]{false,true,false}, 176 new boolean[]{true,false,false} 177 }); 178 179 180 } 181 182 186 static final int S = 1; 187 static final int P = 2; 188 static final int O = 3; 189 190 private void canImplement(String title, TestNode n, int wh, boolean rslt ) { 196 try { 197 switch (wh) { 198 case S: 199 n.asSubject().aProperty(); 200 break; 201 case P: 202 n.asProperty().anObject(); 203 break; 204 case O: 205 n.asObject().aSubject(); 206 break; 207 } 208 assertTrue("IllegalStateException expected.",rslt); 209 } 210 catch (IllegalStateException e) { 211 assertFalse("IllegalStateException at the wrong time.",rslt); 212 } 213 } 214 215 private void canImplement(String title, TestNode n, boolean rslts[] ) { 216 canImplement(title+"/TestSubject",n,S,rslts[0]); 217 canImplement(title+"/TestProperty",n,P,rslts[1]); 218 canImplement(title+"/TestObject",n,O,rslts[2]); 219 } 220 private void canImplement(String title, TestNode n[], boolean rslts[][] ) { 221 for (int i=0;i<n.length;i++){ 222 canImplement(title+"["+i+"]",n[i],rslts[i]); 223 } 224 } 225 226 private void follow(String title, Personality p) { 227 Graph g = new GraphMem(); 228 TestModel model = new TestModelImpl(g,p); 229 graphAdd( g, "a b c;" ); 231 TestNode nodes[] = new TestNode[]{ 232 model.aSubject(), 233 model.aProperty(), 234 model.anObject() 235 }; 236 237 canImplement(title+"(a)",nodes, 239 new boolean[][]{ 240 new boolean[]{true,false,false}, 241 new boolean[]{false,true,false}, 242 new boolean[]{false,false,true} 243 }); 244 245 graphAdd(g, "b a c;" ); 246 247 canImplement(title+"(b)",nodes, 250 new boolean[][]{ 251 new boolean[]{true,true,false}, 252 new boolean[]{true,true,false}, 253 new boolean[]{false,false,true} 254 }); 255 256 g.delete(triple( "a b c" ) ); 257 258 259 canImplement(title+"(c)",nodes, 262 new boolean[][]{ 263 new boolean[]{false,true,false}, 264 new boolean[]{true,false,false}, 265 new boolean[]{false,false,true} 266 }); 267 268 canImplement(title+"(c)",new TestNode[]{ 270 nodes[1].asSubject().aProperty(), 271 nodes[2].asObject().aSubject(), 272 nodes[0].asProperty().anObject() 273 }, 274 new boolean[][]{ 275 new boolean[]{false,true,false}, 276 new boolean[]{true,false,false}, 277 new boolean[]{false,false,true} 278 }); 279 assertTrue("Model cache test",nodes[0].asProperty().anObject()==nodes[2]); 280 } 281 private void cache(String title, Personality p) { 282 Graph g = new GraphMem(); 283 TestModel model = new TestModelImpl(g,p); 284 graphAdd( g, "a b a;" ); 286 287 assertTrue("Caching is on",model.aSubject().asObject()==model.anObject()); 289 290 ((TestModelImpl)model).getNodeCacheControl().setEnabled(false); 291 292 293 assertFalse("Caching is off",model.aSubject()==model.anObject()); 296 297 } 298 public static void testSplitBasic() { 299 basic("Split: ",split); 300 } 301 public static void testComboBasic() { 302 basic("Combo: ",combo); 303 } 304 public void testSplitFollow() { 305 follow("Split: ",split); 306 } 307 public void testComboFollow() { 308 follow("Combo: ",combo); 309 } 310 311 public void testSplitCache() { 312 cache("Split: ",split); 313 } 314 public void testComboCache() { 315 cache("Combo: ",combo); 316 } 317 318 public static void testBitOfBothBasic() { 319 basic("bob: ",bitOfBoth); 320 } 321 public void testBitOfBothFollow() { 322 follow("bob: ",bitOfBoth); 323 } 324 325 public void testBitOfBothCache() { 326 cache("bob: ",bitOfBoth); 327 } 328 329 public static void testBitOfBothSurprise() { 330 333 Graph g = new GraphMem(); 334 TestModel model = new TestModelImpl(g,bitOfBoth); 335 graphAdd( g, "a a a;" ); 337 TestSubject testSubjectImpl = model.aSubject(); 338 assertTrue("BitOfBoth makes subjects using TestSubjectImpl", 339 testSubjectImpl instanceof TestSubjectImpl); 340 TestProperty testAllImpl = testSubjectImpl.aProperty(); 341 assertTrue("BitOfBoth makes properties using TestAllImpl", 342 testAllImpl instanceof TestAllImpl); 343 assertTrue("turning a TestAllImpl into a TestSubject is a no-op", 344 testAllImpl == testAllImpl.asSubject() ); 345 assertTrue("turning a TestAllImpl into a TestSubject is a no-op", 346 testSubjectImpl != testAllImpl.asSubject() ); 347 assertTrue("turning a TestAllImpl into a TestSubject is a no-op", 348 testSubjectImpl.asSubject() != testSubjectImpl.asSubject().asProperty().asSubject() ); 349 350 } 351 352 public static void testBrokenBasic() { 353 try { 354 basic("Broken: ",broken); 358 fail("broken is a misconfigured personality, but it wasn't detected."); 359 } 360 catch (PersonalityConfigException e ) { 361 362 } 363 } 364 365 static class Example 366 { 367 static final Implementation factory = new Implementation() 368 { 369 public EnhNode wrap( Node n, EnhGraph g ) { return new EnhNode( n, g ); } 370 371 public boolean canWrap( Node n, EnhGraph g ) { return n.isURI(); } 372 }; 373 } 374 375 public void testSimple() 376 { 377 Graph g = new GraphMem(); 378 Personality ours = BuiltinPersonalities.model.copy().add( Example.class, Example.factory ); 379 EnhGraph eg = new EnhGraph( g, ours ); 380 Node n = Node.createURI( "spoo:bar" ); 381 EnhNode eNode = new EnhNode( Node.createURI( "spoo:bar" ), eg ); 382 EnhNode eBlank = new EnhNode( Node.createAnon(), eg ); 383 assertTrue( "URI node can be an Example", eNode.supports( Example.class ) ); 384 assertFalse( "Blank node cannot be an Example", eBlank.supports( Example.class ) ); 385 } 386 387 static class AnotherExample 388 { 389 static final Implementation factory = new Implementation() 390 { 391 public EnhNode wrap( Node n, EnhGraph g ) { return new EnhNode( n, g ); } 392 393 public boolean canWrap( Node n, EnhGraph g ) { return n.isURI(); } 394 }; 395 } 396 397 public void testAlreadyLinkedViewException() 398 { 399 Graph g = new GraphMem(); 400 Personality ours = BuiltinPersonalities.model.copy().add( Example.class, Example.factory ); 401 EnhGraph eg = new EnhGraph( g, ours ); 402 Node n = Node.create( "spoo:bar" ); 403 EnhNode eNode = new EnhNode( n, eg ); 404 eNode.viewAs( Example.class ); 405 try 406 { 407 eNode.addView( eNode ); 408 fail( "should raise an AlreadyLinkedViewException " ); 409 } 410 catch (AlreadyLinkedViewException e) 411 {} 412 } 413 414 419 public void testNullPointerTrap() 420 { 421 EnhGraph eg = new EnhGraph( new GraphMem(), BuiltinPersonalities.model ); 422 Node n = Node.create( "eh:something" ); 423 EnhNode en = new EnhNode( n, eg ); 424 try 425 { 426 en.as( TestPackage.class ); 427 fail( "oops" ); 428 } 429 catch (UnsupportedPolymorphismException e) 430 { 431 assertTrue( "exception should have cuplprit graph", eg == e.getBadGraph() ); 432 assertTrue( "exception should have culprit class", TestPackage.class == e.getBadClass() ); 433 } 434 } 435 436 } 437 438 467 | Popular Tags |