KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 24 Jan 2003
9  * Filename $RCSfile: TestList.java,v $
10  * Revision $Revision: 1.10 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/02/21 12:15:01 $
14  * by $Author: andy_seaborne $
15  *
16  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * (see footer for full conditions)
18  *****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.rdf.model.test;
23
24
25 // Imports
26
///////////////
27
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 /**
47  * <p>
48  * A collection of unit tests for the standard implementation of
49  * {@link RDFList}.
50  * </p>
51  *
52  * @author Ian Dickinson, HP Labs
53  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
54  * @version CVS $Id: TestList.java,v 1.10 2005/02/21 12:15:01 andy_seaborne Exp $
55  */

56 public class TestList
57     extends TestCase
58 {
59     // Constants
60
//////////////////////////////////
61

62     public static final String JavaDoc NS = "uri:urn:x-rdf:test#";
63     
64
65     // Static variables
66
//////////////////////////////////
67

68     
69     // Instance variables
70
//////////////////////////////////
71

72
73     // Constructors
74
//////////////////////////////////
75

76     public TestList( String JavaDoc name ) {
77         super( name );
78     }
79
80     // External signature methods
81
//////////////////////////////////
82

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     // Internal implementation methods
118
//////////////////////////////////
119

120     /** Test that an iterator delivers the expected values */
121     protected static void iteratorTest( Iterator i, Object JavaDoc[] 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 JavaDoc next = i.next();
130                 
131             // debugging
132
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     //==============================================================================
151
// Inner class definitions
152
//==============================================================================
153

154     protected static class ListTest extends TestCase {
155         public ListTest( String JavaDoc n ) {super(n);}
156         
157         protected void checkValid( String JavaDoc testName, RDFList l, boolean validExpected ) {
158             l.setStrict( true );
159             boolean valid = l.isValid();
160             // for debugging ... String s = l.getValidityErrorMessage();
161
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             // a list of the nil object, but not typed
208
Resource nil = RDF.nil;
209             m.add( root, p, nil );
210             RDFList l0 = getListRoot( m );
211             checkValid( "valid1", l0, true );
212             
213             // add another node to the head of the list
214
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             //checkValid( "valid3", l1, false );
223

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 JavaDoc[] 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             // get the tail n times, should be nil at the end
267
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             // a list of the nil object, but not typed
286
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             // use set tail to join the lists together
342
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             // cons each of these resources onto the front of the list
375
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             // relate the root to the list
385
m.add( root, p, list );
386
387             // should be isomorphic with list 5
388
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             // add each of these resources onto the end of the list
417
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             // relate the root to the list
427
m.add( root, p, list );
428
429             // should be isomorphic with list 5
430
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             // test normal gets
456
for (int i = 0; i < toGet.length; i++) {
457                 assertEquals( "list element " + i + " is not correct", toGet[i], l1.get( i ) );
458             }
459             
460             // now test we get an exception for going beyong the end of the list
461
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             // change all the values
492
for (int i = 0; i < toSet.length; i++) {
493                 l1.replace( i, toSet[i] );
494             }
495             
496             // then check them
497
for (int i = 0; i < toSet.length; i++) {
498                 assertEquals( "list element " + i + " is not correct", toSet[i], l1.get( i ) );
499             }
500
501             // now test we get an exception for going beyong the end of the list
502
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             // check the indexes are correct
533
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             // cons each a's onto the front of the list
553
for (int i = 0; i < 10; i++) {
554                 list = list.cons( r );
555             }
556             
557             // now index them back again
558
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             // create a list of foos
578
for (int i = 0; i < 5; i++) {
579                 list = list.cons( r );
580             }
581             
582             int listLen = list.size();
583             
584             // now append foos to the root list
585
RDFList root = getListRoot( m );
586             int rootLen = root.size();
587             RDFList appended = root.append( list );
588             
589             // original list should be unchanged
590
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             // new list should be length of combined
597
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             // create a list of foos
616
for (int i = 0; i < 5; i++) {
617                 list = list.cons( r );
618             }
619             
620             int listLen = list.size();
621             
622             // now append foos to the root list
623
RDFList root = getListRoot( m );
624             int rootLen = root.size();
625             root.concatenate( list );
626             
627             // original list should be unchanged
628
checkValid( "concatTest0", list, true );
629             assertEquals( "Original list should be unchanged", listLen, list.size() );
630             
631             // but lhs list has changed
632
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             // create a list of foos
648
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             // concatenate the above resources onto the empty list
659
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 JavaDoc 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 JavaDoc reduce( RDFNode n, Object JavaDoc acc ) {
703                    return ((String JavaDoc) 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 JavaDoc map1(Object JavaDoc x){return ((Resource) x).getLocalName();} } ),
720                           new Object JavaDoc[] {"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             // delete the elements of list0 one at a time
744
while (!list0.isEmpty()) {
745                 list0 = list0.removeHead();
746                 checkValid( "removeTest0", list0, true );
747             }
748             
749             
750             // delete all of list1 in one go
751
list1.removeList();
752             
753             // model should now be empty
754
assertEquals( "Model should be empty after deleting two lists", 0, m.size() );
755             
756             // selective remove
757
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             // create a list of foos
795
Resource[] r0 = new Resource[] {
796                 m.createResource( NS + "a" ), // canonical
797
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" ), // same
804
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" ), // one shorter
811
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" ), // elements swapped
817
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" ), // different name
824
m.createResource( NS + "b" ),
825                 m.createResource( NS + "c" ),
826                 m.createResource( NS + "D" ),
827                 m.createResource( NS + "e" )
828             };
829             
830             Object JavaDoc[][] testSpec = new Object JavaDoc[][] {
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 JavaDoc[]) testSpec[i][0] ).iterator() );
844                 RDFList l1 = nilList.append( Arrays.asList( (Object JavaDoc[]) testSpec[i][1] ).iterator() );
845                 boolean expected = ((Boolean JavaDoc) 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 JavaDoc 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     /** A simple extension to RDFList to test user-subclassing of RDFList */
883     protected static interface UserList extends RDFList {
884     }
885     
886     /** Impl of a simple extension to RDFList to test user-subclassing of RDFList */
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 JavaDoc 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 JavaDoc 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         /**
929          * A factory for generating UserDefList facets from nodes in enhanced graphs.
930          */

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         /** This method returns the Java class object that defines which abstraction facet is presented */
960         public Class JavaDoc 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 /*
974     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
975     All rights reserved.
976
977     Redistribution and use in source and binary forms, with or without
978     modification, are permitted provided that the following conditions
979     are met:
980
981     1. Redistributions of source code must retain the above copyright
982        notice, this list of conditions and the following disclaimer.
983
984     2. Redistributions in binary form must reproduce the above copyright
985        notice, this list of conditions and the following disclaimer in the
986        documentation and/or other materials provided with the distribution.
987
988     3. The name of the author may not be used to endorse or promote products
989        derived from this software without specific prior written permission.
990
991     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
992     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
993     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
994     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
995     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
996     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
997     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
998     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
999     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1000    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1001*/

1002
Popular Tags