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