1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.ontotext.ordi.compatibilitytests;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.NoSuchElementException;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31 import junit.framework.TestCase;
32
33 import org.openrdf.model.Literal;
34 import org.openrdf.model.Resource;
35 import org.openrdf.model.Statement;
36 import org.openrdf.model.URI;
37 import org.openrdf.model.ValueFactory;
38 import org.openrdf.model.impl.ValueFactoryImpl;
39 import org.openrdf.query.Binding;
40 import org.openrdf.query.BindingSet;
41 import org.openrdf.query.impl.EmptyBindingSet;
42 import org.openrdf.query.parser.ParsedQuery;
43 import org.openrdf.query.parser.sparql.SPARQLParser;
44
45 import com.ontotext.ordi.iterator.CloseableIterator;
46 import com.ontotext.ordi.tripleset.Listener;
47 import com.ontotext.ordi.tripleset.ORDIConst;
48 import com.ontotext.ordi.tripleset.TConnection;
49 import com.ontotext.ordi.tripleset.TSource;
50 import com.ontotext.ordi.tripleset.TStatement;
51
52 /**
53 * This is base test for compliance with ORDI tripleset model. To test
54 * implementation please inherit this class and in the constructor of the
55 * extending class initialize your implementation and
56 * TestBaseTriplesetModel.source field.
57 *
58 * TODO: Transaction support semantics is not yet fully determined!
59 *
60 * @author vassil
61 */
62 public abstract class CompatibilityTests extends TestCase {
63
64 protected static final String SAMPLE_TRIPLE_FILE = "/test_triples.nt";
65 protected static final String SAMPLE_SPARQL_FILE = "/test_triples.sparql";
66
67 protected TSource source;
68
69 /*****************************************************************
70 * Test: Basic ORDI operations
71 ****************************************************************/
72 public void testBasicORDIOperationsAdd() throws Exception {
73 TConnection connection = source.getConnection();
74 ValueFactory factory = new ValueFactoryImpl();
75 Resource subj = factory.createBNode();
76 URI pred = factory.createURI("urn:ordi:compatibilitytest:predicate");
77 Literal obj = factory.createLiteral("Comaptibilitytest Literal)");
78 URI namedGraph = factory.createURI("urn:ordi:compatibilitytest:namedgraph");
79 URI ts = factory.createURI("urn:ordi:compatibilitytest:tripleset");
80 URI ts2 = factory.createURI("urn:ordi:compatibilitytest:tripleset2");
81
82 TStatement statement = connection.addStatement(subj, pred, obj, namedGraph);
83 assertEquals(statement.getSubject(), subj);
84 assertEquals(statement.getPredicate(), pred);
85 assertEquals(statement.getObject(), obj);
86 assertEquals(statement.getContext(), namedGraph);
87 assertEquals(statement.getTriplesetIterator().hasNext(), false);
88
89 statement = connection.addStatement(subj, pred, obj, namedGraph, ts);
90 assertEquals(statement.getSubject(), subj);
91 assertEquals(statement.getPredicate(), pred);
92 assertEquals(statement.getObject(), obj);
93 assertEquals(statement.getContext(), namedGraph);
94 assertEquals(statement.getTriplesetIterator().hasNext(), true);
95 assertEquals(statement.getTriplesetIterator().next(), ts);
96
97 assertEquals(connection.associateTripleset(subj, pred, obj, namedGraph, ts2), 1);
98
99 statement = connection.addStatement(subj, pred, obj, namedGraph, ts);
100 assertEquals(statement.getSubject(), subj);
101 assertEquals(statement.getPredicate(), pred);
102 assertEquals(statement.getObject(), obj);
103 assertEquals(statement.getContext(), namedGraph);
104 assertEquals(statement.isMemberOf(ts), true);
105 assertEquals(statement.isMemberOf(ts2), true);
106
107 assertEquals(connection.removeStatement(subj, pred, obj, namedGraph), 1);
108 assertEquals(connection.removeStatement(null, null, null, null), 0);
109 }
110
111 public void testBasicORDIOperationsAssociate() throws Exception {
112 TConnection connection = source.getConnection();
113 ValueFactory factory = new ValueFactoryImpl();
114 Resource subj = factory.createBNode();
115 URI pred = factory.createURI("urn:ordi:compatibilitytest:predicate");
116 Literal obj = factory.createLiteral("Comaptibilitytest Literal");
117 URI namedGraph = factory.createURI("urn:ordi:compatibilitytest:namedgraph");
118 URI ts = factory.createURI("urn:ordi:compatibilitytest:tripleset");
119
120
121
122
123 TStatement statement = connection.addStatement(subj, pred, obj, namedGraph);
124 assertEquals(statement.getSubject(), subj);
125 assertEquals(statement.getPredicate(), pred);
126 assertEquals(statement.getObject(), obj);
127 assertEquals(statement.getContext(), namedGraph);
128
129 for (int i = 0; i < 1000; i++) {
130 assertEquals(connection.associateTripleset(subj, pred,
131 obj, namedGraph, factory.createURI(ts.toString()+i)), 1);
132 statement = connection.addStatement(subj, pred, obj, namedGraph);
133 assertEquals(statement.getSubject(), subj);
134 assertEquals(statement.getPredicate(), pred);
135 assertEquals(statement.getObject(), obj);
136 assertEquals(statement.getContext(), namedGraph);
137 assertEquals(statement.isMemberOf(factory.createURI(ts.toString()+i)), true);
138 }
139
140 statement = connection.addStatement(subj, pred, obj, namedGraph);
141 Iterator<URI> iterator = statement.getTriplesetIterator();
142 for (int i = 0; i < 1000; i++) {
143 iterator.next();
144 }
145 assertFalse(iterator.hasNext());
146
147 for (int i = 0; i < 1000; i++) {
148 assertEquals(connection.deassociateTripleset(subj, pred,
149 obj, namedGraph, factory.createURI(ts.toString()+i)), 1);
150 statement = connection.addStatement(subj, pred, obj, namedGraph);
151 assertEquals(statement.getSubject(), subj);
152 assertEquals(statement.getPredicate(), pred);
153 assertEquals(statement.getObject(), obj);
154 assertEquals(statement.getContext(), namedGraph);
155 assertEquals(statement.isMemberOf(factory.createURI(ts.toString()+i)), false);
156 }
157
158 statement = connection.addStatement(subj, pred, obj, namedGraph);
159 assertEquals(statement.getSubject(), subj);
160 assertEquals(statement.getPredicate(), pred);
161 assertEquals(statement.getObject(), obj);
162 assertEquals(statement.getContext(), namedGraph);
163 assertEquals(statement.getTriplesetIterator().hasNext(), false);
164
165 assertEquals(connection.removeStatement(subj, pred, obj, namedGraph), 1);
166 assertEquals(connection.removeStatement(null, null, null, null), 0);
167 }
168
169 public void testBasicORDIOperationRemove() throws Exception {
170 TConnection connection = source.getConnection();
171 NTParser parser = new NTParser(this.getClass().getResourceAsStream(SAMPLE_TRIPLE_FILE));
172 ValueFactory factory = new ValueFactoryImpl();
173 URI namedGraph = factory.createURI("urn:ordi:compatibilitytest:namedgraph");
174 URI namedGraph2 = factory.createURI("urn:ordi:compatibilitytest:namedgraph2");
175 URI namedGraph3 = factory.createURI("urn:ordi:compatibilitytest:namedgraph3");
176
177 Statement st = null;
178 Statement lastSt = null;
179 int count = 0;
180 while((st = parser.next()) != null) {
181 connection.addStatement(st.getSubject(), st.getPredicate(),
182 st.getObject(), namedGraph);
183 connection.addStatement(st.getSubject(), st.getPredicate(),
184 st.getObject(), namedGraph2);
185 connection.addStatement(st.getSubject(), st.getPredicate(),
186 st.getObject(), namedGraph3);
187 lastSt = st;
188 count++;
189 }
190
191 parser = new NTParser(this.getClass().getResourceAsStream(SAMPLE_TRIPLE_FILE));
192 while((st = parser.next()) != null) {
193 assertEquals(connection.removeStatement(st.getSubject(), st.getPredicate(),
194 st.getObject(), namedGraph), 1);
195 }
196
197 assertEquals(connection.removeStatement(null, null, null, namedGraph2), count);
198
199 int removed = 0;
200 removed = connection.removeStatement(null, lastSt.getPredicate(), lastSt.getObject(), namedGraph3);
201 assertTrue(removed > 0);
202 count -= removed;
203 removed = connection.removeStatement(lastSt.getSubject(), null,
204 null, namedGraph3);
205 assertTrue(removed > 0);
206 count -= removed;
207 removed = connection.removeStatement(null, null, null, namedGraph3);
208 assertTrue(removed > 0);
209 count -= removed;
210 assertTrue(count == 0);
211 assertEquals(connection.removeStatement(null, null, null, null), 0);
212 }
213
214 public void testBasicORDIOperationSearch() throws Exception {
215 TConnection connection = source.getConnection();
216 connection.removeStatement(null, null, null, null);
217
218 NTParser parser = new NTParser(this.getClass().getResourceAsStream(SAMPLE_TRIPLE_FILE));
219 ValueFactory factory = new ValueFactoryImpl();
220 URI namedGraph = factory.createURI("urn:ordi:compatibilitytest:namedgraph");
221 URI namedGraph2 = factory.createURI("urn:ordi:compatibilitytest:namedgraph2");
222 URI namedGraph3 = factory.createURI("urn:ordi:compatibilitytest:namedgraph3");
223 URI ts = factory.createURI("urn:ordi:compatibilitytest:tripleset");
224 URI ts2 = factory.createURI("urn:ordi:compatibilitytest:tripleset2");
225
226 Statement st = null;
227 Statement lastSt = null;
228 while((st = parser.next()) != null) {
229 connection.addStatement(st.getSubject(), st.getPredicate(),
230 st.getObject(), namedGraph);
231 connection.addStatement(st.getSubject(), st.getPredicate(),
232 st.getObject(), namedGraph2, ts);
233 connection.addStatement(st.getSubject(), st.getPredicate(),
234 st.getObject(), namedGraph3, ts, ts2);
235
236 assertEquals(countIteratorResults(connection.search(st.getSubject(),
237 st.getPredicate(), st.getObject(), namedGraph, null)), 1);
238 assertEquals(countIteratorResults(connection.search(st.getSubject(),
239 st.getPredicate(), st.getObject(), namedGraph2, ts)), 1);
240 assertEquals(countIteratorResults(connection.search(st.getSubject(),
241 st.getPredicate(), st.getObject(), namedGraph2, ts2)), 0);
242 assertEquals(countIteratorResults(connection.search(st.getSubject(),
243 st.getPredicate(), st.getObject(), null, ts)), 2);
244 lastSt = st;
245 }
246
247 int result = countIteratorResults(connection.search(null, lastSt.getPredicate(),
248 lastSt.getObject(), namedGraph, null));
249 assertEquals(connection.removeStatement(null, lastSt.getPredicate(),
250 lastSt.getObject(), namedGraph), result);
251
252 result = countIteratorResults(connection.search(null, lastSt.getPredicate(),
253 lastSt.getObject(), namedGraph2, ts2));
254 assertEquals(0, result);
255
256 result = countIteratorResults(connection.search(lastSt.getSubject(), null,
257 lastSt.getObject(), namedGraph2, null));
258 assertEquals(connection.removeStatement(lastSt.getSubject(), null,
259 lastSt.getObject(), namedGraph2), result);
260
261 result = countIteratorResults(connection.search(lastSt.getSubject(), lastSt.getPredicate(),
262 null, namedGraph3, null));
263 assertEquals(connection.removeStatement(lastSt.getSubject(), lastSt.getPredicate(),
264 null, namedGraph3), result);
265
266 connection.removeStatement(null, null, null, null);
267 }
268
269 /*****************************************************************
270 * Test: Listeners
271 ****************************************************************/
272 public void testListeners() throws Exception {
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365 }
366
367 /*****************************************************************
368 * Test: SPARQL Querying
369 ****************************************************************/
370 public void testSPARQLSupport() throws Exception {
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413 }
414
415 /*****************************************************************
416 * Test: Concurrent access
417 ****************************************************************/
418
419
420
421 /*****************************************************************
422 * Helper methods and classes
423 ****************************************************************/
424 private int countIteratorResults(Iterator<?> iterator) {
425 int count = 0;
426 while (iterator.hasNext()) {
427 iterator.next();
428 count++;
429 }
430 return count;
431 }
432
433 private class SPARQLQueryIterator implements Iterator<Object> {
434
435 private BufferedReader reader;
436 private Pattern regexp;
437 private Object[] next;
438 private int lineNumber = 1;
439 private String line;
440
441 public SPARQLQueryIterator(InputStream stream) {
442 reader = new BufferedReader(new InputStreamReader(stream));
443 regexp = Pattern.compile("\\[(.+):(\\d+)\\]");
444 try {
445 line = reader.readLine();
446 }
447 catch (IOException e) {
448 throw new RuntimeException("Could not process file!", e);
449 }
450 }
451
452 public Object[] next() {
453 if (hasNext()) {
454 Object[] result = next;
455 next = null;
456 return result;
457 }
458 throw new NoSuchElementException();
459 }
460
461 public boolean hasNext() {
462 if (next != null) {
463 return true;
464 }
465 try {
466 while (line != null) {
467 if (line.startsWith("#") || line.trim().length() == 0) {
468 }
469 else if (regexp.matcher(line).matches()) {
470 if (next != null) {
471 return true;
472 }
473 next = new Object[3];
474 Matcher match = regexp.matcher(line);
475 match.matches();
476 next[0] = match.group(1);
477 next[1] = Integer.parseInt(match.group(2));
478 next[2] = new StringBuffer();
479 }
480 else {
481 if (next == null) {
482 next = null;
483 }
484 ((StringBuffer) next[2]).append(line);
485 ((StringBuffer) next[2]).append('\n');
486 }
487 line = reader.readLine();
488 lineNumber++;
489 }
490 return next != null;
491 }
492 catch (Exception e) {
493 throw new RuntimeException("Format problem on line " + lineNumber);
494 }
495 }
496
497 public void remove() {
498 throw new UnsupportedOperationException(
499 "Remove is not allowed for this iterator!");
500 }
501 }
502 }