001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestMultiModel.java,v 1.10 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 com.hp.hpl.jena.db.*;
026: import com.hp.hpl.jena.db.impl.IRDBDriver;
027: import com.hp.hpl.jena.rdf.model.*;
028: import com.hp.hpl.jena.vocabulary.DB;
029:
030: import junit.framework.TestCase;
031: import junit.framework.TestSuite;
032:
033: public class TestMultiModel extends TestCase {
034: String DefModel = GraphRDB.DEFAULT;
035:
036: public TestMultiModel(String name) {
037: super (name);
038: }
039:
040: public static TestSuite suite() {
041: return new TestSuite(TestMultiModel.class);
042: }
043:
044: Model model = null;
045: ModelRDB dmod1 = null;
046: ModelRDB dmod2 = null;
047: ModelRDB nmod1 = null;
048: ModelRDB nmod2 = null;
049: IDBConnection conn = null;
050: IRDBDriver dbDriver;
051:
052: protected void setUp() throws java.lang.Exception {
053: conn = TestConnection.makeAndCleanTestConnection();
054: dbDriver = conn.getDriver();
055: model = ModelRDB.createModel(conn);
056: conn.getDriver().setStoreWithModel(DefModel);
057: dmod1 = ModelRDB.createModel(conn, "Def_Model_1");
058: conn.getDriver().setStoreWithModel("Def_Model_1");
059: dmod2 = ModelRDB.createModel(conn, "Def_Model_2");
060: conn.getDriver().setStoreWithModel(null);
061: nmod1 = ModelRDB.createModel(conn, "Named_Model_1");
062: conn.getDriver().setStoreWithModel("Named_Model_1");
063: nmod2 = ModelRDB.createModel(conn, "Named_Model_2");
064: }
065:
066: protected void tearDown() throws java.lang.Exception {
067: dmod1.close();
068: dmod2.close();
069: nmod1.close();
070: nmod2.close();
071: conn.cleanDB();
072: conn.close();
073: conn = null;
074: }
075:
076: public void addToDBGraphProp(Model model, Property prop, String val) {
077: // first, get URI of the graph
078: StmtIterator iter = model.listStatements(new SimpleSelector(
079: null, DB.graphName, (RDFNode) null));
080: assertTrue(iter.hasNext());
081:
082: Statement stmt = iter.nextStatement();
083: assertTrue(iter.hasNext() == false);
084: Resource graphURI = stmt.getSubject();
085: Literal l = model.createLiteral(val);
086: Statement s = model.createStatement(graphURI, prop, l);
087: model.add(s);
088: assertTrue(model.contains(s));
089: }
090:
091: private void addOnModel(Model model, Statement stmt) {
092: model.add(stmt);
093: assertTrue(model.contains(stmt));
094: model.add(stmt);
095: assertTrue(model.contains(stmt));
096: }
097:
098: private void rmvOnModel(Model model, Statement stmt) {
099: assertTrue(model.contains(stmt));
100: model.remove(stmt);
101: assertTrue(!model.contains(stmt));
102: model.add(stmt);
103: assertTrue(model.contains(stmt));
104: model.remove(stmt);
105: assertTrue(!model.contains(stmt));
106: }
107:
108: private void addRemove(Statement stmt) {
109: long cnt = model.size();
110: addOnModel(model, stmt);
111: addOnModel(dmod1, stmt);
112: addOnModel(dmod2, stmt);
113: addOnModel(nmod1, stmt);
114: addOnModel(nmod2, stmt);
115: assertTrue(model.size() == (cnt + 1));
116: assertTrue(dmod1.size() == 1);
117: assertTrue(dmod2.size() == 1);
118: assertTrue(nmod1.size() == 1);
119: assertTrue(nmod2.size() == 1);
120:
121: rmvOnModel(nmod2, stmt);
122: rmvOnModel(nmod1, stmt);
123: rmvOnModel(dmod2, stmt);
124: rmvOnModel(dmod1, stmt);
125: rmvOnModel(model, stmt);
126: assertTrue(model.size() == cnt);
127: assertTrue(dmod1.size() == 0);
128: assertTrue(dmod2.size() == 0);
129: assertTrue(nmod1.size() == 0);
130: assertTrue(nmod2.size() == 0);
131: }
132:
133: public void testAddRemoveURI() {
134: Resource s = model.createResource("test#subject");
135: Property p = model.createProperty("test#predicate");
136: Resource o = model.createResource("test#object");
137:
138: addRemove(model.createStatement(s, p, o));
139: }
140:
141: public void testAddRemoveLiteral() {
142: Resource s = model.createResource("test#subject");
143: Property p = model.createProperty("test#predicate");
144: Literal l = model.createLiteral("testLiteral");
145:
146: addRemove(model.createStatement(s, p, l));
147: }
148:
149: public void testSetLongObjectLenFailure() {
150: try {
151: int len = dbDriver.getLongObjectLength();
152: dbDriver.setLongObjectLength(len / 2);
153: assertTrue(false);
154: } catch (Exception e) {
155: }
156: }
157:
158: public void testLongObjectLen() {
159: long longLen = dbDriver.getLongObjectLength();
160: assertTrue(longLen > 0 && longLen < 100000);
161:
162: String base = ".";
163: StringBuffer buffer = new StringBuffer(1024 + (int) longLen);
164: /* long minLongLen = longLen < 1024 ? longLen - (longLen/2) : longLen - 512; */
165: /* long maxLongLen = longLen + 1024; */
166: /* TODO: find out why this test takes sooooo long (minutes!) with the above bounds */
167: long minLongLen = longLen - 32;
168: long maxLongLen = longLen + 32;
169: assertTrue(minLongLen > 0);
170:
171: Resource s = model.createResource("test#subject");
172: Property p = model.createProperty("test#predicate");
173: Literal l;
174: Statement stmt;
175: while (buffer.length() < minLongLen) { /*( build base string */
176: buffer.append(base);
177: }
178: /* add stmts with long literals */
179: long modelSizeBeg = model.size();
180: while (buffer.length() < maxLongLen) {
181: l = model.createLiteral(buffer.toString());
182: stmt = model.createStatement(s, p, l);
183: model.add(stmt);
184: assertTrue(model.contains(stmt));
185: assertTrue(stmt.getObject().equals(l));
186: buffer.append(base);
187: }
188: assertTrue(model.size() == (modelSizeBeg + maxLongLen - minLongLen));
189: /* remove stmts with long literals */
190: while (buffer.length() > minLongLen) {
191: buffer.deleteCharAt(0);
192: l = model.createLiteral(buffer.toString());
193: stmt = model.createStatement(s, p, l);
194: assertTrue(model.contains(stmt));
195: model.remove(stmt);
196: assertTrue(!model.contains(stmt));
197: }
198: assertTrue(model.size() == modelSizeBeg);
199: }
200:
201: public void testSetLongObjectLen() {
202: int len = dbDriver.getLongObjectLength();
203: int len2 = len - 2;
204: try {
205: tearDown();
206: conn = TestConnection.makeTestConnection();
207: dbDriver = conn.getDriver();
208: len = dbDriver.getLongObjectLength();
209: dbDriver.setLongObjectLength(len2);
210: model = ModelRDB.createModel(conn);
211: } catch (Exception e) {
212: assertTrue(false);
213: }
214: testLongObjectLen();
215:
216: // now make sure longObjectValue persists
217: model.close();
218: try {
219: conn.close();
220: conn = TestConnection.makeTestConnection();
221: dbDriver = conn.getDriver();
222: assertTrue(len == dbDriver.getLongObjectLength());
223: model = ModelRDB.open(conn);
224: assertTrue(len2 == dbDriver.getLongObjectLength());
225: } catch (Exception e) {
226: assertTrue(false);
227: }
228: }
229:
230: public void testAddRemoveHugeLiteral() {
231: String base = Data.strLong;
232: StringBuffer buffer = new StringBuffer(4096);
233: while (buffer.length() < 4000)
234: buffer.append(base);
235: Resource s = model.createResource("test#subject");
236: Property p = model.createProperty("test#predicate");
237: Literal l = model.createLiteral(buffer.toString());
238:
239: addRemove(model.createStatement(s, p, l));
240: }
241:
242: public void testAddRemoveDatatype() {
243: Resource s = model.createResource("test#subject");
244: Property p = model.createProperty("test#predicate");
245: Literal l = model.createTypedLiteral("stringType");
246:
247: addRemove(model.createStatement(s, p, l));
248: }
249:
250: public void testAddRemoveHugeDatatype() {
251: String base = Data.strLong;
252: StringBuffer buffer = new StringBuffer(4096);
253: while (buffer.length() < 4000)
254: buffer.append(base);
255: Resource s = model.createResource("test#subject");
256: Property p = model.createProperty("test#predicate");
257: Literal l2 = model.createTypedLiteral(buffer.toString());
258:
259: addRemove(model.createStatement(s, p, l2));
260: }
261:
262: public void testAddRemoveBNode() {
263: Resource s = model.createResource();
264: Property p = model.createProperty("test#predicate");
265: Resource o = model.createResource();
266:
267: addRemove(model.createStatement(s, p, o));
268:
269: }
270:
271: public void testBNodeIdentityPreservation() {
272: Resource s = model.createResource();
273: Property p = model.createProperty("test#predicate");
274: Resource o = model.createResource();
275:
276: // Create two statements that differ only in
277: // the identity of the bnodes - then perform
278: // add-remove on one and verify the other is
279: // unchanged.
280: Statement spo = model.createStatement(s, p, o);
281: Statement ops = model.createStatement(o, p, s);
282: model.add(spo);
283: addRemove(ops);
284: assertTrue(model.contains(spo));
285: model.remove(spo);
286: }
287:
288: }
289:
290: /*
291: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
292: All rights reserved.
293:
294: Redistribution and use in source and binary forms, with or without
295: modification, are permitted provided that the following conditions
296: are met:
297:
298: 1. Redistributions of source code must retain the above copyright
299: notice, this list of conditions and the following disclaimer.
300:
301: 2. Redistributions in binary form must reproduce the above copyright
302: notice, this list of conditions and the following disclaimer in the
303: documentation and/or other materials provided with the distribution.
304:
305: 3. The name of the author may not be used to endorse or promote products
306: derived from this software without specific prior written permission.
307:
308: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
309: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
310: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
311: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
312: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
313: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
314: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
315: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
316: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
317: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
318: */
|