KickJava   Java API By Example, From Geeks To Geeks.

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


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

231
Popular Tags