Return-Path: X-Original-To: apmail-jena-commits-archive@www.apache.org Delivered-To: apmail-jena-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CDF76E500 for ; Mon, 14 Jan 2013 22:39:17 +0000 (UTC) Received: (qmail 1273 invoked by uid 500); 14 Jan 2013 22:39:17 -0000 Delivered-To: apmail-jena-commits-archive@jena.apache.org Received: (qmail 1248 invoked by uid 500); 14 Jan 2013 22:39:17 -0000 Mailing-List: contact commits-help@jena.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jena.apache.org Delivered-To: mailing list commits@jena.apache.org Received: (qmail 1235 invoked by uid 99); 14 Jan 2013 22:39:17 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Jan 2013 22:39:17 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Jan 2013 22:39:03 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 14B792388C27; Mon, 14 Jan 2013 22:38:42 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1433184 [3/9] - in /jena/trunk/jena-core/src: main/java/com/hp/hpl/jena/graph/compose/ test/java/com/hp/hpl/jena/graph/compose/test/ test/java/com/hp/hpl/jena/graph/test/ test/java/com/hp/hpl/jena/mem/test/ test/java/com/hp/hpl/jena/rdf/ar... Date: Mon, 14 Jan 2013 22:38:37 -0000 To: commits@jena.apache.org From: andy@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130114223842.14B792388C27@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestIterators.java URL: http://svn.apache.org/viewvc/jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestIterators.java?rev=1433184&r1=1433183&r2=1433184&view=diff ============================================================================== --- jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestIterators.java (original) +++ jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestIterators.java Mon Jan 14 22:38:35 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,37 +18,290 @@ package com.hp.hpl.jena.rdf.model.test; -import com.hp.hpl.jena.graph.test.*; -import com.hp.hpl.jena.rdf.model.*; +import com.hp.hpl.jena.rdf.model.Literal; +import com.hp.hpl.jena.rdf.model.NodeIterator; +import com.hp.hpl.jena.rdf.model.NsIterator; +import com.hp.hpl.jena.rdf.model.Property; +import com.hp.hpl.jena.rdf.model.ResIterator; +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 com.hp.hpl.jena.rdf.model.test.helpers.TestingModelFactory; +import com.hp.hpl.jena.vocabulary.RDF; -import junit.framework.TestSuite; +import junit.framework.Assert; -/** - @author kers -*/ -public class TestIterators extends GraphTestBase - { - public static TestSuite suite() - { return new TestSuite( TestIterators.class ); } - - public TestIterators(String name) - { super(name); } - - /** - bug detected in StatementIteratorImpl - next does not - advance current, so remove doesn't work with next; - this test should expose the bug. - */ - public void testIterators() - { - Model m = ModelFactory.createDefaultModel(); - Resource S = m.createResource( "S" ); - Property P = m.createProperty( "P" ); - RDFNode O = m.createResource( "O " ); - m.add( S, P, O ); - StmtIterator it = m.listStatements(); - it.next(); - it.remove(); - assertEquals( "", 0, m.size() ); - } - } +public class TestIterators extends AbstractModelTestBase +{ + int num = 5; + Resource subject[] = new Resource[num]; + Property predicate[] = new Property[num]; + Statement stmts[] = new Statement[num * num]; + + String suri = "http://aldabaran/test6/s"; + String puri = "http://aldabaran/test6/"; + + public TestIterators( final TestingModelFactory modelFactory, + final String name ) + { + super(modelFactory, name); + } + + @Override + public void setUp() throws Exception + { + super.setUp(); + + for (int i = 0; i < num; i++) + { + subject[i] = model.createResource(suri + Integer.toString(i)); + predicate[i] = model + .createProperty(puri + Integer.toString(i), "p"); + } + + for (int i = 0; i < num; i++) + { + for (int j = 0; j < num; j++) + { + final Statement stmt = model.createStatement(subject[i], + predicate[j], model.createTypedLiteral((i * num) + j)); + model.add(stmt); + model.add(stmt); + stmts[(i * num) + j] = stmt; + } + } + } + + /** + * bug detected in StatementIteratorImpl - next does not + * advance current, so remove doesn't work with next; + * this test should expose the bug. + */ + public void testIteratorRemove() + { + final StmtIterator it = model.listStatements(); + try + { + while (it.hasNext()) + { + it.next(); + it.remove(); + } + Assert.assertEquals("Remove failed", 0, model.size()); + } + finally + { + it.close(); + } + + } + + public void testListObjects() + { + int count = 0; + NodeIterator iter; + iter = model.listObjects(); + while (iter.hasNext()) + { + iter.nextNode(); + count++; + } + Assert.assertEquals(num * num, count); + } + + public void testNamespaceIterator() + { + final boolean predf[] = new boolean[num]; + for (int i = 0; i < num; i++) + { + predf[i] = false; + } + final NsIterator nIter = model.listNameSpaces(); + while (nIter.hasNext()) + { + final String ns = nIter.nextNs(); + boolean found = false; + for (int i = 0; i < num; i++) + { + if (ns.equals(predicate[i].getNameSpace())) + { + found = true; + Assert.assertFalse("Should not have found " + predicate[i] + + " already.", predf[i]); + predf[i] = true; + } + } + Assert.assertTrue("Should have found " + ns, found); + } + for (int i = 0; i < num; i++) + { + Assert.assertTrue("Should have found " + predicate[i], predf[i]); + } + } + + public void testObjectsOfProperty() + { + + NodeIterator iter; + final boolean[] object = new boolean[num * num]; + for (int i = 0; i < (num * num); i++) + { + object[i] = false; + } + iter = model.listObjectsOfProperty(predicate[0]); + while (iter.hasNext()) + { + final Literal l = (Literal) iter.nextNode(); + final int i = l.getInt(); + object[i] = true; + } + for (int i = 0; i < (num * num); i++) + { + if ((i % num) == 0) + { + Assert.assertTrue(object[i]); + } + else + { + Assert.assertFalse(object[i]); + } + } + + } + + public void testObjectsOfPropertyAndValue() + { + NodeIterator iter; + final boolean[] object = new boolean[num]; + final Resource subj = model.createResource(); + for (int i = 0; i < num; i++) + { + model.addLiteral(subj, RDF.value, i); + object[i] = false; + } + + iter = model.listObjectsOfProperty(subj, RDF.value); + while (iter.hasNext()) + { + final int i = ((Literal) iter.nextNode()).getInt(); + object[i] = true; + } + for (int i = 0; i < (num); i++) + { + Assert.assertTrue(object[i]); + } + } + + public void testResourceIterator() + { + + final boolean subjf[] = new boolean[num]; + + for (int i = 0; i < num; i++) + { + subjf[i] = false; + } + + boolean found = false; + final ResIterator rIter = model.listSubjects(); + while (rIter.hasNext()) + { + final Resource subj = rIter.nextResource(); + found = false; + for (int i = 0; i < num; i++) + { + if (subj.equals(subject[i])) + { + found = true; + Assert.assertFalse("Should not have found " + subject[i] + + " already.", subjf[i]); + subjf[i] = true; + } + } + Assert.assertTrue("Should have found " + subj, found); + } + for (int i = 0; i < num; i++) + { + Assert.assertTrue("Should have found " + subject[i], subjf[i]); + } + + // System.err.println( + // "WARNING: listNameSpace testing wonky for the moment" ); + // NsIterator nIter = model.listNameSpaces(); + // HashSet fromIterator = new HashSet(); + // HashSet fromPredicates = new HashSet(); + // while (nIter.hasNext()) fromIterator.add( nIter.next() ); + // for (int i = 0; i < num; i += 1) fromPredicates.add( + // predicate[i].getNameSpace() ); + // if (fromIterator.equals( fromPredicates )) + // {} + // else + // { + // System.err.println( "| oh dear." ); + // System.err.println( "| predicate namespaces: " + fromPredicates ); + // System.err.println( "| iterator namespaces: " + fromIterator ); + // } + + } + + public void testStatementIter() + { + final int numStmts = num * num; + final boolean stmtf[] = new boolean[numStmts]; + Assert.assertEquals(numStmts, model.size()); + for (int i = 0; i < numStmts; i++) + { + stmtf[i] = false; + } + + final StmtIterator sIter = model.listStatements(); + while (sIter.hasNext()) + { + final Statement stmt = sIter.nextStatement(); + boolean found = false; + for (int i = 0; i < numStmts; i++) + { + if (stmt.equals(stmts[i])) + { + found = true; + Assert.assertFalse("Should not have found " + stmts[i] + + " already.", stmtf[i]); + stmtf[i] = true; + } + } + Assert.assertTrue("Should have found " + stmt, found); + } + for (int i = 0; i < numStmts; i++) + { + Assert.assertTrue("Should have found " + stmts[i], stmtf[i]); + } + } + // SEE the tests in model.test: TestReifiedStatements and + // TestStatementResources + // { + // System.err.println( + // "WARNING: reification testing suppressed for the moment" ); + // /* Reification is not working properly + // + // for (int i=0; i - * A collection of unit tests for the standard implementation of - * {@link RDFList}. + * A collection of unit tests for the standard implementation of {@link RDFList} + * . *

* - * @author Ian Dickinson, HP Labs - * (email) - * @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 expList = new ArrayList(); - 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 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() {@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 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 expList = new ArrayList(); + 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() { + @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 listAbstractionClass() { + // return UserDefList.class; + // } + // + // public UserDefListImpl( Node n, EnhGraph g ) { + // super( n, g ); + // } + // + // } } Modified: jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListStatements.java URL: http://svn.apache.org/viewvc/jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListStatements.java?rev=1433184&r1=1433183&r2=1433184&view=diff ============================================================================== --- jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListStatements.java (original) +++ jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/rdf/model/test/TestListStatements.java Mon Jan 14 22:38:35 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 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 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 lis = it.toList(); - assertTrue(lis.size() == 1); - } - - @Test - public final void theresAreTwoText() { - StmtIterator it = m.listStatements(s, p, "text", null); - List 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 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 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 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 lis = it.toList(); + Assert.assertTrue(lis.size() == 1); + } } -