KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > test > TestBasicOperations


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestBasicOperations.java,v 1.13 2005/02/21 12:03:15 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.db.test;
8
9 /**
10  *
11  * This tests basic operations on the modelRDB.
12  *
13  * It adds/removes statements of different types and verifys
14  * that the correct statements exist at the correct times.
15  *
16  * To run, you must have a mySQL database operational on
17  * localhost with a database name of "test" and allow use
18  * by a user named "test" with an empty password.
19  *
20  * (Based in part on earlier Jena tests by bwm, kers, et al.)
21  *
22  * @author csayers
23 */

24
25 import com.hp.hpl.jena.db.*;
26 import com.hp.hpl.jena.db.impl.IRDBDriver;
27 import com.hp.hpl.jena.rdf.model.*;
28
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
32
33
34 public class TestBasicOperations extends TestCase {
35
36     public TestBasicOperations(String JavaDoc name) {
37         super(name);
38     }
39
40     public static TestSuite suite() {
41         return new TestSuite(TestBasicOperations.class);
42     }
43
44     ModelRDB model = null;
45     IRDBDriver dbDriver = null;
46     IDBConnection conn = null;
47
48     protected void setUp() throws java.lang.Exception JavaDoc {
49         conn = TestConnection.makeAndCleanTestConnection();
50         model = ModelRDB.createModel(conn);
51         dbDriver = conn.getDriver();
52     }
53
54     protected void tearDown() throws java.lang.Exception JavaDoc {
55         model.close();
56         model = null;
57         conn.cleanDB();
58         conn.close();
59         conn = null;
60     }
61
62     private void addRemove(Statement stmt) {
63         model.add(stmt);
64         assertTrue(model.contains(stmt));
65         model.remove(stmt);
66         assertTrue(!model.contains(stmt));
67         model.add(stmt);
68         assertTrue(model.contains(stmt));
69         model.remove(stmt);
70         assertTrue(!model.contains(stmt));
71         model.add(stmt);
72         model.add(stmt);
73         assertTrue(model.contains(stmt));
74         model.remove(stmt);
75         assertTrue(!model.contains(stmt));
76         model.add(stmt);
77         model.add(stmt);
78         model.add(stmt);
79         model.add(stmt);
80         model.add(stmt);
81         model.add(stmt);
82         assertTrue(model.contains(stmt));
83         model.remove(stmt);
84         assertTrue(!model.contains(stmt));
85     }
86     
87     public void testAddRemoveURI() {
88         Resource s = model.createResource("test#subject");
89         Property p = model.createProperty("test#predicate");
90         Resource o = model.createResource("test#object");
91
92         addRemove(model.createStatement(s, p, o));
93     }
94
95     public void testAddRemoveLiteral() {
96         Resource s = model.createResource("test#subject");
97         Property p = model.createProperty("test#predicate");
98         Literal l = model.createLiteral("testLiteral");
99
100         addRemove(model.createStatement(s, p, l));
101     }
102
103     public void testSetLongObjectLenFailure() {
104         try {
105             int len = dbDriver.getLongObjectLength();
106             dbDriver.setLongObjectLength(len / 2);
107             assertTrue(false);
108         } catch (Exception JavaDoc e) {
109         }
110     }
111
112     public void testLongObjectLen() {
113         long longLen = dbDriver.getLongObjectLength();
114         assertTrue(longLen > 0 && longLen < 100000);
115
116         String JavaDoc base = ".";
117         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(1024 + (int) longLen);
118         /* long minLongLen = longLen < 1024 ? longLen - (longLen/2) : longLen - 512; */
119         /* long maxLongLen = longLen + 1024; */
120         /* TODO: find out why this test takes sooooo long (minutes!) with the above bounds */
121         long minLongLen = longLen - 32;
122         long maxLongLen = longLen + 32;
123         assertTrue(minLongLen > 0);
124
125         Resource s = model.createResource("test#subject");
126         Property p = model.createProperty("test#predicate");
127         Literal l;
128         Statement stmt;
129         while (buffer.length() < minLongLen) { /*( build base string */
130             buffer.append(base);
131         }
132         /* add stmts with long literals */
133         long modelSizeBeg = model.size();
134         while (buffer.length() < maxLongLen) {
135             l = model.createLiteral(buffer.toString());
136             stmt = model.createStatement(s, p, l);
137             model.add(stmt);
138             assertTrue(model.contains(stmt));
139             assertTrue(stmt.getObject().equals(l));
140             buffer.append(base);
141         }
142         assertTrue(model.size() == (modelSizeBeg + maxLongLen - minLongLen));
143         /* remove stmts with long literals */
144         while (buffer.length() > minLongLen) {
145             buffer.deleteCharAt(0);
146             l = model.createLiteral(buffer.toString());
147             stmt = model.createStatement(s, p, l);
148             assertTrue(model.contains(stmt));
149             model.remove(stmt);
150             assertTrue(!model.contains(stmt));
151         }
152         assertTrue(model.size() == modelSizeBeg);
153     }
154
155     public void testSetLongObjectLen() {
156         int len = dbDriver.getLongObjectLength();
157         try {
158             tearDown();
159             conn = TestConnection.makeTestConnection();
160             dbDriver = conn.getDriver();
161             len = dbDriver.getLongObjectLength();
162             dbDriver.setLongObjectLength(len / 2);
163             model = ModelRDB.createModel(conn);
164         } catch (Exception JavaDoc e) {
165             assertTrue(false);
166         }
167         testLongObjectLen();
168
169         // now make sure longObjectValue persists
170
model.close();
171         try {
172             conn.close();
173             conn = TestConnection.makeTestConnection();
174             dbDriver = conn.getDriver();
175             assertTrue(len == dbDriver.getLongObjectLength());
176             model = ModelRDB.open(conn);
177             assertTrue(len / 2 == dbDriver.getLongObjectLength());
178         } catch (Exception JavaDoc e) {
179             assertTrue(false);
180         }
181     }
182
183     public void testAddRemoveHugeLiteral() {
184         String JavaDoc base = "This is a huge string that repeats.";
185         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
186         while (buffer.length() < 4000)
187             buffer.append(base);
188         Resource s = model.createResource("test#subject");
189         Property p = model.createProperty("test#predicate");
190         Literal l = model.createLiteral(buffer.toString());
191
192         addRemove(model.createStatement(s, p, l));
193     }
194     
195     public void testCompressHugeURI() throws java.lang.Exception JavaDoc {
196         // in this test, the prefix exceeed longObjectLength but the
197
// compressed URI is less than longObjectLength
198
IDBConnection conn = TestConnection.makeAndCleanTestConnection();
199         IRDBDriver d = conn.getDriver();
200         d.setDoCompressURI(true);
201         model = ModelRDB.createModel(conn);
202         String JavaDoc pfx = "a123456789";
203         String JavaDoc longPfx = "";
204         long longLen = dbDriver.getLongObjectLength();
205         // make long prefix
206
while ( longPfx.length() < longLen )
207             longPfx += pfx;
208         String JavaDoc URI = longPfx + ":/www.foo/#bar";
209         Resource s = model.createResource(URI);
210         Property p = model.createProperty("test#predicate");
211         Resource o = model.createResource("test#object");
212         addRemove(model.createStatement(s, p, o));
213     }
214
215     public void testCompressHugeURI1() throws java.lang.Exception JavaDoc {
216         // in this test, the prefix exceeed longObjectLength but the
217
// compressed URI also exceeds longObjectLength
218
IDBConnection conn = TestConnection.makeAndCleanTestConnection();
219         IRDBDriver d = conn.getDriver();
220         d.setDoCompressURI(true);
221         model = ModelRDB.createModel(conn);
222         String JavaDoc pfx = "a123456789";
223         String JavaDoc longPfx = "";
224         long longLen = dbDriver.getLongObjectLength();
225         // make long prefix
226
while ( longPfx.length() < longLen )
227             longPfx += pfx;
228         String JavaDoc URI = longPfx + ":/www.foo/#bar" + longPfx;
229         Resource s = model.createResource(URI);
230         Property p = model.createProperty("test#predicate");
231         Resource o = model.createResource("test#object");
232         addRemove(model.createStatement(s, p, o));
233     }
234
235     public void testNoCompressHugeURI() throws java.lang.Exception JavaDoc {
236         // in this test, the prefix exceeed longObjectLength but the
237
// compressed URI is less than longObjectLength
238
IDBConnection conn = TestConnection.makeAndCleanTestConnection();
239         IRDBDriver d = conn.getDriver();
240         d.setDoCompressURI(false);
241         model = ModelRDB.createModel(conn);
242         String JavaDoc pfx = "a123456789";
243         String JavaDoc longPfx = "";
244         long longLen = dbDriver.getLongObjectLength();
245         // make long prefix
246
while ( longPfx.length() < longLen )
247             longPfx += pfx;
248         String JavaDoc URI = longPfx + ":/www.foo/#bar";
249         Resource s = model.createResource(URI);
250         Property p = model.createProperty("test#predicate");
251         Resource o = model.createResource("test#object");
252         addRemove(model.createStatement(s, p, o));
253     }
254     
255     public void testNoCompressHugeURI1() throws java.lang.Exception JavaDoc {
256         // in this test, the prefix exceeed longObjectLength but the
257
// compressed URI also exceeds longObjectLength
258
IDBConnection conn = TestConnection.makeAndCleanTestConnection();
259         IRDBDriver d = conn.getDriver();
260         d.setDoCompressURI(false);
261         model = ModelRDB.createModel(conn);
262         String JavaDoc pfx = "a123456789";
263         String JavaDoc longPfx = "";
264         long longLen = dbDriver.getLongObjectLength();
265         // make long prefix
266
while ( longPfx.length() < longLen )
267             longPfx += pfx;
268         String JavaDoc URI = longPfx + ":/www.foo/#bar" + longPfx;
269         Resource s = model.createResource(URI);
270         Property p = model.createProperty("test#predicate");
271         Resource o = model.createResource("test#object");
272         addRemove(model.createStatement(s, p, o));
273     }
274
275     public void testAddRemoveHugeURI() throws java.lang.Exception JavaDoc {
276         String JavaDoc pfx = "a123456789";
277         String JavaDoc longPfx = "";
278         long longLen = dbDriver.getLongObjectLength();
279         // make long prefix
280
while ( longPfx.length() < longLen )
281             longPfx += pfx;
282         String JavaDoc URI = longPfx + ":/www.foo/#bar" + longPfx;
283         Resource s = model.createResource(URI);
284         Property p = model.createProperty("test#predicate");
285         Resource o = model.createResource("test#object");
286         addRemove(model.createStatement(s, p, o));
287     }
288     
289     public void testPrefixCache() throws java.lang.Exception JavaDoc {
290         // in this test, add a number of long prefixes until the cache
291
// overflows and then make sure they can be retrieved.
292
IDBConnection conn = TestConnection.makeAndCleanTestConnection();
293         IRDBDriver d = conn.getDriver();
294         d.setDoCompressURI(true);
295         model = ModelRDB.createModel(conn);
296         int cacheSize = d.getCompressCacheSize();
297         cacheSize = 10;
298         d.setCompressCacheSize(cacheSize);
299
300         String JavaDoc pfx = "a123456789";
301         String JavaDoc longPfx = "";
302         long longLen = dbDriver.getLongObjectLength();
303         // make long prefix
304
while ( longPfx.length() < longLen )
305             longPfx += pfx;
306         int i;
307         for(i=0;i<cacheSize*2;i++) {
308             String JavaDoc URI = longPfx + i + ":/www.foo/#bar";
309             Resource s = model.createResource(URI);
310             Property p = model.createProperty("test#predicate");
311             Resource o = model.createResource("test#object");
312             model.add(s, p, o);
313         }
314         for(i=0;i<cacheSize*2;i++) {
315             String JavaDoc URI = longPfx + i + ":/www.foo/#bar";
316             Resource s = model.createResource(URI);
317             Property p = model.createProperty("test#predicate");
318             Resource o = model.createResource("test#object");
319             assertTrue(model.contains(s, p, o));
320         }
321
322     }
323     
324     public void testPrefixCachePersists() throws java.lang.Exception JavaDoc {
325         // check that the prefix cache persists and affects all models in db.
326
IDBConnection conn = TestConnection.makeAndCleanTestConnection();
327         IRDBDriver d = conn.getDriver();
328         d.setDoCompressURI(true);
329         model = ModelRDB.createModel(conn);
330         int cacheSize = d.getCompressCacheSize();
331         d.setCompressCacheSize(cacheSize/2);
332         model.close();
333         conn.close();
334         
335         conn = TestConnection.makeTestConnection();
336         d = conn.getDriver();
337         try {
338             d.setDoCompressURI(false);
339             assertFalse(true); // should not get here
340
} catch (Exception JavaDoc e) {
341             model = ModelRDB.createModel(conn,"NamedModel");
342             assertTrue(d.getDoCompressURI() == true);
343             assertTrue(d.getCompressCacheSize() == cacheSize);
344         }
345     }
346
347
348     public void testAddRemoveDatatype() {
349         Resource s = model.createResource("test#subject");
350         Property p = model.createProperty("test#predicate");
351         Literal l = model.createTypedLiteral("stringType");
352
353         addRemove(model.createStatement(s, p, l));
354     }
355
356     public void testAddRemoveHugeDatatype() {
357         String JavaDoc base = "This is a huge string that repeats.";
358         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
359         while (buffer.length() < 4000)
360             buffer.append(base);
361         Resource s = model.createResource("test#subject");
362         Property p = model.createProperty("test#predicate");
363         Literal l2 = model.createTypedLiteral(buffer.toString());
364
365         addRemove(model.createStatement(s, p, l2));
366     }
367
368     public void testAddRemoveBNode() {
369         Resource s = model.createResource();
370         Property p = model.createProperty("test#predicate");
371         Resource o = model.createResource();
372
373         addRemove(model.createStatement(s, p, o));
374
375     }
376
377     public void testBNodeIdentityPreservation() {
378         Resource s = model.createResource();
379         Property p = model.createProperty("test#predicate");
380         Resource o = model.createResource();
381
382         // Create two statements that differ only in
383
// the identity of the bnodes - then perform
384
// add-remove on one and verify the other is
385
// unchanged.
386
Statement spo = model.createStatement(s, p, o);
387         Statement ops = model.createStatement(o, p, s);
388         model.add(spo);
389         addRemove(ops);
390         assertTrue(model.contains(spo));
391         model.remove(spo);
392     }
393
394     public void testDuplicateCheck() {
395         Resource s = model.createResource();
396         Property p = model.createProperty("test#predicate");
397         Resource o = model.createResource();
398         Statement spo = model.createStatement(s, p, o);
399         model.add(spo);
400         try {
401             model.add(spo); // should be fine
402
assertTrue(model.size() == 1);
403         } catch (Exception JavaDoc e) {
404             assertTrue(false); // should not get here
405
}
406         model.setDoDuplicateCheck(false);
407         try {
408             model.add(spo); // should be fine - just inserted a dup
409
assertTrue(model.size() == 2);
410         } catch (Exception JavaDoc e) {
411             assertTrue(false); // should not get here
412
}
413     }
414
415 }
416         
417
418 /*
419     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
420     All rights reserved.
421
422     Redistribution and use in source and binary forms, with or without
423     modification, are permitted provided that the following conditions
424     are met:
425
426     1. Redistributions of source code must retain the above copyright
427        notice, this list of conditions and the following disclaimer.
428
429     2. Redistributions in binary form must reproduce the above copyright
430        notice, this list of conditions and the following disclaimer in the
431        documentation and/or other materials provided with the distribution.
432
433     3. The name of the author may not be used to endorse or promote products
434        derived from this software without specific prior written permission.
435
436     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
437     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
438     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
439     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
440     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
441     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
442     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
443     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
444     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
445     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
446 */

447
Popular Tags