KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestReifierCompareToMem.java,v 1.7 2005/02/21 12:03:34 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 java.util.Iterator JavaDoc;
26
27 import com.hp.hpl.jena.db.*;
28 import com.hp.hpl.jena.rdf.model.*;
29 import com.hp.hpl.jena.vocabulary.RDF;
30
31 import junit.framework.*;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 public class TestReifierCompareToMem extends TestCase
36     {
37         
38     public TestReifierCompareToMem( String JavaDoc name )
39         { super( name ); }
40     
41     public static TestSuite suite()
42         { return new TestSuite( TestReifierCompareToMem.class ); }
43      
44      protected static Log logger = LogFactory.getLog( TestReifierCompareToMem.class );
45
46     Model modelrdb = null;
47     Model modelmem = null;
48     
49     IDBConnection conn = null;
50     
51     protected void setUp() throws java.lang.Exception JavaDoc {
52         conn = TestConnection.makeAndCleanTestConnection();
53         modelrdb = ModelRDB.createModel(conn);
54         modelmem = ModelFactory.createDefaultModel();
55     }
56     
57     protected void tearDown() throws java.lang.Exception JavaDoc {
58         modelrdb.close();
59         modelrdb = null;
60         conn.cleanDB();
61         conn.close();
62         conn = null;
63     }
64     
65     private void addRemove(Statement stmt) {
66         modelrdb.add(stmt);
67         modelmem.add(stmt);
68         
69         compareModels();
70         
71         modelrdb.remove(stmt);
72         modelmem.remove(stmt);
73         
74         compareModels();
75     }
76
77     private void compareModels() {
78         
79         Iterator JavaDoc it = modelmem.listStatements();
80         while( it.hasNext()) {
81             Statement s = (Statement)it.next();
82             if( ! modelrdb.contains(s)) {
83                 logger.error( "Statment:"+s+" is in mem but not rdf");
84                 logModel(modelmem, "Mem");
85                 logModel(modelrdb, "RDF");
86             }
87             assertTrue( modelrdb.contains(s));
88         }
89         it = modelrdb.listStatements();
90         while( it.hasNext()) {
91             Statement s = (Statement)it.next();
92             if( ! modelmem.contains(s)) {
93                 logger.error("Statment:"+s+" is in rdf but not memory");
94                 logModel(modelmem, "Mem");
95                 logModel(modelrdb, "RDF");
96             }
97             assertTrue( modelmem.contains(s));
98         }
99         
100         assertTrue( modelmem.size() == modelrdb.size() );
101     }
102     
103     private void logModel(Model m, String JavaDoc name) {
104         logger.debug("Model");
105         Iterator JavaDoc it = m.listStatements();
106         while( it.hasNext()) {
107             logger.debug( name + ": " + it.next() );
108 // Statement s = (Statement)it.next();
109
// RDFNode object = s.getObject();
110
// if( object instanceof Literal )
111
// logger.debug(name+": "+s.getSubject()+s.getPredicate()+((Literal)object).getValue()+" "+((Literal)object).getDatatype()+" "+((Literal)object).getLanguage());
112
// else
113
// logger.debug(name+": "+it.next());
114
}
115     }
116     
117     public void testAddPredicate() {
118  
119         Resource s = modelrdb.createResource("SSS"), o = modelrdb.createResource("OOO");
120         
121         Statement stmt = modelrdb.createStatement(s,RDF.object,o);
122         modelrdb.add(stmt);
123         modelmem.add(stmt);
124         
125         compareModels();
126         
127         modelrdb.remove(stmt);
128         modelmem.remove(stmt);
129         
130         compareModels();
131                 
132     }
133     
134     public void testAddRemoveFullReification() {
135         
136         Resource s = modelrdb.createResource("SSS"), p = modelrdb.createResource("PPP"), o = modelrdb.createResource("OOO");
137         
138         Statement stmtT = modelrdb.createStatement(s,RDF.type,RDF.Statement);
139         Statement stmtS = modelrdb.createStatement(s,RDF.subject,s);
140         Statement stmtP = modelrdb.createStatement(s,RDF.predicate,p);
141         Statement stmtO = modelrdb.createStatement(s,RDF.object,o);
142
143         modelrdb.add(stmtT);
144         modelmem.add(stmtT);
145
146         compareModels();
147         
148         modelrdb.add(stmtS);
149         modelmem.add(stmtS);
150
151         compareModels();
152         
153         modelrdb.add(stmtP);
154         modelmem.add(stmtP);
155
156         compareModels();
157         
158         modelrdb.add(stmtO);
159         modelmem.add(stmtO);
160         
161         compareModels();
162         
163         modelrdb.remove(stmtO);
164         modelmem.remove(stmtO);
165
166         compareModels();
167
168         modelrdb.remove(stmtP);
169         modelmem.remove(stmtP);
170
171         compareModels();
172
173         modelrdb.remove(stmtS);
174         modelmem.remove(stmtS);
175
176         compareModels();
177
178         modelrdb.remove(stmtT);
179         modelmem.remove(stmtT);
180
181         compareModels();
182     }
183     
184     public void testAddRemoveLiteralObject() {
185         Resource s = modelrdb.createResource("test#subject");
186         Literal l = modelrdb.createLiteral("testLiteral");
187         
188         addRemove( modelrdb.createStatement(s,RDF.object,l));
189     }
190
191     public void testAddRemoveHugeLiteralObject() {
192         String JavaDoc base = "This is a huge string that repeats.";
193         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
194         while(buffer.length() < 4000 )
195             buffer.append(base);
196         Resource s = modelrdb.createResource("test#subject");
197         Literal l = modelrdb.createLiteral(buffer.toString());
198         
199         addRemove( modelrdb.createStatement(s,RDF.object,l));
200     }
201     
202     public void testAddRemoveDatatypeObject() {
203         Resource s = modelrdb.createResource("test#subject");
204         Literal l = modelrdb.createTypedLiteral("stringType");
205         
206         addRemove( modelrdb.createStatement(s,RDF.object,l));
207     }
208
209     public void testAddRemoveHugeDatatypeObject() {
210         String JavaDoc base = "This is a huge string that repeats.";
211         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
212         while(buffer.length() < 4000 )
213             buffer.append(base);
214         Resource s = modelrdb.createResource("test#subject");
215         Literal l2 = modelrdb.createTypedLiteral(buffer.toString());
216         
217         addRemove( modelrdb.createStatement(s,RDF.object,l2));
218     }
219     
220     public void testAddRemoveHugeLiteral2Object() {
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 = modelmem.createResource("test#subject");
226         Literal l2 = modelmem.createLiteral(buffer.toString());
227         Literal l3 = modelmem.createLiteral(buffer.toString()+".");
228         
229         Statement st1 = modelmem.createStatement(s,RDF.object,l2);
230         Statement st2 = modelmem.createStatement(s,RDF.object,l3);
231         modelrdb.add(st1);
232         modelmem.add(st1);
233
234         compareModels();
235
236         modelrdb.add(st2);
237         modelmem.add(st2);
238         
239         compareModels();
240         
241         modelrdb.remove(st2);
242         modelmem.remove(st2);
243  
244         compareModels();
245
246         modelrdb.remove(st1);
247         modelmem.remove(st1);
248             
249     }
250     
251 }
252         
253
254 /*
255     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
256     All rights reserved.
257
258     Redistribution and use in source and binary forms, with or without
259     modification, are permitted provided that the following conditions
260     are met:
261
262     1. Redistributions of source code must retain the above copyright
263        notice, this list of conditions and the following disclaimer.
264
265     2. Redistributions in binary form must reproduce the above copyright
266        notice, this list of conditions and the following disclaimer in the
267        documentation and/or other materials provided with the distribution.
268
269     3. The name of the author may not be used to endorse or promote products
270        derived from this software without specific prior written permission.
271
272     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
273     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
274     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
275     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
276     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
277     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
278     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
279     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
280     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
281     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282 */

283
Popular Tags