You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by sa...@apache.org on 2013/01/29 21:27:39 UTC
svn commit: r1440101 [11/16] - in /jena/branches/streaming-update: ./
apache-jena-libs/ apache-jena/ apache-jena/bat/ apache-jena/bin/ jena-arq/
jena-arq/src-examples/arq/examples/riot/ jena-arq/src/main/java/arq/
jena-arq/src/main/java/arq/cmdline/ je...
Modified: jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestList.java
URL: http://svn.apache.org/viewvc/jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestList.java?rev=1440101&r1=1440100&r2=1440101&view=diff
==============================================================================
--- jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestList.java (original)
+++ jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestList.java Tue Jan 29 20:27:24 2013
@@ -1,14 +1,14 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
+ * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
+ * regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,990 +17,907 @@
*/
// Package
-///////////////
+// /////////////
package com.hp.hpl.jena.rdf.model.test;
-
// Imports
-///////////////
-import java.util.*;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import junit.framework.*;
-
-import com.hp.hpl.jena.enhanced.*;
-import com.hp.hpl.jena.graph.Graph;
-import com.hp.hpl.jena.graph.Node;
-import com.hp.hpl.jena.rdf.model.*;
-import com.hp.hpl.jena.rdf.model.impl.RDFListImpl;
-import com.hp.hpl.jena.shared.JenaException;
+// /////////////
+import com.hp.hpl.jena.rdf.model.ListIndexException;
+import com.hp.hpl.jena.rdf.model.Literal;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.RDFList;
+import com.hp.hpl.jena.rdf.model.RDFNode;
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.test.helpers.TestingModelFactory;
import com.hp.hpl.jena.util.iterator.Map1;
-import com.hp.hpl.jena.vocabulary.*;
+import com.hp.hpl.jena.vocabulary.RDF;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import junit.framework.Assert;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* <p>
- * A collection of unit tests for the standard implementation of
- * {@link RDFList}.
+ * A collection of unit tests for the standard implementation of {@link RDFList}
+ * .
* </p>
*
- * @author Ian Dickinson, HP Labs
- * (<a href="mailto:ian_dickinson@users.sourceforge.net" >email</a>)
- * @version CVS $Id: TestList.java,v 1.3 2010-01-11 09:17:05 chris-dollin Exp $
+ *
*/
-public class TestList
- extends TestCase
+public class TestList extends AbstractModelTestBase
{
- // Constants
- //////////////////////////////////
-
- public static final String NS = "uri:urn:x-rdf:test#";
-
+ // Constants
+ // ////////////////////////////////
- // Static variables
- //////////////////////////////////
+ public static final String NS = "uri:urn:x-rdf:test#";
-
- // Instance variables
- //////////////////////////////////
-
-
- // Constructors
- //////////////////////////////////
-
- public TestList( String name ) {
- super( name );
- }
-
- // External signature methods
- //////////////////////////////////
-
- public static TestSuite suite() {
- TestSuite s = new TestSuite( "TestList" );
-
- for (int i = 0; i <= 5; i++) {
- s.addTest( new CountTest( i ) );
- s.addTest( new TailTest( i ) );
- }
-
- s.addTest( new ValidityTest() );
- s.addTest( new HeadTest() );
- s.addTest( new SetHeadTest() );
- s.addTest( new SetTailTest() );
- s.addTest( new ConsTest() );
- s.addTest( new AddTest() );
- s.addTest( new TestListGet() );
- s.addTest( new ReplaceTest() );
- s.addTest( new IndexTest1() );
- s.addTest( new IndexTest2() );
- s.addTest( new AppendTest() );
- s.addTest( new ConcatenateTest() );
- s.addTest( new ConcatenateTest2() );
- s.addTest( new ApplyTest() );
- s.addTest( new ReduceTest() );
- s.addTest( new RemoveTest() );
- s.addTest( new Map1Test() );
- s.addTest( new ListEqualsTest() );
- s.addTest( new ListSubclassTest() );
- s.addTest( new UserDefinedListTest() );
- s.addTest( new CopyTest() );
- return s;
- }
-
-
-
- // Internal implementation methods
- //////////////////////////////////
-
- /** Test that an iterator delivers the expected values */
- protected static void iteratorTest( Iterator<?> i, Object[] expected ) {
- Logger logger = LoggerFactory.getLogger( TestList.class );
- List<Object> expList = new ArrayList<Object>();
- for (int j = 0; j < expected.length; j++) {
- expList.add( expected[j] );
- }
-
- while (i.hasNext()) {
- Object next = i.next();
-
- // debugging
- if (!expList.contains( next )) {
- logger.debug( "TestList - Unexpected iterator result: " + next );
- }
-
- assertTrue( "Value " + next + " was not expected as a result from this iterator ", expList.contains( next ) );
- assertTrue( "Value " + next + " was not removed from the list ", expList.remove( next ) );
- }
-
- if (!(expList.size() == 0)) {
- logger.debug( "TestList - Expected iterator results not found" );
- for (Iterator<Object> j = expList.iterator(); j.hasNext(); ) {
- logger.debug( "TestList - missing: " + j.next() );
- }
- }
- assertEquals( "There were expected elements from the iterator that were not found", 0, expList.size() );
- }
-
-
- //==============================================================================
- // Inner class definitions
- //==============================================================================
-
- protected static class ListTest extends TestCase {
- public ListTest( String n ) {super(n);}
-
- protected void checkValid( String testName, RDFList l, boolean validExpected ) {
- l.setStrict( true );
- boolean valid = l.isValid();
- // for debugging ... String s = l.getValidityErrorMessage();
- assertEquals( "Validity test " + testName + " returned wrong isValid() result", validExpected, valid );
- }
-
- protected RDFList getListRoot( Model m ) {
- Resource root = m.getResource( NS + "root" );
- assertNotNull( "Root resource should not be null", root );
-
- Resource listHead = root.getRequiredProperty( m.getProperty( NS + "p" ) ).getResource();
-
- RDFList l = listHead.as( RDFList.class );
- assertNotNull( "as(RDFList) should not return null for root", l );
-
- return l;
- }
- }
-
- protected static class CountTest extends ListTest {
- protected int i;
-
- public CountTest( int i ) {
- super( "CountTest" );
- this.i = i;
- }
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list" + i + ".rdf" );
-
- RDFList l0 = getListRoot( m );
- assertEquals( "List size should be " + i, i, l0.size() );
- }
-
- }
-
-
- protected static class ValidityTest extends ListTest {
- public ValidityTest() {
- super( "ValidityTest");
- }
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
-
- Resource root = m.createResource( NS + "root" );
- Property p = m.createProperty( NS, "p");
-
- // a list of the nil object, but not typed
- Resource nil = RDF.nil;
- m.add( root, p, nil );
- RDFList l0 = getListRoot( m );
- checkValid( "valid1", l0, true );
-
- // add another node to the head of the list
- Resource badList = m.createResource();
- m.getRequiredProperty( root, p ).remove();
- m.add( root, p, badList );
- m.add( badList, RDF.type, RDF.List );
-
- RDFList l1 = getListRoot( m );
- checkValid( "valid2", l1, false );
-
- //checkValid( "valid3", l1, false );
-
- m.add( badList, RDF.first, "fred" );
- checkValid( "valid4", l1, false );
-
- m.add( badList, RDF.rest, nil );
- checkValid( "valid5", l1, true );
- }
-
- }
-
-
- protected static class HeadTest extends ListTest {
- public HeadTest() {super( "HeadTest");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list5.rdf" );
-
- RDFList l0 = getListRoot( m );
-
- String[] names = {"a", "b", "c", "d", "e"};
- for (int i = 0; i < names.length; i++) {
- assertEquals( "head of list has incorrect URI", NS + names[i], ((Resource) l0.getHead()).getURI() );
- l0 = l0.getTail();
- }
- }
- }
-
-
- protected static class TailTest extends ListTest {
- protected int i;
-
- public TailTest( int i ) {
- super( "TailTest" );
- this.i = i;
- }
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list" + i + ".rdf" );
-
- RDFList l0 = getListRoot( m );
-
- // get the tail n times, should be nil at the end
- for (int j = 0; j < i; j++) {
- l0 = l0.getTail();
- }
-
- assertTrue( "Should have reached the end of the list after " + i + " getTail()'s", l0.isEmpty() );
- }
- }
-
-
- protected static class SetHeadTest extends ListTest {
- public SetHeadTest() {super( "SetHeadTest");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
-
- Resource root = m.createResource( NS + "root" );
- Property p = m.createProperty( NS, "p");
-
- // a list of the nil object, but not typed
- Resource nil = RDF.nil;
- m.add( nil, RDF.type, RDF.List );
-
- Resource list = m.createResource();
- m.add( list, RDF.type, RDF.List );
- m.add( list, RDF.first, "fred" );
- m.add( list, RDF.rest, nil );
-
- m.add( root, p, list );
- RDFList l1 = getListRoot( m );
- checkValid( "sethead1", l1, true );
-
- assertEquals( "List head should be 'fred'", "fred", ((Literal) l1.getHead()).getString() );
-
- l1.setHead( m.createTypedLiteral( 42 ) );
- checkValid( "sethead2", l1, true );
- assertEquals( "List head should be '42'", 42, ((Literal) l1.getHead()).getInt() );
-
- }
- }
-
-
- protected static class SetTailTest extends ListTest {
- public SetTailTest() {super( "SetTailTest");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
-
- Resource root = m.createResource( NS + "root" );
- Property p = m.createProperty( NS, "p");
-
- Resource nil = RDF.nil;
- m.add( nil, RDF.type, RDF.List );
-
- Resource list0 = m.createResource();
- m.add( list0, RDF.type, RDF.List );
- m.add( list0, RDF.first, "fred" );
- m.add( list0, RDF.rest, nil );
-
- m.add( root, p, list0 );
- RDFList l1 = getListRoot( m );
- checkValid( "settail1", l1, true );
-
- Resource list1 = m.createResource();
- m.add( list1, RDF.type, RDF.List );
- m.add( list1, RDF.first, "george" );
- m.add( list1, RDF.rest, nil );
-
- RDFList l2 = list1.as( RDFList.class );
- assertNotNull( "as(RDFList) should not return null for root", l2 );
- checkValid( "settail2", l2, true );
-
- assertEquals( "l1 should have length 1", 1, l1.size() );
- assertEquals( "l2 should have length 1", 1, l2.size() );
-
- // use set tail to join the lists together
- l1.setTail( l2 );
-
- checkValid( "settail3", l1, true );
- checkValid( "settail4", l2, true );
-
- assertEquals( "l1 should have length 2", 2, l1.size() );
- assertEquals( "l2 should have length 1", 1, l2.size() );
-
- }
- }
-
-
- protected static class ConsTest extends ListTest {
- public ConsTest() {super( "ConsTest" );}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
-
- Resource root = m.createResource( NS + "root" );
- Property p = m.createProperty( NS, "p");
-
- Resource nil = m.getResource( RDF.nil.getURI() );
- RDFList list = nil.as( RDFList.class );
-
- Resource[] toAdd = new Resource[] {
- m.createResource( NS + "e" ),
- m.createResource( NS + "d" ),
- m.createResource( NS + "c" ),
- m.createResource( NS + "b" ),
- m.createResource( NS + "a" ),
- };
-
- // cons each of these resources onto the front of the list
- for (int i = 0; i < toAdd.length; i++) {
- RDFList list0 = list.cons( toAdd[i] );
-
- checkValid( "constest1", list0, true );
- assertTrue( "cons'ed lists should not be equal", !list0.equals( list ) );
-
- list = list0;
- }
-
- // relate the root to the list
- m.add( root, p, list );
-
- // should be isomorphic with list 5
- Model m0 = ModelFactory.createDefaultModel();
- m0.read( "file:testing/ontology/list5.rdf" );
-
- assertTrue( "Cons'ed and read models should be the same", m0.isIsomorphicWith( m ) );
- }
- }
-
-
- protected static class AddTest extends ListTest {
- public AddTest() {super( "AddTest" );}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
-
- Resource root = m.createResource( NS + "root" );
- Property p = m.createProperty( NS, "p");
-
- Resource nil = m.getResource( RDF.nil.getURI() );
- RDFList list = nil.as( RDFList.class );
-
- Resource[] toAdd = new Resource[] {
- m.createResource( NS + "a" ),
- m.createResource( NS + "b" ),
- m.createResource( NS + "c" ),
- m.createResource( NS + "d" ),
- m.createResource( NS + "e" ),
- };
-
- // add each of these resources onto the end of the list
- for (int i = 0; i < toAdd.length; i++) {
- RDFList list0 = list.with( toAdd[i] );
-
- checkValid( "addTest0", list0, true );
- assertTrue( "added'ed lists should be equal", list.equals( nil ) || list0.equals( list ) );
-
- list = list0;
- }
-
- // relate the root to the list
- m.add( root, p, list );
-
- // should be isomorphic with list 5
- Model m0 = ModelFactory.createDefaultModel();
- m0.read( "file:testing/ontology/list5.rdf" );
-
- assertTrue( "Add'ed and read models should be the same", m0.isIsomorphicWith( m ) );
- }
- }
-
-
- protected static class TestListGet extends ListTest {
- public TestListGet() {super("TestListGet");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list5.rdf" );
-
- Resource[] toGet = new Resource[] {
- m.createResource( NS + "a" ),
- m.createResource( NS + "b" ),
- m.createResource( NS + "c" ),
- m.createResource( NS + "d" ),
- m.createResource( NS + "e" ),
- };
-
- RDFList l1 = getListRoot( m );
-
- // test normal gets
- for (int i = 0; i < toGet.length; i++) {
- assertEquals( "list element " + i + " is not correct", toGet[i], l1.get( i ) );
- }
-
- // now test we get an exception for going beyong the end of the list
- boolean gotEx = false;
- try {
- l1.get( toGet.length + 1 );
- }
- catch (ListIndexException e) {
- gotEx = true;
- }
-
- assertTrue( "Should see exception raised by accessing beyond end of list", gotEx );
- }
- }
-
-
- protected static class ReplaceTest extends ListTest {
- public ReplaceTest() {super("ReplaceTest");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list5.rdf" );
-
- Literal[] toSet = new Literal[] {
- m.createLiteral( "a" ),
- m.createLiteral( "b" ),
- m.createLiteral( "c" ),
- m.createLiteral( "d" ),
- m.createLiteral( "e" ),
- };
-
- RDFList l1 = getListRoot( m );
-
- // change all the values
- for (int i = 0; i < toSet.length; i++) {
- l1.replace( i, toSet[i] );
- }
-
- // then check them
- for (int i = 0; i < toSet.length; i++) {
- assertEquals( "list element " + i + " is not correct", toSet[i], l1.get( i ) );
- }
-
- // now test we get an exception for going beyong the end of the list
- boolean gotEx = false;
- try {
- l1.replace( toSet.length + 1, toSet[0] );
- }
- catch (ListIndexException e) {
- gotEx = true;
- }
-
- assertTrue( "Should see exception raised by accessing beyond end of list", gotEx );
- }
- }
-
-
- protected static class IndexTest1 extends ListTest {
- public IndexTest1() {super("IndexTest1");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list5.rdf" );
-
- Resource[] toGet = new Resource[] {
- m.createResource( NS + "a" ),
- m.createResource( NS + "b" ),
- m.createResource( NS + "c" ),
- m.createResource( NS + "d" ),
- m.createResource( NS + "e" ),
- };
-
- RDFList l1 = getListRoot( m );
-
- // check the indexes are correct
- for (int i = 0; i < toGet.length; i++) {
- assertTrue( "list should contain element " + i, l1.contains( toGet[i] ) );
- assertEquals( "list element " + i + " is not correct", i, l1.indexOf( toGet[i] ) );
- }
- }
- }
-
-
- protected static class IndexTest2 extends ListTest {
- public IndexTest2() {super("IndexTest2");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
-
- Resource nil = m.getResource( RDF.nil.getURI() );
- RDFList list = nil.as( RDFList.class );
-
- Resource r = m.createResource( NS + "a" );
-
- // cons each a's onto the front of the list
- for (int i = 0; i < 10; i++) {
- list = list.cons( r );
- }
-
- // now index them back again
- for (int j = 0; j < 10; j++) {
- assertEquals( "index of j'th item should be j", j, list.indexOf( r, j ) );
- }
- }
- }
-
-
- protected static class AppendTest extends ListTest {
- public AppendTest() {super("AppendTest");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list5.rdf" );
-
- Resource nil = m.getResource( RDF.nil.getURI() );
- RDFList list = nil.as( RDFList.class );
-
- Resource r = m.createResource( NS + "foo" );
-
- // create a list of foos
- for (int i = 0; i < 5; i++) {
- list = list.cons( r );
- }
-
- int listLen = list.size();
-
- // now append foos to the root list
- RDFList root = getListRoot( m );
- int rootLen = root.size();
- RDFList appended = root.append( list );
-
- // original list should be unchanged
- checkValid( "appendTest0", root, true );
- assertEquals( "Original list should be unchanged", rootLen, root.size() );
-
- checkValid( "appendTest1", list, true );
- assertEquals( "Original list should be unchanged", listLen, list.size() );
-
- // new list should be length of combined
- checkValid( "appendTest2", appended, true );
- assertEquals( "Appended list not correct length", rootLen + listLen, appended.size() );
- }
- }
-
-
- protected static class ConcatenateTest extends ListTest {
- public ConcatenateTest() {super("ConcatenateTest");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list5.rdf" );
-
- Resource nil = m.getResource( RDF.nil.getURI() );
- RDFList list = nil.as( RDFList.class );
-
- Resource r = m.createResource( NS + "foo" );
-
- // create a list of foos
- for (int i = 0; i < 5; i++) {
- list = list.cons( r );
- }
-
- int listLen = list.size();
-
- // now append foos to the root list
- RDFList root = getListRoot( m );
- int rootLen = root.size();
- root.concatenate( list );
-
- // original list should be unchanged
- checkValid( "concatTest0", list, true );
- assertEquals( "Original list should be unchanged", listLen, list.size() );
-
- // but lhs list has changed
- checkValid( "concatTest1", root, true );
- assertEquals( "Root list should be new length", rootLen + listLen, root.size() );
- }
- }
-
-
- protected static class ConcatenateTest2 extends ListTest {
- public ConcatenateTest2() {super("ConcatenateTest2");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list5.rdf" );
-
- Resource a = m.createResource( NS + "a" );
-
- // create a list of foos
- Resource[] rs = new Resource[] {
- m.createResource( NS + "b" ),
- m.createResource( NS + "c" ),
- m.createResource( NS + "d" ),
- m.createResource( NS + "e" )
- };
-
- RDFList aList = m.createList().cons( a );
- RDFList rsList = m.createList( rs );
-
- // concatenate the above resources onto the empty list
- aList.concatenate( rsList );
- checkValid( "concatTest3", aList, true );
-
- RDFList root = getListRoot( m );
- assertTrue( "Constructed and loaded lists should be the same", aList.sameListAs( root ) );
- }
- }
-
- protected static class CopyTest extends ListTest {
- public CopyTest() {super("CopyTest");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- // check for empty copy error (JENA-360)
- RDFList list = m.createList().copy();
- assertEquals( "Should be a 0 length list", 0, list.size() );
-
- }
- }
-
-
- protected static class ApplyTest extends ListTest {
- public ApplyTest() {super("ApplyTest");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list5.rdf" );
-
- RDFList root = getListRoot( m );
-
- class MyApply implements RDFList.ApplyFn {
- String collect = "";
- @Override
- public void apply( RDFNode n ) {
- collect = collect + ((Resource) n).getLocalName();
- }
- }
-
- MyApply f = new MyApply();
- root.apply( f );
-
- assertEquals( "Result of apply should be concatentation of local names", "abcde", f.collect );
- }
- }
-
-
- protected static class ReduceTest extends ListTest {
- public ReduceTest() {super("ReduceTest");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list5.rdf" );
-
- RDFList root = getListRoot( m );
-
- RDFList.ReduceFn f = new RDFList.ReduceFn() {
- @Override
- public Object reduce( RDFNode n, Object acc ) {
- return ((String) acc) + ((Resource) n).getLocalName();
- }
- };
-
- assertEquals( "Result of reduce should be concatentation of local names", "abcde", root.reduce( f, "" ) );
- }
- }
-
- protected static class Map1Test extends ListTest {
- public Map1Test() {super("Map1Test");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
- m.read( "file:testing/ontology/list5.rdf" );
-
- RDFList root = getListRoot( m );
- iteratorTest( root.mapWith( new Map1<RDFNode, String>() {@Override
- public String map1( RDFNode x ){return ((Resource) x).getLocalName();} } ),
- new Object[] {"a","b","c","d","e"} );
- }
- }
-
- protected static class RemoveTest extends ListTest {
- public RemoveTest() {super( "RemoveTest" );}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
-
- Resource nil = m.getResource( RDF.nil.getURI() );
- RDFList list0 = nil.as( RDFList.class );
- RDFList list1 = nil.as( RDFList.class );
-
- Resource r0 = m.createResource( NS + "x" );
- Resource r1 = m.createResource( NS + "y" );
- Resource r2 = m.createResource( NS + "z" );
-
- for (int i = 0; i < 10; i++) {
- list0 = list0.cons( r0 );
- list1 = list1.cons( r1 );
- }
-
- // delete the elements of list0 one at a time
- while (!list0.isEmpty()) {
- list0 = list0.removeHead();
- checkValid( "removeTest0", list0, true );
- }
-
-
- // delete all of list1 in one go
- list1.removeList();
-
- // model should now be empty
- assertEquals( "Model should be empty after deleting two lists", 0, m.size() );
-
- // selective remove
- RDFList list2 = (nil.as( RDFList.class ))
- .cons( r2 )
- .cons( r1 )
- .cons( r0 );
-
- assertTrue( "list should contain x ", list2.contains( r0 ));
- assertTrue( "list should contain y ", list2.contains( r1 ));
- assertTrue( "list should contain z ", list2.contains( r2 ));
-
- list2 = list2.remove( r1 );
- assertTrue( "list should contain x ", list2.contains( r0 ));
- assertTrue( "list should contain y ", !list2.contains( r1 ));
- assertTrue( "list should contain z ", list2.contains( r2 ));
-
- list2 = list2.remove( r0 );
- assertTrue( "list should contain x ", !list2.contains( r0 ));
- assertTrue( "list should contain y ", !list2.contains( r1 ));
- assertTrue( "list should contain z ", list2.contains( r2 ));
-
- list2 = list2.remove( r2 );
- assertTrue( "list should contain x ", !list2.contains( r0 ));
- assertTrue( "list should contain y ", !list2.contains( r1 ));
- assertTrue( "list should contain z ", !list2.contains( r2 ));
- assertTrue( "list should be empty", list2.isEmpty() );
- }
- }
-
-
- protected static class ListEqualsTest extends ListTest {
- public ListEqualsTest() {super("ListEqualsTest");}
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
-
- Resource nil = m.getResource( RDF.nil.getURI() );
- RDFList nilList = nil.as( RDFList.class );
-
- // create a list of foos
- Resource[] r0 = new Resource[] {
- m.createResource( NS + "a" ), // canonical
- m.createResource( NS + "b" ),
- m.createResource( NS + "c" ),
- m.createResource( NS + "d" ),
- m.createResource( NS + "e" )
- };
- Resource[] r1 = new Resource[] {
- m.createResource( NS + "a" ), // same
- m.createResource( NS + "b" ),
- m.createResource( NS + "c" ),
- m.createResource( NS + "d" ),
- m.createResource( NS + "e" )
- };
- Resource[] r2 = new Resource[] {
- m.createResource( NS + "a" ), // one shorter
- m.createResource( NS + "b" ),
- m.createResource( NS + "c" ),
- m.createResource( NS + "d" )
- };
- Resource[] r3 = new Resource[] {
- m.createResource( NS + "a" ), // elements swapped
- m.createResource( NS + "b" ),
- m.createResource( NS + "d" ),
- m.createResource( NS + "c" ),
- m.createResource( NS + "e" )
- };
- Resource[] r4 = new Resource[] {
- m.createResource( NS + "a" ), // different name
- m.createResource( NS + "b" ),
- m.createResource( NS + "c" ),
- m.createResource( NS + "D" ),
- m.createResource( NS + "e" )
- };
-
- Object[][] testSpec = new Object[][] {
- {r0, r1, Boolean.TRUE},
- {r0, r2, Boolean.FALSE},
- {r0, r3, Boolean.FALSE},
- {r0, r4, Boolean.FALSE},
- {r1, r2, Boolean.FALSE},
- {r1, r3, Boolean.FALSE},
- {r1, r4, Boolean.FALSE},
- {r2, r3, Boolean.FALSE},
- {r2, r4, Boolean.FALSE},
- };
-
- for (int i = 0; i < testSpec.length; i++) {
- RDFList l0 = nilList.append( Arrays.asList( (Resource[]) testSpec[i][0] ).iterator() );
- RDFList l1 = nilList.append( Arrays.asList( (Resource[]) testSpec[i][1] ).iterator() );
- boolean expected = ((Boolean) testSpec[i][2]).booleanValue();
-
- assertEquals( "sameListAs testSpec[" + i + "] incorrect", expected, l0.sameListAs( l1 ) );
- assertEquals( "sameListAs testSpec[" + i + "] (swapped) incorrect", expected, l1.sameListAs( l0 ) );
- }
- }
- }
-
- protected static class ListSubclassTest extends ListTest {
- public ListSubclassTest() {
- super( "ListSubClassTest");
- }
-
- @Override
- public void runTest() {
- Model m = ModelFactory.createDefaultModel();
-
- String NS = "http://example.org/test#";
- Resource a = m.createResource( NS + "a" );
- Resource b = m.createResource( NS + "b" );
-
- Resource cell0 = m.createResource();
- Resource cell1 = m.createResource();
- cell0.addProperty( RDF.first, a );
- cell0.addProperty( RDF.rest, cell1 );
- cell1.addProperty( RDF.first, b );
- cell1.addProperty( RDF.rest, RDF.nil );
-
- UserList ul = new UserListImpl( cell0.asNode(), (EnhGraph) m );
-
- assertEquals( "User list length ", 2, ul.size() );
- assertEquals( "head of user list ", a, ul.getHead() );
-
- RDFList l = ul.as( RDFList.class );
- assertNotNull( "RDFList facet of user-defined list subclass", l );
- }
- }
-
- /** A simple extension to RDFList to test user-subclassing of RDFList */
- protected static interface UserList extends RDFList {
- }
-
- /** Impl of a simple extension to RDFList to test user-subclassing of RDFList */
- protected static class UserListImpl extends RDFListImpl implements UserList {
- public UserListImpl( Node n, EnhGraph g ) {
- super( n, g );
- }
- }
-
-
- protected static class UserDefinedListTest extends ListTest {
- public UserDefinedListTest() {
- super( "UserDefinedListTest");
- }
-
- @Override
- public void runTest() {
- BuiltinPersonalities.model.add( UserDefList.class, UserDefListImpl.factoryForTests );
-
- Model m = ModelFactory.createDefaultModel();
-
- String NS = "http://example.org/test#";
- Resource a = m.createResource( NS + "a" );
- Resource b = m.createResource( NS + "b" );
-
- Resource empty = m.createResource( UserDefListImpl.NIL.getURI() );
- UserDefList ul = empty.as( UserDefList.class );
- assertNotNull( "UserList facet of empty list", ul );
-
- UserDefList ul0 = (UserDefList) ul.cons( b );
- ul0 = (UserDefList) ul0.cons( a );
- assertEquals( "should be length 2", 2, ul0.size() );
- assertTrue( "first statement", m.contains( ul0, UserDefListImpl.FIRST, a ) );
- }
- }
-
- protected static interface UserDefList extends RDFList {}
-
- protected static class UserDefListImpl extends RDFListImpl implements UserDefList {
- @SuppressWarnings("hiding") public static final String NS = "http://example.org/testlist#";
- public static final Property FIRST = ResourceFactory.createProperty( NS+"first" );
- public static final Property REST = ResourceFactory.createProperty( NS+"rest" );
- public static final Resource NIL = ResourceFactory.createResource( NS+"nil" );
- public static final Resource LIST = ResourceFactory.createResource( NS+"List" );
-
- /**
- * A factory for generating UserDefList facets from nodes in enhanced graphs.
- */
- public static Implementation factoryForTests = new Implementation() {
- @Override public EnhNode wrap( Node n, EnhGraph eg ) {
- if (canWrap( n, eg )) {
- UserDefListImpl impl = new UserDefListImpl( n, eg );
-
- Model m = impl.getModel();
- impl.m_listFirst = FIRST.inModel( m );
- impl.m_listRest = REST.inModel( m );
- impl.m_listNil = NIL.inModel( m );
- impl.m_listType = LIST.inModel( m );
-
- return impl;
- }
- else {
- throw new JenaException( "Cannot convert node " + n + " to UserDefList");
- }
- }
-
- @Override public boolean canWrap( Node node, EnhGraph eg ) {
- Graph g = eg.asGraph();
-
- return node.equals( NIL.asNode() ) ||
- g.contains( node, FIRST.asNode(), Node.ANY ) ||
- g.contains( node, REST.asNode(), Node.ANY ) ||
- g.contains( node, RDF.type.asNode(), LIST.asNode() );
- }
- };
-
- /** This method returns the Java class object that defines which abstraction facet is presented */
- @Override public Class<? extends RDFList> listAbstractionClass() {
- return UserDefList.class;
- }
-
- public UserDefListImpl( Node n, EnhGraph g ) {
- super( n, g );
- }
-
- }
+ /** Test that an iterator delivers the expected values */
+ protected static void iteratorTest( final Iterator<?> i,
+ final Object[] expected )
+ {
+ final Logger logger = LoggerFactory.getLogger(TestList.class);
+ final List<Object> expList = new ArrayList<Object>();
+ for (final Object element : expected)
+ {
+ expList.add(element);
+ }
+
+ while (i.hasNext())
+ {
+ final Object next = i.next();
+
+ // debugging
+ if (!expList.contains(next))
+ {
+ logger.debug("TestList - Unexpected iterator result: " + next);
+ }
+
+ Assert.assertTrue("Value " + next
+ + " was not expected as a result from this iterator ",
+ expList.contains(next));
+ Assert.assertTrue("Value " + next
+ + " was not removed from the list ", expList.remove(next));
+ }
+
+ if (!(expList.size() == 0))
+ {
+ logger.debug("TestList - Expected iterator results not found");
+ for (final Object object : expList)
+ {
+ logger.debug("TestList - missing: " + object);
+ }
+ }
+ Assert.assertEquals(
+ "There were expected elements from the iterator that were not found",
+ 0, expList.size());
+ }
+
+ // Static variables
+ // ////////////////////////////////
+
+ // Instance variables
+ // ////////////////////////////////
+
+ // Constructors
+ // ////////////////////////////////
+
+ // public TestList( String name ) {
+ // super( name );
+ // }
+
+ // External signature methods
+ // ////////////////////////////////
+ /*
+ * public static TestSuite suite() {
+ * TestSuite s = new TestSuite( "TestList" );
+ * TestList tl = new TestList();
+ * for (int i = 0; i <= 5; i++) {
+ * s.addTest( new CountTest( i ) );
+ * s.addTest( new TailTest( i ) );
+ * }
+ *
+ * s.addTest( new ValidityTest() );
+ * s.addTest( new HeadTest() );
+ * s.addTest( new SetHeadTest() );
+ * s.addTest( new SetTailTest() );
+ * s.addTest( new ConsTest() );
+ * s.addTest( new AddTest() );
+ * s.addTest( new TestListGet() );
+ * s.addTest( new ReplaceTest() );
+ * s.addTest( new IndexTest1() );
+ * s.addTest( new IndexTest2() );
+ * s.addTest( new AppendTest() );
+ * s.addTest( new ConcatenateTest() );
+ * s.addTest( new ConcatenateTest2() );
+ * s.addTest( new ApplyTest() );
+ * s.addTest( new ReduceTest() );
+ * s.addTest( new RemoveTest() );
+ * s.addTest( new Map1Test() );
+ * s.addTest( new ListEqualsTest() );
+ * s.addTest( new ListSubclassTest() );
+ * s.addTest( new UserDefinedListTest() );
+ *
+ * return s;
+ * }
+ */
+
+ // public ListTest( String n ) {super(n);}
+
+ public TestList( final TestingModelFactory modelFactory, final String name )
+ {
+ super(modelFactory, name);
+ }
+
+ protected void checkValid( final String testName, final RDFList l,
+ final boolean validExpected )
+ {
+ l.setStrict(true);
+ final boolean valid = l.isValid();
+ // for debugging ... String s = l.getValidityErrorMessage();
+ Assert.assertEquals("Validity test " + testName
+ + " returned wrong isValid() result", validExpected, valid);
+ }
+
+ // Internal implementation methods
+ // ////////////////////////////////
+
+ protected RDFList getListRoot( final Model m )
+ {
+ final Resource root = m.getResource(TestList.NS + "root");
+ Assert.assertNotNull("Root resource should not be null", root);
+
+ final Resource listHead = root.getRequiredProperty(
+ m.getProperty(TestList.NS + "p")).getResource();
+
+ final RDFList l = listHead.as(RDFList.class);
+ Assert.assertNotNull("as(RDFList) should not return null for root", l);
+
+ return l;
+ }
+
+ public void testAdd()
+ {
+
+ final Resource root = model.createResource(TestList.NS + "root");
+ final Property p = model.createProperty(TestList.NS, "p");
+
+ final Resource nil = model.getResource(RDF.nil.getURI());
+ RDFList list = nil.as(RDFList.class);
+
+ final Resource[] toAdd = new Resource[] {
+ model.createResource(TestList.NS + "a"),
+ model.createResource(TestList.NS + "b"),
+ model.createResource(TestList.NS + "c"),
+ model.createResource(TestList.NS + "d"),
+ model.createResource(TestList.NS + "e"), };
+
+ // add each of these resources onto the end of the list
+ for (final Resource element : toAdd)
+ {
+ final RDFList list0 = list.with(element);
+
+ checkValid("addTest0", list0, true);
+ Assert.assertTrue("added'ed lists should be equal",
+ list.equals(nil) || list0.equals(list));
+
+ list = list0;
+ }
+
+ // relate the root to the list
+ model.add(root, p, list);
+
+ // should be isomorphic with list 5
+ final Model m0 = ModelFactory.createDefaultModel();
+ m0.read("file:testing/ontology/list5.rdf");
+
+ Assert.assertTrue("Add'ed and read models should be the same",
+ m0.isIsomorphicWith(model));
+
+ }
+
+ public void testAppend()
+ {
+ model.read("file:testing/ontology/list5.rdf");
+
+ final Resource nil = model.getResource(RDF.nil.getURI());
+ RDFList list = nil.as(RDFList.class);
+
+ final Resource r = model.createResource(TestList.NS + "foo");
+
+ // create a list of foos
+ for (int i = 0; i < 5; i++)
+ {
+ list = list.cons(r);
+ }
+
+ final int listLen = list.size();
+
+ // now append foos to the root list
+ final RDFList root = getListRoot(model);
+ final int rootLen = root.size();
+
+ final RDFList appended = root.append(list);
+
+ // original list should be unchanged
+ checkValid("appendTest0", root, true);
+ Assert.assertEquals("Original list should be unchanged", rootLen,
+ root.size());
+
+ checkValid("appendTest1", list, true);
+ Assert.assertEquals("Original list should be unchanged", listLen,
+ list.size());
+
+ // new list should be length of combined
+ checkValid("appendTest2", appended, true);
+ Assert.assertEquals("Appended list not correct length", rootLen
+ + listLen, appended.size());
+ }
+
+ public void testApply()
+ {
+ model.read("file:testing/ontology/list5.rdf");
+
+ final RDFList root = getListRoot(model);
+
+ class MyApply implements RDFList.ApplyFn
+ {
+ String collect = "";
+
+ @Override
+ public void apply( final RDFNode n )
+ {
+ collect = collect + ((Resource) n).getLocalName();
+ }
+ }
+
+ final MyApply f = new MyApply();
+ root.apply(f);
+
+ Assert.assertEquals(
+ "Result of apply should be concatentation of local names",
+ "abcde", f.collect);
+
+ }
+
+ public void testConcatenate()
+ {
+ model.read("file:testing/ontology/list5.rdf");
+
+ final Resource nil = model.getResource(RDF.nil.getURI());
+ RDFList list = nil.as(RDFList.class);
+
+ final Resource r = model.createResource(TestList.NS + "foo");
+
+ // create a list of foos
+ for (int i = 0; i < 5; i++)
+ {
+ list = list.cons(r);
+ }
+
+ final int listLen = list.size();
+
+ // now append foos to the root list
+ final RDFList root = getListRoot(model);
+ final int rootLen = root.size();
+ root.concatenate(list);
+
+ // original list should be unchanged
+ checkValid("concatTest0", list, true);
+ Assert.assertEquals("Original list should be unchanged", listLen,
+ list.size());
+
+ // but lhs list has changed
+ checkValid("concatTest1", root, true);
+ Assert.assertEquals("Root list should be new length",
+ rootLen + listLen, root.size());
+ }
+
+ public void testConcatenate2()
+ {
+ model.read("file:testing/ontology/list5.rdf");
+
+ final Resource a = model.createResource(TestList.NS + "a");
+
+ // create a list of foos
+ final Resource[] rs = new Resource[] {
+ model.createResource(TestList.NS + "b"),
+ model.createResource(TestList.NS + "c"),
+ model.createResource(TestList.NS + "d"),
+ model.createResource(TestList.NS + "e") };
+
+ final RDFList aList = model.createList().cons(a);
+ final RDFList rsList = model.createList(rs);
+
+ // concatenate the above resources onto the empty list
+ aList.concatenate(rsList);
+ checkValid("concatTest3", aList, true);
+
+ final RDFList root = getListRoot(model);
+ Assert.assertTrue("Constructed and loaded lists should be the same",
+ aList.sameListAs(root));
+ }
+
+ public void testCons()
+ {
+ final Resource root = model.createResource(TestList.NS + "root");
+ final Property p = model.createProperty(TestList.NS, "p");
+
+ final Resource nil = model.getResource(RDF.nil.getURI());
+ RDFList list = nil.as(RDFList.class);
+
+ final Resource[] toAdd = new Resource[] {
+ model.createResource(TestList.NS + "e"),
+ model.createResource(TestList.NS + "d"),
+ model.createResource(TestList.NS + "c"),
+ model.createResource(TestList.NS + "b"),
+ model.createResource(TestList.NS + "a"), };
+
+ // cons each of these resources onto the front of the list
+ for (final Resource element : toAdd)
+ {
+ final RDFList list0 = list.cons(element);
+
+ checkValid("constest1", list0, true);
+ Assert.assertTrue("cons'ed lists should not be equal",
+ !list0.equals(list));
+
+ list = list0;
+ }
+
+ // relate the root to the list
+ model.add(root, p, list);
+
+ // should be isomorphic with list 5
+ final Model m0 = ModelFactory.createDefaultModel();
+ m0.read("file:testing/ontology/list5.rdf");
+
+ Assert.assertTrue("Cons'ed and read models should be the same",
+ m0.isIsomorphicWith(model));
+ }
+
+ public void testCount()
+ {
+ for (int i = 0; i <= 5; i++)
+ {
+ model.removeAll();
+ model.read("file:testing/ontology/list" + i + ".rdf");
+
+ final RDFList l0 = getListRoot(model);
+ Assert.assertEquals("List size should be " + i, i, l0.size());
+ }
+
+ }
+
+ public void testHead()
+ {
+ model.read("file:testing/ontology/list5.rdf");
+
+ RDFList l0 = getListRoot(model);
+
+ final String[] names = { "a", "b", "c", "d", "e" };
+ for (final String name : names)
+ {
+ Assert.assertEquals("head of list has incorrect URI", TestList.NS
+ + name, ((Resource) l0.getHead()).getURI());
+ l0 = l0.getTail();
+ }
+ }
+
+ public void testIndex1()
+ {
+ model.read("file:testing/ontology/list5.rdf");
+
+ final Resource[] toGet = new Resource[] {
+ model.createResource(TestList.NS + "a"),
+ model.createResource(TestList.NS + "b"),
+ model.createResource(TestList.NS + "c"),
+ model.createResource(TestList.NS + "d"),
+ model.createResource(TestList.NS + "e"), };
+
+ final RDFList l1 = getListRoot(model);
+
+ // check the indexes are correct
+ for (int i = 0; i < toGet.length; i++)
+ {
+ Assert.assertTrue("list should contain element " + i,
+ l1.contains(toGet[i]));
+ Assert.assertEquals("list element " + i + " is not correct", i,
+ l1.indexOf(toGet[i]));
+ }
+ }
+
+ public void testIndex2()
+ {
+
+ final Resource nil = model.getResource(RDF.nil.getURI());
+ RDFList list = nil.as(RDFList.class);
+
+ final Resource r = model.createResource(TestList.NS + "a");
+
+ // cons each a's onto the front of the list
+ for (int i = 0; i < 10; i++)
+ {
+ list = list.cons(r);
+ }
+
+ // now index them back again
+ for (int j = 0; j < 10; j++)
+ {
+ Assert.assertEquals("index of j'th item should be j", j,
+ list.indexOf(r, j));
+ }
+
+ }
+
+ public void testListEquals()
+ {
+ final Resource nil = model.getResource(RDF.nil.getURI());
+ final RDFList nilList = nil.as(RDFList.class);
+
+ // create a list of foos
+ final Resource[] r0 = new Resource[] {
+ model.createResource(TestList.NS + "a"), // canonical
+ model.createResource(TestList.NS + "b"),
+ model.createResource(TestList.NS + "c"),
+ model.createResource(TestList.NS + "d"),
+ model.createResource(TestList.NS + "e") };
+ final Resource[] r1 = new Resource[] {
+ model.createResource(TestList.NS + "a"), // same
+ model.createResource(TestList.NS + "b"),
+ model.createResource(TestList.NS + "c"),
+ model.createResource(TestList.NS + "d"),
+ model.createResource(TestList.NS + "e") };
+ final Resource[] r2 = new Resource[] {
+ model.createResource(TestList.NS + "a"), // one shorter
+ model.createResource(TestList.NS + "b"),
+ model.createResource(TestList.NS + "c"),
+ model.createResource(TestList.NS + "d") };
+ final Resource[] r3 = new Resource[] {
+ model.createResource(TestList.NS + "a"), // elements
+ // swapped
+ model.createResource(TestList.NS + "b"),
+ model.createResource(TestList.NS + "d"),
+ model.createResource(TestList.NS + "c"),
+ model.createResource(TestList.NS + "e") };
+ final Resource[] r4 = new Resource[] {
+ model.createResource(TestList.NS + "a"), // different
+ // name
+ model.createResource(TestList.NS + "b"),
+ model.createResource(TestList.NS + "c"),
+ model.createResource(TestList.NS + "D"),
+ model.createResource(TestList.NS + "e") };
+
+ final Object[][] testSpec = new Object[][] { { r0, r1, Boolean.TRUE },
+ { r0, r2, Boolean.FALSE }, { r0, r3, Boolean.FALSE },
+ { r0, r4, Boolean.FALSE }, { r1, r2, Boolean.FALSE },
+ { r1, r3, Boolean.FALSE }, { r1, r4, Boolean.FALSE },
+ { r2, r3, Boolean.FALSE }, { r2, r4, Boolean.FALSE }, };
+
+ for (int i = 0; i < testSpec.length; i++)
+ {
+ final RDFList l0 = nilList.append(Arrays.asList(
+ (Resource[]) testSpec[i][0]).iterator());
+ final RDFList l1 = nilList.append(Arrays.asList(
+ (Resource[]) testSpec[i][1]).iterator());
+ final boolean expected = ((Boolean) testSpec[i][2]).booleanValue();
+
+ Assert.assertEquals("sameListAs testSpec[" + i + "] incorrect",
+ expected, l0.sameListAs(l1));
+ Assert.assertEquals("sameListAs testSpec[" + i
+ + "] (swapped) incorrect", expected, l1.sameListAs(l0));
+ }
+ }
+
+ public void testListGet()
+ {
+ model.read("file:testing/ontology/list5.rdf");
+
+ final Resource[] toGet = new Resource[] {
+ model.createResource(TestList.NS + "a"),
+ model.createResource(TestList.NS + "b"),
+ model.createResource(TestList.NS + "c"),
+ model.createResource(TestList.NS + "d"),
+ model.createResource(TestList.NS + "e"), };
+
+ final RDFList l1 = getListRoot(model);
+
+ // test normal gets
+ for (int i = 0; i < toGet.length; i++)
+ {
+ Assert.assertEquals("list element " + i + " is not correct",
+ toGet[i], l1.get(i));
+ }
+
+ // now test we get an exception for going beyong the end of the list
+ boolean gotEx = false;
+ try
+ {
+ l1.get(toGet.length + 1);
+ }
+ catch (final ListIndexException e)
+ {
+ gotEx = true;
+ }
+
+ Assert.assertTrue(
+ "Should see exception raised by accessing beyond end of list",
+ gotEx);
+ }
+
+ public void testMap1()
+ {
+ model.read("file:testing/ontology/list5.rdf");
+
+ final RDFList root = getListRoot(model);
+ TestList.iteratorTest(root.mapWith(new Map1<RDFNode, String>() {
+ @Override
+ public String map1( final RDFNode x )
+ {
+ return ((Resource) x).getLocalName();
+ }
+ }), new Object[] { "a", "b", "c", "d", "e" });
+
+ }
+
+ public void testReduce()
+ {
+ model.read("file:testing/ontology/list5.rdf");
+
+ final RDFList root = getListRoot(model);
+
+ final RDFList.ReduceFn f = new RDFList.ReduceFn() {
+ @Override
+ public Object reduce( final RDFNode n, final Object acc )
+ {
+ return ((String) acc) + ((Resource) n).getLocalName();
+ }
+ };
+
+ Assert.assertEquals(
+ "Result of reduce should be concatentation of local names",
+ "abcde", root.reduce(f, ""));
+ }
+
+ public void testRemove()
+ {
+
+ final Resource nil = model.getResource(RDF.nil.getURI());
+ RDFList list0 = nil.as(RDFList.class);
+ RDFList list1 = nil.as(RDFList.class);
+
+ final Resource r0 = model.createResource(TestList.NS + "x");
+ final Resource r1 = model.createResource(TestList.NS + "y");
+ final Resource r2 = model.createResource(TestList.NS + "z");
+
+ for (int i = 0; i < 10; i++)
+ {
+ list0 = list0.cons(r0);
+ list1 = list1.cons(r1);
+ }
+
+ // delete the elements of list0 one at a time
+ while (!list0.isEmpty())
+ {
+ list0 = list0.removeHead();
+ checkValid("removeTest0", list0, true);
+ }
+
+ // delete all of list1 in one go
+ list1.removeList();
+
+ // model should now be empty
+ Assert.assertEquals("Model should be empty after deleting two lists",
+ 0, model.size());
+
+ // selective remove
+ RDFList list2 = (nil.as(RDFList.class)).cons(r2).cons(r1).cons(r0);
+
+ Assert.assertTrue("list should contain x ", list2.contains(r0));
+ Assert.assertTrue("list should contain y ", list2.contains(r1));
+ Assert.assertTrue("list should contain z ", list2.contains(r2));
+
+ list2 = list2.remove(r1);
+ Assert.assertTrue("list should contain x ", list2.contains(r0));
+ Assert.assertTrue("list should contain y ", !list2.contains(r1));
+ Assert.assertTrue("list should contain z ", list2.contains(r2));
+
+ list2 = list2.remove(r0);
+ Assert.assertTrue("list should contain x ", !list2.contains(r0));
+ Assert.assertTrue("list should contain y ", !list2.contains(r1));
+ Assert.assertTrue("list should contain z ", list2.contains(r2));
+
+ list2 = list2.remove(r2);
+ Assert.assertTrue("list should contain x ", !list2.contains(r0));
+ Assert.assertTrue("list should contain y ", !list2.contains(r1));
+ Assert.assertTrue("list should contain z ", !list2.contains(r2));
+ Assert.assertTrue("list should be empty", list2.isEmpty());
+ }
+
+ public void testReplace()
+ {
+ model.read("file:testing/ontology/list5.rdf");
+
+ final Literal[] toSet = new Literal[] { model.createLiteral("a"),
+ model.createLiteral("b"), model.createLiteral("c"),
+ model.createLiteral("d"), model.createLiteral("e"), };
+
+ final RDFList l1 = getListRoot(model);
+
+ // change all the values
+ for (int i = 0; i < toSet.length; i++)
+ {
+ l1.replace(i, toSet[i]);
+ }
+
+ // then check them
+ for (int i = 0; i < toSet.length; i++)
+ {
+ Assert.assertEquals("list element " + i + " is not correct",
+ toSet[i], l1.get(i));
+ }
+
+ // now test we get an exception for going beyong the end of the list
+ boolean gotEx = false;
+ try
+ {
+ l1.replace(toSet.length + 1, toSet[0]);
+ }
+ catch (final ListIndexException e)
+ {
+ gotEx = true;
+ }
+
+ Assert.assertTrue(
+ "Should see exception raised by accessing beyond end of list",
+ gotEx);
+
+ }
+
+ public void testSetHead()
+ {
+
+ final Resource root = model.createResource(TestList.NS + "root");
+ final Property p = model.createProperty(TestList.NS, "p");
+
+ // a list of the nil object, but not typed
+ final Resource nil = RDF.nil;
+ model.add(nil, RDF.type, RDF.List);
+
+ final Resource list = model.createResource();
+ model.add(list, RDF.type, RDF.List);
+ model.add(list, RDF.first, "fred");
+ model.add(list, RDF.rest, nil);
+
+ model.add(root, p, list);
+ final RDFList l1 = getListRoot(model);
+ checkValid("sethead1", l1, true);
+
+ Assert.assertEquals("List head should be 'fred'", "fred",
+ ((Literal) l1.getHead()).getString());
+
+ l1.setHead(model.createTypedLiteral(42));
+ checkValid("sethead2", l1, true);
+ Assert.assertEquals("List head should be '42'", 42,
+ ((Literal) l1.getHead()).getInt());
+
+ }
+
+ public void testSetTail()
+ {
+ final Model m = ModelFactory.createDefaultModel();
+
+ final Resource root = m.createResource(TestList.NS + "root");
+ final Property p = m.createProperty(TestList.NS, "p");
+
+ final Resource nil = RDF.nil;
+ m.add(nil, RDF.type, RDF.List);
+
+ final Resource list0 = m.createResource();
+ m.add(list0, RDF.type, RDF.List);
+ m.add(list0, RDF.first, "fred");
+ m.add(list0, RDF.rest, nil);
+
+ m.add(root, p, list0);
+ final RDFList l1 = getListRoot(m);
+ checkValid("settail1", l1, true);
+
+ final Resource list1 = m.createResource();
+ m.add(list1, RDF.type, RDF.List);
+ m.add(list1, RDF.first, "george");
+ m.add(list1, RDF.rest, nil);
+
+ final RDFList l2 = list1.as(RDFList.class);
+ Assert.assertNotNull("as(RDFList) should not return null for root", l2);
+ checkValid("settail2", l2, true);
+
+ Assert.assertEquals("l1 should have length 1", 1, l1.size());
+ Assert.assertEquals("l2 should have length 1", 1, l2.size());
+
+ // use set tail to join the lists together
+ l1.setTail(l2);
+
+ checkValid("settail3", l1, true);
+ checkValid("settail4", l2, true);
+
+ Assert.assertEquals("l1 should have length 2", 2, l1.size());
+ Assert.assertEquals("l2 should have length 1", 1, l2.size());
+
+ }
+
+ public void testTail()
+ {
+ for (int i = 0; i <= 5; i++)
+ {
+ model.read("file:testing/ontology/list" + i + ".rdf");
+
+ RDFList l0 = getListRoot(model);
+
+ // get the tail n times, should be nil at the end
+ for (int j = 0; j < i; j++)
+ {
+ l0 = l0.getTail();
+ }
+
+ Assert.assertTrue("Should have reached the end of the list after "
+ + i + " getTail()'s", l0.isEmpty());
+ }
+ }
+
+ public void testValidity()
+ {
+
+ final Resource root = model.createResource(TestList.NS + "root");
+ final Property p = model.createProperty(TestList.NS, "p");
+
+ // a list of the nil object, but not typed
+ final Resource nil = RDF.nil;
+ model.add(root, p, nil);
+ final RDFList l0 = getListRoot(model);
+ checkValid("valid1", l0, true);
+
+ // add another node to the head of the list
+ final Resource badList = model.createResource();
+ model.getRequiredProperty(root, p).remove();
+ model.add(root, p, badList);
+ model.add(badList, RDF.type, RDF.List);
+
+ final RDFList l1 = getListRoot(model);
+ checkValid("valid2", l1, false);
+
+ // checkValid( "valid3", l1, false );
+
+ model.add(badList, RDF.first, "fred");
+ checkValid("valid4", l1, false);
+
+ model.add(badList, RDF.rest, nil);
+ checkValid("valid5", l1, true);
+
+ }
+
+ // public void testListSubclass() {
+ // String NS = "http://example.org/test#";
+ // Resource a = model.createResource( NS + "a" );
+ // Resource b = model.createResource( NS + "b" );
+ //
+ // Resource cell0 = model.createResource();
+ // Resource cell1 = model.createResource();
+ // cell0.addProperty( RDF.first, a );
+ // cell0.addProperty( RDF.rest, cell1 );
+ // cell1.addProperty( RDF.first, b );
+ // cell1.addProperty( RDF.rest, RDF.nil );
+ //
+ // UserList ul = getUserListInstance(cell0);
+ //
+ // assertEquals( "User list length ", 2, ul.size() );
+ // assertEquals( "head of user list ", a, ul.getHead() );
+ //
+ // RDFList l = ul.as( RDFList.class );
+ // assertNotNull( "RDFList facet of user-defined list subclass", l );
+ //
+ // }
+ //
+ // /** A simple extension to RDFList to test user-subclassing of RDFList */
+ // protected static interface UserList extends RDFList {
+ // }
+ //
+ // /** Impl of a simple extension to RDFList to test user-subclassing of
+ // RDFList */
+ // protected static class UserListImpl extends RDFListImpl implements
+ // UserList {
+ // public UserListImpl( Node n, EnhGraph g ) {
+ // super( n, g );
+ // }
+ // }
+ //
+ // public UserList getUserListInstance( Resource r )
+ // {
+ // return new UserListImpl( r.asNode(), (EnhGraph) model );
+ // }
+ //
+ // public void testUserDefinedList() {
+ // BuiltinPersonalities.model.add( UserDefList.class,
+ // UserDefListImpl.factoryForTests );
+ //
+ // String NS = "http://example.org/test#";
+ // Resource a = model.createResource( NS + "a" );
+ // Resource b = model.createResource( NS + "b" );
+ //
+ // Resource empty = model.createResource( UserDefListImpl.NIL.getURI() );
+ // UserDefList ul = empty.as( UserDefList.class );
+ // assertNotNull( "UserList facet of empty list", ul );
+ //
+ // UserDefList ul0 = (UserDefList) ul.cons( b );
+ // ul0 = (UserDefList) ul0.cons( a );
+ // assertEquals( "should be length 2", 2, ul0.size() );
+ // assertTrue( "first statement", model.contains( ul0,
+ // UserDefListImpl.FIRST, a ) );
+ // }
+ //
+ // protected static interface UserDefList extends RDFList {}
+ //
+ // protected static class UserDefListImpl extends RDFListImpl implements
+ // UserDefList {
+ // @SuppressWarnings("hiding") public static final String NS =
+ // "http://example.org/testlist#";
+ // public static final Property FIRST = ResourceFactory.createProperty(
+ // NS+"first" );
+ // public static final Property REST = ResourceFactory.createProperty(
+ // NS+"rest" );
+ // public static final Resource NIL = ResourceFactory.createResource(
+ // NS+"nil" );
+ // public static final Resource LIST = ResourceFactory.createResource(
+ // NS+"List" );
+ //
+ // /**
+ // * A factory for generating UserDefList facets from nodes in enhanced
+ // graphs.
+ // */
+ // public static Implementation factoryForTests = new Implementation() {
+ // @Override public EnhNode wrap( Node n, EnhGraph eg ) {
+ // if (canWrap( n, eg )) {
+ // UserDefListImpl impl = new UserDefListImpl( n, eg );
+ //
+ // Model model = impl.getModel();
+ // impl.m_listFirst = FIRST.inModel( model );
+ // impl.m_listRest = REST.inModel( model );
+ // impl.m_listNil = NIL.inModel( model );
+ // impl.m_listType = LIST.inModel( model );
+ //
+ // return impl;
+ // }
+ // else {
+ // throw new JenaException( "Cannot convert node " + n + " to UserDefList");
+ // }
+ // }
+ //
+ // @Override public boolean canWrap( Node node, EnhGraph eg ) {
+ // Graph g = eg.asGraph();
+ //
+ // return node.equals( NIL.asNode() ) ||
+ // g.contains( node, FIRST.asNode(), Node.ANY ) ||
+ // g.contains( node, REST.asNode(), Node.ANY ) ||
+ // g.contains( node, RDF.type.asNode(), LIST.asNode() );
+ // }
+ // };
+ //
+ // /** This method returns the Java class object that defines which
+ // abstraction facet is presented */
+ // @Override public Class<? extends RDFList> listAbstractionClass() {
+ // return UserDefList.class;
+ // }
+ //
+ // public UserDefListImpl( Node n, EnhGraph g ) {
+ // super( n, g );
+ // }
+ //
+ // }
}
Modified: jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListStatements.java
URL: http://svn.apache.org/viewvc/jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListStatements.java?rev=1440101&r1=1440100&r2=1440101&view=diff
==============================================================================
--- jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListStatements.java (original)
+++ jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListStatements.java Tue Jan 29 20:27:24 2013
@@ -1,14 +1,14 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
+ * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
+ * regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,87 +18,110 @@
package com.hp.hpl.jena.rdf.model.test;
-import static org.junit.Assert.assertTrue ;
-
-import java.util.List ;
-
-import junit.framework.JUnit4TestAdapter ;
-import org.junit.AfterClass ;
-import org.junit.BeforeClass ;
-import org.junit.Test ;
-
-import com.hp.hpl.jena.rdf.model.* ;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.rdf.model.StmtIterator;
+
+import java.util.List;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
public class TestListStatements
{
- private static Model m;
- private static Resource s;
- private static Property p;
-
- public static junit.framework.Test suite() {
- return new JUnit4TestAdapter(TestListStatements.class) ;
- }
-
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- m = ModelFactory.createDefaultModel();
- Resource s = m.createResource("http://www.a.com/s");
- Property p = m.createProperty("http://www.a.com/p");
-
- m.add(s,p,m.createResource("http://www.a.com/o"));
- m.add(s,p,"texte","fr");
- m.add(s,p,"text","en");
- m.add(s,p,"text");
- m.add(m.createLiteralStatement(s,p,1789));
- }
-
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- m = null;
- s = null;
- p = null;
- }
-
- @Test
- public final void thereAre4Literals() {
- StmtIterator it = m.listStatements(s, p, null, null);
- assertTrue(it.toList().size() == 4);
- }
-
- @Test
- public final void thereIsOneFrench() {
- StmtIterator it = m.listStatements(s, p, null, "fr");
- List<Statement> lis = it.toList();
- assertTrue(lis.size() == 1);
- assertTrue(lis.get(0).getObject().toString().equals("texte@fr"));
- }
-
- @Test
- public final void thereAre2LitsWoLang() {
- StmtIterator it = m.listStatements(s, p, null, "");
- assertTrue(it.toList().size() == 2);
- }
-
- @Test
- public final void theresOneTextEN() {
- StmtIterator it = m.listStatements(s, p, "text", "en");
- List<Statement> lis = it.toList();
- assertTrue(lis.size() == 1);
- assertTrue(lis.get(0).getObject().toString().equals("text@en"));
- }
-
- @Test
- public final void theresOneTextWoLang() {
- StmtIterator it = m.listStatements(s, p, "text", "");
- List<Statement> lis = it.toList();
- assertTrue(lis.size() == 1);
- }
-
- @Test
- public final void theresAreTwoText() {
- StmtIterator it = m.listStatements(s, p, "text", null);
- List<Statement> lis = it.toList();
- assertTrue(lis.size() == 2);
- }
+ private static Model m;
+ private static Resource s;
+ private static Property p;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception
+ {
+ TestListStatements.m = ModelFactory.createDefaultModel();
+ final Resource s = TestListStatements.m
+ .createResource("http://www.a.com/s");
+ final Property p = TestListStatements.m
+ .createProperty("http://www.a.com/p");
+
+ TestListStatements.m.add(s, p,
+ TestListStatements.m.createResource("http://www.a.com/o"));
+ TestListStatements.m.add(s, p, "texte", "fr");
+ TestListStatements.m.add(s, p, "text", "en");
+ TestListStatements.m.add(s, p, "text");
+ TestListStatements.m.add(TestListStatements.m.createLiteralStatement(s,
+ p, 1789));
+ }
+
+ public static junit.framework.Test suite()
+ {
+ return new JUnit4TestAdapter(TestListStatements.class);
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception
+ {
+ TestListStatements.m = null;
+ TestListStatements.s = null;
+ TestListStatements.p = null;
+ }
+
+ @Test
+ public final void thereAre2LitsWoLang()
+ {
+ final StmtIterator it = TestListStatements.m.listStatements(
+ TestListStatements.s, TestListStatements.p, null, "");
+ Assert.assertTrue(it.toList().size() == 2);
+ }
+
+ @Test
+ public final void thereAre4Literals()
+ {
+ final StmtIterator it = TestListStatements.m.listStatements(
+ TestListStatements.s, TestListStatements.p, null, null);
+ Assert.assertTrue(it.toList().size() == 4);
+ }
+
+ @Test
+ public final void thereIsOneFrench()
+ {
+ final StmtIterator it = TestListStatements.m.listStatements(
+ TestListStatements.s, TestListStatements.p, null, "fr");
+ final List<Statement> lis = it.toList();
+ Assert.assertTrue(lis.size() == 1);
+ Assert.assertTrue(lis.get(0).getObject().toString().equals("texte@fr"));
+ }
+
+ @Test
+ public final void theresAreTwoText()
+ {
+ final StmtIterator it = TestListStatements.m.listStatements(
+ TestListStatements.s, TestListStatements.p, "text", null);
+ final List<Statement> lis = it.toList();
+ Assert.assertTrue(lis.size() == 2);
+ }
+
+ @Test
+ public final void theresOneTextEN()
+ {
+ final StmtIterator it = TestListStatements.m.listStatements(
+ TestListStatements.s, TestListStatements.p, "text", "en");
+ final List<Statement> lis = it.toList();
+ Assert.assertTrue(lis.size() == 1);
+ Assert.assertTrue(lis.get(0).getObject().toString().equals("text@en"));
+ }
+
+ @Test
+ public final void theresOneTextWoLang()
+ {
+ final StmtIterator it = TestListStatements.m.listStatements(
+ TestListStatements.s, TestListStatements.p, "text", "");
+ final List<Statement> lis = it.toList();
+ Assert.assertTrue(lis.size() == 1);
+ }
}
-
Modified: jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListSubjectsEtc.java
URL: http://svn.apache.org/viewvc/jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListSubjectsEtc.java?rev=1440101&r1=1440100&r2=1440101&view=diff
==============================================================================
--- jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListSubjectsEtc.java (original)
+++ jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListSubjectsEtc.java Tue Jan 29 20:27:24 2013
@@ -1,14 +1,14 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
+ * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
+ * regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,58 +18,87 @@
package com.hp.hpl.jena.rdf.model.test;
-import com.hp.hpl.jena.rdf.model.*;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.NodeIterator;
+import com.hp.hpl.jena.rdf.model.ResIterator;
+import com.hp.hpl.jena.rdf.model.test.helpers.ModelHelper;
+import com.hp.hpl.jena.rdf.model.test.helpers.TestingModelFactory;
+import com.hp.hpl.jena.test.JenaTestBase;
-import junit.framework.TestSuite;
+import junit.framework.Assert;
/**
- TestListSubjectsEtc - tests for listSubjects, listObjects [and listPredicates, if
- it were to exist]
- TODO make preperly generic, add missing test cases [we're relying, at root,
- on SimpleQueryHandler]
-
- @author kers
+ * TestListSubjectsEtc - tests for listSubjects, listObjects [and
+ * listPredicates, if
+ * it were to exist]
+ * TODO make preperly generic, add missing test cases [we're relying, at root,
+ * on SimpleQueryHandler]
*/
-public class TestListSubjectsEtc extends ModelTestBase
- {
- public TestListSubjectsEtc( String name )
- { super( name ); }
-
- public static TestSuite suite()
- { return new TestSuite( TestListSubjectsEtc.class ); }
-
- public void testListSubjectsNoRemove()
- {
- Model m = modelWithStatements( "a P b; b Q c; c R a" );
- ResIterator it = m.listSubjects();
- it.next();
- try { it.remove(); fail( "listSubjects should not support .remove()" ); }
- catch (UnsupportedOperationException e) { pass(); }
- }
-
- public void testListObjectsNoRemove()
- {
- Model m = modelWithStatements( "a P b; b Q c; c R a" );
- NodeIterator it = m.listObjects();
- it.next();
- try { it.remove(); fail( "listObjects should not support .remove()" ); }
- catch (UnsupportedOperationException e) { pass(); }
- }
-
- public void testListSubjectsWorksAfterRemoveProperties()
- {
- Model m = modelWithStatements( "p1 before terminal; p2 before terminal" );
- m.createResource( "eh:/p1" ).removeProperties();
- assertIsoModels( modelWithStatements( "p2 before terminal" ), m );
- assertEquals( resourceSet( "p2" ), m.listSubjects().toSet() );
- }
-
- public void testListSubjectsWorksAfterRemovePropertiesWIthLots()
- {
- Model m = modelWithStatements( "p2 before terminal" );
- for (int i = 0; i < 100; i += 1) modelAdd( m, "p1 hasValue " + i );
- m.createResource( "eh:/p1" ).removeProperties();
- assertIsoModels( modelWithStatements( "p2 before terminal" ), m );
- assertEquals( resourceSet( "p2" ), m.listSubjects().toSet() );
- }
- }
+public class TestListSubjectsEtc extends AbstractModelTestBase
+{
+ public TestListSubjectsEtc( final TestingModelFactory modelFactory,
+ final String name )
+ {
+ super(modelFactory, name);
+ }
+
+ public void testListObjectsNoRemove()
+ {
+ final Model m = ModelHelper.modelWithStatements(this,
+ "a P b; b Q c; c R a");
+ final NodeIterator it = m.listObjects();
+ it.next();
+ try
+ {
+ it.remove();
+ Assert.fail("listObjects should not support .remove()");
+ }
+ catch (final UnsupportedOperationException e)
+ {
+ JenaTestBase.pass();
+ }
+ }
+
+ public void testListSubjectsNoRemove()
+ {
+ final Model m = ModelHelper.modelWithStatements(this,
+ "a P b; b Q c; c R a");
+ final ResIterator it = m.listSubjects();
+ it.next();
+ try
+ {
+ it.remove();
+ Assert.fail("listSubjects should not support .remove()");
+ }
+ catch (final UnsupportedOperationException e)
+ {
+ JenaTestBase.pass();
+ }
+ }
+
+ public void testListSubjectsWorksAfterRemoveProperties()
+ {
+ final Model m = ModelHelper.modelWithStatements(this,
+ "p1 before terminal; p2 before terminal");
+ m.createResource("eh:/p1").removeProperties();
+ ModelHelper.assertIsoModels(
+ ModelHelper.modelWithStatements(this, "p2 before terminal"), m);
+ Assert.assertEquals(ModelHelper.resourceSet("p2"), m.listSubjects()
+ .toSet());
+ }
+
+ public void testListSubjectsWorksAfterRemovePropertiesWIthLots()
+ {
+ final Model m = ModelHelper.modelWithStatements(this,
+ "p2 before terminal");
+ for (int i = 0; i < 100; i += 1)
+ {
+ ModelHelper.modelAdd(m, "p1 hasValue " + i);
+ }
+ m.createResource("eh:/p1").removeProperties();
+ ModelHelper.assertIsoModels(
+ ModelHelper.modelWithStatements(this, "p2 before terminal"), m);
+ Assert.assertEquals(ModelHelper.resourceSet("p2"), m.listSubjects()
+ .toSet());
+ }
+}
Modified: jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestLiteralImpl.java
URL: http://svn.apache.org/viewvc/jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestLiteralImpl.java?rev=1440101&r1=1440100&r2=1440101&view=diff
==============================================================================
--- jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestLiteralImpl.java (original)
+++ jena/branches/streaming-update/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestLiteralImpl.java Tue Jan 29 20:27:24 2013
@@ -1,14 +1,14 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
+ * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
+ * regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,110 +18,161 @@
package com.hp.hpl.jena.rdf.model.test;
-import junit.framework.TestSuite ;
+import com.hp.hpl.jena.datatypes.TypeMapper;
+import com.hp.hpl.jena.graph.impl.AdhocDatatype;
+import com.hp.hpl.jena.rdf.model.Literal;
+import com.hp.hpl.jena.rdf.model.LiteralRequiredException;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.test.helpers.ModelHelper;
+import com.hp.hpl.jena.rdf.model.test.helpers.TestingModelFactory;
+import com.hp.hpl.jena.test.JenaTestBase;
-import com.hp.hpl.jena.datatypes.TypeMapper ;
-import com.hp.hpl.jena.graph.impl.AdhocDatatype ;
-import com.hp.hpl.jena.rdf.model.* ;
+import junit.framework.Assert;
/**
- TestLiteralImpl - minimal, this is the first time an extra test has been needed above
- the regression testing.
-
- @author kers
-*/
-public class TestLiteralImpl extends ModelTestBase
- {
- public TestLiteralImpl( String name )
- { super( name ); }
-
- public static TestSuite suite()
- { return new TestSuite( TestLiteralImpl.class ); }
-
- /**
- Test that a non-literal node cannot be as'ed into a literal
- */
- public void testCannotAsNonLiteral()
- { Model m = ModelFactory.createDefaultModel();
- try
- { resource( m, "plumPie" ).as( Literal.class );
- fail( "non-literal cannot be converted to literal" ); }
- catch (LiteralRequiredException l)
- { pass(); } }
-
- /**
- Test that a literal node can be as'ed into a literal.
- */
- public void testAsLiteral()
- { Model m = ModelFactory.createDefaultModel();
- literal( m, "17" ).as( Literal.class ); }
-
- public void testLiteralHasModel()
- {
- Model m = ModelFactory.createDefaultModel();
- testLiteralHasModel( m, m.createLiteral( "hello, world" ) );
- testLiteralHasModel( m, m.createLiteral( "hello, world", "en-UK" ) );
- testLiteralHasModel( m, m.createLiteral( "hello, world", true ) );
- testLiteralHasModel( m, m.createTypedLiteral( "hello, world" ) );
- testLiteralHasModel( m, m.createTypedLiteral( false ) );
- testLiteralHasModel( m, m.createTypedLiteral( 17 ) );
- testLiteralHasModel( m, m.createTypedLiteral( 'x' ) );
- }
-
- private void testLiteralHasModel( Model m, Literal lit )
- { assertSame( m, lit.getModel() ); }
-
- public void testInModel()
- {
- Model m1 = ModelFactory.createDefaultModel();
- Model m2 = ModelFactory.createDefaultModel();
- Literal l1 = m1.createLiteral( "17" );
- Literal l2 = l1.inModel( m2 );
- assertEquals( l1, l2 );
- assertSame( m2, l2.getModel() );
- }
-
- static class UniqueValueClass1 {
- String value ;
- UniqueValueClass1(String value) { this.value = value ; }
- @Override public String toString() { return value ; }
- }
-
- static class UniqueValueClass2 {
- String value ;
- UniqueValueClass2(String value) { this.value = value ; }
- @Override public String toString() { return value ; }
- }
-
- public void testSameAdhocClassUS()
- {
- Model m = ModelFactory.createDefaultModel();
- UniqueValueClass1 ra = new UniqueValueClass1("rhubarb") ;
- UniqueValueClass1 rb = new UniqueValueClass1("cottage") ;
- assertNull( "not expecting registered RDF Datatype", TypeMapper.getInstance().getTypeByValue( ra ) );
- Literal la = m.createTypedLiteral( ra ); // Sets the type mapper - contaminates it with UniqueValueClass1
- Literal lb = m.createTypedLiteral( rb );
- assertInstanceOf( AdhocDatatype.class, la.getDatatype() );
- assertSame( la.getDatatype(), lb.getDatatype() );
- assertNotNull(TypeMapper.getInstance().getTypeByValue( ra ) );
- }
-
- // Tests are not necessarily run in order so use UniqueValueClass2
- public void testTypedLiteralTypesAndValues()
- {
- Model m = ModelFactory.createDefaultModel();
- //Resource r = m.createResource( "eh:/rhubarb" );
- UniqueValueClass2 r = new UniqueValueClass2("rhubarb") ;
- assertNull( "not expecting registered RDF Datatype", TypeMapper.getInstance().getTypeByValue( r ) );
- Literal typed = m.createTypedLiteral( r ); // Sets the type mapper - contaminates it with UniqueValueClass2
- Literal string = m.createLiteral( r.value );
- assertEquals( string.getLexicalForm(), typed.getLexicalForm() );
- assertEquals( string.getLanguage(), typed.getLanguage() );
- assertDiffer( string.getDatatypeURI(), typed.getDatatypeURI() );
- assertNotNull( "a datatype should have been invented for Resource[Impl]", typed.getDatatype() );
- assertDiffer( typed, string );
- assertDiffer( typed.getValue(), string.getValue() );
- assertEquals( r, typed.getValue() );
- assertDiffer( typed.hashCode(), string.hashCode() );
- }
+ * TestLiteralImpl - minimal, this is the first time an extra test has
+ * been needed above
+ * the regression testing.
+ */
+public class TestLiteralImpl extends AbstractModelTestBase
+{
+ static class UniqueValueClass1
+ {
+ String value;
+
+ UniqueValueClass1( final String value )
+ {
+ this.value = value;
+ }
+
+ @Override
+ public String toString()
+ {
+ return value;
+ }
+ }
+
+ static class UniqueValueClass2
+ {
+ String value;
+
+ UniqueValueClass2( final String value )
+ {
+ this.value = value;
+ }
+
+ @Override
+ public String toString()
+ {
+ return value;
+ }
+ }
+
+ public TestLiteralImpl( final TestingModelFactory modelFactory,
+ final String name )
+ {
+ super(modelFactory, name);
+ }
+
+ /**
+ * Test that a literal node can be as'ed into a literal.
+ */
+ public void testAsLiteral()
+ {
+ ModelHelper.literal(model, "17").as(Literal.class);
+ }
+
+ /**
+ * Test that a non-literal node cannot be as'ed into a literal
+ */
+ public void testCannotAsNonLiteral()
+ {
+ try
+ {
+ ModelHelper.resource(model, "plumPie").as(Literal.class);
+ Assert.fail("non-literal cannot be converted to literal");
+ }
+ catch (final LiteralRequiredException l)
+ {
+ JenaTestBase.pass();
+ }
+ }
+
+ public void testInModel()
+ {
+
+ final Model m1 = createModel();
+ final Model m2 = createModel();
+ final Literal l1 = m1.createLiteral("17");
+ final Literal l2 = l1.inModel(m2);
+ Assert.assertEquals(l1, l2);
+ Assert.assertSame(m2, l2.getModel());
+ }
+
+ public void testLiteralHasModel()
+ {
+ testLiteralHasModel(model, model.createLiteral("hello, world"));
+ testLiteralHasModel(model, model.createLiteral("hello, world", "en-UK"));
+ testLiteralHasModel(model, model.createLiteral("hello, world", true));
+ testLiteralHasModel(model, model.createTypedLiteral("hello, world"));
+ testLiteralHasModel(model, model.createTypedLiteral(false));
+ testLiteralHasModel(model, model.createTypedLiteral(17));
+ testLiteralHasModel(model, model.createTypedLiteral('x'));
+ }
+
+ private void testLiteralHasModel( final Model m, final Literal lit )
+ {
+ Assert.assertSame(m, lit.getModel());
+ }
+
+ public void testSameAdhocClassUS()
+ {
+ try
+ {
+ final UniqueValueClass1 ra = new UniqueValueClass1("rhubarb");
+ final UniqueValueClass1 rb = new UniqueValueClass1("cottage");
+ Assert.assertNull("not expecting registered RDF Datatype",
+ TypeMapper.getInstance().getTypeByValue(ra));
+ final Literal la = model.createTypedLiteral(ra); // Sets the type
+ // mapper
+ // - contaminates it
+ // with
+ // UniqueValueClass1
+ final Literal lb = model.createTypedLiteral(rb);
+ JenaTestBase
+ .assertInstanceOf(AdhocDatatype.class, la.getDatatype());
+ Assert.assertSame(la.getDatatype(), lb.getDatatype());
+ Assert.assertNotNull(TypeMapper.getInstance().getTypeByValue(ra));
+ }
+ finally
+ {
+ TypeMapper.reset();
+ }
+ }
+
+ // Tests are not necessarily run in order so use UniqueValueClass2
+ public void testTypedLiteralTypesAndValues()
+ {
+ // Resource r = model.createResource( "eh:/rhubarb" );
+ final UniqueValueClass2 r = new UniqueValueClass2("rhubarb");
+ Assert.assertNull("not expecting registered RDF Datatype", TypeMapper
+ .getInstance().getTypeByValue(r));
+ final Literal typed = model.createTypedLiteral(r); // Sets the type
+ // mapper -
+ // contaminates it
+ // with
+ // UniqueValueClass2
+ final Literal string = model.createLiteral(r.value);
+ Assert.assertEquals(string.getLexicalForm(), typed.getLexicalForm());
+ Assert.assertEquals(string.getLanguage(), typed.getLanguage());
+ JenaTestBase.assertDiffer(string.getDatatypeURI(),
+ typed.getDatatypeURI());
+ Assert.assertNotNull(
+ "a datatype should have been invented for Resource[Impl]",
+ typed.getDatatype());
+ JenaTestBase.assertDiffer(typed, string);
+ JenaTestBase.assertDiffer(typed.getValue(), string.getValue());
+ Assert.assertEquals(r, typed.getValue());
+ JenaTestBase.assertDiffer(typed.hashCode(), string.hashCode());
+ }
}