KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestMultiModel.java,v 1.4 2005/02/21 12:03:18 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 import com.hp.hpl.jena.vocabulary.DB;
29
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33
34
35 public class TestMultiModel extends TestCase
36     {
37     String JavaDoc DefModel = GraphRDB.DEFAULT;
38
39         
40     public TestMultiModel( String JavaDoc name )
41         { super( name ); }
42     
43     public static TestSuite suite()
44         { return new TestSuite( TestMultiModel.class ); }
45      
46     Model model = null;
47     ModelRDB dmod1 = null;
48     ModelRDB dmod2 = null;
49     ModelRDB nmod1 = null;
50     ModelRDB nmod2 = null;
51     IDBConnection conn = null;
52     IRDBDriver dbDriver;
53     
54     protected void setUp() throws java.lang.Exception JavaDoc {
55         conn = TestConnection.makeAndCleanTestConnection();
56         dbDriver = conn.getDriver();
57         model = ModelRDB.createModel(conn);
58         conn.getDriver().setStoreWithModel(DefModel);
59         dmod1 = ModelRDB.createModel(conn,"Def_Model_1");
60         conn.getDriver().setStoreWithModel("Def_Model_1");
61         dmod2 = ModelRDB.createModel(conn, "Def_Model_2");
62         conn.getDriver().setStoreWithModel(null);
63         nmod1 = ModelRDB.createModel(conn,"Named_Model_1");
64         conn.getDriver().setStoreWithModel("Named_Model_1");
65         nmod2 = ModelRDB.createModel(conn,"Named_Model_2");
66     }
67     
68     protected void tearDown() throws java.lang.Exception JavaDoc {
69         dmod1.close(); dmod2.close();
70         nmod1.close(); nmod2.close();
71         conn.cleanDB();
72         conn.close();
73         conn = null;
74     }
75     
76     
77     public void addToDBGraphProp ( Model model, Property prop, String JavaDoc val ) {
78         // first, get URI of the graph
79
StmtIterator iter = model.listStatements(
80             new SimpleSelector(null, DB.graphName, (RDFNode) null));
81         assertTrue(iter.hasNext());
82         
83         Statement stmt = iter.nextStatement();
84         assertTrue(iter.hasNext() == false);
85         Resource graphURI = stmt.getSubject();
86         Literal l = model.createLiteral(val);
87         Statement s = model.createStatement(graphURI,prop,l);
88         model.add(s);
89         assertTrue(model.contains(s));
90     }
91     
92     private void addOnModel(Model model, Statement stmt) {
93         model.add(stmt);
94         assertTrue( model.contains(stmt) );
95         model.add(stmt);
96         assertTrue( model.contains(stmt) );
97     }
98     
99     private void rmvOnModel(Model model, Statement stmt) {
100         assertTrue( model.contains(stmt) );
101         model.remove(stmt);
102         assertTrue( !model.contains(stmt) );
103         model.add(stmt);
104         assertTrue( model.contains(stmt) );
105         model.remove(stmt);
106         assertTrue( !model.contains(stmt) );
107     }
108
109     
110     private void addRemove(Statement stmt) {
111         addOnModel(model,stmt);
112         addOnModel(dmod1,stmt);
113         addOnModel(dmod2,stmt);
114         addOnModel(nmod1,stmt);
115         addOnModel(nmod2,stmt);
116         
117         rmvOnModel(nmod2,stmt);
118         rmvOnModel(nmod1,stmt);
119         rmvOnModel(dmod2,stmt);
120         rmvOnModel(dmod1,stmt);
121         rmvOnModel(model,stmt);
122     }
123     
124     public void testAddRemoveURI() {
125         Resource s = model.createResource("test#subject");
126         Property p = model.createProperty("test#predicate");
127         Resource o = model.createResource("test#object");
128         
129         addRemove( model.createStatement(s,p,o));
130     }
131     
132     public void testAddRemoveLiteral() {
133         Resource s = model.createResource("test#subject");
134         Property p = model.createProperty("test#predicate");
135         Literal l = model.createLiteral("testLiteral");
136         
137         addRemove( model.createStatement(s,p,l));
138     }
139
140     public void testSetLongObjectLenFailure() {
141         try {
142             int len = dbDriver.getLongObjectLength();
143             dbDriver.setLongObjectLength(len / 2);
144             assertTrue(false);
145         } catch (Exception JavaDoc e) {
146         }
147     }
148
149     public void testLongObjectLen() {
150         long longLen = dbDriver.getLongObjectLength();
151         assertTrue(longLen > 0 && longLen < 100000);
152
153         String JavaDoc base = ".";
154         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(1024 + (int) longLen);
155         /* long minLongLen = longLen < 1024 ? longLen - (longLen/2) : longLen - 512; */
156         /* long maxLongLen = longLen + 1024; */
157         /* TODO: find out why this test takes sooooo long (minutes!) with the above bounds */
158         long minLongLen = longLen - 32;
159         long maxLongLen = longLen + 32;
160         assertTrue(minLongLen > 0);
161
162         Resource s = model.createResource("test#subject");
163         Property p = model.createProperty("test#predicate");
164         Literal l;
165         Statement stmt;
166         while (buffer.length() < minLongLen) { /*( build base string */
167             buffer.append(base);
168         }
169         /* add stmts with long literals */
170         long modelSizeBeg = model.size();
171         while (buffer.length() < maxLongLen) {
172             l = model.createLiteral(buffer.toString());
173             stmt = model.createStatement(s, p, l);
174             model.add(stmt);
175             assertTrue(model.contains(stmt));
176             assertTrue(stmt.getObject().equals(l));
177             buffer.append(base);
178         }
179         assertTrue(model.size() == (modelSizeBeg + maxLongLen - minLongLen));
180         /* remove stmts with long literals */
181         while (buffer.length() > minLongLen) {
182             buffer.deleteCharAt(0);
183             l = model.createLiteral(buffer.toString());
184             stmt = model.createStatement(s, p, l);
185             assertTrue(model.contains(stmt));
186             model.remove(stmt);
187             assertTrue(!model.contains(stmt));
188         }
189         assertTrue(model.size() == modelSizeBeg);
190     }
191
192     public void testSetLongObjectLen() {
193         int len = dbDriver.getLongObjectLength();
194         try {
195             tearDown();
196             conn = TestConnection.makeTestConnection();
197             dbDriver = conn.getDriver();
198             len = dbDriver.getLongObjectLength();
199             dbDriver.setLongObjectLength(len / 2);
200             model = ModelRDB.createModel(conn);
201         } catch (Exception JavaDoc e) {
202             assertTrue(false);
203         }
204         testLongObjectLen();
205
206         // now make sure longObjectValue persists
207
model.close();
208         try {
209             conn.close();
210             conn = TestConnection.makeTestConnection();
211             dbDriver = conn.getDriver();
212             assertTrue(len == dbDriver.getLongObjectLength());
213             model = ModelRDB.open(conn);
214             assertTrue(len / 2 == dbDriver.getLongObjectLength());
215         } catch (Exception JavaDoc e) {
216             assertTrue(false);
217         }
218     }
219     
220     public void testAddRemoveHugeLiteral() {
221         String JavaDoc base = "This is a huge string that repeats.";
222         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
223         while(buffer.length() < 4000 )
224             buffer.append(base);
225         Resource s = model.createResource("test#subject");
226         Property p = model.createProperty("test#predicate");
227         Literal l = model.createLiteral(buffer.toString());
228         
229         addRemove( model.createStatement(s,p,l));
230     }
231     
232     public void testAddRemoveDatatype() {
233         Resource s = model.createResource("test#subject");
234         Property p = model.createProperty("test#predicate");
235         Literal l = model.createTypedLiteral("stringType");
236         
237         addRemove( model.createStatement(s,p,l));
238     }
239
240     public void testAddRemoveHugeDatatype() {
241         String JavaDoc base = "This is a huge string that repeats.";
242         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
243         while(buffer.length() < 4000 )
244             buffer.append(base);
245         Resource s = model.createResource("test#subject");
246         Property p = model.createProperty("test#predicate");
247         Literal l2 = model.createTypedLiteral(buffer.toString());
248         
249         addRemove( model.createStatement(s,p,l2));
250     }
251     
252    public void testAddRemoveBNode() {
253         Resource s = model.createResource();
254         Property p = model.createProperty("test#predicate");
255         Resource o = model.createResource();
256                 
257         addRemove( model.createStatement(s,p,o));
258         
259    }
260    
261    public void testBNodeIdentityPreservation() {
262         Resource s = model.createResource();
263         Property p = model.createProperty("test#predicate");
264         Resource o = model.createResource();
265          
266         // Create two statements that differ only in
267
// the identity of the bnodes - then perform
268
// add-remove on one and verify the other is
269
// unchanged.
270
Statement spo = model.createStatement(s,p,o);
271         Statement ops = model.createStatement(o,p,s);
272         model.add(spo);
273         addRemove(ops);
274         assertTrue( model.contains(spo));
275         model.remove(spo);
276    }
277    
278
279 }
280         
281
282 /*
283     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
284     All rights reserved.
285
286     Redistribution and use in source and binary forms, with or without
287     modification, are permitted provided that the following conditions
288     are met:
289
290     1. Redistributions of source code must retain the above copyright
291        notice, this list of conditions and the following disclaimer.
292
293     2. Redistributions in binary form must reproduce the above copyright
294        notice, this list of conditions and the following disclaimer in the
295        documentation and/or other materials provided with the distribution.
296
297     3. The name of the author may not be used to endorse or promote products
298        derived from this software without specific prior written permission.
299
300     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
301     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
302     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
303     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
304     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
305     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
306     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
307     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
308     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
309     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
310 */

311
Popular Tags