Source Code Cross Referenced for TestList.java in  » RSS-RDF » Jena-2.5.5 » com » hp » hpl » jena » rdf » model » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » RSS RDF » Jena 2.5.5 » com.hp.hpl.jena.rdf.model.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*****************************************************************************
0002:         * Source code information
0003:         * -----------------------
0004:         * Original author    Ian Dickinson, HP Labs Bristol
0005:         * Author email       Ian.Dickinson@hp.com
0006:         * Package            Jena 2
0007:         * Web                http://sourceforge.net/projects/jena/
0008:         * Created            24 Jan 2003
0009:         * Filename           $RCSfile: TestList.java,v $
0010:         * Revision           $Revision: 1.15 $
0011:         * Release status     $State: Exp $
0012:         *
0013:         * Last modified on   $Date: 2008/01/02 12:04:42 $
0014:         *               by   $Author: andy_seaborne $
0015:         *
0016:         * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
0017:         * (see footer for full conditions)
0018:         *****************************************************************************/package com.hp.hpl.jena.rdf.model.test;
0019:
0020:        // Imports
0021:        ///////////////
0022:        import java.util.*;
0023:
0024:        import org.apache.commons.logging.Log;
0025:        import org.apache.commons.logging.LogFactory;
0026:
0027:        import junit.framework.*;
0028:
0029:        import com.hp.hpl.jena.enhanced.*;
0030:        import com.hp.hpl.jena.enhanced.EnhGraph;
0031:        import com.hp.hpl.jena.graph.Graph;
0032:        import com.hp.hpl.jena.graph.Node;
0033:        import com.hp.hpl.jena.rdf.model.*;
0034:        import com.hp.hpl.jena.rdf.model.impl.RDFListImpl;
0035:        import com.hp.hpl.jena.shared.JenaException;
0036:        import com.hp.hpl.jena.util.iterator.Map1;
0037:        import com.hp.hpl.jena.vocabulary.*;
0038:
0039:        /**
0040:         * <p>
0041:         * A collection of unit tests for the standard implementation of 
0042:         * {@link RDFList}.
0043:         * </p>
0044:         * 
0045:         * @author Ian Dickinson, HP Labs 
0046:         *         (<a  href="mailto:Ian.Dickinson@hp.com" >email</a>)
0047:         * @version CVS $Id: TestList.java,v 1.15 2008/01/02 12:04:42 andy_seaborne Exp $
0048:         */
0049:        public class TestList extends TestCase {
0050:            // Constants
0051:            //////////////////////////////////
0052:
0053:            public static final String NS = "uri:urn:x-rdf:test#";
0054:
0055:            // Static variables
0056:            //////////////////////////////////
0057:
0058:            // Instance variables
0059:            //////////////////////////////////
0060:
0061:            // Constructors
0062:            //////////////////////////////////
0063:
0064:            public TestList(String name) {
0065:                super (name);
0066:            }
0067:
0068:            // External signature methods
0069:            //////////////////////////////////
0070:
0071:            public static TestSuite suite() {
0072:                TestSuite s = new TestSuite("TestList");
0073:
0074:                for (int i = 0; i <= 5; i++) {
0075:                    s.addTest(new CountTest(i));
0076:                    s.addTest(new TailTest(i));
0077:                }
0078:
0079:                s.addTest(new ValidityTest());
0080:                s.addTest(new HeadTest());
0081:                s.addTest(new SetHeadTest());
0082:                s.addTest(new SetTailTest());
0083:                s.addTest(new ConsTest());
0084:                s.addTest(new AddTest());
0085:                s.addTest(new TestListGet());
0086:                s.addTest(new ReplaceTest());
0087:                s.addTest(new IndexTest1());
0088:                s.addTest(new IndexTest2());
0089:                s.addTest(new AppendTest());
0090:                s.addTest(new ConcatenateTest());
0091:                s.addTest(new ConcatenateTest2());
0092:                s.addTest(new ApplyTest());
0093:                s.addTest(new ReduceTest());
0094:                s.addTest(new RemoveTest());
0095:                s.addTest(new Map1Test());
0096:                s.addTest(new ListEqualsTest());
0097:                s.addTest(new ListSubclassTest());
0098:                s.addTest(new UserDefinedListTest());
0099:
0100:                return s;
0101:            }
0102:
0103:            // Internal implementation methods
0104:            //////////////////////////////////
0105:
0106:            /** Test that an iterator delivers the expected values */
0107:            protected static void iteratorTest(Iterator i, Object[] expected) {
0108:                Log logger = LogFactory.getLog(TestList.class);
0109:                List expList = new ArrayList();
0110:                for (int j = 0; j < expected.length; j++) {
0111:                    expList.add(expected[j]);
0112:                }
0113:
0114:                while (i.hasNext()) {
0115:                    Object next = i.next();
0116:
0117:                    // debugging
0118:                    if (!expList.contains(next)) {
0119:                        logger.debug("TestList - Unexpected iterator result: "
0120:                                + next);
0121:                    }
0122:
0123:                    assertTrue(
0124:                            "Value "
0125:                                    + next
0126:                                    + " was not expected as a result from this iterator ",
0127:                            expList.contains(next));
0128:                    assertTrue("Value " + next
0129:                            + " was not removed from the list ", expList
0130:                            .remove(next));
0131:                }
0132:
0133:                if (!(expList.size() == 0)) {
0134:                    logger
0135:                            .debug("TestList - Expected iterator results not found");
0136:                    for (Iterator j = expList.iterator(); j.hasNext();) {
0137:                        logger.debug("TestList - missing: " + j.next());
0138:                    }
0139:                }
0140:                assertEquals(
0141:                        "There were expected elements from the iterator that were not found",
0142:                        0, expList.size());
0143:            }
0144:
0145:            //==============================================================================
0146:            // Inner class definitions
0147:            //==============================================================================
0148:
0149:            protected static class ListTest extends TestCase {
0150:                public ListTest(String n) {
0151:                    super (n);
0152:                }
0153:
0154:                protected void checkValid(String testName, RDFList l,
0155:                        boolean validExpected) {
0156:                    l.setStrict(true);
0157:                    boolean valid = l.isValid();
0158:                    // for debugging ... String s = l.getValidityErrorMessage();
0159:                    assertEquals("Validity test " + testName
0160:                            + " returned wrong isValid() result",
0161:                            validExpected, valid);
0162:                }
0163:
0164:                protected RDFList getListRoot(Model m) {
0165:                    Resource root = m.getResource(NS + "root");
0166:                    assertNotNull("Root resource should not be null", root);
0167:
0168:                    Resource listHead = root.getRequiredProperty(
0169:                            m.getProperty(NS + "p")).getResource();
0170:
0171:                    RDFList l = (RDFList) listHead.as(RDFList.class);
0172:                    assertNotNull(
0173:                            "as(RDFList) should not return null for root", l);
0174:
0175:                    return l;
0176:                }
0177:            }
0178:
0179:            protected static class CountTest extends ListTest {
0180:                protected int i;
0181:
0182:                public CountTest(int i) {
0183:                    super ("CountTest");
0184:                    this .i = i;
0185:                }
0186:
0187:                public void runTest() {
0188:                    Model m = ModelFactory.createDefaultModel();
0189:                    m.read("file:testing/ontology/list" + i + ".rdf");
0190:
0191:                    RDFList l0 = getListRoot(m);
0192:                    assertEquals("List size should be " + i, i, l0.size());
0193:                }
0194:
0195:            }
0196:
0197:            protected static class ValidityTest extends ListTest {
0198:                public ValidityTest() {
0199:                    super ("ValidityTest");
0200:                }
0201:
0202:                public void runTest() {
0203:                    Model m = ModelFactory.createDefaultModel();
0204:
0205:                    Resource root = m.createResource(NS + "root");
0206:                    Property p = m.createProperty(NS, "p");
0207:
0208:                    // a list of the nil object, but not typed
0209:                    Resource nil = RDF.nil;
0210:                    m.add(root, p, nil);
0211:                    RDFList l0 = getListRoot(m);
0212:                    checkValid("valid1", l0, true);
0213:
0214:                    // add another node to the head of the list
0215:                    Resource badList = m.createResource();
0216:                    m.getRequiredProperty(root, p).remove();
0217:                    m.add(root, p, badList);
0218:                    m.add(badList, RDF.type, RDF.List);
0219:
0220:                    RDFList l1 = getListRoot(m);
0221:                    checkValid("valid2", l1, false);
0222:
0223:                    //checkValid( "valid3", l1, false );
0224:
0225:                    m.add(badList, RDF.first, "fred");
0226:                    checkValid("valid4", l1, false);
0227:
0228:                    m.add(badList, RDF.rest, nil);
0229:                    checkValid("valid5", l1, true);
0230:                }
0231:
0232:            }
0233:
0234:            protected static class HeadTest extends ListTest {
0235:                public HeadTest() {
0236:                    super ("HeadTest");
0237:                }
0238:
0239:                public void runTest() {
0240:                    Model m = ModelFactory.createDefaultModel();
0241:                    m.read("file:testing/ontology/list5.rdf");
0242:
0243:                    RDFList l0 = getListRoot(m);
0244:
0245:                    String[] names = { "a", "b", "c", "d", "e" };
0246:                    for (int i = 0; i < names.length; i++) {
0247:                        assertEquals("head of list has incorrect URI", NS
0248:                                + names[i], ((Resource) l0.getHead()).getURI());
0249:                        l0 = l0.getTail();
0250:                    }
0251:                }
0252:            }
0253:
0254:            protected static class TailTest extends ListTest {
0255:                protected int i;
0256:
0257:                public TailTest(int i) {
0258:                    super ("TailTest");
0259:                    this .i = i;
0260:                }
0261:
0262:                public void runTest() {
0263:                    Model m = ModelFactory.createDefaultModel();
0264:                    m.read("file:testing/ontology/list" + i + ".rdf");
0265:
0266:                    RDFList l0 = getListRoot(m);
0267:
0268:                    // get the tail n times, should be nil at the end
0269:                    for (int j = 0; j < i; j++) {
0270:                        l0 = l0.getTail();
0271:                    }
0272:
0273:                    assertTrue("Should have reached the end of the list after "
0274:                            + i + " getTail()'s", l0.isEmpty());
0275:                }
0276:            }
0277:
0278:            protected static class SetHeadTest extends ListTest {
0279:                public SetHeadTest() {
0280:                    super ("SetHeadTest");
0281:                }
0282:
0283:                public void runTest() {
0284:                    Model m = ModelFactory.createDefaultModel();
0285:
0286:                    Resource root = m.createResource(NS + "root");
0287:                    Property p = m.createProperty(NS, "p");
0288:
0289:                    // a list of the nil object, but not typed
0290:                    Resource nil = RDF.nil;
0291:                    m.add(nil, RDF.type, RDF.List);
0292:
0293:                    Resource list = m.createResource();
0294:                    m.add(list, RDF.type, RDF.List);
0295:                    m.add(list, RDF.first, "fred");
0296:                    m.add(list, RDF.rest, nil);
0297:
0298:                    m.add(root, p, list);
0299:                    RDFList l1 = getListRoot(m);
0300:                    checkValid("sethead1", l1, true);
0301:
0302:                    assertEquals("List head should be 'fred'", "fred",
0303:                            ((Literal) l1.getHead()).getString());
0304:
0305:                    l1.setHead(m.createTypedLiteral(42));
0306:                    checkValid("sethead2", l1, true);
0307:                    assertEquals("List head should be '42'", 42, ((Literal) l1
0308:                            .getHead()).getInt());
0309:
0310:                }
0311:            }
0312:
0313:            protected static class SetTailTest extends ListTest {
0314:                public SetTailTest() {
0315:                    super ("SetTailTest");
0316:                }
0317:
0318:                public void runTest() {
0319:                    Model m = ModelFactory.createDefaultModel();
0320:
0321:                    Resource root = m.createResource(NS + "root");
0322:                    Property p = m.createProperty(NS, "p");
0323:
0324:                    Resource nil = RDF.nil;
0325:                    m.add(nil, RDF.type, RDF.List);
0326:
0327:                    Resource list0 = m.createResource();
0328:                    m.add(list0, RDF.type, RDF.List);
0329:                    m.add(list0, RDF.first, "fred");
0330:                    m.add(list0, RDF.rest, nil);
0331:
0332:                    m.add(root, p, list0);
0333:                    RDFList l1 = getListRoot(m);
0334:                    checkValid("settail1", l1, true);
0335:
0336:                    Resource list1 = m.createResource();
0337:                    m.add(list1, RDF.type, RDF.List);
0338:                    m.add(list1, RDF.first, "george");
0339:                    m.add(list1, RDF.rest, nil);
0340:
0341:                    RDFList l2 = (RDFList) list1.as(RDFList.class);
0342:                    assertNotNull(
0343:                            "as(RDFList) should not return null for root", l2);
0344:                    checkValid("settail2", l2, true);
0345:
0346:                    assertEquals("l1 should have length 1", 1, l1.size());
0347:                    assertEquals("l2 should have length 1", 1, l2.size());
0348:
0349:                    // use set tail to join the lists together
0350:                    l1.setTail(l2);
0351:
0352:                    checkValid("settail3", l1, true);
0353:                    checkValid("settail4", l2, true);
0354:
0355:                    assertEquals("l1 should have length 2", 2, l1.size());
0356:                    assertEquals("l2 should have length 1", 1, l2.size());
0357:
0358:                }
0359:            }
0360:
0361:            protected static class ConsTest extends ListTest {
0362:                public ConsTest() {
0363:                    super ("ConsTest");
0364:                }
0365:
0366:                public void runTest() {
0367:                    Model m = ModelFactory.createDefaultModel();
0368:
0369:                    Resource root = m.createResource(NS + "root");
0370:                    Property p = m.createProperty(NS, "p");
0371:
0372:                    Resource nil = m.getResource(RDF.nil.getURI());
0373:                    RDFList list = (RDFList) nil.as(RDFList.class);
0374:
0375:                    Resource[] toAdd = new Resource[] {
0376:                            m.createResource(NS + "e"),
0377:                            m.createResource(NS + "d"),
0378:                            m.createResource(NS + "c"),
0379:                            m.createResource(NS + "b"),
0380:                            m.createResource(NS + "a"), };
0381:
0382:                    // cons each of these resources onto the front of the list
0383:                    for (int i = 0; i < toAdd.length; i++) {
0384:                        RDFList list0 = list.cons(toAdd[i]);
0385:
0386:                        checkValid("constest1", list0, true);
0387:                        assertTrue("cons'ed lists should not be equal", !list0
0388:                                .equals(list));
0389:
0390:                        list = list0;
0391:                    }
0392:
0393:                    // relate the root to the list
0394:                    m.add(root, p, list);
0395:
0396:                    // should be isomorphic with list 5
0397:                    Model m0 = ModelFactory.createDefaultModel();
0398:                    m0.read("file:testing/ontology/list5.rdf");
0399:
0400:                    assertTrue("Cons'ed and read models should be the same", m0
0401:                            .isIsomorphicWith(m));
0402:                }
0403:            }
0404:
0405:            protected static class AddTest extends ListTest {
0406:                public AddTest() {
0407:                    super ("AddTest");
0408:                }
0409:
0410:                public void runTest() {
0411:                    Model m = ModelFactory.createDefaultModel();
0412:
0413:                    Resource root = m.createResource(NS + "root");
0414:                    Property p = m.createProperty(NS, "p");
0415:
0416:                    Resource nil = m.getResource(RDF.nil.getURI());
0417:                    RDFList list = (RDFList) nil.as(RDFList.class);
0418:
0419:                    Resource[] toAdd = new Resource[] {
0420:                            m.createResource(NS + "a"),
0421:                            m.createResource(NS + "b"),
0422:                            m.createResource(NS + "c"),
0423:                            m.createResource(NS + "d"),
0424:                            m.createResource(NS + "e"), };
0425:
0426:                    // add each of these resources onto the end of the list
0427:                    for (int i = 0; i < toAdd.length; i++) {
0428:                        RDFList list0 = list.with(toAdd[i]);
0429:
0430:                        checkValid("addTest0", list0, true);
0431:                        assertTrue("added'ed lists should be equal", list
0432:                                .equals(nil)
0433:                                || list0.equals(list));
0434:
0435:                        list = list0;
0436:                    }
0437:
0438:                    // relate the root to the list
0439:                    m.add(root, p, list);
0440:
0441:                    // should be isomorphic with list 5
0442:                    Model m0 = ModelFactory.createDefaultModel();
0443:                    m0.read("file:testing/ontology/list5.rdf");
0444:
0445:                    assertTrue("Add'ed and read models should be the same", m0
0446:                            .isIsomorphicWith(m));
0447:                }
0448:            }
0449:
0450:            protected static class TestListGet extends ListTest {
0451:                public TestListGet() {
0452:                    super ("TestListGet");
0453:                }
0454:
0455:                public void runTest() {
0456:                    Model m = ModelFactory.createDefaultModel();
0457:                    m.read("file:testing/ontology/list5.rdf");
0458:
0459:                    Resource[] toGet = new Resource[] {
0460:                            m.createResource(NS + "a"),
0461:                            m.createResource(NS + "b"),
0462:                            m.createResource(NS + "c"),
0463:                            m.createResource(NS + "d"),
0464:                            m.createResource(NS + "e"), };
0465:
0466:                    RDFList l1 = getListRoot(m);
0467:
0468:                    // test normal gets
0469:                    for (int i = 0; i < toGet.length; i++) {
0470:                        assertEquals("list element " + i + " is not correct",
0471:                                toGet[i], l1.get(i));
0472:                    }
0473:
0474:                    // now test we get an exception for going beyong the end of the list
0475:                    boolean gotEx = false;
0476:                    try {
0477:                        l1.get(toGet.length + 1);
0478:                    } catch (ListIndexException e) {
0479:                        gotEx = true;
0480:                    }
0481:
0482:                    assertTrue(
0483:                            "Should see exception raised by accessing beyond end of list",
0484:                            gotEx);
0485:                }
0486:            }
0487:
0488:            protected static class ReplaceTest extends ListTest {
0489:                public ReplaceTest() {
0490:                    super ("ReplaceTest");
0491:                }
0492:
0493:                public void runTest() {
0494:                    Model m = ModelFactory.createDefaultModel();
0495:                    m.read("file:testing/ontology/list5.rdf");
0496:
0497:                    Literal[] toSet = new Literal[] { m.createLiteral("a"),
0498:                            m.createLiteral("b"), m.createLiteral("c"),
0499:                            m.createLiteral("d"), m.createLiteral("e"), };
0500:
0501:                    RDFList l1 = getListRoot(m);
0502:
0503:                    // change all the values 
0504:                    for (int i = 0; i < toSet.length; i++) {
0505:                        l1.replace(i, toSet[i]);
0506:                    }
0507:
0508:                    // then check them
0509:                    for (int i = 0; i < toSet.length; i++) {
0510:                        assertEquals("list element " + i + " is not correct",
0511:                                toSet[i], l1.get(i));
0512:                    }
0513:
0514:                    // now test we get an exception for going beyong the end of the list
0515:                    boolean gotEx = false;
0516:                    try {
0517:                        l1.replace(toSet.length + 1, toSet[0]);
0518:                    } catch (ListIndexException e) {
0519:                        gotEx = true;
0520:                    }
0521:
0522:                    assertTrue(
0523:                            "Should see exception raised by accessing beyond end of list",
0524:                            gotEx);
0525:                }
0526:            }
0527:
0528:            protected static class IndexTest1 extends ListTest {
0529:                public IndexTest1() {
0530:                    super ("IndexTest1");
0531:                }
0532:
0533:                public void runTest() {
0534:                    Model m = ModelFactory.createDefaultModel();
0535:                    m.read("file:testing/ontology/list5.rdf");
0536:
0537:                    Resource[] toGet = new Resource[] {
0538:                            m.createResource(NS + "a"),
0539:                            m.createResource(NS + "b"),
0540:                            m.createResource(NS + "c"),
0541:                            m.createResource(NS + "d"),
0542:                            m.createResource(NS + "e"), };
0543:
0544:                    RDFList l1 = getListRoot(m);
0545:
0546:                    // check the indexes are correct
0547:                    for (int i = 0; i < toGet.length; i++) {
0548:                        assertTrue("list should contain element " + i, l1
0549:                                .contains(toGet[i]));
0550:                        assertEquals("list element " + i + " is not correct",
0551:                                i, l1.indexOf(toGet[i]));
0552:                    }
0553:                }
0554:            }
0555:
0556:            protected static class IndexTest2 extends ListTest {
0557:                public IndexTest2() {
0558:                    super ("IndexTest2");
0559:                }
0560:
0561:                public void runTest() {
0562:                    Model m = ModelFactory.createDefaultModel();
0563:
0564:                    Resource nil = m.getResource(RDF.nil.getURI());
0565:                    RDFList list = (RDFList) nil.as(RDFList.class);
0566:
0567:                    Resource r = m.createResource(NS + "a");
0568:
0569:                    // cons each a's onto the front of the list
0570:                    for (int i = 0; i < 10; i++) {
0571:                        list = list.cons(r);
0572:                    }
0573:
0574:                    // now index them back again
0575:                    for (int j = 0; j < 10; j++) {
0576:                        assertEquals("index of j'th item should be j", j, list
0577:                                .indexOf(r, j));
0578:                    }
0579:                }
0580:            }
0581:
0582:            protected static class AppendTest extends ListTest {
0583:                public AppendTest() {
0584:                    super ("AppendTest");
0585:                }
0586:
0587:                public void runTest() {
0588:                    Model m = ModelFactory.createDefaultModel();
0589:                    m.read("file:testing/ontology/list5.rdf");
0590:
0591:                    Resource nil = m.getResource(RDF.nil.getURI());
0592:                    RDFList list = (RDFList) nil.as(RDFList.class);
0593:
0594:                    Resource r = m.createResource(NS + "foo");
0595:
0596:                    // create a list of foos
0597:                    for (int i = 0; i < 5; i++) {
0598:                        list = list.cons(r);
0599:                    }
0600:
0601:                    int listLen = list.size();
0602:
0603:                    // now append foos to the root list
0604:                    RDFList root = getListRoot(m);
0605:                    int rootLen = root.size();
0606:                    RDFList appended = root.append(list);
0607:
0608:                    // original list should be unchanged
0609:                    checkValid("appendTest0", root, true);
0610:                    assertEquals("Original list should be unchanged", rootLen,
0611:                            root.size());
0612:
0613:                    checkValid("appendTest1", list, true);
0614:                    assertEquals("Original list should be unchanged", listLen,
0615:                            list.size());
0616:
0617:                    // new list should be length of combined 
0618:                    checkValid("appendTest2", appended, true);
0619:                    assertEquals("Appended list not correct length", rootLen
0620:                            + listLen, appended.size());
0621:                }
0622:            }
0623:
0624:            protected static class ConcatenateTest extends ListTest {
0625:                public ConcatenateTest() {
0626:                    super ("ConcatenateTest");
0627:                }
0628:
0629:                public void runTest() {
0630:                    Model m = ModelFactory.createDefaultModel();
0631:                    m.read("file:testing/ontology/list5.rdf");
0632:
0633:                    Resource nil = m.getResource(RDF.nil.getURI());
0634:                    RDFList list = (RDFList) nil.as(RDFList.class);
0635:
0636:                    Resource r = m.createResource(NS + "foo");
0637:
0638:                    // create a list of foos
0639:                    for (int i = 0; i < 5; i++) {
0640:                        list = list.cons(r);
0641:                    }
0642:
0643:                    int listLen = list.size();
0644:
0645:                    // now append foos to the root list
0646:                    RDFList root = getListRoot(m);
0647:                    int rootLen = root.size();
0648:                    root.concatenate(list);
0649:
0650:                    // original list should be unchanged
0651:                    checkValid("concatTest0", list, true);
0652:                    assertEquals("Original list should be unchanged", listLen,
0653:                            list.size());
0654:
0655:                    // but lhs list has changed
0656:                    checkValid("concatTest1", root, true);
0657:                    assertEquals("Root list should be new length", rootLen
0658:                            + listLen, root.size());
0659:                }
0660:            }
0661:
0662:            protected static class ConcatenateTest2 extends ListTest {
0663:                public ConcatenateTest2() {
0664:                    super ("ConcatenateTest2");
0665:                }
0666:
0667:                public void runTest() {
0668:                    Model m = ModelFactory.createDefaultModel();
0669:                    m.read("file:testing/ontology/list5.rdf");
0670:
0671:                    Resource a = m.createResource(NS + "a");
0672:
0673:                    // create a list of foos
0674:                    Resource[] rs = new Resource[] {
0675:                            m.createResource(NS + "b"),
0676:                            m.createResource(NS + "c"),
0677:                            m.createResource(NS + "d"),
0678:                            m.createResource(NS + "e") };
0679:
0680:                    RDFList aList = m.createList().cons(a);
0681:                    RDFList rsList = m.createList(rs);
0682:
0683:                    // concatenate the above resources onto the empty list
0684:                    aList.concatenate(rsList);
0685:                    checkValid("concatTest3", aList, true);
0686:
0687:                    RDFList root = getListRoot(m);
0688:                    assertTrue(
0689:                            "Constructed and loaded lists should be the same",
0690:                            aList.sameListAs(root));
0691:                }
0692:            }
0693:
0694:            protected static class ApplyTest extends ListTest {
0695:                public ApplyTest() {
0696:                    super ("ApplyTest");
0697:                }
0698:
0699:                public void runTest() {
0700:                    Model m = ModelFactory.createDefaultModel();
0701:                    m.read("file:testing/ontology/list5.rdf");
0702:
0703:                    RDFList root = getListRoot(m);
0704:
0705:                    class MyApply implements  RDFList.ApplyFn {
0706:                        String collect = "";
0707:
0708:                        public void apply(RDFNode n) {
0709:                            collect = collect + ((Resource) n).getLocalName();
0710:                        }
0711:                    }
0712:
0713:                    MyApply f = new MyApply();
0714:                    root.apply(f);
0715:
0716:                    assertEquals(
0717:                            "Result of apply should be concatentation of local names",
0718:                            "abcde", f.collect);
0719:                }
0720:            }
0721:
0722:            protected static class ReduceTest extends ListTest {
0723:                public ReduceTest() {
0724:                    super ("ReduceTest");
0725:                }
0726:
0727:                public void runTest() {
0728:                    Model m = ModelFactory.createDefaultModel();
0729:                    m.read("file:testing/ontology/list5.rdf");
0730:
0731:                    RDFList root = getListRoot(m);
0732:
0733:                    RDFList.ReduceFn f = new RDFList.ReduceFn() {
0734:                        public Object reduce(RDFNode n, Object acc) {
0735:                            return ((String) acc)
0736:                                    + ((Resource) n).getLocalName();
0737:                        }
0738:                    };
0739:
0740:                    assertEquals(
0741:                            "Result of reduce should be concatentation of local names",
0742:                            "abcde", root.reduce(f, ""));
0743:                }
0744:            }
0745:
0746:            protected static class Map1Test extends ListTest {
0747:                public Map1Test() {
0748:                    super ("Map1Test");
0749:                }
0750:
0751:                public void runTest() {
0752:                    Model m = ModelFactory.createDefaultModel();
0753:                    m.read("file:testing/ontology/list5.rdf");
0754:
0755:                    RDFList root = getListRoot(m);
0756:                    iteratorTest(root.mapWith(new Map1() {
0757:                        public Object map1(Object x) {
0758:                            return ((Resource) x).getLocalName();
0759:                        }
0760:                    }), new Object[] { "a", "b", "c", "d", "e" });
0761:                }
0762:            }
0763:
0764:            protected static class RemoveTest extends ListTest {
0765:                public RemoveTest() {
0766:                    super ("RemoveTest");
0767:                }
0768:
0769:                public void runTest() {
0770:                    Model m = ModelFactory.createDefaultModel();
0771:
0772:                    Resource nil = m.getResource(RDF.nil.getURI());
0773:                    RDFList list0 = (RDFList) nil.as(RDFList.class);
0774:                    RDFList list1 = (RDFList) nil.as(RDFList.class);
0775:
0776:                    Resource r0 = m.createResource(NS + "x");
0777:                    Resource r1 = m.createResource(NS + "y");
0778:                    Resource r2 = m.createResource(NS + "z");
0779:
0780:                    for (int i = 0; i < 10; i++) {
0781:                        list0 = list0.cons(r0);
0782:                        list1 = list1.cons(r1);
0783:                    }
0784:
0785:                    // delete the elements of list0 one at a time
0786:                    while (!list0.isEmpty()) {
0787:                        list0 = list0.removeHead();
0788:                        checkValid("removeTest0", list0, true);
0789:                    }
0790:
0791:                    // delete all of list1 in one go
0792:                    list1.removeList();
0793:
0794:                    // model should now be empty
0795:                    assertEquals(
0796:                            "Model should be empty after deleting two lists",
0797:                            0, m.size());
0798:
0799:                    // selective remove
0800:                    RDFList list2 = ((RDFList) nil.as(RDFList.class)).cons(r2)
0801:                            .cons(r1).cons(r0);
0802:
0803:                    assertTrue("list should contain x ", list2.contains(r0));
0804:                    assertTrue("list should contain y ", list2.contains(r1));
0805:                    assertTrue("list should contain z ", list2.contains(r2));
0806:
0807:                    list2 = list2.remove(r1);
0808:                    assertTrue("list should contain x ", list2.contains(r0));
0809:                    assertTrue("list should contain y ", !list2.contains(r1));
0810:                    assertTrue("list should contain z ", list2.contains(r2));
0811:
0812:                    list2 = list2.remove(r0);
0813:                    assertTrue("list should contain x ", !list2.contains(r0));
0814:                    assertTrue("list should contain y ", !list2.contains(r1));
0815:                    assertTrue("list should contain z ", list2.contains(r2));
0816:
0817:                    list2 = list2.remove(r2);
0818:                    assertTrue("list should contain x ", !list2.contains(r0));
0819:                    assertTrue("list should contain y ", !list2.contains(r1));
0820:                    assertTrue("list should contain z ", !list2.contains(r2));
0821:                    assertTrue("list should be empty", list2.isEmpty());
0822:                }
0823:            }
0824:
0825:            protected static class ListEqualsTest extends ListTest {
0826:                public ListEqualsTest() {
0827:                    super ("ListEqualsTest");
0828:                }
0829:
0830:                public void runTest() {
0831:                    Model m = ModelFactory.createDefaultModel();
0832:
0833:                    Resource nil = m.getResource(RDF.nil.getURI());
0834:                    RDFList nilList = (RDFList) nil.as(RDFList.class);
0835:
0836:                    // create a list of foos
0837:                    Resource[] r0 = new Resource[] {
0838:                            m.createResource(NS + "a"), // canonical
0839:                            m.createResource(NS + "b"),
0840:                            m.createResource(NS + "c"),
0841:                            m.createResource(NS + "d"),
0842:                            m.createResource(NS + "e") };
0843:                    Resource[] r1 = new Resource[] {
0844:                            m.createResource(NS + "a"), // same
0845:                            m.createResource(NS + "b"),
0846:                            m.createResource(NS + "c"),
0847:                            m.createResource(NS + "d"),
0848:                            m.createResource(NS + "e") };
0849:                    Resource[] r2 = new Resource[] {
0850:                            m.createResource(NS + "a"), // one shorter
0851:                            m.createResource(NS + "b"),
0852:                            m.createResource(NS + "c"),
0853:                            m.createResource(NS + "d") };
0854:                    Resource[] r3 = new Resource[] {
0855:                            m.createResource(NS + "a"), // elements swapped
0856:                            m.createResource(NS + "b"),
0857:                            m.createResource(NS + "d"),
0858:                            m.createResource(NS + "c"),
0859:                            m.createResource(NS + "e") };
0860:                    Resource[] r4 = new Resource[] {
0861:                            m.createResource(NS + "a"), // different name
0862:                            m.createResource(NS + "b"),
0863:                            m.createResource(NS + "c"),
0864:                            m.createResource(NS + "D"),
0865:                            m.createResource(NS + "e") };
0866:
0867:                    Object[][] testSpec = new Object[][] {
0868:                            { r0, r1, Boolean.TRUE },
0869:                            { r0, r2, Boolean.FALSE },
0870:                            { r0, r3, Boolean.FALSE },
0871:                            { r0, r4, Boolean.FALSE },
0872:                            { r1, r2, Boolean.FALSE },
0873:                            { r1, r3, Boolean.FALSE },
0874:                            { r1, r4, Boolean.FALSE },
0875:                            { r2, r3, Boolean.FALSE },
0876:                            { r2, r4, Boolean.FALSE }, };
0877:
0878:                    for (int i = 0; i < testSpec.length; i++) {
0879:                        RDFList l0 = nilList.append(Arrays.asList(
0880:                                (Object[]) testSpec[i][0]).iterator());
0881:                        RDFList l1 = nilList.append(Arrays.asList(
0882:                                (Object[]) testSpec[i][1]).iterator());
0883:                        boolean expected = ((Boolean) testSpec[i][2])
0884:                                .booleanValue();
0885:
0886:                        assertEquals(
0887:                                "sameListAs testSpec[" + i + "] incorrect",
0888:                                expected, l0.sameListAs(l1));
0889:                        assertEquals("sameListAs testSpec[" + i
0890:                                + "] (swapped) incorrect", expected, l1
0891:                                .sameListAs(l0));
0892:                    }
0893:                }
0894:            }
0895:
0896:            protected static class ListSubclassTest extends ListTest {
0897:                public ListSubclassTest() {
0898:                    super ("ListSubClassTest");
0899:                }
0900:
0901:                public void runTest() {
0902:                    Model m = ModelFactory.createDefaultModel();
0903:
0904:                    String NS = "http://example.org/test#";
0905:                    Resource a = m.createResource(NS + "a");
0906:                    Resource b = m.createResource(NS + "b");
0907:
0908:                    Resource cell0 = m.createResource();
0909:                    Resource cell1 = m.createResource();
0910:                    cell0.addProperty(RDF.first, a);
0911:                    cell0.addProperty(RDF.rest, cell1);
0912:                    cell1.addProperty(RDF.first, b);
0913:                    cell1.addProperty(RDF.rest, RDF.nil);
0914:
0915:                    UserList ul = new UserListImpl(cell0.asNode(), (EnhGraph) m);
0916:
0917:                    assertEquals("User list length ", 2, ul.size());
0918:                    assertEquals("head of user list ", a, ul.getHead());
0919:
0920:                    RDFList l = (RDFList) ul.as(RDFList.class);
0921:                    assertNotNull(
0922:                            "RDFList facet of user-defined list subclass", l);
0923:                }
0924:            }
0925:
0926:            /** A simple extension to RDFList to test user-subclassing of RDFList */
0927:            protected static interface UserList extends RDFList {
0928:            }
0929:
0930:            /** Impl of a simple extension to RDFList to test user-subclassing of RDFList */
0931:            protected static class UserListImpl extends RDFListImpl implements 
0932:                    UserList {
0933:                public UserListImpl(Node n, EnhGraph g) {
0934:                    super (n, g);
0935:                }
0936:            }
0937:
0938:            protected static class UserDefinedListTest extends ListTest {
0939:                public UserDefinedListTest() {
0940:                    super ("UserDefinedListTest");
0941:                }
0942:
0943:                public void runTest() {
0944:                    BuiltinPersonalities.model.add(UserDefList.class,
0945:                            UserDefListImpl.factory);
0946:
0947:                    Model m = ModelFactory.createDefaultModel();
0948:
0949:                    String NS = "http://example.org/test#";
0950:                    Resource a = m.createResource(NS + "a");
0951:                    Resource b = m.createResource(NS + "b");
0952:
0953:                    Resource empty = m.createResource(UserDefListImpl.NIL
0954:                            .getURI());
0955:                    UserDefList ul = (UserDefList) empty.as(UserDefList.class);
0956:                    assertNotNull("UserList facet of empty list", ul);
0957:
0958:                    UserDefList ul0 = (UserDefList) ul.cons(b);
0959:                    ul0 = (UserDefList) ul0.cons(a);
0960:                    assertEquals("should be length 2", 2, ul0.size());
0961:                    assertTrue("first statement", m.contains(ul0,
0962:                            UserDefListImpl.FIRST, a));
0963:                }
0964:            }
0965:
0966:            protected static interface UserDefList extends RDFList {
0967:            }
0968:
0969:            protected static class UserDefListImpl extends RDFListImpl
0970:                    implements  UserDefList {
0971:                public static final String NS = "http://example.org/testlist#";
0972:                public static final Property FIRST = ResourceFactory
0973:                        .createProperty(NS + "first");
0974:                public static final Property REST = ResourceFactory
0975:                        .createProperty(NS + "rest");
0976:                public static final Resource NIL = ResourceFactory
0977:                        .createResource(NS + "nil");
0978:                public static final Resource LIST = ResourceFactory
0979:                        .createResource(NS + "List");
0980:
0981:                /**
0982:                 * A factory for generating UserDefList facets from nodes in enhanced graphs.
0983:                 */
0984:                public static Implementation factory = new Implementation() {
0985:                    public EnhNode wrap(Node n, EnhGraph eg) {
0986:                        if (canWrap(n, eg)) {
0987:                            UserDefListImpl impl = new UserDefListImpl(n, eg);
0988:
0989:                            Model m = impl.getModel();
0990:                            impl.m_listFirst = (Property) FIRST.inModel(m);
0991:                            impl.m_listRest = (Property) REST.inModel(m);
0992:                            impl.m_listNil = (Resource) NIL.inModel(m);
0993:                            impl.m_listType = (Resource) LIST.inModel(m);
0994:
0995:                            return impl;
0996:                        } else {
0997:                            throw new JenaException("Cannot convert node " + n
0998:                                    + " to UserDefList");
0999:                        }
1000:                    }
1001:
1002:                    public boolean canWrap(Node node, EnhGraph eg) {
1003:                        Graph g = eg.asGraph();
1004:
1005:                        return node.equals(NIL.asNode())
1006:                                || g.contains(node, FIRST.asNode(), Node.ANY)
1007:                                || g.contains(node, REST.asNode(), Node.ANY)
1008:                                || g.contains(node, RDF.type.asNode(), LIST
1009:                                        .asNode());
1010:                    }
1011:                };
1012:
1013:                /** This method returns the Java class object that defines which abstraction facet is presented */
1014:                public Class listAbstractionClass() {
1015:                    return UserDefList.class;
1016:                }
1017:
1018:                public UserDefListImpl(Node n, EnhGraph g) {
1019:                    super (n, g);
1020:                }
1021:
1022:            }
1023:
1024:        }
1025:
1026:        /*
1027:         (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
1028:         All rights reserved.
1029:
1030:         Redistribution and use in source and binary forms, with or without
1031:         modification, are permitted provided that the following conditions
1032:         are met:
1033:
1034:         1. Redistributions of source code must retain the above copyright
1035:         notice, this list of conditions and the following disclaimer.
1036:
1037:         2. Redistributions in binary form must reproduce the above copyright
1038:         notice, this list of conditions and the following disclaimer in the
1039:         documentation and/or other materials provided with the distribution.
1040:
1041:         3. The name of the author may not be used to endorse or promote products
1042:         derived from this software without specific prior written permission.
1043:
1044:         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1045:         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1046:         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1047:         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1048:         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1049:         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1050:         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1051:         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1052:         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1053:         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1054:         */
w_w_w_.___ja___v__a__2___s___.___co___m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.