0001: /*
0002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
0003: [See end of file]
0004: $Id: AbstractTestGraph.java,v 1.79 2008/01/02 12:05:31 andy_seaborne Exp $i
0005: */
0006:
0007: package com.hp.hpl.jena.graph.test;
0008:
0009: import com.hp.hpl.jena.util.CollectionFactory;
0010: import com.hp.hpl.jena.util.iterator.*;
0011: import com.hp.hpl.jena.graph.*;
0012: import com.hp.hpl.jena.graph.query.*;
0013: import com.hp.hpl.jena.rdf.model.Model;
0014: import com.hp.hpl.jena.rdf.model.ModelFactory;
0015: import com.hp.hpl.jena.shared.*;
0016:
0017: import java.io.FileInputStream;
0018: import java.io.FileNotFoundException;
0019: import java.util.*;
0020:
0021: /**
0022: AbstractTestGraph provides a bunch of basic tests for something that
0023: purports to be a Graph. The abstract method getGraph must be overridden
0024: in subclasses to deliver a Graph of interest.
0025:
0026: @author kers
0027: */
0028: public/* abstract */class AbstractTestGraph extends GraphTestBase {
0029: public AbstractTestGraph(String name) {
0030: super (name);
0031: }
0032:
0033: /**
0034: Returns a Graph to take part in the test. Must be overridden in
0035: a subclass.
0036: */
0037: // public abstract Graph getGraph();
0038: public Graph getGraph() {
0039: return Factory.createGraphMem();
0040: }
0041:
0042: public Graph getGraphWith(String facts) {
0043: Graph g = getGraph();
0044: graphAdd(g, facts);
0045: return g;
0046: }
0047:
0048: public void testCloseSetsIsClosed() {
0049: Graph g = getGraph();
0050: assertFalse("unclosed Graph shouild not be isClosed()", g
0051: .isClosed());
0052: g.close();
0053: assertTrue("closed Graph should be isClosed()", g.isClosed());
0054: }
0055:
0056: /**
0057: This test case was generated by Ian and was caused by GraphMem
0058: not keeping up with changes to the find interface.
0059: */
0060: public void testFindAndContains() {
0061: Graph g = getGraph();
0062: Node r = NodeCreateUtils.create("r"), s = NodeCreateUtils
0063: .create("s"), p = NodeCreateUtils.create("P");
0064: g.add(Triple.create(r, p, s));
0065: assertTrue(g.contains(r, p, Node.ANY));
0066: assertEquals(1, g.find(r, p, Node.ANY).toList().size());
0067: }
0068:
0069: public void testRepeatedSubjectDoesNotConceal() {
0070: Graph g = getGraphWith("s P o; s Q r");
0071: assertTrue(g.contains(triple("s P o")));
0072: assertTrue(g.contains(triple("s Q r")));
0073: assertTrue(g.contains(triple("?? P o")));
0074: assertTrue(g.contains(triple("?? Q r")));
0075: assertTrue(g.contains(triple("?? P ??")));
0076: assertTrue(g.contains(triple("?? Q ??")));
0077: }
0078:
0079: public void testFindByFluidTriple() {
0080: Graph g = getGraphWith("x y z ");
0081: Set expect = tripleSet("x y z");
0082: assertEquals(expect, g.find(triple("?? y z")).toSet());
0083: assertEquals(expect, g.find(triple("x ?? z")).toSet());
0084: assertEquals(expect, g.find(triple("x y ??")).toSet());
0085: }
0086:
0087: public void testContainsConcrete() {
0088: Graph g = getGraphWith("s P o; _x _R _y; x S 0");
0089: assertTrue(g.contains(triple("s P o")));
0090: assertTrue(g.contains(triple("_x _R _y")));
0091: assertTrue(g.contains(triple("x S 0")));
0092: /* */
0093: assertFalse(g.contains(triple("s P Oh")));
0094: assertFalse(g.contains(triple("S P O")));
0095: assertFalse(g.contains(triple("s p o")));
0096: assertFalse(g.contains(triple("_x _r _y")));
0097: assertFalse(g.contains(triple("x S 1")));
0098: }
0099:
0100: public void testContainsFluid() {
0101: Graph g = getGraphWith("x R y; a P b");
0102: assertTrue(g.contains(triple("?? R y")));
0103: assertTrue(g.contains(triple("x ?? y")));
0104: assertTrue(g.contains(triple("x R ??")));
0105: assertTrue(g.contains(triple("?? P b")));
0106: assertTrue(g.contains(triple("a ?? b")));
0107: assertTrue(g.contains(triple("a P ??")));
0108: assertTrue(g.contains(triple("?? R y")));
0109: /* */
0110: assertFalse(g.contains(triple("?? R b")));
0111: assertFalse(g.contains(triple("a ?? y")));
0112: assertFalse(g.contains(triple("x P ??")));
0113: assertFalse(g.contains(triple("?? R x")));
0114: assertFalse(g.contains(triple("x ?? R")));
0115: assertFalse(g.contains(triple("a S ??")));
0116: }
0117:
0118: /**
0119: Check that contains respects by-value semantics.
0120: */
0121: public void testContainsByValue() {
0122: if (getGraph().getCapabilities().handlesLiteralTyping()) { // TODO fix the adhocness of this
0123: Graph g1 = getGraphWith("x P '1'xsd:integer");
0124: assertTrue(g1.contains(triple("x P '01'xsd:int")));
0125: //
0126: Graph g2 = getGraphWith("x P '1'xsd:int");
0127: assertTrue(g2.contains(triple("x P '1'xsd:integer")));
0128: //
0129: Graph g3 = getGraphWith("x P '123'xsd:string");
0130: assertTrue(g3.contains(triple("x P '123'")));
0131: }
0132: }
0133:
0134: public void testMatchLanguagedLiteralCaseInsensitive() {
0135: Graph m = graphWith("a p 'chat'en");
0136:
0137: // TODO: should be Graph m = getGraphWith( "a p 'chat'en" );
0138: if (m.getCapabilities().handlesLiteralTyping()) {
0139: Node chaten = node("'chat'en"), chatEN = node("'chat'EN");
0140: assertDiffer(chaten, chatEN);
0141: assertTrue(chaten.sameValueAs(chatEN));
0142: assertEquals(chaten.getIndexingValue(), chatEN
0143: .getIndexingValue());
0144: assertEquals(1, m.find(Node.ANY, Node.ANY, chaten).toList()
0145: .size());
0146: assertEquals(1, m.find(Node.ANY, Node.ANY, chatEN).toList()
0147: .size());
0148: }
0149: }
0150:
0151: public void testMatchBothLanguagedLiteralsCaseInsensitive() {
0152: Graph m = graphWith("a p 'chat'en; a p 'chat'EN");
0153:
0154: // TODO: should be Graph m = getGraphWith( "a p 'chat'en; a p 'chat'EN" );
0155: if (m.getCapabilities().handlesLiteralTyping()) {
0156: Node chaten = node("'chat'en"), chatEN = node("'chat'EN");
0157: assertDiffer(chaten, chatEN);
0158: assertTrue(chaten.sameValueAs(chatEN));
0159: assertEquals(chaten.getIndexingValue(), chatEN
0160: .getIndexingValue());
0161: assertEquals(2, m.find(Node.ANY, Node.ANY, chaten).toList()
0162: .size());
0163: assertEquals(2, m.find(Node.ANY, Node.ANY, chatEN).toList()
0164: .size());
0165: }
0166: }
0167:
0168: public void testNoMatchAgainstUnlanguagesLiteral() {
0169: Graph m = graphWith("a p 'chat'en; a p 'chat'");
0170: // TODO: should be Graph m = getGraphWith( "a p 'chat'en; a p 'chat'" );
0171: if (m.getCapabilities().handlesLiteralTyping()) {
0172: Node chaten = node("'chat'en"), chatEN = node("'chat'EN");
0173: assertDiffer(chaten, chatEN);
0174: assertTrue(chaten.sameValueAs(chatEN));
0175: assertEquals(chaten.getIndexingValue(), chatEN
0176: .getIndexingValue());
0177: assertEquals(1, m.find(Node.ANY, Node.ANY, chaten).toList()
0178: .size());
0179: assertEquals(1, m.find(Node.ANY, Node.ANY, chatEN).toList()
0180: .size());
0181: }
0182: }
0183:
0184: /**
0185: test isEmpty - moved from the QueryHandler code.
0186: */
0187: public void testIsEmpty() {
0188: Graph g = getGraph();
0189: if (canBeEmpty(g)) {
0190: assertTrue(g.isEmpty());
0191: g.add(Triple.create("S P O"));
0192: assertFalse(g.isEmpty());
0193: g.add(Triple.create("A B C"));
0194: assertFalse(g.isEmpty());
0195: g.add(Triple.create("S P O"));
0196: assertFalse(g.isEmpty());
0197: g.delete(Triple.create("S P O"));
0198: assertFalse(g.isEmpty());
0199: g.delete(Triple.create("A B C"));
0200: assertTrue(g.isEmpty());
0201: }
0202: }
0203:
0204: public void testAGraph() {
0205: String title = this .getClass().getName();
0206: Graph g = getGraph();
0207: int baseSize = g.size();
0208: graphAdd(g, "x R y; p S q; a T b");
0209: /* */
0210: assertContainsAll(title + ": simple graph", g,
0211: "x R y; p S q; a T b");
0212: assertEquals(title + ": size", baseSize + 3, g.size());
0213: graphAdd(g,
0214: "spindizzies lift cities; Diracs communicate instantaneously");
0215: assertEquals(title + ": size after adding", baseSize + 5, g
0216: .size());
0217: g.delete(triple("x R y"));
0218: g.delete(triple("a T b"));
0219: assertEquals(title + ": size after deleting", baseSize + 3, g
0220: .size());
0221: assertContainsAll(title + ": modified simple graph", g,
0222: "p S q; spindizzies lift cities; Diracs communicate instantaneously");
0223: assertOmitsAll(title + ": modified simple graph", g,
0224: "x R y; a T b");
0225: /* */
0226: ClosableIterator it = g.find(Node.ANY, node("lift"), Node.ANY);
0227: assertTrue(title + ": finds some triple(s)", it.hasNext());
0228: assertEquals(title + ": finds a 'lift' triple",
0229: triple("spindizzies lift cities"), it.next());
0230: assertFalse(title + ": finds exactly one triple", it.hasNext());
0231: it.close();
0232: }
0233:
0234: // public void testStuff()
0235: // {
0236: //// testAGraph( "StoreMem", new GraphMem() );
0237: //// testAGraph( "StoreMemBySubject", new GraphMem() );
0238: //// String [] empty = new String [] {};
0239: //// Graph g = graphWith( "x R y; p S q; a T b" );
0240: //// /* */
0241: //// assertContainsAll( "simple graph", g, "x R y; p S q; a T b" );
0242: //// graphAdd( g, "spindizzies lift cities; Diracs communicate instantaneously" );
0243: //// g.delete( triple( "x R y" ) );
0244: //// g.delete( triple( "a T b" ) );
0245: //// assertContainsAll( "modified simple graph", g, "p S q; spindizzies lift cities; Diracs communicate instantaneously" );
0246: //// assertOmitsAll( "modified simple graph", g, "x R y; a T b" );
0247: // }
0248:
0249: /**
0250: Test that Graphs have transaction support methods, and that if they fail
0251: on some g they fail because they do not support the operation.
0252: */
0253: public void testHasTransactions() {
0254: Graph g = getGraph();
0255: TransactionHandler th = g.getTransactionHandler();
0256: th.transactionsSupported();
0257: try {
0258: th.begin();
0259: } catch (UnsupportedOperationException x) {
0260: }
0261: try {
0262: th.abort();
0263: } catch (UnsupportedOperationException x) {
0264: }
0265: try {
0266: th.commit();
0267: } catch (UnsupportedOperationException x) {
0268: }
0269: /* */
0270: Command cmd = new Command() {
0271: public Object execute() {
0272: return null;
0273: }
0274: };
0275: try {
0276: th.executeInTransaction(cmd);
0277: } catch (UnsupportedOperationException x) {
0278: }
0279: }
0280:
0281: public void testExecuteInTransactionCatchesThrowable() {
0282: Graph g = getGraph();
0283: TransactionHandler th = g.getTransactionHandler();
0284: if (th.transactionsSupported()) {
0285: Command cmd = new Command() {
0286: public Object execute() throws Error {
0287: throw new Error();
0288: }
0289: };
0290: try {
0291: th.executeInTransaction(cmd);
0292: } catch (JenaException x) {
0293: }
0294: }
0295: }
0296:
0297: static final Triple[] tripleArray = tripleArray("S P O; A R B; X Q Y");
0298:
0299: static final List tripleList = Arrays
0300: .asList(tripleArray("i lt j; p equals q"));
0301:
0302: static final Triple[] setTriples = tripleArray("scissors cut paper; paper wraps stone; stone breaks scissors");
0303:
0304: static final Set tripleSet = CollectionFactory
0305: .createHashedSet(Arrays.asList(setTriples));
0306:
0307: public void testBulkUpdate() {
0308: Graph g = getGraph();
0309: BulkUpdateHandler bu = g.getBulkUpdateHandler();
0310: Graph items = graphWith("pigs might fly; dead can dance");
0311: int initialSize = g.size();
0312: /* */
0313: bu.add(tripleArray);
0314: testContains(g, tripleArray);
0315: testOmits(g, tripleList);
0316: /* */
0317: bu.add(tripleList);
0318: testContains(g, tripleList);
0319: testContains(g, tripleArray);
0320: /* */
0321: bu.add(tripleSet.iterator());
0322: testContains(g, tripleSet.iterator());
0323: testContains(g, tripleList);
0324: testContains(g, tripleArray);
0325: /* */
0326: bu.add(items);
0327: testContains(g, items);
0328: testContains(g, tripleSet.iterator());
0329: testContains(g, tripleArray);
0330: testContains(g, tripleList);
0331: /* */
0332: bu.delete(tripleArray);
0333: testOmits(g, tripleArray);
0334: testContains(g, tripleList);
0335: testContains(g, tripleSet.iterator());
0336: testContains(g, items);
0337: /* */
0338: bu.delete(tripleSet.iterator());
0339: testOmits(g, tripleSet.iterator());
0340: testOmits(g, tripleArray);
0341: testContains(g, tripleList);
0342: testContains(g, items);
0343: /* */
0344: bu.delete(items);
0345: testOmits(g, tripleSet.iterator());
0346: testOmits(g, tripleArray);
0347: testContains(g, tripleList);
0348: testOmits(g, items);
0349: /* */
0350: bu.delete(tripleList);
0351: assertEquals("graph has original size", initialSize, g.size());
0352: }
0353:
0354: public void testBulkAddWithReification() {
0355: testBulkAddWithReification(false);
0356: testBulkAddWithReification(true);
0357: }
0358:
0359: public void testBulkAddWithReificationPreamble() {
0360: Graph g = getGraph();
0361: xSPO(g.getReifier());
0362: assertFalse(getReificationTriples(g.getReifier()).isEmpty());
0363: }
0364:
0365: public void testBulkAddWithReification(boolean withReifications) {
0366: Graph graphToUpdate = getGraph();
0367: BulkUpdateHandler bu = graphToUpdate.getBulkUpdateHandler();
0368: Graph graphToAdd = graphWith("pigs might fly; dead can dance");
0369: Reifier updatedReifier = graphToUpdate.getReifier();
0370: Reifier addedReifier = graphToAdd.getReifier();
0371: xSPOyXYZ(addedReifier);
0372: bu.add(graphToAdd, withReifications);
0373: assertIsomorphic(
0374: withReifications ? getReificationTriples(addedReifier)
0375: : graphWith(""),
0376: getReificationTriples(updatedReifier));
0377: }
0378:
0379: protected void xSPOyXYZ(Reifier r) {
0380: xSPO(r);
0381: r.reifyAs(NodeCreateUtils.create("y"), Triple.create("X Y Z"));
0382: }
0383:
0384: protected void aABC(Reifier r) {
0385: r.reifyAs(NodeCreateUtils.create("a"), Triple.create("A B C"));
0386: }
0387:
0388: protected void xSPO(Reifier r) {
0389: r.reifyAs(NodeCreateUtils.create("x"), Triple.create("S P O"));
0390: }
0391:
0392: public void testRemove() {
0393: testRemove("?? ?? ??", "?? ?? ??");
0394: testRemove("S ?? ??", "S ?? ??");
0395: testRemove("S ?? ??", "?? P ??");
0396: testRemove("S ?? ??", "?? ?? O");
0397: testRemove("?? P ??", "S ?? ??");
0398: testRemove("?? P ??", "?? P ??");
0399: testRemove("?? P ??", "?? ?? O");
0400: testRemove("?? ?? O", "S ?? ??");
0401: testRemove("?? ?? O", "?? P ??");
0402: testRemove("?? ?? O", "?? ?? O");
0403: }
0404:
0405: public void testRemove(String findRemove, String findCheck) {
0406: Graph g = getGraphWith("S P O");
0407: ExtendedIterator it = g.find(Triple.create(findRemove));
0408: try {
0409: it.next();
0410: it.remove();
0411: it.close();
0412: assertEquals("remove with " + findRemove + ":", 0, g.size());
0413: assertFalse(g.contains(Triple.create(findCheck)));
0414: } catch (UnsupportedOperationException e) {
0415: it.close();
0416: assertFalse(g.getCapabilities().iteratorRemoveAllowed());
0417: }
0418: it.close();
0419: }
0420:
0421: public void testBulkRemoveWithReification() {
0422: testBulkUpdateRemoveWithReification(true);
0423: testBulkUpdateRemoveWithReification(false);
0424: }
0425:
0426: public void testBulkUpdateRemoveWithReification(
0427: boolean withReifications) {
0428: Graph g = getGraph();
0429: BulkUpdateHandler bu = g.getBulkUpdateHandler();
0430: Graph items = graphWith("pigs might fly; dead can dance");
0431: Reifier gr = g.getReifier(), ir = items.getReifier();
0432: xSPOyXYZ(ir);
0433: xSPO(gr);
0434: aABC(gr);
0435: bu.delete(items, withReifications);
0436: Graph answer = graphWith("");
0437: Reifier ar = answer.getReifier();
0438: if (withReifications)
0439: aABC(ar);
0440: else {
0441: xSPO(ar);
0442: aABC(ar);
0443: }
0444: assertIsomorphic(getReificationTriples(ar),
0445: getReificationTriples(gr));
0446: }
0447:
0448: public void testHasCapabilities() {
0449: Graph g = getGraph();
0450: Capabilities c = g.getCapabilities();
0451: boolean sa = c.sizeAccurate();
0452: boolean aaSome = c.addAllowed();
0453: boolean aaAll = c.addAllowed(true);
0454: boolean daSome = c.deleteAllowed();
0455: boolean daAll = c.deleteAllowed(true);
0456: boolean cbe = c.canBeEmpty();
0457: }
0458:
0459: public void testFind() {
0460: Graph g = getGraph();
0461: graphAdd(g, "S P O");
0462: assertDiffer(new HashSet(), g
0463: .find(Node.ANY, Node.ANY, Node.ANY).toSet());
0464: assertDiffer(new HashSet(), g.find(Triple.ANY).toSet());
0465: }
0466:
0467: protected boolean canBeEmpty(Graph g) {
0468: return g.isEmpty();
0469: }
0470:
0471: public void testEventRegister() {
0472: Graph g = getGraph();
0473: GraphEventManager gem = g.getEventManager();
0474: assertSame(gem, gem.register(new RecordingListener()));
0475: }
0476:
0477: /**
0478: Test that we can safely unregister a listener that isn't registered.
0479: */
0480: public void testEventUnregister() {
0481: getGraph().getEventManager().unregister(L);
0482: }
0483:
0484: /**
0485: Handy triple for test purposes.
0486: */
0487: protected Triple SPO = Triple.create("S P O");
0488: protected RecordingListener L = new RecordingListener();
0489:
0490: /**
0491: Utility: get a graph, register L with its manager, return the graph.
0492: */
0493: protected Graph getAndRegister(GraphListener gl) {
0494: Graph g = getGraph();
0495: g.getEventManager().register(gl);
0496: return g;
0497: }
0498:
0499: public void testAddTriple() {
0500: Graph g = getAndRegister(L);
0501: g.add(SPO);
0502: L.assertHas(new Object[] { "add", g, SPO });
0503: }
0504:
0505: public void testDeleteTriple() {
0506: Graph g = getAndRegister(L);
0507: g.delete(SPO);
0508: L.assertHas(new Object[] { "delete", g, SPO });
0509: }
0510:
0511: public void testListSubjects() {
0512: Set emptySubjects = listSubjects(getGraphWith(""));
0513: Graph g = getGraphWith("x P y; y Q z");
0514: assertEquals(nodeSet("x y"), remove(listSubjects(g),
0515: emptySubjects));
0516: g.delete(triple("x P y"));
0517: assertEquals(nodeSet("y"), remove(listSubjects(g),
0518: emptySubjects));
0519: }
0520:
0521: protected Set listSubjects(Graph g) {
0522: return iteratorToSet(g.queryHandler().subjectsFor(Node.ANY,
0523: Node.ANY));
0524: }
0525:
0526: public void testListPredicates() {
0527: Set emptyPredicates = listPredicates(getGraphWith(""));
0528: Graph g = getGraphWith("x P y; y Q z");
0529: assertEquals(nodeSet("P Q"), remove(listPredicates(g),
0530: emptyPredicates));
0531: g.delete(triple("x P y"));
0532: assertEquals(nodeSet("Q"), remove(listPredicates(g),
0533: emptyPredicates));
0534: }
0535:
0536: protected Set listPredicates(Graph g) {
0537: return iteratorToSet(g.queryHandler().predicatesFor(Node.ANY,
0538: Node.ANY));
0539: }
0540:
0541: public void testListObjects() {
0542: Set emptyObjects = listObjects(getGraphWith(""));
0543: Graph g = getGraphWith("x P y; y Q z");
0544: assertEquals(nodeSet("y z"), remove(listObjects(g),
0545: emptyObjects));
0546: g.delete(triple("x P y"));
0547: assertEquals(nodeSet("z"), remove(listObjects(g), emptyObjects));
0548: }
0549:
0550: protected Set listObjects(Graph g) {
0551: return iteratorToSet(g.queryHandler().objectsFor(Node.ANY,
0552: Node.ANY));
0553: }
0554:
0555: /**
0556: Answer a set with all the elements of <code>A</code> except those
0557: in <code>B</code>.
0558: */
0559: private Set remove(Set A, Set B) {
0560: Set result = new HashSet(A);
0561: result.removeAll(B);
0562: return result;
0563: }
0564:
0565: /**
0566: Ensure that triples removed by calling .remove() on the iterator returned by
0567: a find() will generate deletion notifications.
0568: */
0569: public void testEventDeleteByFind() {
0570: Graph g = getAndRegister(L);
0571: if (g.getCapabilities().iteratorRemoveAllowed()) {
0572: Triple toRemove = triple("remove this triple");
0573: g.add(toRemove);
0574: ExtendedIterator rtr = g.find(toRemove);
0575: assertTrue("ensure a(t least) one triple", rtr.hasNext());
0576: rtr.next();
0577: rtr.remove();
0578: rtr.close();
0579: L.assertHas(new Object[] { "add", g, toRemove, "delete", g,
0580: toRemove });
0581: }
0582: }
0583:
0584: public void testTwoListeners() {
0585: RecordingListener L1 = new RecordingListener();
0586: RecordingListener L2 = new RecordingListener();
0587: Graph g = getGraph();
0588: GraphEventManager gem = g.getEventManager();
0589: gem.register(L1).register(L2);
0590: g.add(SPO);
0591: L2.assertHas(new Object[] { "add", g, SPO });
0592: L1.assertHas(new Object[] { "add", g, SPO });
0593: }
0594:
0595: public void testUnregisterWorks() {
0596: Graph g = getGraph();
0597: GraphEventManager gem = g.getEventManager();
0598: gem.register(L).unregister(L);
0599: g.add(SPO);
0600: L.assertHas(new Object[] {});
0601: }
0602:
0603: public void testRegisterTwice() {
0604: Graph g = getAndRegister(L);
0605: g.getEventManager().register(L);
0606: g.add(SPO);
0607: L.assertHas(new Object[] { "add", g, SPO, "add", g, SPO });
0608: }
0609:
0610: public void testUnregisterOnce() {
0611: Graph g = getAndRegister(L);
0612: g.getEventManager().register(L).unregister(L);
0613: g.delete(SPO);
0614: L.assertHas(new Object[] { "delete", g, SPO });
0615: }
0616:
0617: public void testBulkAddArrayEvent() {
0618: Graph g = getAndRegister(L);
0619: Triple[] triples = tripleArray("x R y; a P b");
0620: g.getBulkUpdateHandler().add(triples);
0621: L.assertHas(new Object[] { "add[]", g, triples });
0622: }
0623:
0624: public void testBulkAddList() {
0625: Graph g = getAndRegister(L);
0626: List elems = Arrays
0627: .asList(tripleArray("bells ring loudly; pigs might fly"));
0628: g.getBulkUpdateHandler().add(elems);
0629: L.assertHas(new Object[] { "addList", g, elems });
0630: }
0631:
0632: public void testBulkDeleteArray() {
0633: Graph g = getAndRegister(L);
0634: Triple[] triples = tripleArray("x R y; a P b");
0635: g.getBulkUpdateHandler().delete(triples);
0636: L.assertHas(new Object[] { "delete[]", g, triples });
0637: }
0638:
0639: public void testBulkDeleteList() {
0640: Graph g = getAndRegister(L);
0641: List elems = Arrays
0642: .asList(tripleArray("bells ring loudly; pigs might fly"));
0643: g.getBulkUpdateHandler().delete(elems);
0644: L.assertHas(new Object[] { "deleteList", g, elems });
0645: }
0646:
0647: public void testBulkAddIterator() {
0648: Graph g = getAndRegister(L);
0649: Triple[] triples = tripleArray("I wrote this; you read that; I wrote this");
0650: g.getBulkUpdateHandler().add(asIterator(triples));
0651: L.assertHas(new Object[] { "addIterator", g,
0652: Arrays.asList(triples) });
0653: }
0654:
0655: public void testBulkDeleteIterator() {
0656: Graph g = getAndRegister(L);
0657: Triple[] triples = tripleArray("I wrote this; you read that; I wrote this");
0658: g.getBulkUpdateHandler().delete(asIterator(triples));
0659: L.assertHas(new Object[] { "deleteIterator", g,
0660: Arrays.asList(triples) });
0661: }
0662:
0663: public Iterator asIterator(Triple[] triples) {
0664: return Arrays.asList(triples).iterator();
0665: }
0666:
0667: public void testBulkAddGraph() {
0668: Graph g = getAndRegister(L);
0669: Graph triples = graphWith("this type graph; I type slowly");
0670: g.getBulkUpdateHandler().add(triples);
0671: L.assertHas(new Object[] { "addGraph", g, triples });
0672: }
0673:
0674: public void testBulkDeleteGraph() {
0675: Graph g = getAndRegister(L);
0676: Graph triples = graphWith("this type graph; I type slowly");
0677: g.getBulkUpdateHandler().delete(triples);
0678: L.assertHas(new Object[] { "deleteGraph", g, triples });
0679: }
0680:
0681: public void testGeneralEvent() {
0682: Graph g = getAndRegister(L);
0683: Object value = new int[] {};
0684: g.getEventManager().notifyEvent(g, value);
0685: L.assertHas(new Object[] { "someEvent", g, value });
0686: }
0687:
0688: public void testRemoveAllEvent() {
0689: Graph g = getAndRegister(L);
0690: g.getBulkUpdateHandler().removeAll();
0691: L.assertHas(new Object[] { "someEvent", g,
0692: GraphEvents.removeAll });
0693: }
0694:
0695: public void testRemoveSomeEvent() {
0696: Graph g = getAndRegister(L);
0697: Node S = node("S"), P = node("??"), O = node("??");
0698: g.getBulkUpdateHandler().remove(S, P, O);
0699: Object event = GraphEvents.remove(S, P, O);
0700: L.assertHas(new Object[] { "someEvent", g, event });
0701: }
0702:
0703: /**
0704: * Test that nodes can be found in all triple positions.
0705: * However, testing for literals in subject positions is suppressed
0706: * at present to avoid problems with InfGraphs which try to prevent
0707: * such constructs leaking out to the RDF layer.
0708: */
0709: public void testContainsNode() {
0710: Graph g = getGraph();
0711: graphAdd(g, "a P b; _c _Q _d; a 11 12");
0712: QueryHandler qh = g.queryHandler();
0713: assertTrue(qh.containsNode(node("a")));
0714: assertTrue(qh.containsNode(node("P")));
0715: assertTrue(qh.containsNode(node("b")));
0716: assertTrue(qh.containsNode(node("_c")));
0717: assertTrue(qh.containsNode(node("_Q")));
0718: assertTrue(qh.containsNode(node("_d")));
0719: // assertTrue( qh.containsNode( node( "10" ) ) );
0720: assertTrue(qh.containsNode(node("11")));
0721: assertTrue(qh.containsNode(node("12")));
0722: /* */
0723: assertFalse(qh.containsNode(node("x")));
0724: assertFalse(qh.containsNode(node("_y")));
0725: assertFalse(qh.containsNode(node("99")));
0726: }
0727:
0728: public void testSubjectsFor() {
0729: Graph g = getGraphWith("a P b; a Q c; a P d; b P x; c Q y");
0730: testSameSubjects(g, Node.ANY, Node.ANY);
0731: testSameSubjects(g, node("P"), Node.ANY);
0732: testSameSubjects(g, node("Q"), node("c"));
0733: }
0734:
0735: protected void testSameSubjects(Graph g, Node p, Node o) {
0736: Set bis = iteratorToSet(SimpleQueryHandler.subjectsFor(g, p, o));
0737: Set qhs = iteratorToSet(g.queryHandler().subjectsFor(p, o));
0738: assertEquals(bis, qhs);
0739: }
0740:
0741: public void testListSubjectsNoRemove() {
0742: Graph g = getGraphWith("a P b; b Q c; c R a");
0743: Iterator it = g.queryHandler().subjectsFor(Node.ANY, Node.ANY);
0744: it.next();
0745: try {
0746: it.remove();
0747: fail("listSubjects for " + g.getClass()
0748: + " should not support .remove()");
0749: } catch (UnsupportedOperationException e) {
0750: pass();
0751: }
0752: }
0753:
0754: public void testObjectsFor() {
0755: Graph g = getGraphWith("b P a; c Q a; d P a; x P b; y Q c");
0756: testSameObjects(g, Node.ANY, Node.ANY);
0757: testSameObjects(g, node("P"), Node.ANY);
0758: testSameObjects(g, node("Q"), node("c"));
0759: }
0760:
0761: protected void testSameObjects(Graph g, Node s, Node p) {
0762: Set bis = iteratorToSet(SimpleQueryHandler.objectsFor(g, s, p));
0763: Set qhs = iteratorToSet(g.queryHandler().objectsFor(s, p));
0764: assertEquals(bis, qhs);
0765: }
0766:
0767: public void testListObjectsNoRemove() {
0768: Graph g = getGraphWith("a P b; b Q c; c R a");
0769: Iterator it = g.queryHandler().objectsFor(Node.ANY, Node.ANY);
0770: it.next();
0771: try {
0772: it.remove();
0773: fail("listObjects for " + g.getClass()
0774: + " should not support .remove()");
0775: } catch (UnsupportedOperationException e) {
0776: pass();
0777: }
0778: }
0779:
0780: public void testListObjectNoDuplicates() {
0781: Graph g = getGraphWith("a P 1; b P 1");
0782: int count = 0;
0783: Node one = node("1");
0784: Iterator it = g.queryHandler().objectsFor(Node.ANY, Node.ANY);
0785: while (it.hasNext())
0786: if (it.next().equals(one))
0787: count += 1;
0788: assertEquals(1, count);
0789: }
0790:
0791: public void testPredicatesFor() {
0792: Graph g = getGraphWith("a P b; c Q d; e R f; g P b; h Q i");
0793: testSamePredicates(g, Node.ANY, Node.ANY);
0794: testSamePredicates(g, Node.ANY, node("b"));
0795: testSamePredicates(g, node("g"), Node.ANY);
0796: testSamePredicates(g, node("e"), node("f"));
0797: }
0798:
0799: protected void testSamePredicates(Graph g, Node s, Node o) {
0800: Set bis = iteratorToSet(SimpleQueryHandler.predicatesFor(g, s,
0801: o));
0802: Set qhs = iteratorToSet(g.queryHandler().predicatesFor(s, o));
0803: assertEquals(bis, qhs);
0804: }
0805:
0806: public void testListPredicatesNoRemove() {
0807: Graph g = getGraphWith("a P b; b Q c; c R a");
0808: Iterator it = g.queryHandler()
0809: .predicatesFor(Node.ANY, Node.ANY);
0810: it.next();
0811: try {
0812: it.remove();
0813: fail("listPredicates for " + g.getClass()
0814: + " should not support .remove()");
0815: } catch (UnsupportedOperationException e) {
0816: pass();
0817: }
0818: }
0819:
0820: public void testRemoveAll() {
0821: testRemoveAll("");
0822: testRemoveAll("a R b");
0823: testRemoveAll("c S d; e:ff GGG hhhh; _i J 27; Ell Em 'en'");
0824: }
0825:
0826: public void testRemoveAll(String triples) {
0827: Graph g = getGraph();
0828: graphAdd(g, triples);
0829: g.getBulkUpdateHandler().removeAll();
0830: assertTrue(g.isEmpty());
0831: }
0832:
0833: public void testGetStatisticsHandler() {
0834: Graph g = getGraph();
0835: GraphStatisticsHandler h = g.getStatisticsHandler();
0836: assertSame(h, g.getStatisticsHandler());
0837: }
0838:
0839: /**
0840: Test cases for RemoveSPO(); each entry is a triple (add, remove, result).
0841: <ul>
0842: <li>add - the triples to add to the graph to start with
0843: <li>remove - the pattern to use in the removal
0844: <li>result - the triples that should remain in the graph
0845: </ul>
0846: */
0847: protected String[][] cases = { { "x R y", "x R y", "" },
0848: { "x R y; a P b", "x R y", "a P b" },
0849: { "x R y; a P b", "?? R y", "a P b" },
0850: { "x R y; a P b", "x R ??", "a P b" },
0851: { "x R y; a P b", "x ?? y", "a P b" },
0852: { "x R y; a P b", "?? ?? ??", "" },
0853: { "x R y; a P b; c P d", "?? P ??", "x R y" },
0854: { "x R y; a P b; x S y", "x ?? ??", "a P b" }, };
0855:
0856: /**
0857: Test that remove(s, p, o) works, in the presence of inferencing graphs that
0858: mean emptyness isn't available. This is why we go round the houses and
0859: test that expected ~= initialContent + addedStuff - removed - initialContent.
0860: */
0861: public void testRemoveSPO() {
0862: for (int i = 0; i < cases.length; i += 1)
0863: for (int j = 0; j < 3; j += 1) {
0864: Graph content = getGraph();
0865: Graph baseContent = copy(content);
0866: graphAdd(content, cases[i][0]);
0867: Triple remove = triple(cases[i][1]);
0868: Graph expected = graphWith(cases[i][2]);
0869: content.getBulkUpdateHandler().remove(
0870: remove.getSubject(), remove.getPredicate(),
0871: remove.getObject());
0872: Graph finalContent = remove(copy(content), baseContent);
0873: assertIsomorphic(cases[i][1], expected, finalContent);
0874: }
0875: }
0876:
0877: /** testIsomorphism from file data
0878: * @throws FileNotFoundException */
0879: public void testIsomorphismFile() throws FileNotFoundException {
0880: testIsomorphismXMLFile(1, true);
0881: testIsomorphismXMLFile(2, true);
0882: testIsomorphismXMLFile(3, true);
0883: testIsomorphismXMLFile(4, true);
0884: testIsomorphismXMLFile(5, false);
0885: testIsomorphismXMLFile(6, false);
0886: testIsomorphismNTripleFile(7, true);
0887: testIsomorphismNTripleFile(8, false);
0888:
0889: }
0890:
0891: private void testIsomorphismNTripleFile(int i, boolean result)
0892: throws FileNotFoundException {
0893: testIsomorphismFile(i, "N-TRIPLE", "nt", result);
0894: }
0895:
0896: private void testIsomorphismXMLFile(int i, boolean result)
0897: throws FileNotFoundException {
0898: testIsomorphismFile(i, "RDF/XML", "rdf", result);
0899:
0900: }
0901:
0902: String filebase = "testing/regression/testModelEquals/";
0903:
0904: private void testIsomorphismFile(int n, String lang, String suffix,
0905: boolean result) throws FileNotFoundException {
0906:
0907: Graph g1 = getGraph();
0908: Graph g2 = getGraph();
0909: Model m1 = ModelFactory.createModelForGraph(g1);
0910: Model m2 = ModelFactory.createModelForGraph(g2);
0911: m1.read(new FileInputStream(filebase + Integer.toString(n)
0912: + "-1." + suffix), "http://www.example.org/", lang);
0913: m2.read(new FileInputStream(filebase + Integer.toString(n)
0914: + "-2." + suffix), "http://www.example.org/", lang);
0915: boolean rslt = g1.isIsomorphicWith(g2) == result;
0916: if (!rslt) {
0917: System.out.println("g1:");
0918: m1.write(System.out, "N-TRIPLE");
0919: System.out.println("g2:");
0920: m2.write(System.out, "N-TRIPLE");
0921: }
0922: assertTrue("Isomorphism test failed", rslt);
0923: }
0924:
0925: protected void add(Graph toUpdate, Graph toAdd) {
0926: toUpdate.getBulkUpdateHandler().add(toAdd);
0927: }
0928:
0929: protected Graph remove(Graph toUpdate, Graph toRemove) {
0930: toUpdate.getBulkUpdateHandler().delete(toRemove);
0931: return toUpdate;
0932: }
0933:
0934: protected Graph copy(Graph g) {
0935: Graph result = Factory.createDefaultGraph();
0936: result.getBulkUpdateHandler().add(g);
0937: return result;
0938: }
0939:
0940: protected Graph getClosed() {
0941: Graph result = getGraph();
0942: result.close();
0943: return result;
0944: }
0945:
0946: // public void testClosedDelete()
0947: // {
0948: // try { getClosed().delete( triple( "x R y" ) ); fail( "delete when closed" ); }
0949: // catch (ClosedException c) { /* as required */ }
0950: // }
0951: //
0952: // public void testClosedAdd()
0953: // {
0954: // try { getClosed().add( triple( "x R y" ) ); fail( "add when closed" ); }
0955: // catch (ClosedException c) { /* as required */ }
0956: // }
0957: //
0958: // public void testClosedContainsTriple()
0959: // {
0960: // try { getClosed().contains( triple( "x R y" ) ); fail( "contains[triple] when closed" ); }
0961: // catch (ClosedException c) { /* as required */ }
0962: // }
0963: //
0964: // public void testClosedContainsSPO()
0965: // {
0966: // Node a = Node.ANY;
0967: // try { getClosed().contains( a, a, a ); fail( "contains[SPO] when closed" ); }
0968: // catch (ClosedException c) { /* as required */ }
0969: // }
0970: //
0971: // public void testClosedFindTriple()
0972: // {
0973: // try { getClosed().find( triple( "x R y" ) ); fail( "find [triple] when closed" ); }
0974: // catch (ClosedException c) { /* as required */ }
0975: // }
0976: //
0977: // public void testClosedFindSPO()
0978: // {
0979: // Node a = Node.ANY;
0980: // try { getClosed().find( a, a, a ); fail( "find[SPO] when closed" ); }
0981: // catch (ClosedException c) { /* as required */ }
0982: // }
0983: //
0984: // public void testClosedSize()
0985: // {
0986: // try { getClosed().size(); fail( "size when closed (" + this.getClass() + ")" ); }
0987: // catch (ClosedException c) { /* as required */ }
0988: // }
0989:
0990: }
0991:
0992: /*
0993: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
0994: All rights reserved.
0995:
0996: Redistribution and use in source and binary forms, with or without
0997: modification, are permitted provided that the following conditions
0998: are met:
0999:
1000: 1. Redistributions of source code must retain the above copyright
1001: notice, this list of conditions and the following disclaimer.
1002:
1003: 2. Redistributions in binary form must reproduce the above copyright
1004: notice, this list of conditions and the following disclaimer in the
1005: documentation and/or other materials provided with the distribution.
1006:
1007: 3. The name of the author may not be used to endorse or promote products
1008: derived from this software without specific prior written permission.
1009:
1010: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1011: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1012: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1013: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1014: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1015: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1016: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1017: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1018: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1019: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1020: */
|