1 19 20 package com.hp.hpl.jena.rdf.model.test; 23 24 25 import java.util.*; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import junit.framework.*; 33 34 import com.hp.hpl.jena.enhanced.*; 35 import com.hp.hpl.jena.enhanced.EnhGraph; 36 import com.hp.hpl.jena.graph.Graph; 37 import com.hp.hpl.jena.graph.Node; 38 import com.hp.hpl.jena.rdf.model.*; 39 import com.hp.hpl.jena.rdf.model.impl.RDFListImpl; 40 import com.hp.hpl.jena.shared.JenaException; 41 import com.hp.hpl.jena.util.iterator.Map1; 42 import com.hp.hpl.jena.vocabulary.*; 43 44 45 46 56 public class TestList 57 extends TestCase 58 { 59 62 public static final String NS = "uri:urn:x-rdf:test#"; 63 64 65 68 69 72 73 76 public TestList( String name ) { 77 super( name ); 78 } 79 80 83 public static TestSuite suite() { 84 TestSuite s = new TestSuite( "TestList" ); 85 86 for (int i = 0; i <= 5; i++) { 87 s.addTest( new CountTest( i ) ); 88 s.addTest( new TailTest( i ) ); 89 } 90 91 s.addTest( new ValidityTest() ); 92 s.addTest( new HeadTest() ); 93 s.addTest( new SetHeadTest() ); 94 s.addTest( new SetTailTest() ); 95 s.addTest( new ConsTest() ); 96 s.addTest( new AddTest() ); 97 s.addTest( new TestListGet() ); 98 s.addTest( new ReplaceTest() ); 99 s.addTest( new IndexTest1() ); 100 s.addTest( new IndexTest2() ); 101 s.addTest( new AppendTest() ); 102 s.addTest( new ConcatenateTest() ); 103 s.addTest( new ConcatenateTest2() ); 104 s.addTest( new ApplyTest() ); 105 s.addTest( new ReduceTest() ); 106 s.addTest( new RemoveTest() ); 107 s.addTest( new Map1Test() ); 108 s.addTest( new ListEqualsTest() ); 109 s.addTest( new ListSubclassTest() ); 110 s.addTest( new UserDefinedListTest() ); 111 112 return s; 113 } 114 115 116 117 120 121 protected static void iteratorTest( Iterator i, Object [] expected ) { 122 Log logger = LogFactory.getLog( TestList.class ); 123 List expList = new ArrayList(); 124 for (int j = 0; j < expected.length; j++) { 125 expList.add( expected[j] ); 126 } 127 128 while (i.hasNext()) { 129 Object next = i.next(); 130 131 if (!expList.contains( next )) { 133 logger.debug( "TestList - Unexpected iterator result: " + next ); 134 } 135 136 assertTrue( "Value " + next + " was not expected as a result from this iterator ", expList.contains( next ) ); 137 assertTrue( "Value " + next + " was not removed from the list ", expList.remove( next ) ); 138 } 139 140 if (!(expList.size() == 0)) { 141 logger.debug( "TestList - Expected iterator results not found" ); 142 for (Iterator j = expList.iterator(); j.hasNext(); ) { 143 logger.debug( "TestList - missing: " + j.next() ); 144 } 145 } 146 assertEquals( "There were expected elements from the iterator that were not found", 0, expList.size() ); 147 } 148 149 150 154 protected static class ListTest extends TestCase { 155 public ListTest( String n ) {super(n);} 156 157 protected void checkValid( String testName, RDFList l, boolean validExpected ) { 158 l.setStrict( true ); 159 boolean valid = l.isValid(); 160 assertEquals( "Validity test " + testName + " returned wrong isValid() result", validExpected, valid ); 162 } 163 164 protected RDFList getListRoot( Model m ) { 165 Resource root = m.getResource( NS + "root" ); 166 assertNotNull( "Root resource should not be null", root ); 167 168 Resource listHead = root.getRequiredProperty( m.getProperty( NS + "p" ) ).getResource(); 169 170 RDFList l = (RDFList) listHead.as( RDFList.class ); 171 assertNotNull( "as(RDFList) should not return null for root", l ); 172 173 return l; 174 } 175 } 176 177 protected static class CountTest extends ListTest { 178 protected int i; 179 180 public CountTest( int i ) { 181 super( "CountTest" ); 182 this.i = i; 183 } 184 185 public void runTest() { 186 Model m = ModelFactory.createDefaultModel(); 187 m.read( "file:testing/ontology/list" + i + ".rdf" ); 188 189 RDFList l0 = getListRoot( m ); 190 assertEquals( "List size should be " + i, i, l0.size() ); 191 } 192 193 } 194 195 196 protected static class ValidityTest extends ListTest { 197 public ValidityTest() { 198 super( "ValidityTest"); 199 } 200 201 public void runTest() { 202 Model m = ModelFactory.createDefaultModel(); 203 204 Resource root = m.createResource( NS + "root" ); 205 Property p = m.createProperty( NS, "p"); 206 207 Resource nil = RDF.nil; 209 m.add( root, p, nil ); 210 RDFList l0 = getListRoot( m ); 211 checkValid( "valid1", l0, true ); 212 213 Resource badList = m.createResource(); 215 m.getRequiredProperty( root, p ).remove(); 216 m.add( root, p, badList ); 217 m.add( badList, RDF.type, RDF.List ); 218 219 RDFList l1 = getListRoot( m ); 220 checkValid( "valid2", l1, false ); 221 222 224 m.add( badList, RDF.first, "fred" ); 225 checkValid( "valid4", l1, false ); 226 227 m.add( badList, RDF.rest, nil ); 228 checkValid( "valid5", l1, true ); 229 } 230 231 } 232 233 234 protected static class HeadTest extends ListTest { 235 public HeadTest() {super( "HeadTest");} 236 237 public void runTest() { 238 Model m = ModelFactory.createDefaultModel(); 239 m.read( "file:testing/ontology/list5.rdf" ); 240 241 RDFList l0 = getListRoot( m ); 242 243 String [] names = {"a", "b", "c", "d", "e"}; 244 for (int i = 0; i < names.length; i++) { 245 assertEquals( "head of list has incorrect URI", NS + names[i], ((Resource) l0.getHead()).getURI() ); 246 l0 = l0.getTail(); 247 } 248 } 249 } 250 251 252 protected static class TailTest extends ListTest { 253 protected int i; 254 255 public TailTest( int i ) { 256 super( "TailTest" ); 257 this.i = i; 258 } 259 260 public void runTest() { 261 Model m = ModelFactory.createDefaultModel(); 262 m.read( "file:testing/ontology/list" + i + ".rdf" ); 263 264 RDFList l0 = getListRoot( m ); 265 266 for (int j = 0; j < i; j++) { 268 l0 = l0.getTail(); 269 } 270 271 assertTrue( "Should have reached the end of the list after " + i + " getTail()'s", l0.isEmpty() ); 272 } 273 } 274 275 276 protected static class SetHeadTest extends ListTest { 277 public SetHeadTest() {super( "SetHeadTest");} 278 279 public void runTest() { 280 Model m = ModelFactory.createDefaultModel(); 281 282 Resource root = m.createResource( NS + "root" ); 283 Property p = m.createProperty( NS, "p"); 284 285 Resource nil = RDF.nil; 287 m.add( nil, RDF.type, RDF.List ); 288 289 Resource list = m.createResource(); 290 m.add( list, RDF.type, RDF.List ); 291 m.add( list, RDF.first, "fred" ); 292 m.add( list, RDF.rest, nil ); 293 294 m.add( root, p, list ); 295 RDFList l1 = getListRoot( m ); 296 checkValid( "sethead1", l1, true ); 297 298 assertEquals( "List head should be 'fred'", "fred", ((Literal) l1.getHead()).getString() ); 299 300 l1.setHead( m.createLiteral( 42 ) ); 301 checkValid( "sethead2", l1, true ); 302 assertEquals( "List head should be '42'", 42, ((Literal) l1.getHead()).getInt() ); 303 304 } 305 } 306 307 308 protected static class SetTailTest extends ListTest { 309 public SetTailTest() {super( "SetTailTest");} 310 311 public void runTest() { 312 Model m = ModelFactory.createDefaultModel(); 313 314 Resource root = m.createResource( NS + "root" ); 315 Property p = m.createProperty( NS, "p"); 316 317 Resource nil = RDF.nil; 318 m.add( nil, RDF.type, RDF.List ); 319 320 Resource list0 = m.createResource(); 321 m.add( list0, RDF.type, RDF.List ); 322 m.add( list0, RDF.first, "fred" ); 323 m.add( list0, RDF.rest, nil ); 324 325 m.add( root, p, list0 ); 326 RDFList l1 = getListRoot( m ); 327 checkValid( "settail1", l1, true ); 328 329 Resource list1 = m.createResource(); 330 m.add( list1, RDF.type, RDF.List ); 331 m.add( list1, RDF.first, "george" ); 332 m.add( list1, RDF.rest, nil ); 333 334 RDFList l2 = (RDFList) list1.as( RDFList.class ); 335 assertNotNull( "as(RDFList) should not return null for root", l2 ); 336 checkValid( "settail2", l2, true ); 337 338 assertEquals( "l1 should have length 1", 1, l1.size() ); 339 assertEquals( "l2 should have length 1", 1, l2.size() ); 340 341 l1.setTail( l2 ); 343 344 checkValid( "settail3", l1, true ); 345 checkValid( "settail4", l2, true ); 346 347 assertEquals( "l1 should have length 2", 2, l1.size() ); 348 assertEquals( "l2 should have length 1", 1, l2.size() ); 349 350 } 351 } 352 353 354 protected static class ConsTest extends ListTest { 355 public ConsTest() {super( "ConsTest" );} 356 357 public void runTest() { 358 Model m = ModelFactory.createDefaultModel(); 359 360 Resource root = m.createResource( NS + "root" ); 361 Property p = m.createProperty( NS, "p"); 362 363 Resource nil = m.getResource( RDF.nil.getURI() ); 364 RDFList list = (RDFList) nil.as( RDFList.class ); 365 366 Resource[] toAdd = new Resource[] { 367 m.createResource( NS + "e" ), 368 m.createResource( NS + "d" ), 369 m.createResource( NS + "c" ), 370 m.createResource( NS + "b" ), 371 m.createResource( NS + "a" ), 372 }; 373 374 for (int i = 0; i < toAdd.length; i++) { 376 RDFList list0 = list.cons( toAdd[i] ); 377 378 checkValid( "constest1", list0, true ); 379 assertTrue( "cons'ed lists should not be equal", !list0.equals( list ) ); 380 381 list = list0; 382 } 383 384 m.add( root, p, list ); 386 387 Model m0 = ModelFactory.createDefaultModel(); 389 m0.read( "file:testing/ontology/list5.rdf" ); 390 391 assertTrue( "Cons'ed and read models should be the same", m0.isIsomorphicWith( m ) ); 392 } 393 } 394 395 396 protected static class AddTest extends ListTest { 397 public AddTest() {super( "AddTest" );} 398 399 public void runTest() { 400 Model m = ModelFactory.createDefaultModel(); 401 402 Resource root = m.createResource( NS + "root" ); 403 Property p = m.createProperty( NS, "p"); 404 405 Resource nil = m.getResource( RDF.nil.getURI() ); 406 RDFList list = (RDFList) nil.as( RDFList.class ); 407 408 Resource[] toAdd = new Resource[] { 409 m.createResource( NS + "a" ), 410 m.createResource( NS + "b" ), 411 m.createResource( NS + "c" ), 412 m.createResource( NS + "d" ), 413 m.createResource( NS + "e" ), 414 }; 415 416 for (int i = 0; i < toAdd.length; i++) { 418 RDFList list0 = list.with( toAdd[i] ); 419 420 checkValid( "addTest0", list0, true ); 421 assertTrue( "added'ed lists should be equal", list.equals( nil ) || list0.equals( list ) ); 422 423 list = list0; 424 } 425 426 m.add( root, p, list ); 428 429 Model m0 = ModelFactory.createDefaultModel(); 431 m0.read( "file:testing/ontology/list5.rdf" ); 432 433 assertTrue( "Add'ed and read models should be the same", m0.isIsomorphicWith( m ) ); 434 } 435 } 436 437 438 protected static class TestListGet extends ListTest { 439 public TestListGet() {super("TestListGet");} 440 441 public void runTest() { 442 Model m = ModelFactory.createDefaultModel(); 443 m.read( "file:testing/ontology/list5.rdf" ); 444 445 Resource[] toGet = new Resource[] { 446 m.createResource( NS + "a" ), 447 m.createResource( NS + "b" ), 448 m.createResource( NS + "c" ), 449 m.createResource( NS + "d" ), 450 m.createResource( NS + "e" ), 451 }; 452 453 RDFList l1 = getListRoot( m ); 454 455 for (int i = 0; i < toGet.length; i++) { 457 assertEquals( "list element " + i + " is not correct", toGet[i], l1.get( i ) ); 458 } 459 460 boolean gotEx = false; 462 try { 463 l1.get( toGet.length + 1 ); 464 } 465 catch (ListIndexException e) { 466 gotEx = true; 467 } 468 469 assertTrue( "Should see exception raised by accessing beyond end of list", gotEx ); 470 } 471 } 472 473 474 protected static class ReplaceTest extends ListTest { 475 public ReplaceTest() {super("ReplaceTest");} 476 477 public void runTest() { 478 Model m = ModelFactory.createDefaultModel(); 479 m.read( "file:testing/ontology/list5.rdf" ); 480 481 Literal[] toSet = new Literal[] { 482 m.createLiteral( "a" ), 483 m.createLiteral( "b" ), 484 m.createLiteral( "c" ), 485 m.createLiteral( "d" ), 486 m.createLiteral( "e" ), 487 }; 488 489 RDFList l1 = getListRoot( m ); 490 491 for (int i = 0; i < toSet.length; i++) { 493 l1.replace( i, toSet[i] ); 494 } 495 496 for (int i = 0; i < toSet.length; i++) { 498 assertEquals( "list element " + i + " is not correct", toSet[i], l1.get( i ) ); 499 } 500 501 boolean gotEx = false; 503 try { 504 l1.replace( toSet.length + 1, toSet[0] ); 505 } 506 catch (ListIndexException e) { 507 gotEx = true; 508 } 509 510 assertTrue( "Should see exception raised by accessing beyond end of list", gotEx ); 511 } 512 } 513 514 515 protected static class IndexTest1 extends ListTest { 516 public IndexTest1() {super("IndexTest1");} 517 518 public void runTest() { 519 Model m = ModelFactory.createDefaultModel(); 520 m.read( "file:testing/ontology/list5.rdf" ); 521 522 Resource[] toGet = new Resource[] { 523 m.createResource( NS + "a" ), 524 m.createResource( NS + "b" ), 525 m.createResource( NS + "c" ), 526 m.createResource( NS + "d" ), 527 m.createResource( NS + "e" ), 528 }; 529 530 RDFList l1 = getListRoot( m ); 531 532 for (int i = 0; i < toGet.length; i++) { 534 assertTrue( "list should contain element " + i, l1.contains( toGet[i] ) ); 535 assertEquals( "list element " + i + " is not correct", i, l1.indexOf( toGet[i] ) ); 536 } 537 } 538 } 539 540 541 protected static class IndexTest2 extends ListTest { 542 public IndexTest2() {super("IndexTest2");} 543 544 public void runTest() { 545 Model m = ModelFactory.createDefaultModel(); 546 547 Resource nil = m.getResource( RDF.nil.getURI() ); 548 RDFList list = (RDFList) nil.as( RDFList.class ); 549 550 Resource r = m.createResource( NS + "a" ); 551 552 for (int i = 0; i < 10; i++) { 554 list = list.cons( r ); 555 } 556 557 for (int j = 0; j < 10; j++) { 559 assertEquals( "index of j'th item should be j", j, list.indexOf( r, j ) ); 560 } 561 } 562 } 563 564 565 protected static class AppendTest extends ListTest { 566 public AppendTest() {super("AppendTest");} 567 568 public void runTest() { 569 Model m = ModelFactory.createDefaultModel(); 570 m.read( "file:testing/ontology/list5.rdf" ); 571 572 Resource nil = m.getResource( RDF.nil.getURI() ); 573 RDFList list = (RDFList) nil.as( RDFList.class ); 574 575 Resource r = m.createResource( NS + "foo" ); 576 577 for (int i = 0; i < 5; i++) { 579 list = list.cons( r ); 580 } 581 582 int listLen = list.size(); 583 584 RDFList root = getListRoot( m ); 586 int rootLen = root.size(); 587 RDFList appended = root.append( list ); 588 589 checkValid( "appendTest0", root, true ); 591 assertEquals( "Original list should be unchanged", rootLen, root.size() ); 592 593 checkValid( "appendTest1", list, true ); 594 assertEquals( "Original list should be unchanged", listLen, list.size() ); 595 596 checkValid( "appendTest2", appended, true ); 598 assertEquals( "Appended list not correct length", rootLen + listLen, appended.size() ); 599 } 600 } 601 602 603 protected static class ConcatenateTest extends ListTest { 604 public ConcatenateTest() {super("ConcatenateTest");} 605 606 public void runTest() { 607 Model m = ModelFactory.createDefaultModel(); 608 m.read( "file:testing/ontology/list5.rdf" ); 609 610 Resource nil = m.getResource( RDF.nil.getURI() ); 611 RDFList list = (RDFList) nil.as( RDFList.class ); 612 613 Resource r = m.createResource( NS + "foo" ); 614 615 for (int i = 0; i < 5; i++) { 617 list = list.cons( r ); 618 } 619 620 int listLen = list.size(); 621 622 RDFList root = getListRoot( m ); 624 int rootLen = root.size(); 625 root.concatenate( list ); 626 627 checkValid( "concatTest0", list, true ); 629 assertEquals( "Original list should be unchanged", listLen, list.size() ); 630 631 checkValid( "concatTest1", root, true ); 633 assertEquals( "Root list should be new length", rootLen + listLen, root.size() ); 634 } 635 } 636 637 638 protected static class ConcatenateTest2 extends ListTest { 639 public ConcatenateTest2() {super("ConcatenateTest2");} 640 641 public void runTest() { 642 Model m = ModelFactory.createDefaultModel(); 643 m.read( "file:testing/ontology/list5.rdf" ); 644 645 Resource a = m.createResource( NS + "a" ); 646 647 Resource[] rs = new Resource[] { 649 m.createResource( NS + "b" ), 650 m.createResource( NS + "c" ), 651 m.createResource( NS + "d" ), 652 m.createResource( NS + "e" ) 653 }; 654 655 RDFList aList = m.createList().cons( a ); 656 RDFList rsList = m.createList( rs ); 657 658 aList.concatenate( rsList ); 660 checkValid( "concatTest3", aList, true ); 661 662 RDFList root = getListRoot( m ); 663 assertTrue( "Constructed and loaded lists should be the same", aList.sameListAs( root ) ); 664 } 665 } 666 667 668 protected static class ApplyTest extends ListTest { 669 public ApplyTest() {super("ApplyTest");} 670 671 public void runTest() { 672 Model m = ModelFactory.createDefaultModel(); 673 m.read( "file:testing/ontology/list5.rdf" ); 674 675 RDFList root = getListRoot( m ); 676 677 class MyApply implements RDFList.ApplyFn { 678 String collect = ""; 679 public void apply( RDFNode n ) { 680 collect = collect + ((Resource) n).getLocalName(); 681 } 682 } 683 684 MyApply f = new MyApply(); 685 root.apply( f ); 686 687 assertEquals( "Result of apply should be concatentation of local names", "abcde", f.collect ); 688 } 689 } 690 691 692 protected static class ReduceTest extends ListTest { 693 public ReduceTest() {super("ReduceTest");} 694 695 public void runTest() { 696 Model m = ModelFactory.createDefaultModel(); 697 m.read( "file:testing/ontology/list5.rdf" ); 698 699 RDFList root = getListRoot( m ); 700 701 RDFList.ReduceFn f = new RDFList.ReduceFn() { 702 public Object reduce( RDFNode n, Object acc ) { 703 return ((String ) acc) + ((Resource) n).getLocalName(); 704 } 705 }; 706 707 assertEquals( "Result of reduce should be concatentation of local names", "abcde", root.reduce( f, "" ) ); 708 } 709 } 710 711 protected static class Map1Test extends ListTest { 712 public Map1Test() {super("Map1Test");} 713 714 public void runTest() { 715 Model m = ModelFactory.createDefaultModel(); 716 m.read( "file:testing/ontology/list5.rdf" ); 717 718 RDFList root = getListRoot( m ); 719 iteratorTest( root.mapWith( new Map1() {public Object map1(Object x){return ((Resource) x).getLocalName();} } ), 720 new Object [] {"a","b","c","d","e"} ); 721 } 722 } 723 724 protected static class RemoveTest extends ListTest { 725 public RemoveTest() {super( "RemoveTest" );} 726 727 public void runTest() { 728 Model m = ModelFactory.createDefaultModel(); 729 730 Resource nil = m.getResource( RDF.nil.getURI() ); 731 RDFList list0 = (RDFList) nil.as( RDFList.class ); 732 RDFList list1 = (RDFList) nil.as( RDFList.class ); 733 734 Resource r0 = m.createResource( NS + "x" ); 735 Resource r1 = m.createResource( NS + "y" ); 736 Resource r2 = m.createResource( NS + "z" ); 737 738 for (int i = 0; i < 10; i++) { 739 list0 = list0.cons( r0 ); 740 list1 = list1.cons( r1 ); 741 } 742 743 while (!list0.isEmpty()) { 745 list0 = list0.removeHead(); 746 checkValid( "removeTest0", list0, true ); 747 } 748 749 750 list1.removeList(); 752 753 assertEquals( "Model should be empty after deleting two lists", 0, m.size() ); 755 756 RDFList list2 = ((RDFList) nil.as( RDFList.class )) 758 .cons( r2 ) 759 .cons( r1 ) 760 .cons( r0 ); 761 762 assertTrue( "list should contain x ", list2.contains( r0 )); 763 assertTrue( "list should contain y ", list2.contains( r1 )); 764 assertTrue( "list should contain z ", list2.contains( r2 )); 765 766 list2 = list2.remove( r1 ); 767 assertTrue( "list should contain x ", list2.contains( r0 )); 768 assertTrue( "list should contain y ", !list2.contains( r1 )); 769 assertTrue( "list should contain z ", list2.contains( r2 )); 770 771 list2 = list2.remove( r0 ); 772 assertTrue( "list should contain x ", !list2.contains( r0 )); 773 assertTrue( "list should contain y ", !list2.contains( r1 )); 774 assertTrue( "list should contain z ", list2.contains( r2 )); 775 776 list2 = list2.remove( r2 ); 777 assertTrue( "list should contain x ", !list2.contains( r0 )); 778 assertTrue( "list should contain y ", !list2.contains( r1 )); 779 assertTrue( "list should contain z ", !list2.contains( r2 )); 780 assertTrue( "list should be empty", list2.isEmpty() ); 781 } 782 } 783 784 785 protected static class ListEqualsTest extends ListTest { 786 public ListEqualsTest() {super("ListEqualsTest");} 787 788 public void runTest() { 789 Model m = ModelFactory.createDefaultModel(); 790 791 Resource nil = m.getResource( RDF.nil.getURI() ); 792 RDFList nilList = (RDFList) nil.as( RDFList.class ); 793 794 Resource[] r0 = new Resource[] { 796 m.createResource( NS + "a" ), m.createResource( NS + "b" ), 798 m.createResource( NS + "c" ), 799 m.createResource( NS + "d" ), 800 m.createResource( NS + "e" ) 801 }; 802 Resource[] r1 = new Resource[] { 803 m.createResource( NS + "a" ), m.createResource( NS + "b" ), 805 m.createResource( NS + "c" ), 806 m.createResource( NS + "d" ), 807 m.createResource( NS + "e" ) 808 }; 809 Resource[] r2 = new Resource[] { 810 m.createResource( NS + "a" ), m.createResource( NS + "b" ), 812 m.createResource( NS + "c" ), 813 m.createResource( NS + "d" ) 814 }; 815 Resource[] r3 = new Resource[] { 816 m.createResource( NS + "a" ), m.createResource( NS + "b" ), 818 m.createResource( NS + "d" ), 819 m.createResource( NS + "c" ), 820 m.createResource( NS + "e" ) 821 }; 822 Resource[] r4 = new Resource[] { 823 m.createResource( NS + "a" ), m.createResource( NS + "b" ), 825 m.createResource( NS + "c" ), 826 m.createResource( NS + "D" ), 827 m.createResource( NS + "e" ) 828 }; 829 830 Object [][] testSpec = new Object [][] { 831 {r0, r1, Boolean.TRUE}, 832 {r0, r2, Boolean.FALSE}, 833 {r0, r3, Boolean.FALSE}, 834 {r0, r4, Boolean.FALSE}, 835 {r1, r2, Boolean.FALSE}, 836 {r1, r3, Boolean.FALSE}, 837 {r1, r4, Boolean.FALSE}, 838 {r2, r3, Boolean.FALSE}, 839 {r2, r4, Boolean.FALSE}, 840 }; 841 842 for (int i = 0; i < testSpec.length; i++) { 843 RDFList l0 = nilList.append( Arrays.asList( (Object []) testSpec[i][0] ).iterator() ); 844 RDFList l1 = nilList.append( Arrays.asList( (Object []) testSpec[i][1] ).iterator() ); 845 boolean expected = ((Boolean ) testSpec[i][2]).booleanValue(); 846 847 assertEquals( "sameListAs testSpec[" + i + "] incorrect", expected, l0.sameListAs( l1 ) ); 848 assertEquals( "sameListAs testSpec[" + i + "] (swapped) incorrect", expected, l1.sameListAs( l0 ) ); 849 } 850 } 851 } 852 853 protected static class ListSubclassTest extends ListTest { 854 public ListSubclassTest() { 855 super( "ListSubClassTest"); 856 } 857 858 public void runTest() { 859 Model m = ModelFactory.createDefaultModel(); 860 861 String NS = "http://example.org/test#"; 862 Resource a = m.createResource( NS + "a" ); 863 Resource b = m.createResource( NS + "b" ); 864 865 Resource cell0 = m.createResource(); 866 Resource cell1 = m.createResource(); 867 cell0.addProperty( RDF.first, a ); 868 cell0.addProperty( RDF.rest, cell1 ); 869 cell1.addProperty( RDF.first, b ); 870 cell1.addProperty( RDF.rest, RDF.nil ); 871 872 UserList ul = new UserListImpl( cell0.getNode(), (EnhGraph) m ); 873 874 assertEquals( "User list length ", 2, ul.size() ); 875 assertEquals( "head of user list ", a, ul.getHead() ); 876 877 RDFList l = (RDFList) ul.as( RDFList.class ); 878 assertNotNull( "RDFList facet of user-defined list subclass", l ); 879 } 880 } 881 882 883 protected static interface UserList extends RDFList { 884 } 885 886 887 protected static class UserListImpl extends RDFListImpl implements UserList { 888 public UserListImpl( Node n, EnhGraph g ) { 889 super( n, g ); 890 } 891 } 892 893 894 protected static class UserDefinedListTest extends ListTest { 895 public UserDefinedListTest() { 896 super( "UserDefinedListTest"); 897 } 898 899 public void runTest() { 900 BuiltinPersonalities.model.add( UserDefList.class, UserDefListImpl.factory ); 901 902 Model m = ModelFactory.createDefaultModel(); 903 904 String NS = "http://example.org/test#"; 905 Resource a = m.createResource( NS + "a" ); 906 Resource b = m.createResource( NS + "b" ); 907 908 Resource empty = m.createResource( UserDefListImpl.NIL.getURI() ); 909 UserDefList ul = (UserDefList) empty.as( UserDefList.class ); 910 assertNotNull( "UserList facet of empty list", ul ); 911 912 UserDefList ul0 = (UserDefList) ul.cons( b ); 913 ul0 = (UserDefList) ul0.cons( a ); 914 assertEquals( "should be length 2", 2, ul0.size() ); 915 assertTrue( "first statement", m.contains( ul0, UserDefListImpl.FIRST, a ) ); 916 } 917 } 918 919 protected static interface UserDefList extends RDFList {} 920 921 protected static class UserDefListImpl extends RDFListImpl implements UserDefList { 922 public static final String NS = "http://example.org/testlist#"; 923 public static final Property FIRST = ResourceFactory.createProperty( NS+"first" ); 924 public static final Property REST = ResourceFactory.createProperty( NS+"rest" ); 925 public static final Resource NIL = ResourceFactory.createResource( NS+"nil" ); 926 public static final Resource LIST = ResourceFactory.createResource( NS+"List" ); 927 928 931 public static Implementation factory = new Implementation() { 932 public EnhNode wrap( Node n, EnhGraph eg ) { 933 if (canWrap( n, eg )) { 934 UserDefListImpl impl = new UserDefListImpl( n, eg ); 935 936 Model m = impl.getModel(); 937 impl.m_listFirst = (Property) FIRST.inModel( m ); 938 impl.m_listRest = (Property) REST.inModel( m ); 939 impl.m_listNil = (Resource) NIL.inModel( m ); 940 impl.m_listType = (Resource) LIST.inModel( m ); 941 942 return impl; 943 } 944 else { 945 throw new JenaException( "Cannot convert node " + n + " to UserDefList"); 946 } 947 } 948 949 public boolean canWrap( Node node, EnhGraph eg ) { 950 Graph g = eg.asGraph(); 951 952 return node.equals( NIL.asNode() ) || 953 g.contains( node, FIRST.asNode(), Node.ANY ) || 954 g.contains( node, REST.asNode(), Node.ANY ) || 955 g.contains( node, RDF.type.asNode(), LIST.asNode() ); 956 } 957 }; 958 959 960 public Class listAbstractionClass() { 961 return UserDefList.class; 962 } 963 964 public UserDefListImpl( Node n, EnhGraph g ) { 965 super( n, g ); 966 } 967 968 } 969 970 } 971 972 973 1002 | Popular Tags |