001: /*
002: * LICENSE INFORMATION
003: * Copyright 2005-2007 by FZI (http://www.fzi.de).
004: * Licensed under a BSD license (http://www.opensource.org/licenses/bsd-license.php)
005: * <OWNER> = Max Völkel
006: * <ORGANIZATION> = FZI Forschungszentrum Informatik Karlsruhe, Karlsruhe, Germany
007: * <YEAR> = 2007
008: *
009: * Project information at http://semweb4j.org/rdf2go
010: */
011: package org.ontoware.rdf2go.model;
012:
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.io.InputStreamReader;
016: import java.io.StringWriter;
017: import java.util.ArrayList;
018: import java.util.Iterator;
019:
020: import junit.framework.Assert;
021: import junit.framework.TestCase;
022:
023: import org.junit.Test;
024: import org.ontoware.aifbcommons.collection.ClosableIterable;
025: import org.ontoware.aifbcommons.collection.ClosableIterator;
026: import org.ontoware.rdf2go.ModelFactory;
027: import org.ontoware.rdf2go.RDF2Go;
028: import org.ontoware.rdf2go.exception.ModelRuntimeException;
029: import org.ontoware.rdf2go.model.impl.QuadPatternImpl;
030: import org.ontoware.rdf2go.model.impl.StatementImpl;
031: import org.ontoware.rdf2go.model.node.URI;
032: import org.ontoware.rdf2go.model.node.Variable;
033: import org.ontoware.rdf2go.model.node.impl.URIImpl;
034: import org.ontoware.rdf2go.testdata.TestData;
035: import org.ontoware.rdf2go.vocabulary.RDF;
036: import org.ontoware.rdf2go.vocabulary.RDFS;
037: import org.slf4j.Logger;
038: import org.slf4j.LoggerFactory;
039:
040: /**
041: * Use this to test implementations of ModelSet. Overwrite the class and
042: * implement the "setup" method, initializing modelset and modelfactory.
043: *
044: * @author voelkel
045: * @author sauermann
046: */
047: public abstract class AbstractModelSetTest extends TestCase {
048:
049: public static URI a = new URIImpl("test://test/a");
050:
051: // TODO test new open() policies
052:
053: public static URI b = new URIImpl("test://test/b");
054:
055: public static URI c = new URIImpl("test://test/c");
056:
057: public static URI dt = new URIImpl("test://somedata/dt");
058:
059: public static URI graphuri1 = new URIImpl("urn:first");
060:
061: public static URI graphuri2 = new URIImpl("urn:second");
062:
063: public static URI object = new URIImpl("test://test/c");
064:
065: public static URI predicate = new URIImpl("test://test/b");
066:
067: public static URI subject = new URIImpl("test://test/a");
068:
069: /**
070: * there are two graphs in the default test data
071: */
072: public static int TESTGRAPHCOUNT = 2;
073:
074: public static <T> ArrayList<T> asArrayListAndClose(
075: ClosableIterator<T> it) {
076: ArrayList<T> result = new ArrayList<T>();
077: while (it.hasNext()) {
078: result.add(it.next());
079: }
080: it.close();
081: return result;
082: }
083:
084: Logger log = LoggerFactory.getLogger(AbstractModelSetTest.class);
085:
086: protected ModelSet modelset = null;
087:
088: /**
089: *
090: */
091: public AbstractModelSetTest() {
092: super ();
093: }
094:
095: /**
096: * @param arg0
097: */
098: public AbstractModelSetTest(String arg0) {
099: super (arg0);
100: }
101:
102: /**
103: * Add some test data to the modelsets . Test data will use graphuri1,
104: * graphuri2
105: *
106: */
107: protected void addTestDataToModelSet() throws Exception {
108: assertNotNull("should be initialised by test method already",
109: this .modelset);
110: // add two models
111: Model foaf = TestData.loadFoafBuffered(getModelFactory());
112: Model m = this .modelset.getModel(graphuri1);
113: m.open();
114: m.addAll(foaf.iterator());
115: Model ical = TestData.loadICALBuffered(getModelFactory());
116: m = this .modelset.getModel(graphuri2);
117: m.open();
118: m.addAll(ical.iterator());
119: assertTrue("the test data works", foaf.size() > 10);
120: assertTrue("the test data works", ical.size() > 10);
121: assertTrue("the modelset contains some triples", this .modelset
122: .size() > 20);
123: assertEquals(foaf.size() + ical.size(), this .modelset.size());
124: // never close them when you loaded them buffered
125: // foaf.close();
126: // ical.close();
127: m.close();
128: }
129:
130: /**
131: *
132: * @return a fresh ModelSet, to be tested
133: */
134: public abstract ModelFactory getModelFactory();
135:
136: @Override
137: public void setUp() throws Exception {
138: // done by each test, to allow for different Reasoning settings
139: // TODO: Leo: I think this is crap, you never test any reasoning
140: // settings
141: // and now this class has 100 more lines than needed, and in half of the
142: // tests the modelsets are not closed (horrray for consistency)
143: // this is exactly what setup and teardown is for, if you want to check
144: // reasoning, how about making a SECOND modelset in this one Test that
145: // does not exist yet?????????
146: }
147:
148: @Override
149: public void tearDown() throws Exception {
150: if (this .modelset != null) {
151: this .modelset.close();
152: this .modelset = null;
153: }
154: System.gc();
155: }
156:
157: @Test
158: public void testAddDataFromFileByCopying() throws Exception {
159: this .modelset = getModelFactory().createModelSet();
160: this .modelset.open();
161: this .modelset.readFrom(TestData.getFoafAsStream(),
162: Syntax.RdfXml);
163: assertTrue(this .modelset.size() > 0);
164:
165: ModelSet target = getModelFactory().createModelSet();
166: target.open();
167:
168: TestUtils.copy(this .modelset, target);
169: assertEquals(this .modelset.size(), target.size());
170: target.close();
171: }
172:
173: /**
174: * above test sometimes failed, here is a trace into the details that can
175: * happen
176: *
177: * @throws Exception
178: */
179: @Test
180: public void testAddDataFromFileByCopyingMoreDetail()
181: throws Exception {
182: this .modelset = getModelFactory().createModelSet();
183: this .modelset.open();
184: this .modelset.readFrom(TestData.getFoafAsStream(),
185: Syntax.RdfXml);
186:
187: assertTrue(this .modelset.size() > 0);
188: // the modelset loads into the default model
189: assertFalse(
190: "as we loaded data only in default model, there are no other models",
191: this .modelset.getModels().hasNext());
192:
193: // the default model has something
194: Model m = this .modelset.getDefaultModel();
195: m.open();
196: assertEquals("the default model has foaf", 536, m.size());
197: int sizeByIterator = TestUtils.countAndClose(m);
198: assertEquals("the default model can use an iterator", 536,
199: sizeByIterator);
200: m.close();
201:
202: ModelSet target = getModelFactory().createModelSet();
203: target.open();
204:
205: // Es liegt also daran, das modelset.read die daten in irgendein nicht
206: // mehr
207: // zugreifbared modell mit dem context "null" steckt. Aber wenn man ein
208: // model
209: // mti "null" macht, ist es leer. Da stimmt was nicht mit sesame.
210: TestUtils.copy(this .modelset, target);
211:
212: assertEquals(this .modelset.size(), target.size());
213: m.close();
214: }
215:
216: /**
217: * AbstractModelSetTest does not seem to test ModelSet.addModel (RDF2Go
218: * 4.4.1-rc1), so we do it here.
219: */
220: public void testAddModel() {
221: ModelFactory modelFactory = getModelFactory();
222:
223: // create a ModelSet holding a single Model with a single Statement
224: ModelSet modelSet = modelFactory.createModelSet();
225: modelSet.open();
226:
227: Model model = modelSet.getModel(graphuri1);
228: model.open();
229: model.addStatement(a, b, c);
230:
231: // some sanity checking on the ModelSet
232: assertEquals(1, modelSet.size());
233:
234: // see if we can add the Model to the ModelSet: no deadlocks and nothing
235: // should change
236: modelSet.addModel(model);
237: assertEquals(1, modelSet.size());
238:
239: // check that something does change when we add a different, stand-alone
240: // Model
241: Model model2 = modelFactory.createModel(graphuri2);
242: model2.open();
243: model2.addStatement(subject, predicate, object);
244: modelSet.addModel(model2);
245: assertEquals(2, modelSet.size());
246:
247: // clean-up
248: model.close();
249: model2.close();
250: modelSet.close();
251: }
252:
253: public void testAddRemovePatternsWithNull() {
254: modelset = getModelFactory().createModelSet();
255: modelset.open();
256: Assert.assertTrue(modelset.isOpen());
257: modelset.addStatement(null, a, b, c);
258: Assert.assertTrue(modelset.isOpen());
259: Assert.assertEquals(1, modelset.size());
260: Assert.assertTrue(modelset.isOpen());
261: modelset.removeStatements(null, Variable.ANY, Variable.ANY,
262: Variable.ANY);
263: Assert.assertTrue(modelset.isOpen());
264: Assert.assertEquals(0, modelset.size());
265: modelset.close();
266: }
267:
268: @Test
269: public void testContainsModel() throws ModelRuntimeException {
270: this .modelset = getModelFactory().createModelSet();
271: this .modelset.open();
272: assertFalse(this .modelset.containsModel(graphuri1));
273: Model model = this .modelset.getModel(graphuri1);
274: model.open();
275: model.addStatement(a, b, c);
276: assertTrue(this .modelset.containsModel(graphuri1));
277:
278: model.removeStatement(a, b, c);
279: // Still under discussion if this test shall fail or not
280: // TODO: decide if this assertion should be made
281: // assertFalse(this.modelset.containsModel(graphuri1));
282: model.close();
283: }
284:
285: @Test
286: public void testContainsStatements() throws ModelRuntimeException {
287: this .modelset = getModelFactory().createModelSet();
288: this .modelset.open();
289: assertFalse(this .modelset
290: .containsStatements(graphuri1, a, b, c));
291: Model model = this .modelset.getModel(graphuri1);
292: model.open();
293: model.addStatement(a, b, c);
294: assertTrue(this .modelset.containsStatements(graphuri1, a, b, c));
295:
296: // also with wildcard
297: assertTrue(this .modelset.containsStatements(Variable.ANY, a, b,
298: c));
299:
300: model.removeStatement(a, b, c);
301: //
302: assertFalse(this .modelset
303: .containsStatements(graphuri1, a, b, c));
304: model.close();
305: }
306:
307: @Test
308: public void testCopyModelSets() throws Exception {
309: this .modelset = getModelFactory().createModelSet();
310: this .modelset.open();
311: addTestDataToModelSet();
312: // add a statement in the default context
313: Model model = this .modelset.getDefaultModel();
314: model.open();
315: model.addStatement(subject, predicate, object);
316: ModelSet m = getModelFactory().createModelSet();
317: m.open();
318: TestUtils.copy(this .modelset, m);
319: assertEquals("copied all from source to target", this .modelset
320: .size(), m.size());
321: m.close();
322: }
323:
324: @Test
325: public void testCreateStatement() {
326: modelset = getModelFactory().createModelSet();
327: modelset.open();
328:
329: Statement s = modelset.createStatement(a, b, c);
330: assertEquals(s, new StatementImpl(null, a, b, c));
331:
332: }
333:
334: @Test
335: public void testCreateURI() throws ModelRuntimeException {
336: this .modelset = getModelFactory().createModelSet();
337: this .modelset.open();
338: URI u = this .modelset.createURI("urn:test:x");
339: assertNotNull(u);
340: }
341:
342: @Test
343: public void testDeleteStatement() {
344: ModelSet set = RDF2Go.getModelFactory().createModelSet();
345: set.open();
346: Assert.assertEquals(0, set.size());
347: set.addStatement(graphuri1, a, b, c);
348: Assert.assertEquals(1, set.size());
349: set.addStatement(graphuri2, a, b, c);
350: Assert.assertEquals(2, set.size());
351: set.removeStatement(set.createStatement(graphuri1, a, b, c));
352: Assert.assertEquals(1, set.size());
353: set.addStatement(null, a, b, c);
354: Assert.assertEquals(2, set.size());
355: set.removeStatement(set.createStatement(null, a, b, c));
356: Assert.assertEquals(1, set.size());
357: }
358:
359: /**
360: * Gunnar: need a function that queries and removes triples from all
361: * contexts... i.e. remove all (null, rdf:type, null) from ALL contexts...
362: *
363: * Max: So basically this would mean handling of Triple-patterns not just
364: * triples, in the add/remove methods. Ok.
365: *
366: *
367: * @throws Exception
368: * @throws ModelRuntimeException
369: */
370: public void testDeleteTriplePatternInAllGraphs()
371: throws ModelRuntimeException, Exception {
372: ModelSet set = RDF2Go.getModelFactory().createModelSet();
373: set.open();
374:
375: Iterator<? extends Model> it = set.getModels();
376: while (it.hasNext()) {
377: Model model = it.next();
378: // This line breaks really badly on sesame2...
379: // and on many other locking stores as well ..
380: // model.removeAll(model.findStatements(Variable.ANY, RDF.type,
381: // Variable.ANY).iterator());
382:
383: // just added this method
384: model
385: .removeStatements(Variable.ANY, RDF.type,
386: Variable.ANY);
387: }
388: }
389:
390: /** test find with (c,x,y,z) on contained model */
391: @Test
392: public void testFindStatements() throws ModelRuntimeException {
393: this .modelset = getModelFactory().createModelSet();
394: this .modelset.open();
395: Model m = this .modelset.getModel(graphuri1);
396: m.open();
397: m.addStatement(a, b, c);
398: ClosableIterator<? extends Statement> it = this .modelset
399: .findStatements(graphuri1, a, b, c);
400: assertTrue(it.hasNext());
401: it.next();
402: assertFalse(it.hasNext());
403: it.close();
404: m.close();
405: }
406:
407: /**
408: * Test find with (*,x,y,z) on modelset
409: *
410: * @throws ModelRuntimeException
411: */
412: @Test
413: public void testFindStatements2() throws ModelRuntimeException {
414: this .modelset = getModelFactory().createModelSet();
415: this .modelset.open();
416: URI a = new URIImpl("urn:test:a");
417: URI b = new URIImpl("urn:test:b");
418: URI c = new URIImpl("urn:test:c");
419: URI d = new URIImpl("urn:test:d");
420: this .modelset.addStatement(a, b, c, d);
421:
422: ClosableIterator<? extends Statement> it = this .modelset
423: .findStatements(Variable.ANY, b, c, d);
424: Statement stmt = it.next();
425: Assert.assertNotNull(stmt.getContext());
426: Assert.assertEquals(a, stmt.getContext());
427: it.close();
428: }
429:
430: /**
431: * The default model as defined in the SPARQL semantics. Each data set
432: * consisting of Models/Graphs has one default graph.
433: *
434: */
435: @Test
436: public void testGetDefaultModel() {
437: this .modelset = getModelFactory().createModelSet();
438: this .modelset.open();
439: Model defaultModel = this .modelset.getDefaultModel();
440: assertNotNull(defaultModel);
441: assertNull("the default model must have the NULL context",
442: defaultModel.getContextURI());
443: assertTrue(
444: "new open/close policy: open modelsets return open models",
445: defaultModel.isOpen());
446: assertTrue(defaultModel.isEmpty());
447: // add stuff to it
448: defaultModel.addStatement(a, b, c);
449: assertTrue(defaultModel.contains(a, b, c));
450: defaultModel.removeStatement(a, b, c);
451: assertTrue(defaultModel.isEmpty());
452: defaultModel.close();
453: }
454:
455: @Test
456: public void testGetModel() throws Exception {
457: this .modelset = getModelFactory().createModelSet();
458: this .modelset.open();
459: addTestDataToModelSet();
460: this .modelset.close();
461: Model m = this .modelset.getModel(graphuri1);
462: m.open();
463: assertNotNull(m);
464: assertTrue(
465: "graph1 contains less than 10 statements, it contains: "
466: + m.size(), m.size() > 10);
467: m = this .modelset.getModel(graphuri2);
468: m.open();
469: assertNotNull(m);
470: assertTrue(
471: "graph2 contains more than 10 statements, it contains: "
472: + m.size(), m.size() > 10);
473: m.close();
474: }
475:
476: @Test
477: public void testGetModels() throws Exception {
478: this .modelset = getModelFactory().createModelSet();
479: this .modelset.open();
480: addTestDataToModelSet();
481: ClosableIterator<Model> i = modelset.getModels();
482: ArrayList<Model> m = asArrayListAndClose(i);
483: assertEquals(TESTGRAPHCOUNT, m.size());
484: }
485:
486: // TODO (wth, 15.08.2007) should all this tests which state: "write test
487: // here" be written? yes
488:
489: @Test
490: public void testGetModelURIs() throws Exception {
491: this .modelset = getModelFactory().createModelSet();
492: this .modelset.open();
493: addTestDataToModelSet();
494: ClosableIterator<URI> l = this .modelset.getModelURIs();
495: ArrayList<URI> test = new ArrayList<URI>();
496: test.add(graphuri1);
497: test.add(graphuri2);
498: ArrayList<URI> uris = asArrayListAndClose(l);
499: assertEquals(2, uris.size());
500: test.removeAll(uris);
501: assertEquals(0, test.size());
502: }
503:
504: @Test
505: public void testGetUnderlyingModelSetImplementation() {
506: this .modelset = getModelFactory().createModelSet();
507: this .modelset.open();
508: assertNotNull(this .modelset
509: .getUnderlyingModelSetImplementation());
510: }
511:
512: @Test
513: public void testLoadDataIntoDefaultModel() throws Exception {
514: this .modelset = getModelFactory().createModelSet();
515: this .modelset.open();
516: this .modelset.readFrom(TestData.getFoafAsStream());
517: assertTrue(this .modelset.size() > 10);
518: // check default model
519: Model def = this .modelset.getDefaultModel();
520: def.open();
521: assertTrue(def.isOpen());
522: assertNotNull(def);
523: assertTrue("default model has something", def.size() > 10);
524: ArrayList<URI> uris = asArrayListAndClose(this .modelset
525: .getModelURIs());
526: assertEquals("default model has no context uri", 0, uris.size());
527: def.close();
528: }
529:
530: @Test
531: public void testOpenClose() {
532: this .modelset = getModelFactory().createModelSet();
533: this .modelset.open();
534:
535: // TODO write test here
536: // tricky... a lot of things have to be considered - ask max
537: }
538:
539: public void testRDF2GoBug() {
540: ModelSet added = getModelFactory().createModelSet();
541: added.open();
542: for (ClosableIterator<? extends Statement> i = added.iterator(); i
543: .hasNext();) {
544: i.next();
545: }
546: }
547:
548: public void testRDF2GoBug2() {
549: ModelSet added = getModelFactory().createModelSet();
550: added.open();
551: Statement s = new StatementImpl(new URIImpl("urn:testcontext"),
552: new URIImpl("urn:test"), new URIImpl("urn:testpred"),
553: new URIImpl("urn:testobj"));
554: added.addStatement(s);
555: for (ClosableIterator<? extends Statement> i = added
556: .findStatements(new QuadPatternImpl(Variable.ANY,
557: Variable.ANY, Variable.ANY, Variable.ANY)); i
558: .hasNext();) {
559: i.next();
560: }
561: }
562:
563: public void testRDF2GoBugAddRemove() {
564: ModelSet modelSet = getModelFactory().createModelSet();
565: // modelSet = new ObservableModelSetImpl(modelSet);
566: modelSet.open();
567: URI uri = modelSet.createURI("http://example");
568: modelSet.addStatement(null, uri, uri, uri);
569: modelSet.removeStatements(null, Variable.ANY, Variable.ANY,
570: Variable.ANY);
571: modelSet.close();
572: }
573:
574: @Test
575: public void testReadFromInputStream() {
576: this .modelset = getModelFactory().createModelSet();
577: this .modelset.open();
578: InputStream in = TestData.getFoafAsStream();
579: try {
580: this .modelset.readFrom(in);
581: } catch (ModelRuntimeException e) {
582: fail();
583: } catch (IOException e) {
584: fail();
585: } finally {
586: try {
587: in.close();
588: } catch (IOException e) {
589: fail();
590: }
591: }
592: }
593:
594: @Test
595: public void testReadFromInputStreamIntoDefaultModel()
596: throws Exception {
597: this .modelset = getModelFactory().createModelSet();
598: this .modelset.open();
599: this .modelset.readFrom(TestData.getFoafAsStream(),
600: Syntax.RdfXml);
601:
602: assertTrue(this .modelset.size() > 0);
603: // the modelset loads into the default model
604: assertFalse(
605: "as we loaded data only in default model, there are no other models",
606: this .modelset.getModels().hasNext());
607:
608: // the default model has something
609: Model m = this .modelset.getDefaultModel();
610: m.open();
611: assertEquals("the default model has foaf", 536, m.size());
612:
613: int sizeByIterator = TestUtils.countAndClose(m);
614: assertEquals("the default model can use an iterator", 536,
615: sizeByIterator);
616: }
617:
618: @Test
619: public void testReadFromInputStreamIntoNamedModel()
620: throws Exception {
621: this .modelset = getModelFactory().createModelSet();
622: this .modelset.open();
623: Model m = this .modelset.getModel(graphuri1);
624: m.open();
625: m.readFrom(TestData.getFoafAsStream(), Syntax.RdfXml);
626: m.close();
627:
628: // add data to second graph
629: m = this .modelset.getModel(graphuri2);
630: m.open();
631: m.readFrom(TestData.getICALAsStream(), Syntax.RdfXml);
632: m.close();
633:
634: // assertEquals("The modelset contains exactly loaded triples.",
635: // TestData.FOAF_SIZE + TestData.ICALSIZE, this.modelset.size());
636: // the modelset loads into the named graph
637: int modelcount = TestUtils.countAndClose(this .modelset
638: .getModels());
639: assertEquals("there are two models", 2, modelcount);
640: // no default model
641: m = this .modelset.getDefaultModel();
642: m.open();
643: assertEquals("nothing in default model", 0, m.size());
644: m.close();
645:
646: // get the named model1
647: m = this .modelset.getModel(graphuri1);
648: m.open();
649: assertEquals("the named graph model has foaf",
650: TestData.FOAFSIZE, m.size());
651: // at some point, iterators were broken here, so test if it returns one
652: int sizeByIterator = TestUtils.countAndClose(m);
653: assertEquals("the model supports iterators", TestData.FOAFSIZE,
654: sizeByIterator);
655: m.close();
656:
657: // get the named model2
658: m = this .modelset.getModel(graphuri2);
659: m.open();
660: assertEquals("the named graph model has ical",
661: TestData.ICALSIZE, m.size());
662: m.close();
663:
664: }
665:
666: @Test
667: public void testReadFromInputStreamSyntax() {
668: this .modelset = getModelFactory().createModelSet();
669: this .modelset.open();
670:
671: // TODO write test here
672: }
673:
674: @Test
675: public void testReadFromReader() {
676: this .modelset = getModelFactory().createModelSet();
677: this .modelset.open();
678:
679: InputStreamReader isr = new InputStreamReader(TestData
680: .getFoafAsStream());
681: try {
682: this .modelset.readFrom(isr);
683: } catch (ModelRuntimeException e) {
684: fail();
685: } catch (IOException e) {
686: fail();
687: } finally {
688: try {
689: isr.close();
690: } catch (IOException e) {
691: fail();
692: }
693: }
694: }
695:
696: @Test
697: public void testReadFromReaderSyntax() {
698: this .modelset = getModelFactory().createModelSet();
699: this .modelset.open();
700:
701: // TODO write test here
702: }
703:
704: @Test
705: public void testRemoveAll() throws ModelRuntimeException {
706: this .modelset = getModelFactory().createModelSet();
707: this .modelset.open();
708: Model m = this .modelset.getModel(graphuri1);
709: m.open();
710: assertEquals(0, m.size());
711: m.addStatement(a, b, c);
712: assertTrue(m.size() > 0);
713: this .modelset.removeAll();
714: m.close();
715: m = this .modelset.getModel(graphuri1);
716: m.open();
717: assertEquals(0, m.size());
718: m.close();
719: }
720:
721: @Test
722: public void testRemoveModel() throws ModelRuntimeException {
723: this .modelset = getModelFactory().createModelSet();
724: this .modelset.open();
725: Model m = this .modelset.getModel(graphuri1);
726: m.open();
727: assertEquals(0, m.size());
728: m.addStatement(a, b, c);
729: assertTrue(m.size() > 0);
730: this .modelset.removeModel(graphuri1);
731: m.close();
732: m = this .modelset.getModel(graphuri1);
733: m.open();
734: assertEquals(0, m.size());
735: m.close();
736: }
737:
738: @Test
739: public void testRemoveModel2() throws ModelRuntimeException,
740: IOException {
741: Model rdfModel = RDF2Go.getModelFactory().createModel();
742: rdfModel.open();
743:
744: rdfModel.readFrom(TestData.getFoafAsStream(), Syntax.RdfXml);
745: rdfModel.close();
746:
747: Model naoModel = RDF2Go.getModelFactory().createModel();
748: naoModel.open();
749: naoModel.readFrom(TestData.getICALAsStream(), Syntax.RdfXml);
750: naoModel.close();
751:
752: ModelSet mainRepository = RDF2Go.getModelFactory()
753: .createModelSet();
754: mainRepository.open();
755:
756: URI rdfModelURI = new URIImpl("ontology:rdf");
757: URI naoModelURI = new URIImpl("ontology:nao");
758:
759: Model model = mainRepository.getModel(rdfModelURI);
760: model.open();
761: rdfModel.open();
762: model.addAll(rdfModel.iterator());
763: rdfModel.close();
764: model.close();
765:
766: model = mainRepository.getModel(naoModelURI);
767: model.open();
768: naoModel.open();
769: model.addAll(naoModel.iterator());
770: naoModel.close();
771: model.close();
772:
773: log.debug("Removing model: " + rdfModelURI);
774: mainRepository.removeModel(rdfModelURI);
775:
776: mainRepository.close();
777: }
778:
779: @Test
780: public void testSize() throws Exception {
781: this .modelset = getModelFactory().createModelSet();
782: this .modelset.open();
783: this .modelset.readFrom(TestData.getFoafAsStream());
784: assertEquals("Size of foaf", 536, this .modelset.size());
785: }
786:
787: @Test
788: public void testSparqlAsk() {
789: this .modelset = getModelFactory().createModelSet();
790: this .modelset.open();
791:
792: // TODO add test when sparql ask is availalbe (not yet, as of
793: // 17.08.2007)
794:
795: // fail("Not yet implemented");
796: }
797:
798: @Test
799: public void testSparqlConstruct() throws Exception {
800: this .modelset = getModelFactory().createModelSet();
801: this .modelset.open();
802:
803: addTestDataToModelSet();
804: ClosableIterable<? extends Statement> i = this .modelset
805: .sparqlConstruct("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n "
806: + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"
807: + "construct {?s rdf:type ?o} where {?s rdf:type ?o}");
808: int size = TestUtils.countAndClose(i);
809: assertEquals("sparql construct works getting types", 395, size);
810: }
811:
812: @Test
813: public void testSparqlDescribe() {
814: this .modelset = getModelFactory().createModelSet();
815: this .modelset.open();
816: // TODO add test when sparql describe is available (not yet, as of
817: // 17.08.2007)
818:
819: // fail("Not yet implemented");
820:
821: }
822:
823: @Test
824: public void testSparqlSelect() throws ModelRuntimeException {
825: this .modelset = getModelFactory().createModelSet();
826: this .modelset.open();
827: Model m = this .modelset.getDefaultModel();
828: m.open();
829: m.addStatement(a, b, c);
830:
831: QueryResultTable table = this .modelset
832: .sparqlSelect("SELECT ?s ?p ?o WHERE { ?s ?p ?o . }");
833: ClosableIterator<QueryRow> it = table.iterator();
834: assertTrue(it.hasNext());
835: it.next();
836: assertFalse(it.hasNext());
837: it.close();
838: }
839:
840: @Test
841: public void testSparqlSelectFOAF() throws ModelRuntimeException,
842: IOException {
843: this .modelset = getModelFactory().createModelSet();
844: this .modelset.open();
845:
846: // URL foafURL = new URL("http://xmlns.com/foaf/0.1/20050603.rdf");
847: // this.modelset.readFrom( foafURL.openStream() );
848: InputStream in = TestData.getFoafAsStream();
849: this .modelset.readFrom(in);
850:
851: QueryResultTable table = this .modelset
852: .sparqlSelect("SELECT ?s ?o WHERE { ?s <"
853: + RDFS.comment + "> ?o . }");
854: ClosableIterator<QueryRow> it = table.iterator();
855: assertTrue(it.hasNext());
856: it.next();
857: assertTrue(it.hasNext());
858: it.close();
859: }
860:
861: @Test
862: public void testWriteToOutputStream() throws Exception {
863: this .modelset = getModelFactory().createModelSet();
864: this .modelset.open();
865: addTestDataToModelSet();
866:
867: try {
868: this .modelset.writeTo(System.out);
869: } catch (ModelRuntimeException e) {
870: fail();
871: } catch (IOException e) {
872: fail();
873: }
874: }
875:
876: @Test
877: public void testWriteToOutputStreamSyntax() throws Exception {
878: this .modelset = getModelFactory().createModelSet();
879: this .modelset.open();
880: addTestDataToModelSet();
881:
882: try {
883: this .modelset.writeTo(System.out, Syntax.Turtle);
884: } catch (ModelRuntimeException e) {
885: fail();
886: } catch (IOException e) {
887: fail();
888: }
889: }
890:
891: @Test
892: public void testWriteToWriter() throws Exception {
893: this .modelset = getModelFactory().createModelSet();
894: this .modelset.open();
895: addTestDataToModelSet();
896:
897: StringWriter sw = new StringWriter();
898: try {
899: this .modelset.writeTo(sw);
900: } catch (ModelRuntimeException e) {
901: fail();
902: } catch (IOException e) {
903: fail();
904: }
905: assertTrue(sw.getBuffer().toString().length() > 1000);
906: sw.close();
907: }
908:
909: @Test
910: public void testWriteToWriterSyntax() {
911: this .modelset = getModelFactory().createModelSet();
912: this .modelset.open();
913:
914: // TODO write test
915: }
916:
917: @Test
918: public void testRTGO_39() {
919: ModelSet modelset = RDF2Go.getModelFactory().createModelSet();
920: modelset.open();
921: for (ClosableIterator<Statement> i = modelset.iterator(); i
922: .hasNext();) {
923: i.next();
924: }
925: }
926:
927: }
|