001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestReifierCompareToMem.java,v 1.11 2008/01/02 12:08:14 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.rdf.model.*;
029: import com.hp.hpl.jena.vocabulary.RDF;
030:
031: import junit.framework.*;
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: public class TestReifierCompareToMem extends TestCase {
036:
037: public TestReifierCompareToMem(String name) {
038: super (name);
039: }
040:
041: public static TestSuite suite() {
042: return new TestSuite(TestReifierCompareToMem.class);
043: }
044:
045: protected static Log logger = LogFactory
046: .getLog(TestReifierCompareToMem.class);
047:
048: Model modelrdb = null;
049: Model modelmem = null;
050:
051: IDBConnection conn = null;
052:
053: protected void setUp() throws java.lang.Exception {
054: conn = TestConnection.makeAndCleanTestConnection();
055: modelrdb = ModelRDB.createModel(conn);
056: modelmem = ModelFactory.createDefaultModel();
057: }
058:
059: protected void tearDown() throws java.lang.Exception {
060: modelrdb.close();
061: modelrdb = null;
062: conn.cleanDB();
063: conn.close();
064: conn = null;
065: }
066:
067: private void addRemove(Statement stmt) {
068: modelrdb.add(stmt);
069: modelmem.add(stmt);
070:
071: compareModels();
072:
073: modelrdb.remove(stmt);
074: modelmem.remove(stmt);
075:
076: compareModels();
077: }
078:
079: private void compareModels() {
080:
081: Iterator it = modelmem.listStatements();
082: while (it.hasNext()) {
083: Statement s = (Statement) it.next();
084: if (!modelrdb.contains(s)) {
085: logger
086: .error("Statment:" + s
087: + " is in mem but not rdf");
088: logModel(modelmem, "Mem");
089: logModel(modelrdb, "RDF");
090: }
091: assertTrue(modelrdb.contains(s));
092: }
093: it = modelrdb.listStatements();
094: while (it.hasNext()) {
095: Statement s = (Statement) it.next();
096: if (!modelmem.contains(s)) {
097: logger.error("Statment:" + s
098: + " is in rdf but not memory");
099: logModel(modelmem, "Mem");
100: logModel(modelrdb, "RDF");
101: }
102: assertTrue(modelmem.contains(s));
103: }
104:
105: assertTrue(modelmem.size() == modelrdb.size());
106: }
107:
108: private void logModel(Model m, String name) {
109: logger.debug("Model");
110: Iterator it = m.listStatements();
111: while (it.hasNext()) {
112: logger.debug(name + ": " + it.next());
113: // Statement s = (Statement)it.next();
114: // RDFNode object = s.getObject();
115: // if( object instanceof Literal )
116: // logger.debug(name+": "+s.getSubject()+s.getPredicate()+((Literal)object).getValue()+" "+((Literal)object).getDatatype()+" "+((Literal)object).getLanguage());
117: // else
118: // logger.debug(name+": "+it.next());
119: }
120: }
121:
122: public void testAddPredicate() {
123:
124: Resource s = modelrdb.createResource("SSS"), o = modelrdb
125: .createResource("OOO");
126:
127: Statement stmt = modelrdb.createStatement(s, RDF.object, o);
128: modelrdb.add(stmt);
129: modelmem.add(stmt);
130:
131: compareModels();
132:
133: modelrdb.remove(stmt);
134: modelmem.remove(stmt);
135:
136: compareModels();
137:
138: }
139:
140: public void testAddRemoveFullReification() {
141:
142: Resource s = modelrdb.createResource("SSS"), p = modelrdb
143: .createResource("PPP"), o = modelrdb
144: .createResource("OOO");
145:
146: Statement stmtT = modelrdb.createStatement(s, RDF.type,
147: RDF.Statement);
148: Statement stmtS = modelrdb.createStatement(s, RDF.subject, s);
149: Statement stmtP = modelrdb.createStatement(s, RDF.predicate, p);
150: Statement stmtO = modelrdb.createStatement(s, RDF.object, o);
151:
152: modelrdb.add(stmtT);
153: modelmem.add(stmtT);
154:
155: compareModels();
156:
157: modelrdb.add(stmtS);
158: modelmem.add(stmtS);
159:
160: compareModels();
161:
162: modelrdb.add(stmtP);
163: modelmem.add(stmtP);
164:
165: compareModels();
166:
167: modelrdb.add(stmtO);
168: modelmem.add(stmtO);
169:
170: compareModels();
171:
172: modelrdb.remove(stmtO);
173: modelmem.remove(stmtO);
174:
175: compareModels();
176:
177: modelrdb.remove(stmtP);
178: modelmem.remove(stmtP);
179:
180: compareModels();
181:
182: modelrdb.remove(stmtS);
183: modelmem.remove(stmtS);
184:
185: compareModels();
186:
187: modelrdb.remove(stmtT);
188: modelmem.remove(stmtT);
189:
190: compareModels();
191: }
192:
193: public void testAddRemoveLiteralObject() {
194: Resource s = modelrdb.createResource("test#subject");
195: Literal l = modelrdb.createLiteral("testLiteral");
196:
197: addRemove(modelrdb.createStatement(s, RDF.object, l));
198: }
199:
200: public void testAddRemoveHugeLiteralObject() {
201: String base = Data.strLong;
202: StringBuffer buffer = new StringBuffer(4096);
203: while (buffer.length() < 4000)
204: buffer.append(base);
205: Resource s = modelrdb.createResource("test#subject");
206: Literal l = modelrdb.createLiteral(buffer.toString());
207:
208: addRemove(modelrdb.createStatement(s, RDF.object, l));
209: }
210:
211: public void testAddRemoveDatatypeObject() {
212: Resource s = modelrdb.createResource("test#subject");
213: Literal l = modelrdb.createTypedLiteral("stringType");
214:
215: addRemove(modelrdb.createStatement(s, RDF.object, l));
216: }
217:
218: public void testAddRemoveHugeDatatypeObject() {
219: String base = Data.strLong;
220: StringBuffer buffer = new StringBuffer(4096);
221: while (buffer.length() < 4000)
222: buffer.append(base);
223: Resource s = modelrdb.createResource("test#subject");
224: Literal l2 = modelrdb.createTypedLiteral(buffer.toString());
225:
226: addRemove(modelrdb.createStatement(s, RDF.object, l2));
227: }
228:
229: public void testAddRemoveHugeLiteral2Object() {
230: String base = Data.strLong;
231: StringBuffer buffer = new StringBuffer(4096);
232: while (buffer.length() < 4000)
233: buffer.append(base);
234: Resource s = modelmem.createResource("test#subject");
235: Literal l2 = modelmem.createLiteral(buffer.toString());
236: Literal l3 = modelmem.createLiteral(buffer.toString() + ".");
237:
238: Statement st1 = modelmem.createStatement(s, RDF.object, l2);
239: Statement st2 = modelmem.createStatement(s, RDF.object, l3);
240: modelrdb.add(st1);
241: modelmem.add(st1);
242:
243: compareModels();
244:
245: modelrdb.add(st2);
246: modelmem.add(st2);
247:
248: compareModels();
249:
250: modelrdb.remove(st2);
251: modelmem.remove(st2);
252:
253: compareModels();
254:
255: modelrdb.remove(st1);
256: modelmem.remove(st1);
257:
258: }
259:
260: }
261:
262: /*
263: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
264: All rights reserved.
265:
266: Redistribution and use in source and binary forms, with or without
267: modification, are permitted provided that the following conditions
268: are met:
269:
270: 1. Redistributions of source code must retain the above copyright
271: notice, this list of conditions and the following disclaimer.
272:
273: 2. Redistributions in binary form must reproduce the above copyright
274: notice, this list of conditions and the following disclaimer in the
275: documentation and/or other materials provided with the distribution.
276:
277: 3. The name of the author may not be used to endorse or promote products
278: derived from this software without specific prior written permission.
279:
280: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
281: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
282: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
283: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
284: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
285: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
286: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
288: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
289: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
290: */
|