001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestCompatability.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 open/create operations on the modelRDB.
012: *
013: * To run, you must have a mySQL database operational on
014: * localhost with a database name of "test" and allow use
015: * by a user named "test" with an empty password.
016: *
017: * NOTE - this will generate deprecated API warnings when compiled -
018: * this is deliberate - it is designed to test the deprecated APIs
019: * to make sure they still work.
020: *
021: * (copied from the Jena 1 RDB tests written by der).
022: *
023: * The only code changes from Jena 1 ModelRDB operations are the
024: * in the cleanup code (it was calling getStore()).
025: *
026: * @author csayers
027: * @version $Revision: 1.10 $
028: */
029:
030: import com.hp.hpl.jena.rdf.model.*;
031: import com.hp.hpl.jena.db.*;
032: import com.hp.hpl.jena.db.impl.*;
033: import com.hp.hpl.jena.regression.*;
034: import com.hp.hpl.jena.shared.*;
035:
036: import junit.framework.*;
037:
038: public class TestCompatability extends TestCase {
039:
040: public TestCompatability(String name) {
041: super (name);
042: }
043:
044: protected void setUp() throws java.lang.Exception {
045: }
046:
047: protected void tearDown() throws java.lang.Exception {
048: }
049:
050: public static TestSuite suite() {
051: ConfigTestCaseRDB config = new ConfigTestCaseRDB(
052: TestPackage.M_DB_URL, TestPackage.M_DB_USER,
053: TestPackage.M_DB_PASSWD, "Generic", TestPackage.M_DB);
054:
055: TestSuite suite = new TestSuite();
056: suite.addTest(new TestCaseRDB("test0", config));
057: suite.addTest(new TestCaseRDB("test1", config));
058: suite.addTest(new TestCaseRDB("test2", config));
059: suite.addTest(new TestCaseRDB("test3", config));
060: suite.addTest(new TestCaseRDB("test4", config));
061: suite.addTest(new TestCaseRDB("test5", config));
062: suite.addTest(new TestCaseRDB("test6", config));
063: suite.addTest(new TestCaseRDB("test7", config));
064: suite.addTest(new TestCaseRDB("test8", config));
065: suite.addTest(new TestCaseRDB("test9", config));
066: suite.addTest(new TestCaseRDB("test10", config));
067: suite.addTest(new TestCaseRDB("test11", config));
068: suite.addTest(new TestCaseRDB("test12", config));
069: suite.addTest(new TestCaseRDB("test13", config));
070: suite.addTest(new TestCaseRDB("test14", config));
071: suite.addTest(new TestCaseRDB("test15", config));
072: suite.addTest(new TestCaseRDB("test16", config));
073: suite.addTest(new TestCaseRDB("test17", config));
074: suite.addTest(new TestCaseRDB("test18", config));
075: suite.addTest(new TestCaseRDB("test19", config));
076:
077: return suite;
078:
079: }
080:
081: /** Inner class which provides config information to TestCaseRDB */
082:
083: protected static class ConfigTestCaseRDB {
084:
085: /** base uri for the test databases*/
086: String m_baseuri;
087:
088: /** User name for access the databases */
089: String m_user;
090:
091: /** Password for this user */
092: String m_password;
093:
094: /** table layout version to test */
095: String m_layout;
096:
097: /** database type under test */
098: String m_databaseType;
099:
100: /** flag if this database config supports multiple models per database */
101:
102: boolean supportsMultipleModels;
103:
104: /** flag if this database config supports jena-style reification */
105:
106: boolean supportsJenaReification;
107:
108: /** flag if the tearDown code should leave the DB tables intact by doing a manual database cleanup */
109:
110: boolean noReformat;
111:
112: /** Database connection */
113:
114: DBConnection m_dbconn = null;
115:
116: /** Create config.
117: * Needs a base uri for the database, user name and login, format and database type to test.
118: * For databases which support multiple models given the whole database uri. For single model
119: * databases give a base uri to which the model names should be appended.
120: */
121:
122: ConfigTestCaseRDB(String baseuri, String user, String password,
123: String layout, String database) {
124:
125: m_baseuri = baseuri;
126: m_user = user;
127: m_password = password;
128: m_layout = layout;
129: m_databaseType = database;
130:
131: try {
132: Class.forName(TestPackage.M_DBDRIVER_CLASS); // ADDED
133: } catch (Exception e) {
134: throw new JenaException(
135: "Unable to instantiate driver: "
136: + TestPackage.M_DBDRIVER_CLASS);
137: }
138:
139: try {
140: DBConnection temp = new DBConnection(baseuri, user,
141: password);
142: IRDBDriver driver = temp.getDriver(layout, database);
143: supportsMultipleModels = driver
144: .supportsMultipleModels();
145: supportsJenaReification = driver
146: .supportsJenaReification();
147: } catch (RDFRDBException e) {
148:
149: supportsMultipleModels = false;
150: }
151: noReformat = false;
152: }
153:
154: /** Create config.
155: * Needs a base uri for the database, user name and login, format and database type to test.
156: * For databases which support multiple models given the whole database uri. For single model
157: * databases give a base uri to which the model names should be appended.
158: */
159:
160: ConfigTestCaseRDB(String baseuri, String user, String password,
161: String layout, String database, boolean noReformat) {
162: this (baseuri, user, password, layout, database);
163: this .noReformat = noReformat;
164: }
165:
166: /** Create a model of the given name for this database config */
167:
168: ModelRDB createModel(String name) {
169: if (supportsMultipleModels) {
170: if (m_dbconn == null) {
171: m_dbconn = new DBConnection(m_baseuri, m_user,
172: m_password, m_databaseType);
173: if (!m_dbconn.isFormatOK()) {
174: IRDBDriver driver = m_dbconn.getDriver(
175: m_layout, m_databaseType);
176: driver.formatDB();
177: }
178: }
179: if (m_dbconn.containsModel(name))
180: ModelRDB.deleteModel(m_dbconn, name);
181: return ModelRDB.createModel(m_dbconn, name);
182: } else {
183: DBConnection dbcon = new DBConnection(m_baseuri + name,
184: m_user, m_password);
185: if (m_dbconn.containsDefaultModel())
186: try {
187: m_dbconn.cleanDB();
188: } catch (Exception e) {
189: }
190: ;
191: return ModelRDB.create(dbcon, m_layout, m_databaseType);
192: }
193: }
194: } /// End of inner class ConfigTestCaseRDB
195:
196: /** Adapt the overall jena test suite to use an RDB store */
197:
198: protected static class TestCaseRDB extends TestCaseBasic {
199:
200: ConfigTestCaseRDB m_config;
201:
202: public TestCaseRDB(String name, ConfigTestCaseRDB config) {
203: super (name);
204: m_config = config;
205: }
206:
207: // Override set up to create RDB models instead of mem models
208: public void setUp() {
209: m1 = m_config.createModel("jr1");
210: m2 = m_config.createModel("jr2");
211: m3 = m_config.createModel("jr3");
212: m4 = m_config.createModel("jr4");
213: }
214:
215: public void tearDown() {
216: if (m_config.supportsMultipleModels && !m_config.noReformat) {
217: // The brute force clean deletes the entire DB so only need to do it once
218: cleanModel((ModelRDB) m1);
219: m2.close();
220: m3.close();
221: m4.close();
222: // Close connection so next time it reformats the DB
223:
224: try {
225: m_config.m_dbconn.close();
226: } catch (java.sql.SQLException e) {
227: System.out
228: .println("Problem during db clean up in regression test");
229: }
230: m_config.m_dbconn = null;
231: } else {
232: cleanModel((ModelRDB) m1);
233: cleanModel((ModelRDB) m2);
234: cleanModel((ModelRDB) m3);
235: cleanModel((ModelRDB) m4);
236: }
237: }
238:
239: private void cleanModel(ModelRDB m) {
240: try {
241: if (m_config.noReformat) {
242: // Do a slow, brute force manual clean to avoid the database getting reformatted
243: // This is needed to supporting checking of legacy formats
244: for (StmtIterator i = m.listStatements(); i
245: .hasNext();) {
246: i.next();
247: i.remove();
248: }
249: } else {
250: // Turn off messages about deleting tables that aren't there any more.
251: //int l = Log.getInstance().getLevel();
252: //Log.getInstance().setLevel(Log.SEVERE);
253: //IRDBDriver driver = m.getStore().getDriver();
254: //driver.cleanDB();
255: //driver.close();
256: m.getConnection().cleanDB();
257: m.close();
258: //Log.getInstance().setLevel(l);
259: }
260: } catch (Exception e) {
261: assertTrue(
262: "Problem clearning up regression databases: "
263: + e, false);
264: }
265: }
266: } // End of inner class TestCaseRDB
267: }
268:
269: /*
270: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
271: All rights reserved.
272:
273: Redistribution and use in source and binary forms, with or without
274: modification, are permitted provided that the following conditions
275: are met:
276:
277: 1. Redistributions of source code must retain the above copyright
278: notice, this list of conditions and the following disclaimer.
279:
280: 2. Redistributions in binary form must reproduce the above copyright
281: notice, this list of conditions and the following disclaimer in the
282: documentation and/or other materials provided with the distribution.
283:
284: 3. The name of the author may not be used to endorse or promote products
285: derived from this software without specific prior written permission.
286:
287: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
288: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
289: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
290: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
291: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
292: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
293: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
294: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
295: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
296: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
297: */
|