0001: /*
0002: * LICENSE INFORMATION
0003: * Copyright 2005-2007 by FZI (http://www.fzi.de).
0004: * Licensed under a BSD license (http://www.opensource.org/licenses/bsd-license.php)
0005: * <OWNER> = Max Völkel
0006: * <ORGANIZATION> = FZI Forschungszentrum Informatik Karlsruhe, Karlsruhe, Germany
0007: * <YEAR> = 2007
0008: *
0009: * Project information at http://semweb4j.org/rdf2go
0010: */
0011: package org.ontoware.rdf2go.model;
0012:
0013: import java.io.IOException;
0014: import java.io.InputStream;
0015: import java.io.InputStreamReader;
0016: import java.util.ArrayList;
0017: import java.util.Iterator;
0018: import java.util.List;
0019:
0020: import junit.framework.TestCase;
0021:
0022: import org.junit.Assert;
0023: import org.junit.Test;
0024: import org.ontoware.aifbcommons.collection.ClosableIterator;
0025: import org.ontoware.rdf2go.ModelFactory;
0026: import org.ontoware.rdf2go.RDF2Go;
0027: import org.ontoware.rdf2go.Reasoning;
0028: import org.ontoware.rdf2go.exception.MalformedQueryException;
0029: import org.ontoware.rdf2go.exception.ModelRuntimeException;
0030: import org.ontoware.rdf2go.exception.ReasoningNotSupportedException;
0031: import org.ontoware.rdf2go.model.impl.DiffImpl;
0032: import org.ontoware.rdf2go.model.impl.TriplePatternImpl;
0033: import org.ontoware.rdf2go.model.node.BlankNode;
0034: import org.ontoware.rdf2go.model.node.DatatypeLiteral;
0035: import org.ontoware.rdf2go.model.node.LanguageTagLiteral;
0036: import org.ontoware.rdf2go.model.node.Node;
0037: import org.ontoware.rdf2go.model.node.PlainLiteral;
0038: import org.ontoware.rdf2go.model.node.Resource;
0039: import org.ontoware.rdf2go.model.node.URI;
0040: import org.ontoware.rdf2go.model.node.Variable;
0041: import org.ontoware.rdf2go.model.node.impl.URIImpl;
0042: import org.ontoware.rdf2go.testdata.TestData;
0043: import org.ontoware.rdf2go.vocabulary.RDF;
0044: import org.ontoware.rdf2go.vocabulary.RDFS;
0045: import org.slf4j.Logger;
0046: import org.slf4j.LoggerFactory;
0047:
0048: public abstract class AbstractModelTest extends TestCase {
0049:
0050: private static final Logger log = LoggerFactory
0051: .getLogger(AbstractModelTest.class);
0052:
0053: public static URI a = new URIImpl("test://test/a", false);
0054:
0055: public static URI b = new URIImpl("test://test/b", false);
0056:
0057: public static URI c = new URIImpl("test://test/c", false);
0058:
0059: public static URI subject = new URIImpl("test://test/a", false);
0060:
0061: public static URI predicate = new URIImpl("test://test/b", false);
0062:
0063: public static URI object = new URIImpl("test://test/c", false);
0064:
0065: public static URI dt = new URIImpl("test://somedata/dt", false);
0066:
0067: /** @return a Model to be used in the test. It must be fresh, e.g. unused */
0068: public abstract ModelFactory getModelFactory();
0069:
0070: @Test
0071: public void testIsOpen() {
0072: assert getModelFactory() != null;
0073: ModelFactory mf = getModelFactory();
0074: assert mf != null;
0075: Model model = mf.createModel();
0076: assert model != null;
0077: model.open();
0078: assertNotNull(model);
0079: assertTrue(model.isOpen());
0080: model.close();
0081: }
0082:
0083: /*
0084: * Class under test for void addStatement(URI, URI, URI)
0085: */
0086: @Test
0087: public void testAddStatementSimple() throws Exception {
0088: Model model = getModelFactory().createModel();
0089: model.open();
0090: model.addStatement(subject, predicate, object);
0091: ClosableIterator<? extends Statement> sit = model
0092: .findStatements(subject, predicate, object);
0093: assertNotNull(sit);
0094: assertTrue(sit.hasNext());
0095: while (sit.hasNext()) {
0096: // should be just one
0097: Statement s = sit.next();
0098: assertEquals(subject, s.getSubject());
0099: assertEquals(predicate, s.getPredicate());
0100: assertEquals(object, s.getObject());
0101: }
0102: sit.close();
0103: model.close();
0104: }
0105:
0106: /*
0107: * Class under test for void addStatement(URI, URI, URI)
0108: */
0109: @Test
0110: public void testAddTwoStatements() throws Exception {
0111: Model model = getModelFactory().createModel();
0112: model.open();
0113: model.addStatement(a, b, "Jim");
0114: model.addStatement(a, c, "Jon");
0115: ClosableIterator<Statement> sit = model.iterator();
0116: assertTrue(sit.hasNext());
0117: int count = 0;
0118: while (sit.hasNext()) {
0119: count++;
0120: @SuppressWarnings("unused")
0121: Statement s;
0122: s = sit.next();
0123: }
0124: assertEquals(2, count);
0125: sit.close(); // redudant
0126: model.close();
0127: }
0128:
0129: /*
0130: * Class under test for void removeStatement(URI, URI, URI) and void
0131: * addStatement(URI, URI, URI)
0132: */
0133: @Test
0134: public void testRemoveStatementSimple() throws Exception {
0135: Model model = getModelFactory().createModel();
0136: model.open();
0137: model = getModelFactory().createModel();
0138: model.open();
0139: model.addStatement(subject, predicate, object);
0140: ClosableIterator<? extends Statement> iter = model
0141: .findStatements(subject, predicate, object);
0142: assertTrue(iter.hasNext());
0143: while (iter.hasNext())
0144: iter.next();
0145: iter.close();
0146:
0147: model.removeStatement(subject, predicate, object);
0148: ClosableIterator<? extends Statement> it = model
0149: .findStatements(subject, predicate, object);
0150: assertFalse(it.hasNext());
0151: it.close();
0152: model.close();
0153: }
0154:
0155: /*
0156: * Class under test for void addStatement(URI, URI, String) with a Literal
0157: * without Language Tag or Data Type
0158: */
0159: @Test
0160: public void testAddStatementURIURIString() throws Exception {
0161: Model model = getModelFactory().createModel();
0162: model.open();
0163: model = getModelFactory().createModel();
0164: model.open();
0165: model.addStatement(subject, predicate, "Test");
0166: ClosableIterator<? extends Statement> sit = model
0167: .findStatements(subject, predicate, model
0168: .createPlainLiteral("Test"));
0169: assertTrue(sit.hasNext());
0170: while (sit.hasNext()) {
0171: // should be just one
0172: Statement s = sit.next();
0173: assertEquals(subject, s.getSubject());
0174: assertEquals(predicate, s.getPredicate());
0175:
0176: assertTrue(s.getObject().equals("Test"));
0177: }
0178: sit.close();
0179: model.close();
0180: }
0181:
0182: /*
0183: * Class under test for void removeStatement(URI, URI, String)
0184: */
0185: @Test
0186: public void testRemoveStatementURIURIString() throws Exception {
0187: Model model = getModelFactory().createModel();
0188: model.open();
0189: model.addStatement(subject, predicate, "Test");
0190:
0191: ClosableIterator<? extends Statement> iter = model
0192: .findStatements(subject, predicate, model
0193: .createPlainLiteral("Test"));
0194: assertTrue(iter.hasNext());
0195: while (iter.hasNext())
0196: iter.next();
0197: iter.close();
0198: model.removeStatement(subject, predicate, "Test");
0199: ClosableIterator<? extends Statement> it = model
0200: .findStatements(subject, predicate, model
0201: .createPlainLiteral("Test"));
0202: assertFalse(it.hasNext());
0203: it.close();
0204: model.close();
0205: }
0206:
0207: /*
0208: * Class under test for void addStatement(URI, URI, String, String)
0209: */
0210: @Test
0211: public void testAddStatementLanguageTaggedLiteral()
0212: throws Exception {
0213: Model model = getModelFactory().createModel();
0214: model.open();
0215: model.addStatement(subject, predicate, "Test", "DE");
0216: ClosableIterator<? extends Statement> sit = model
0217: .findStatements(subject, predicate, Variable.ANY);
0218: assertTrue(sit.hasNext());
0219: Statement s = sit.next();
0220:
0221: assertEquals(subject, s.getSubject());
0222: assertEquals(predicate, s.getPredicate());
0223:
0224: assertTrue("Object is " + s.getObject().getClass()
0225: + ", expected was LanguageTagLiteral",
0226: s.getObject() instanceof LanguageTagLiteral);
0227: LanguageTagLiteral rdflit = (LanguageTagLiteral) s.getObject();
0228: assertEquals("Test", rdflit.getValue());
0229: assertTrue("DE".equalsIgnoreCase(rdflit.getLanguageTag()));
0230: // should be only one iteration
0231: assertFalse(sit.hasNext());
0232: sit.close();
0233: model.close();
0234: }
0235:
0236: /*
0237: * Class under test for void removeStatement(URI, URI, String, String)
0238: */
0239: @Test
0240: public void testRemoveStatementLanguageTaggedLiteral()
0241: throws Exception {
0242: Model model = getModelFactory().createModel();
0243: model.open();
0244: model.addStatement(subject, predicate, "Test", "DE");
0245: assertTrue(model.contains(subject, predicate, model
0246: .createLanguageTagLiteral("Test", "DE")));
0247: ClosableIterator<Statement> iter = model.iterator();
0248: assertTrue(iter.hasNext());
0249: iter.close();
0250: model.removeStatement(subject, predicate, "Test", "DE");
0251: assertFalse(model.contains(subject, predicate, model
0252: .createLanguageTagLiteral("Test", "DE")));
0253: model.close();
0254: }
0255:
0256: /*
0257: * Class under test for void addStatement(URI, URI, String, URI)
0258: */
0259: @Test
0260: public void testAddStatementDatatypedLiteral() throws Exception {
0261: Model model = getModelFactory().createModel();
0262: model.open();
0263: model.addStatement(subject, predicate, "Test", dt);
0264: assertEquals(1, model.size());
0265: ClosableIterator<Statement> sit = model.iterator();
0266: assertNotNull(sit);
0267: assertTrue(sit.hasNext());
0268: while (sit.hasNext()) {
0269: // should be just one
0270: Statement s = sit.next();
0271: assertEquals(subject, s.getSubject());
0272: assertEquals(predicate, s.getPredicate());
0273: Object literalObject = s.getObject();
0274: assertNotNull(literalObject);
0275: assertTrue("literalObject is if type: "
0276: + literalObject.getClass(),
0277: literalObject instanceof DatatypeLiteral);
0278: DatatypeLiteral rdflit = (DatatypeLiteral) s.getObject();
0279: assertEquals("Test", rdflit.getValue());
0280: assertEquals(dt, rdflit.getDatatype());
0281: }
0282: sit.close();
0283: model.close();
0284: }
0285:
0286: /*
0287: * Class under test for void removeStatement(URI, URI, String, URI)
0288: */
0289: @Test
0290: public void testRemoveStatementDatatypedLiteral() throws Exception {
0291: Model model = getModelFactory().createModel();
0292: model.open();
0293: model.addStatement(subject, predicate, "Test", dt);
0294: assertTrue(model.contains(subject, predicate, model
0295: .createDatatypeLiteral("Test", dt)));
0296:
0297: ClosableIterator<Statement> iter = model.iterator();
0298: assertTrue(iter.hasNext());
0299:
0300: while (iter.hasNext())
0301: iter.next();
0302: iter.close();
0303:
0304: model.removeStatement(subject, predicate, "Test", dt);
0305: assertFalse(model.contains(subject, predicate, model
0306: .createDatatypeLiteral("Test", dt)));
0307: model.close();
0308: }
0309:
0310: /*
0311: * Class under test for void addStatement(URI, URI, Object) and
0312: * removeStatement(URI, URI, Object)
0313: */
0314: @Test
0315: public void testAddRemoveStatementBlankNode() throws Exception {
0316: Model model = getModelFactory().createModel();
0317: model.open();
0318:
0319: BlankNode blankNode = model.createBlankNode();
0320:
0321: model.addStatement(subject, predicate, blankNode);
0322: ClosableIterator<? extends Statement> sit = model
0323: .findStatements(subject, predicate, blankNode);
0324: assertTrue(sit.hasNext());
0325: while (sit.hasNext()) {
0326: // should be just one
0327: Statement s = sit.next();
0328: assertEquals(subject, s.getSubject());
0329: assertEquals(predicate, s.getPredicate());
0330: assertEquals(blankNode, s.getObject());
0331: }
0332: sit.close();
0333:
0334: model.removeStatement(subject, predicate, blankNode);
0335: assertFalse(model.contains(subject, predicate, blankNode));
0336: model.close();
0337: }
0338:
0339: /*
0340: * Class under test for void addStatement(Object, URI, URI) and
0341: * removeStatement(Object, URI, URI)
0342: */
0343: @Test
0344: public void testAddRemoveStatementBlankNode2() throws Exception {
0345: Model model = getModelFactory().createModel();
0346: model.open();
0347:
0348: BlankNode blankNode = model.createBlankNode();
0349:
0350: model.addStatement(blankNode, predicate, object);
0351: ClosableIterator<? extends Statement> sit = model
0352: .findStatements(blankNode, predicate, object);
0353: assertTrue(sit.hasNext());
0354: while (sit.hasNext()) {
0355: // should be just one
0356: Statement s = sit.next();
0357: assertNotNull(s);
0358: assertTrue(s.getSubject() instanceof BlankNode);
0359: BlankNode back = (BlankNode) s.getSubject();
0360:
0361: assertTrue(blankNode.equals(back));
0362: assertEquals(blankNode, s.getSubject());
0363: assertEquals(predicate, s.getPredicate());
0364: assertEquals(object, s.getObject());
0365: }
0366: sit.close();
0367: model.removeStatement(blankNode, predicate, object);
0368: assertFalse(model.contains(blankNode, predicate, object));
0369: model.close();
0370: }
0371:
0372: /*
0373: * Class under test for void addStatement(Object, URI, String) and
0374: * removeStatement(Object, URI, String)
0375: */
0376: @Test
0377: public void testAddRemoveStatementObjectURIString()
0378: throws Exception {
0379: Model model = getModelFactory().createModel();
0380: model.open();
0381:
0382: BlankNode blankNode = model.createBlankNode();
0383:
0384: model.addStatement(blankNode, predicate, "Test");
0385: ClosableIterator<? extends Statement> sit = model
0386: .findStatements(new TriplePatternImpl(blankNode,
0387: predicate, "Test"));
0388: assertTrue(sit.hasNext());
0389: while (sit.hasNext()) {
0390: // should be just one
0391: Statement s = sit.next();
0392: assertEquals("blank node equality", blankNode, s
0393: .getSubject());
0394: assertEquals(predicate, s.getPredicate());
0395: assertEquals(s.getObject(), "Test");
0396: }
0397: sit.close();
0398: model.removeStatement(blankNode, predicate, "Test");
0399: ClosableIterator<? extends Statement> it = model
0400: .findStatements(new TriplePatternImpl(blankNode,
0401: predicate, "Test"));
0402: assertFalse(it.hasNext());
0403: it.close();
0404: model.close();
0405: }
0406:
0407: /*
0408: * Class under test for void addStatement(Object, URI, Object) and
0409: * removeStatement(Object, URI, Object)
0410: */
0411: @Test
0412: public void testAddRemoveStatementURIObjectURIObject()
0413: throws Exception {
0414: Model model = getModelFactory().createModel();
0415: model.open();
0416:
0417: BlankNode blankNodeSubject = model.createBlankNode();
0418: BlankNode blankNodeObject = model.createBlankNode();
0419:
0420: model
0421: .addStatement(blankNodeSubject, predicate,
0422: blankNodeObject);
0423: ClosableIterator<? extends Statement> sit = model
0424: .findStatements(blankNodeSubject, predicate,
0425: blankNodeObject);
0426: assertTrue(sit.hasNext());
0427: while (sit.hasNext()) {
0428: // should be just one
0429: Statement s = sit.next();
0430: assertEquals(blankNodeSubject, s.getSubject());
0431: assertEquals(predicate, s.getPredicate());
0432: assertEquals(blankNodeObject, s.getObject());
0433: }
0434: sit.close();
0435: model.removeStatement(blankNodeSubject, predicate,
0436: blankNodeObject);
0437: assertFalse(model.contains(blankNodeSubject, predicate,
0438: blankNodeObject));
0439: model.close();
0440: }
0441:
0442: /*
0443: * Test method for 'org.ontoware.rdf2go.Model.getNewBlankNode()'
0444: */
0445: @Test
0446: public void testGetNewBlankNode() {
0447: Model model = getModelFactory().createModel();
0448: model.open();
0449: int size = 10;
0450: List<BlankNode> bNodes = new ArrayList<BlankNode>(size);
0451: for (int i = 0; i < size; i++) {
0452: bNodes.add(model.createBlankNode());
0453: }
0454:
0455: for (int i = 0; i < size; i++) {
0456: for (int j = i + 1; j < size; j++) {
0457: assertFalse(bNodes.get(i).equals(bNodes.get(j)));
0458: }
0459: }
0460: model.close();
0461: }
0462:
0463: /*
0464: * Test method for
0465: * 'org.ontoware.rdf2go.Model.getStatementWithLiteralAndNoLanguageTag(URI,
0466: * URI, String)'
0467: */
0468: @Test
0469: public void testGetStatementWithLiteralAndNoLanguageTagURIURIString()
0470: throws Exception {
0471: Model model = getModelFactory().createModel();
0472: model.open();
0473: model.addStatement(subject, predicate, "Test", "DE");
0474: model.addStatement(subject, predicate, "Test");
0475:
0476: ClosableIterator<? extends Statement> sit = model
0477: .findStatements(Variable.ANY, predicate, Variable.ANY);
0478: assertTrue(sit.hasNext());
0479: Statement s = sit.next();
0480: assertEquals(s.getSubject(), subject);
0481: assertEquals(s.getPredicate(), predicate);
0482: assertEquals(s.getObject().asLiteral().getValue(), "Test");
0483:
0484: assertTrue(sit.hasNext());
0485: sit.next();
0486: assertFalse(sit.hasNext());
0487: sit.close();
0488: model.close();
0489: }
0490:
0491: /*
0492: * Test method for
0493: * 'org.ontoware.rdf2go.Model.getStatementWithLiteralAndNoLanguageTag(Object,
0494: * URI, String)'
0495: */
0496: @Test
0497: public void testGetStatementWithLiteralAndNoLanguageTagObjectURIString()
0498: throws Exception {
0499: Model model = getModelFactory().createModel();
0500: model.open();
0501: BlankNode blankNodeSubject1 = model.createBlankNode();
0502: BlankNode blankNodeSubject2 = model.createBlankNode();
0503: model.addStatement(blankNodeSubject1, predicate, "Test");
0504: model.addStatement(blankNodeSubject2, predicate, "Test", "DE");
0505:
0506: ClosableIterator<? extends Statement> sit = model
0507: .findStatements(Variable.ANY, predicate, Variable.ANY);
0508: assertTrue(sit.hasNext());
0509: Statement s = sit.next();
0510: // assertEquals(s.getSubject(), blankNodeSubject1);
0511: assertEquals(s.getPredicate(), predicate);
0512: assertEquals(s.getObject().asLiteral().getValue(), "Test");
0513: sit.next();
0514: assertFalse(sit.hasNext());
0515: sit.close();
0516: model.close();
0517: }
0518:
0519: /*
0520: * Test method for 'org.ontoware.rdf2go.Model.query(String)'
0521: */
0522: @Test
0523: public void testQuery() throws Exception {
0524: Model model = getModelFactory().createModel();
0525: model.open();
0526: String query = "PREFIX \t:\t<test://test/>\n"
0527: + "CONSTRUCT { ?s ?p \"Test2\" } WHERE { ?s ?p \"Test2\" }";
0528: BlankNode bNode = model.createBlankNode();
0529: model.addStatement(subject, predicate, "Test1");
0530: model.addStatement(subject, predicate, "Test2");
0531: model.addStatement(bNode, predicate, "Test2");
0532: ClosableIterator<? extends Statement> iter = model
0533: .sparqlConstruct(query).iterator();
0534:
0535: assertTrue(iter.hasNext());
0536: Statement s = iter.next();
0537: assertEquals(predicate, s.getPredicate());
0538: assertEquals(s.getObject(), "Test2");
0539: assertTrue(iter.hasNext());
0540: s = iter.next();
0541: assertEquals(predicate, s.getPredicate());
0542: assertEquals(s.getObject(), "Test2");
0543: assertFalse(iter.hasNext());
0544: iter.close();
0545: model.close();
0546: }
0547:
0548: @Test
0549: public void testSelectQuery() throws Exception {
0550: Model model = getModelFactory().createModel();
0551: model.open();
0552: String query = "PREFIX \t:\t<test://test/>\n"
0553: + "SELECT ?s ?p \n" + "WHERE { ?s ?p \"Test2\" }";
0554: BlankNode bNode = model.createBlankNode();
0555: model.addStatement(subject, predicate, "Test1");
0556: model.addStatement(subject, predicate, "Test2");
0557: model.addStatement(bNode, predicate, "Test2");
0558: QueryResultTable result = model.sparqlSelect(query);
0559: ClosableIterator<QueryRow> it = result.iterator();
0560: assertTrue(it.hasNext());
0561: QueryRow row = it.next();
0562: assertTrue(result.getVariables().size() == 2);
0563: assertEquals(predicate, row.getValue("p"));
0564: assertTrue(subject.equals(row.getValue("s"))
0565: || bNode.equals(row.getValue("s")));
0566: row = it.next();
0567: assertEquals(predicate, row.getValue("p"));
0568: assertTrue(subject.equals(row.getValue("s"))
0569: || bNode.equals(row.getValue("s")));
0570: it.close();
0571: model.close();
0572: }
0573:
0574: @Test
0575: public void testOpenClose() {
0576: Model model = RDF2Go.getModelFactory().createModel();
0577: assertFalse(model.isOpen());
0578: model.open();
0579: assertTrue(model.isOpen());
0580: model.close();
0581: }
0582:
0583: /**
0584: * Check for valid and invalid URIs
0585: *
0586: */
0587: public void testURIs() {
0588: Model model = getModelFactory().createModel();
0589: model.open();
0590: try {
0591: model.createURI("file:///c/my%20documents/blah.pdf");
0592: } catch (ModelRuntimeException e) {
0593: fail();
0594: }
0595: model.close();
0596: }
0597:
0598: @Test
0599: public void testRdfsReasoning()
0600: throws ReasoningNotSupportedException,
0601: ModelRuntimeException {
0602: Model model = getModelFactory().createModel(Reasoning.rdfs);
0603: model.open();
0604:
0605: log.debug("Using internal impl: "
0606: + model.getUnderlyingModelImplementation().getClass());
0607:
0608: // a rdf:type classA
0609: // classA rdfs:subClassOf classB
0610: // -->
0611: // a rdf:type classB
0612:
0613: URI a = model.createURI("urn:test:a");
0614: URI classA = model.createURI("urn:test:classA");
0615: URI classB = model.createURI("urn:test:classB");
0616:
0617: model.addStatement(a, RDF.type, classA);
0618: model.addStatement(classA, RDFS.subClassOf, classB);
0619:
0620: boolean inferencedStatement = model.contains(Variable.ANY,
0621: RDF.type, classB);
0622: if (!inferencedStatement) {
0623: model.dump();
0624: }
0625: assertTrue(inferencedStatement);
0626: model.close();
0627: }
0628:
0629: @Test
0630: public void testRdfsReasoning2()
0631: throws ReasoningNotSupportedException,
0632: ModelRuntimeException {
0633: Model model = getModelFactory().createModel(Reasoning.rdfs);
0634: model.open();
0635:
0636: URI resourceA = new URIImpl("urn:resource:A");
0637: URI resourceB = new URIImpl("urn:resource:B");
0638: URI propertyA = new URIImpl("urn:prop:A");
0639: URI propertyB = new URIImpl("urn:prop:B");
0640: URI propertyC = new URIImpl("urn:prop:C");
0641:
0642: model.addStatement(propertyA, propertyB, propertyC);
0643: model.addStatement(propertyB, RDFS.subPropertyOf,
0644: RDFS.subPropertyOf);
0645: Assert.assertTrue(model.contains(propertyA, RDFS.subPropertyOf,
0646: propertyC));
0647:
0648: model.addStatement(resourceA, propertyA, resourceB);
0649: Assert.assertTrue(model.contains(resourceA, propertyC,
0650: resourceB));
0651:
0652: model.close();
0653: }
0654:
0655: @Test
0656: public void testSimpleQuery1() throws Exception {
0657: Model model = getModelFactory().createModel();
0658: model.open();
0659: URI a = new URIImpl("test://a");
0660: URI b = new URIImpl("test://b");
0661: URI c = new URIImpl("test://c");
0662: model.addStatement(a, b, c);
0663: ClosableIterator<? extends Statement> it = model
0664: .findStatements(a, b, c);
0665: assertTrue(it.hasNext());
0666: it.close();
0667: model.close();
0668: }
0669:
0670: @Test
0671: public void testSimpleQuery() throws Exception {
0672: Model m = getModelFactory().createModel();
0673: m.open();
0674: URI a = new URIImpl("test://a");
0675: URI b = new URIImpl("test://b");
0676: URI c = new URIImpl("test://c");
0677: m.addStatement(a, b, c);
0678: assertEquals(1, m.size());
0679:
0680: ClosableIterator<? extends Statement> it = m.findStatements(
0681: Variable.ANY, b, c);
0682: assertTrue(it.hasNext());
0683: it.close();
0684: m.close();
0685: }
0686:
0687: @Test
0688: public void testDiff_empty() throws ModelRuntimeException {
0689: Model a = getModelFactory().createModel();
0690: a.open();
0691: Model b = getModelFactory().createModel();
0692: b.open();
0693: Diff diff = a.getDiff(b.iterator());
0694: assertFalse(diff.getAdded().iterator().hasNext());
0695: assertFalse(diff.getRemoved().iterator().hasNext());
0696: a.close();
0697: b.close();
0698: }
0699:
0700: @Test
0701: public void testDiff_different() throws ModelRuntimeException {
0702: Model a = getModelFactory().createModel();
0703: a.open();
0704: Model b = getModelFactory().createModel();
0705: b.open();
0706:
0707: a.addStatement(new URIImpl("urn:test:a", false), new URIImpl(
0708: "urn:test:b", false), new URIImpl("urn:test:c", false));
0709: b.addStatement(new URIImpl("urn:test:x", false), new URIImpl(
0710: "urn:test:y", false), new URIImpl("urn:test:z", false));
0711: Diff diff = a.getDiff(b.iterator());
0712: assertTrue(diff.getAdded().iterator().hasNext());
0713: assertTrue(diff.getRemoved().iterator().hasNext());
0714: a.close();
0715: b.close();
0716: }
0717:
0718: @Test
0719: public void testDiff_same() throws ModelRuntimeException {
0720: Model a = getModelFactory().createModel();
0721: a.open();
0722: Model b = getModelFactory().createModel();
0723: b.open();
0724:
0725: a.addStatement(new URIImpl("urn:test:a", false), new URIImpl(
0726: "urn:test:b", false), new URIImpl("urn:test:c", false));
0727:
0728: b.addStatement(new URIImpl("urn:test:a", false), new URIImpl(
0729: "urn:test:b", false), new URIImpl("urn:test:c", false));
0730:
0731: ClosableIterator<Statement> i = a.iterator();
0732: Statement aStmt = i.next();
0733: i.close();
0734: i = b.iterator();
0735: Statement bStmt = i.next();
0736: i.close();
0737:
0738: assertTrue("a statement should be equal to itself", aStmt
0739: .equals(aStmt));
0740: assertEquals(aStmt, aStmt);
0741: assertTrue(aStmt.equals(bStmt));
0742: assertEquals(aStmt, bStmt);
0743:
0744: assertEquals(aStmt.hashCode(), aStmt.hashCode());
0745: assertEquals(aStmt.getSubject().hashCode(), bStmt.getSubject()
0746: .hashCode());
0747: assertEquals(aStmt.getPredicate().hashCode(), bStmt
0748: .getPredicate().hashCode());
0749: assertEquals(aStmt.getObject().hashCode(), bStmt.getObject()
0750: .hashCode());
0751: assertEquals(aStmt.hashCode(), bStmt.hashCode());
0752:
0753: Diff diff = a.getDiff(b.iterator());
0754: assertFalse(diff.getAdded().iterator().hasNext());
0755: assertFalse(diff.getRemoved().iterator().hasNext());
0756:
0757: a.close();
0758: b.close();
0759: }
0760:
0761: @Test
0762: public void testUpdate() {
0763: Model remove = RDF2Go.getModelFactory().createModel();
0764: remove.open();
0765: Model add = RDF2Go.getModelFactory().createModel();
0766: add.open();
0767: add.addStatement(a, b, c);
0768: Diff diff = new DiffImpl(add.iterator(), remove.iterator());
0769: add.close();
0770: remove.close();
0771:
0772: Model model = RDF2Go.getModelFactory().createModel();
0773: model.open();
0774: model.update(diff);
0775:
0776: Assert.assertTrue(model.contains(a, b, c));
0777: }
0778:
0779: @Test
0780: public void testComparable() throws ModelRuntimeException {
0781: Model a = getModelFactory().createModel();
0782: a.open();
0783: Model b = getModelFactory().createModel();
0784: b.open();
0785:
0786: a.addStatement(new URIImpl("urn:test:a", false), new URIImpl(
0787: "urn:test:b", false), new URIImpl("urn:test:c", false));
0788:
0789: ClosableIterator<Statement> i = a.iterator();
0790: Statement aStmt = i.next();
0791: i.close();
0792:
0793: assertEquals(0, aStmt.compareTo(aStmt));
0794:
0795: b.addStatement(new URIImpl("urn:test:a", false), new URIImpl(
0796: "urn:test:b", false), new URIImpl("urn:test:c", false));
0797: i = b.iterator();
0798: Statement bStmt = i.next();
0799: i.close();
0800:
0801: assertEquals(0, aStmt.compareTo(bStmt));
0802: assertEquals(0, bStmt.compareTo(aStmt));
0803:
0804: a.close();
0805: b.close();
0806: }
0807:
0808: @Test
0809: public void testComparableWithDifferent()
0810: throws ModelRuntimeException {
0811: Model a = getModelFactory().createModel();
0812: a.open();
0813: Model b = getModelFactory().createModel();
0814: b.open();
0815:
0816: a.addStatement(new URIImpl("urn:test:a", false), new URIImpl(
0817: "urn:test:b", false), new URIImpl("urn:test:c", false));
0818: Statement aStmt = a.iterator().next();
0819:
0820: assertEquals(0, aStmt.compareTo(aStmt));
0821:
0822: b.addStatement(new URIImpl("urn:test:x", false), new URIImpl(
0823: "urn:test:y", false), new URIImpl("urn:test:z", false));
0824: Statement bStmt = b.iterator().next();
0825:
0826: // one should be positive, one negative, both not 0
0827: assertTrue(aStmt.compareTo(bStmt) * bStmt.compareTo(aStmt) < 0);
0828: a.close();
0829: b.close();
0830: }
0831:
0832: @Test
0833: public void testSparqlSelectWithURIS() throws Exception {
0834: Model m = getModelFactory().createModel(Reasoning.rdfs);
0835: m.open();
0836:
0837: m.removeAll();
0838:
0839: URI hasContent = new URIImpl("prop://hasContent");
0840: URI hasTag = new URIImpl("prop://hasTag");
0841: URI tagSemweb = new URIImpl("tag://semweb");
0842: URI tagPaper = new URIImpl("tag://paper");
0843: URI fileA = new URIImpl("file://a");
0844: URI fileB = new URIImpl("file://b");
0845: URI assignment1 = new URIImpl("ass://1");
0846: URI assignment2 = new URIImpl("ass://2");
0847: URI assignment3 = new URIImpl("ass://1");
0848:
0849: // a = 'paper'
0850: m.addStatement(assignment1, hasTag, tagPaper);
0851: m.addStatement(assignment1, hasContent, fileA);
0852: // a = 'semweb'
0853: m.addStatement(assignment2, hasTag, tagSemweb);
0854: m.addStatement(assignment2, hasContent, fileA);
0855: // b = 'semweb'
0856: m.addStatement(assignment3, hasTag, tagSemweb);
0857: m.addStatement(assignment3, hasContent, fileB);
0858:
0859: QueryResultTable result = m.sparqlSelect("SELECT ?f WHERE "
0860: + "{ ?a <" + hasContent + "> ?f. " + "?a <" + hasTag
0861: + "> <" + tagSemweb + "> . }");
0862:
0863: // expect A and B
0864: ClosableIterator<QueryRow> i = result.iterator();
0865: QueryRow firstSolution = i.next();
0866: assertNotNull(firstSolution);
0867: assertEquals(1, result.getVariables().size());
0868: assertTrue(firstSolution.getValue("f").equals(fileA)
0869: || firstSolution.getValue("f").equals(fileB));
0870: i.close();
0871: m.close();
0872:
0873: }
0874:
0875: @Test
0876: public void testSparqlAsk() throws Exception {
0877: Model m = getModelFactory().createModel(Reasoning.rdfs);
0878: m.open();
0879: URI hasTag = new URIImpl("prop://hasTag");
0880: URI tagSemweb = new URIImpl("tag://semweb");
0881: URI fileA = new URIImpl("file://a");
0882: m.addStatement(fileA, hasTag, tagSemweb);
0883: Assert.assertTrue(m.sparqlAsk("ASK WHERE { " + fileA.toSPARQL()
0884: + " " + hasTag.toSPARQL() + " ?tag . }"));
0885: Assert.assertTrue(m.sparqlAsk("ASK WHERE { " + fileA.toSPARQL()
0886: + " " + " ?prop " + tagSemweb.toSPARQL() + " . }"));
0887: Assert.assertFalse(m.sparqlAsk("ASK WHERE { "
0888: + fileA.toSPARQL() + " " + "<prop://bogus>"
0889: + " ?tag . }"));
0890: Assert.assertTrue(m.sparqlAsk("ASK WHERE { ?s ?p ?o . }"));
0891: m.close();
0892: }
0893:
0894: @Test
0895: public void testSparqlSelectWithStrings() throws Exception {
0896: Model m = getModelFactory().createModel(Reasoning.rdfs);
0897: m.open();
0898:
0899: URI hasContent = new URIImpl("prop://hasContent");
0900: URI hasTag = new URIImpl("prop://hasTag");
0901: String tagSemweb = "semweb";
0902: String tagPaper = "paper";
0903: URI fileA = new URIImpl("file://a");
0904: URI fileB = new URIImpl("file://b");
0905: URI assignment1 = new URIImpl("ass://1");
0906: URI assignment2 = new URIImpl("ass://2");
0907: URI assignment3 = new URIImpl("ass://1");
0908:
0909: // a = 'paper'
0910: m.addStatement(assignment1, hasTag, tagPaper);
0911: m.addStatement(assignment1, hasContent, fileA);
0912: // a = 'semweb'
0913: m.addStatement(assignment2, hasTag, tagSemweb);
0914: m.addStatement(assignment2, hasContent, fileA);
0915: // b = 'semweb'
0916: m.addStatement(assignment3, hasTag, tagSemweb);
0917: m.addStatement(assignment3, hasContent, fileB);
0918:
0919: QueryResultTable result = m.sparqlSelect("SELECT ?f WHERE "
0920: + "{ ?a <" + hasContent + "> ?f. " + "?a <" + hasTag
0921: + "> '" + tagSemweb + "' . }");
0922:
0923: // expect A and B
0924: ClosableIterator<QueryRow> i = result.iterator();
0925: QueryRow firstSolution = i.next();
0926: assertNotNull(firstSolution);
0927: assertEquals(1, result.getVariables().size());
0928: assertTrue(firstSolution.getValue("f").equals(fileA)
0929: || firstSolution.getValue("f").equals(fileB));
0930: i.close();
0931: m.close();
0932: }
0933:
0934: @Test
0935: public void testAsk() throws ModelRuntimeException {
0936: Model m = getModelFactory().createModel(Reasoning.none);
0937: m.open();
0938: URI a = new URIImpl("urn:test:a");
0939: URI b = new URIImpl("urn:test:b");
0940: URI c = new URIImpl("urn:test:c");
0941: m.addStatement(a, b, c);
0942: m.dump();
0943:
0944: assertTrue(m.sparqlAsk("ASK { ?s ?p ?x . }"));
0945: assertTrue(m
0946: .sparqlAsk("ASK { <urn:test:a> <urn:test:b> ?x . }"));
0947: assertTrue(m
0948: .sparqlAsk("ASK { <urn:test:a> <urn:test:b> <urn:test:c> . }"));
0949: m.close();
0950: }
0951:
0952: @Test
0953: public void testReadFromFile() throws ModelRuntimeException,
0954: IOException {
0955: Model model = getModelFactory().createModel(Reasoning.none);
0956: model.open();
0957: assertNotNull(model);
0958: InputStream reader = TestData.getFoafAsStream();
0959: assertNotNull("testdata stream should not be null", reader);
0960: Syntax rdfxml = Syntax.RdfXml;
0961: assertNotNull(rdfxml);
0962: model.readFrom(reader, rdfxml);
0963: model.close();
0964: }
0965:
0966: @Test
0967: public void testStringEncoding() {
0968: Model model = getModelFactory().createModel(Reasoning.none);
0969: model.open();
0970:
0971: log
0972: .debug("create a String that contains each possible unicode value once. May take a while.");
0973: char[] allchars = new char[Character.MAX_VALUE];
0974: for (char i = 0; i < allchars.length; i++) {
0975: allchars[i] = i;
0976: }
0977: String inString = new String(allchars);
0978:
0979: model.addStatement(a, b, inString);
0980:
0981: ClosableIterator<Statement> it = model.iterator();
0982: Statement stmt = it.next();
0983: it.close();
0984: model.close();
0985:
0986: Assert.assertEquals(a, stmt.getSubject());
0987: Assert.assertEquals(b, stmt.getPredicate());
0988: Assert.assertEquals(inString, stmt.getObject().asLiteral()
0989: .getValue());
0990: }
0991:
0992: @Test
0993: public void testWriteRead() throws ModelRuntimeException {
0994:
0995: log.debug("Launching test");
0996:
0997: Model m = RDF2Go.getModelFactory().createModel();
0998: m.open();
0999:
1000: URI konrad = m.createURI("urn:x-example:konrad");
1001: URI kennt = m.createURI("urn:x-example:kennt");
1002: URI max = m.createURI("urn:x-example:max");
1003:
1004: m.addStatement(konrad, kennt, max);
1005:
1006: String queryString = "SELECT ?x WHERE { <" + konrad + "> <"
1007: + kennt + "> ?x}";
1008: QueryResultTable table = m.sparqlSelect(queryString);
1009: ClosableIterator<QueryRow> it = table.iterator();
1010: QueryRow row = it.next();
1011: assertFalse("iterator should have only one result", it
1012: .hasNext());
1013: Node n = row.getValue("x");
1014: assertEquals(n, max);
1015:
1016: m.dump();
1017: }
1018:
1019: /**
1020: * how to make simple sparql queries and cope with the results
1021: */
1022: @Test
1023: public void testGetSelectQueryResult()
1024: throws MalformedQueryException, ModelRuntimeException {
1025:
1026: Model model = RDF2Go.getModelFactory().createModel();
1027: model.open();
1028: QueryResultTable table = model
1029: .sparqlSelect("SELECT ?a ?b ?c WHERE { ?a ?b ?c }");
1030: Iterator<QueryRow> iterator = table.iterator();
1031:
1032: while (iterator.hasNext()) {
1033: QueryRow row = iterator.next();
1034: for (String varname : table.getVariables()) {
1035: @SuppressWarnings("unused")
1036: Node x = row.getValue(varname);
1037: }
1038: }
1039:
1040: }
1041:
1042: /**
1043: * how to write basic model usage operations
1044: */
1045: @Test
1046: public void testBasicUsage() {
1047:
1048: // get a model
1049: Model model = RDF2Go.getModelFactory().createModel();
1050: model.open();
1051:
1052: // add statements
1053:
1054: // the cleanest way
1055: Resource s = model.createURI("urn:test:a");
1056: URI p = model
1057: .createURI("http://www.w3.org/2000/01/rdf-schema#label");
1058: Node o = model.createPlainLiteral("Hello World A");
1059: model.addStatement(s, p, o);
1060:
1061: // a shortcut: built-in URIs for RDF and RDFS
1062: model.addStatement("urn:test:b", RDFS.label, "Hello World B");
1063:
1064: // a shortcut: resource URIs and plain literal objects as strings
1065: model.addStatement("urn:test:c", RDFS.label, "Hello World C");
1066:
1067: // list statements
1068: for (Statement stmt : model)
1069: System.out.println(stmt);
1070:
1071: // query for triple pattern
1072: ClosableIterator<? extends Statement> it = model
1073: .findStatements(s, Variable.ANY, Variable.ANY);
1074: while (it.hasNext()) {
1075: System.out.println(it.next());
1076: }
1077: it.close();
1078:
1079: }
1080:
1081: @Test
1082: public void testReadFromFileWithSyntaxArgument()
1083: throws ModelRuntimeException, IOException {
1084: InputStream stream = TestData.getICALAsStream();
1085: Assert.assertNotNull(stream);
1086: InputStreamReader reader = new InputStreamReader(stream,
1087: "UTF-8");
1088:
1089: Model model = getModelFactory().createModel();
1090: model.open();
1091: model.readFrom(reader, Syntax.RdfXml);
1092:
1093: reader.close();
1094: stream.close();
1095: model.close();
1096: }
1097:
1098: @Test
1099: public void testCheckForValidURI() {
1100: Model model = getModelFactory().createModel();
1101: model.open();
1102: assertFalse(model.isValidURI("ping"));
1103: assertTrue(model.isValidURI("http://i.am.a.uri"));
1104: model.close();
1105: }
1106:
1107: @Test
1108: public void testAutoCommit() throws ModelRuntimeException {
1109: Model model = getModelFactory().createModel();
1110: model.open();
1111:
1112: assertFalse(model.isLocked());
1113: model.lock();
1114: assertTrue(model.isLocked());
1115:
1116: model.addStatement(subject, predicate, "Test", "DE");
1117: model.unlock();
1118: assertFalse(model.isLocked());
1119:
1120: assertTrue(model.contains(subject, predicate, model
1121: .createLanguageTagLiteral("Test", "DE")));
1122:
1123: model.close();
1124: }
1125:
1126: /* assert that language tags are always in lower-case */
1127: @Test
1128: public void testLowerCaseLanguageTag() throws Exception {
1129: Model model = getModelFactory().createModel();
1130: model.open();
1131:
1132: model.addStatement(subject, predicate, "Test", "DE");
1133: model.addStatement(subject, predicate, "Test");
1134:
1135: ClosableIterator<? extends Statement> iterator = model
1136: .findStatements(Variable.ANY, predicate, Variable.ANY);
1137: assertTrue(iterator.hasNext());
1138:
1139: while (iterator.hasNext()) {
1140: Statement statement = iterator.next();
1141: assertEquals(statement.getSubject(), subject);
1142: assertEquals(statement.getPredicate(), predicate);
1143:
1144: if (statement.getObject() instanceof LanguageTagLiteral) {
1145: assertEquals(((LanguageTagLiteral) (statement
1146: .getObject())).getValue(), "Test");
1147: assertEquals(((LanguageTagLiteral) (statement
1148: .getObject())).getLanguageTag(), "de");
1149: } else {
1150: assertTrue(statement.getObject() instanceof PlainLiteral);
1151: assertTrue(((PlainLiteral) (statement.getObject()))
1152: .getValue().equals("Test"));
1153: }
1154: }
1155:
1156: assertFalse(iterator.hasNext());
1157:
1158: iterator.close();
1159: model.close();
1160: }
1161:
1162: // TODO test public ClosableIterable<Statement> sparqlDescribe(String query)
1163:
1164: }
|