001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestBasicOperations.java,v 1.23 2008/01/02 12:08:13 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.db.test;
008:
009: /**
010: *
011: * This tests basic operations on the modelRDB.
012: *
013: * It adds/removes statements of different types and verifys
014: * that the correct statements exist at the correct times.
015: *
016: * To run, you must have a mySQL database operational on
017: * localhost with a database name of "test" and allow use
018: * by a user named "test" with an empty password.
019: *
020: * (Based in part on earlier Jena tests by bwm, kers, et al.)
021: *
022: * @author csayers
023: */
024:
025: import java.util.Iterator;
026:
027: import com.hp.hpl.jena.db.*;
028: import com.hp.hpl.jena.db.impl.IRDBDriver;
029: import com.hp.hpl.jena.rdf.model.*;
030: import com.hp.hpl.jena.vocabulary.RDF;
031:
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: public class TestBasicOperations extends TestCase {
036:
037: public TestBasicOperations(String name) {
038: super (name);
039: }
040:
041: public static TestSuite suite() {
042: return new TestSuite(TestBasicOperations.class);
043: }
044:
045: ModelRDB model = null;
046: IRDBDriver dbDriver = null;
047: IDBConnection conn = null;
048:
049: protected void setUp() throws java.lang.Exception {
050: conn = TestConnection.makeAndCleanTestConnection();
051: model = ModelRDB.createModel(conn);
052: dbDriver = conn.getDriver();
053: }
054:
055: protected void tearDown() throws java.lang.Exception {
056: if (model != null)
057: model.close();
058: model = null;
059: if (conn != null) {
060: conn.cleanDB();
061: conn.close();
062: }
063: conn = null;
064: }
065:
066: private void addRemove(Statement stmt) {
067: model.add(stmt);
068: assertTrue(model.contains(stmt));
069: model.remove(stmt);
070: assertTrue(!model.contains(stmt));
071: model.add(stmt);
072: assertTrue(model.contains(stmt));
073: model.remove(stmt);
074: assertTrue(!model.contains(stmt));
075: model.add(stmt);
076: model.add(stmt);
077: assertTrue(model.contains(stmt));
078: model.remove(stmt);
079: assertTrue(!model.contains(stmt));
080: model.add(stmt);
081: model.add(stmt);
082: model.add(stmt);
083: model.add(stmt);
084: model.add(stmt);
085: model.add(stmt);
086: assertTrue(model.contains(stmt));
087: model.remove(stmt);
088: assertTrue(!model.contains(stmt));
089: }
090:
091: private Statement crAssertedStmt(String subj, String pred,
092: String obj) {
093: Resource s = model.createResource(subj);
094: Property p = model.createProperty(pred);
095: Resource o = model.createResource(obj);
096: return crAssertedStmt(s, p, o);
097: }
098:
099: private Statement crAssertedStmt(Resource s, Property p, RDFNode o) {
100: Statement stmt = model.createStatement(s, p, o);
101: model.add(stmt);
102: return stmt;
103: }
104:
105: private Resource crReifiedStmt(String node, Statement stmt) {
106: Resource n = model.createResource(node);
107: Resource s = stmt.getSubject();
108: Property p = stmt.getPredicate();
109: RDFNode o = stmt.getObject();
110:
111: crAssertedStmt(n, RDF.subject, s);
112: crAssertedStmt(n, RDF.predicate, p);
113: crAssertedStmt(n, RDF.object, o);
114: crAssertedStmt(n, RDF.type, RDF.Statement);
115: return n;
116: }
117:
118: public void testAddRemoveURI() {
119: Resource s = model.createResource("test#subject");
120: Property p = model.createProperty("test#predicate");
121: Resource o = model.createResource("test#object");
122:
123: addRemove(model.createStatement(s, p, o));
124: }
125:
126: public void testAddRemoveLiteral() {
127: Resource s = model.createResource("test#subject");
128: Property p = model.createProperty("test#predicate");
129: Literal l = model.createLiteral("testLiteral");
130:
131: addRemove(model.createStatement(s, p, l));
132: }
133:
134: public void testSetLongObjectLenFailure() {
135: try {
136: int len = dbDriver.getLongObjectLength();
137: dbDriver.setLongObjectLength(len / 2);
138: assertTrue(false);
139: } catch (Exception e) {
140: }
141: }
142:
143: public void testLongObjectLen() {
144: long longLen = dbDriver.getLongObjectLength();
145: assertTrue(longLen > 0 && longLen < 100000);
146:
147: String base = ".";
148: StringBuffer buffer = new StringBuffer(1024 + (int) longLen);
149: /* long minLongLen = longLen < 1024 ? longLen - (longLen/2) : longLen - 512; */
150: /* long maxLongLen = longLen + 1024; */
151: /* TODO: find out why this test takes sooooo long (minutes!) with the above bounds */
152: long minLongLen = longLen - 32;
153: long maxLongLen = longLen + 32;
154: assertTrue(minLongLen > 0);
155:
156: Resource s = model.createResource("test#subject");
157: Property p = model.createProperty("test#predicate");
158: Literal l;
159: Statement stmt;
160: while (buffer.length() < minLongLen) { /*( build base string */
161: buffer.append(base);
162: }
163: /* add stmts with long literals */
164: long modelSizeBeg = model.size();
165: while (buffer.length() < maxLongLen) {
166: l = model.createLiteral(buffer.toString());
167: stmt = model.createStatement(s, p, l);
168: model.add(stmt);
169: assertTrue(model.contains(stmt));
170: assertTrue(stmt.getObject().equals(l));
171: buffer.append(base);
172: }
173: assertTrue(model.size() == (modelSizeBeg + maxLongLen - minLongLen));
174: /* remove stmts with long literals */
175: while (buffer.length() > minLongLen) {
176: buffer.deleteCharAt(0);
177: l = model.createLiteral(buffer.toString());
178: stmt = model.createStatement(s, p, l);
179: assertTrue(model.contains(stmt));
180: model.remove(stmt);
181: assertTrue(!model.contains(stmt));
182: }
183: assertTrue(model.size() == modelSizeBeg);
184: }
185:
186: public void testSetLongObjectLen() {
187: int len = dbDriver.getLongObjectLength();
188: int len2 = len - 2;
189: try {
190: tearDown();
191: conn = TestConnection.makeTestConnection();
192: dbDriver = conn.getDriver();
193: len = dbDriver.getLongObjectLength();
194: dbDriver.setLongObjectLength(len2);
195: model = ModelRDB.createModel(conn);
196: } catch (Exception e) {
197: assertTrue(false);
198: }
199: testLongObjectLen();
200:
201: // now make sure longObjectValue persists
202: model.close();
203: try {
204: conn.close();
205: conn = TestConnection.makeTestConnection();
206: dbDriver = conn.getDriver();
207: assertTrue(len == dbDriver.getLongObjectLength());
208: model = ModelRDB.open(conn);
209: assertTrue(len2 == dbDriver.getLongObjectLength());
210: } catch (Exception e) {
211: assertTrue(false);
212: }
213: }
214:
215: public void testSetLongObjectLenMax() {
216: int len = dbDriver.getLongObjectLength();
217: int newLen = len;
218: int lenMax = dbDriver.getLongObjectLengthMax();
219: int hdrLen = 32; // allow 32 bytes for hdrs, etc.
220: try {
221: tearDown();
222: conn = TestConnection.makeTestConnection();
223: dbDriver = conn.getDriver();
224: len = dbDriver.getLongObjectLength();
225: lenMax = dbDriver.getLongObjectLengthMax();
226: if (len == lenMax)
227: return; // nothing to test
228: newLen = lenMax - hdrLen;
229: dbDriver.setLongObjectLength(newLen);
230: model = ModelRDB.createModel(conn);
231: } catch (Exception e) {
232: assertTrue(false);
233: }
234: testLongObjectLen();
235:
236: // now make sure longObjectValue persists
237: model.close();
238: try {
239: conn.close();
240: conn = TestConnection.makeTestConnection();
241: dbDriver = conn.getDriver();
242: assertTrue(len == dbDriver.getLongObjectLength());
243: model = ModelRDB.open(conn);
244: assertTrue(newLen == dbDriver.getLongObjectLength());
245: } catch (Exception e) {
246: assertTrue(false);
247: }
248: }
249:
250: public void testAddRemoveUTFLiteral() {
251: String str = Data.strLong;
252: Resource s = model.createResource("test#subject");
253: Property p = model.createProperty("test#predicate");
254: Literal l = model.createLiteral(str);
255:
256: addRemove(model.createStatement(s, p, l));
257: }
258:
259: public void testAddRemoveLiteralSpecials() {
260: String str = Data.strSpecial;
261: Resource s = model.createResource("test#subject");
262: Property p = model.createProperty("test#predicate");
263: Literal l = model.createLiteral(str);
264: addRemove(model.createStatement(s, p, l));
265: }
266:
267: public void testAddRemoveHugeLiteral() {
268: String base = Data.strLong;
269: StringBuffer buffer = new StringBuffer(4096);
270: while (buffer.length() < 4000)
271: buffer.append(base);
272: Resource s = model.createResource("test#subject");
273: Property p = model.createProperty("test#predicate");
274: Literal l = model.createLiteral(buffer.toString());
275:
276: addRemove(model.createStatement(s, p, l));
277: }
278:
279: public void testCompressHugeURI() throws java.lang.Exception {
280: // in this test, the prefix exceeed longObjectLength but the
281: // compressed URI is less than longObjectLength
282: IDBConnection conn = TestConnection
283: .makeAndCleanTestConnection();
284: IRDBDriver d = conn.getDriver();
285: d.setDoCompressURI(true);
286: model = ModelRDB.createModel(conn);
287: String pfx = "a123456789";
288: String longPfx = "";
289: long longLen = dbDriver.getLongObjectLength();
290: // make long prefix
291: while (longPfx.length() < longLen)
292: longPfx += pfx;
293: String URI = longPfx + ":/www.foo/#bar";
294: Resource s = model.createResource(URI);
295: Property p = model.createProperty("test#predicate");
296: Resource o = model.createResource("test#object");
297: addRemove(model.createStatement(s, p, o));
298: }
299:
300: public void testCompressHugeURI1() throws java.lang.Exception {
301: // in this test, the prefix exceeed longObjectLength but the
302: // compressed URI also exceeds longObjectLength
303: IDBConnection conn = TestConnection
304: .makeAndCleanTestConnection();
305: IRDBDriver d = conn.getDriver();
306: d.setDoCompressURI(true);
307: model = ModelRDB.createModel(conn);
308: String pfx = "a123456789";
309: String longPfx = "";
310: long longLen = dbDriver.getLongObjectLength();
311: // make long prefix
312: while (longPfx.length() < longLen)
313: longPfx += pfx;
314: String URI = longPfx + ":/www.foo/#bar" + longPfx;
315: Resource s = model.createResource(URI);
316: Property p = model.createProperty("test#predicate");
317: Resource o = model.createResource("test#object");
318: addRemove(model.createStatement(s, p, o));
319: }
320:
321: public void testNoCompressHugeURI() throws java.lang.Exception {
322: // in this test, the prefix exceeed longObjectLength but the
323: // compressed URI is less than longObjectLength
324: IDBConnection conn = TestConnection
325: .makeAndCleanTestConnection();
326: IRDBDriver d = conn.getDriver();
327: d.setDoCompressURI(false);
328: model = ModelRDB.createModel(conn);
329: String pfx = "a123456789";
330: String longPfx = "";
331: long longLen = dbDriver.getLongObjectLength();
332: // make long prefix
333: while (longPfx.length() < longLen)
334: longPfx += pfx;
335: String URI = longPfx + ":/www.foo/#bar";
336: Resource s = model.createResource(URI);
337: Property p = model.createProperty("test#predicate");
338: Resource o = model.createResource("test#object");
339: addRemove(model.createStatement(s, p, o));
340: }
341:
342: public void testNoCompressHugeURI1() throws java.lang.Exception {
343: // in this test, the prefix exceeed longObjectLength but the
344: // compressed URI also exceeds longObjectLength
345: IDBConnection conn = TestConnection
346: .makeAndCleanTestConnection();
347: IRDBDriver d = conn.getDriver();
348: d.setDoCompressURI(false);
349: model = ModelRDB.createModel(conn);
350: String pfx = "a123456789";
351: String longPfx = "";
352: long longLen = dbDriver.getLongObjectLength();
353: // make long prefix
354: while (longPfx.length() < longLen)
355: longPfx += pfx;
356: String URI = longPfx + ":/www.foo/#bar" + longPfx;
357: Resource s = model.createResource(URI);
358: Property p = model.createProperty("test#predicate");
359: Resource o = model.createResource("test#object");
360: addRemove(model.createStatement(s, p, o));
361: }
362:
363: public void testAddRemoveHugeURI() throws java.lang.Exception {
364: String pfx = "a123456789";
365: String longPfx = "";
366: long longLen = dbDriver.getLongObjectLength();
367: // make long prefix
368: while (longPfx.length() < longLen)
369: longPfx += pfx;
370: String URI = longPfx + ":/www.foo/#bar" + longPfx;
371: Resource s = model.createResource(URI);
372: Property p = model.createProperty("test#predicate");
373: Resource o = model.createResource("test#object");
374: addRemove(model.createStatement(s, p, o));
375: }
376:
377: public void testPrefixCache() throws java.lang.Exception {
378: // in this test, add a number of long prefixes until the cache
379: // overflows and then make sure they can be retrieved.
380: IDBConnection conn = TestConnection
381: .makeAndCleanTestConnection();
382: IRDBDriver d = conn.getDriver();
383: d.setDoCompressURI(true);
384: model = ModelRDB.createModel(conn);
385: int cacheSize = d.getCompressCacheSize();
386: cacheSize = 10;
387: d.setCompressCacheSize(cacheSize);
388:
389: String pfx = "a123456789";
390: String longPfx = "";
391: long longLen = dbDriver.getLongObjectLength();
392: // make long prefix
393: while (longPfx.length() < longLen)
394: longPfx += pfx;
395: int i;
396: for (i = 0; i < cacheSize * 2; i++) {
397: String URI = longPfx + i + ":/www.foo/#bar";
398: Resource s = model.createResource(URI);
399: Property p = model.createProperty("test#predicate");
400: Resource o = model.createResource("test#object");
401: model.add(s, p, o);
402: }
403: for (i = 0; i < cacheSize * 2; i++) {
404: String URI = longPfx + i + ":/www.foo/#bar";
405: Resource s = model.createResource(URI);
406: Property p = model.createProperty("test#predicate");
407: Resource o = model.createResource("test#object");
408: assertTrue(model.contains(s, p, o));
409: }
410:
411: }
412:
413: public void testPrefixCachePersists() throws java.lang.Exception {
414: // check that the prefix cache persists and affects all models in db.
415: IDBConnection conn = TestConnection
416: .makeAndCleanTestConnection();
417: IRDBDriver d = conn.getDriver();
418: d.setDoCompressURI(true);
419: model = ModelRDB.createModel(conn);
420: int cacheSize = d.getCompressCacheSize();
421: d.setCompressCacheSize(cacheSize / 2);
422: model.close();
423: conn.close();
424:
425: conn = TestConnection.makeTestConnection();
426: d = conn.getDriver();
427: try {
428: d.setDoCompressURI(false);
429: assertFalse(true); // should not get here
430: } catch (Exception e) {
431: model = ModelRDB.createModel(conn, "NamedModel");
432: assertTrue(d.getDoCompressURI() == true);
433: assertTrue(d.getCompressCacheSize() == cacheSize);
434: }
435: }
436:
437: public void testAddRemoveDatatype() {
438: Resource s = model.createResource("test#subject");
439: Property p = model.createProperty("test#predicate");
440: Literal l = model.createTypedLiteral("stringType");
441:
442: addRemove(model.createStatement(s, p, l));
443: }
444:
445: public void testAddRemoveHugeDatatype() {
446: String base = Data.strLong;
447: StringBuffer buffer = new StringBuffer(4096);
448: while (buffer.length() < 4000)
449: buffer.append(base);
450: Resource s = model.createResource("test#subject");
451: Property p = model.createProperty("test#predicate");
452: Literal l2 = model.createTypedLiteral(buffer.toString());
453:
454: addRemove(model.createStatement(s, p, l2));
455: }
456:
457: public void testAddRemoveBNode() {
458: Resource s = model.createResource();
459: Property p = model.createProperty("test#predicate");
460: Resource o = model.createResource();
461:
462: addRemove(model.createStatement(s, p, o));
463:
464: }
465:
466: public void testBNodeIdentityPreservation() {
467: Resource s = model.createResource();
468: Property p = model.createProperty("test#predicate");
469: Resource o = model.createResource();
470:
471: // Create two statements that differ only in
472: // the identity of the bnodes - then perform
473: // add-remove on one and verify the other is
474: // unchanged.
475: Statement spo = model.createStatement(s, p, o);
476: Statement ops = model.createStatement(o, p, s);
477: model.add(spo);
478: addRemove(ops);
479: assertTrue(model.contains(spo));
480: model.remove(spo);
481: }
482:
483: public void testDuplicateCheck() {
484: Resource s = model.createResource();
485: Property p = model.createProperty("test#predicate");
486: Resource o = model.createResource();
487: Statement spo = model.createStatement(s, p, o);
488: model.add(spo);
489: try {
490: model.add(spo); // should be fine
491: assertTrue(model.size() == 1);
492: } catch (Exception e) {
493: assertTrue(false); // should not get here
494: }
495: model.setDoDuplicateCheck(false);
496: try {
497: model.add(spo); // should be fine - just inserted a dup
498: assertTrue(model.size() == 2);
499: } catch (Exception e) {
500: assertTrue(false); // should not get here
501: }
502: }
503:
504: private int countIter(Iterator it) {
505: int i = 0;
506: while (it.hasNext()) {
507: Statement s = (Statement) it.next();
508: i++;
509: }
510: return i;
511: }
512:
513: private int countAll() {
514: Iterator it = model.listStatements();
515: return countIter(it);
516: }
517:
518: private int countSubj(Resource s) {
519: Iterator it = model.listStatements(s, (Property) null,
520: (RDFNode) null);
521: return countIter(it);
522: }
523:
524: private int countObj(RDFNode o) {
525: Iterator it = model.listStatements((Resource) null,
526: (Property) null, o);
527: return countIter(it);
528: }
529:
530: private int countPred(Property o) {
531: Iterator it = model.listStatements((Resource) null,
532: (Property) o, (RDFNode) null);
533: return countIter(it);
534: }
535:
536: public void testQueryOnlyOption() {
537: GraphRDB g = new GraphRDB(conn, null, null,
538: GraphRDB.OPTIMIZE_ALL_REIFICATIONS_AND_HIDE_NOTHING,
539: false);
540: model = new ModelRDB(g);
541:
542: Statement a1 = crAssertedStmt("s1", "p1", "o1");
543: Statement a2 = crAssertedStmt("s2", "p2", "s1");
544: Resource r1 = crReifiedStmt("r1", a1);
545: Resource r2 = crReifiedStmt("r2", a2);
546:
547: // should find 10 statements total
548: int cnt;
549: Resource s1 = a1.getSubject();
550: Property p1 = a1.getPredicate();
551: RDFNode o1 = a1.getObject();
552:
553: cnt = countAll();
554: assertTrue(cnt == 10);
555: cnt = countSubj(s1);
556: assertTrue(cnt == 1);
557: cnt = countObj(o1);
558: assertTrue(cnt == 2);
559: cnt = countObj(s1);
560: assertTrue(cnt == 3);
561: cnt = countPred(p1);
562: assertTrue(cnt == 1);
563: cnt = countPred(RDF.predicate);
564: assertTrue(cnt == 2);
565: cnt = countSubj(r2);
566: assertTrue(cnt == 4);
567:
568: model.setQueryOnlyAsserted(true);
569: cnt = countAll();
570: assertTrue(cnt == 2);
571: cnt = countSubj(s1);
572: assertTrue(cnt == 1);
573: cnt = countObj(o1);
574: assertTrue(cnt == 1);
575: cnt = countObj(s1);
576: assertTrue(cnt == 1);
577: cnt = countPred(p1);
578: assertTrue(cnt == 1);
579: cnt = countPred(RDF.predicate);
580: assertTrue(cnt == 0);
581: cnt = countSubj(r2);
582: assertTrue(cnt == 0);
583:
584: model.setQueryOnlyReified(true);
585: cnt = countAll();
586: assertTrue(cnt == 8);
587: cnt = countSubj(s1);
588: assertTrue(cnt == 0);
589: cnt = countObj(o1);
590: assertTrue(cnt == 1);
591: cnt = countObj(s1);
592: assertTrue(cnt == 2);
593: cnt = countPred(p1);
594: assertTrue(cnt == 0);
595: cnt = countPred(RDF.predicate);
596: assertTrue(cnt == 2);
597: cnt = countSubj(r2);
598: assertTrue(cnt == 4);
599:
600: model.setQueryOnlyReified(false);
601: cnt = countAll();
602: assertTrue(cnt == 10);
603: }
604:
605: }
606:
607: /*
608: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
609: All rights reserved.
610:
611: Redistribution and use in source and binary forms, with or without
612: modification, are permitted provided that the following conditions
613: are met:
614:
615: 1. Redistributions of source code must retain the above copyright
616: notice, this list of conditions and the following disclaimer.
617:
618: 2. Redistributions in binary form must reproduce the above copyright
619: notice, this list of conditions and the following disclaimer in the
620: documentation and/or other materials provided with the distribution.
621:
622: 3. The name of the author may not be used to endorse or promote products
623: derived from this software without specific prior written permission.
624:
625: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
626: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
627: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
628: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
629: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
630: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
631: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
632: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
633: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
634: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
635: */
|