KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestCompatability.java,v 1.7 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 open/create operations on the modelRDB.
12  *
13  * To run, you must have a mySQL database operational on
14  * localhost with a database name of "test" and allow use
15  * by a user named "test" with an empty password.
16  *
17  * NOTE - this will generate deprecated API warnings when compiled -
18  * this is deliberate - it is designed to test the deprecated APIs
19  * to make sure they still work.
20  *
21  * (copied from the Jena 1 RDB tests written by der).
22  *
23  * The only code changes from Jena 1 ModelRDB operations are the
24  * in the cleanup code (it was calling getStore()).
25  *
26  * @author csayers
27  * @version $Revision: 1.7 $
28 */

29
30 import com.hp.hpl.jena.rdf.model.*;
31 import com.hp.hpl.jena.db.*;
32 import com.hp.hpl.jena.db.impl.*;
33 import com.hp.hpl.jena.regression.*;
34 import com.hp.hpl.jena.shared.*;
35
36 import junit.framework.*;
37
38
39 public class TestCompatability extends TestCase {
40         
41     public TestCompatability( String JavaDoc name )
42         { super( name ); }
43         
44     protected void setUp() throws java.lang.Exception JavaDoc {
45     }
46     
47     protected void tearDown() throws java.lang.Exception JavaDoc {
48     }
49     
50
51     public static TestSuite suite() {
52         ConfigTestCaseRDB config = new ConfigTestCaseRDB(TestPackage.M_DB_URL, TestPackage.M_DB_USER, TestPackage.M_DB_PASSWD, "Generic", TestPackage.M_DB );
53         
54         TestSuite suite = new TestSuite();
55         suite.addTest(new TestCaseRDB("test0", config));
56         suite.addTest(new TestCaseRDB("test1", config));
57         suite.addTest(new TestCaseRDB("test2", config));
58         suite.addTest(new TestCaseRDB("test3", config));
59         suite.addTest(new TestCaseRDB("test4", config));
60         suite.addTest(new TestCaseRDB("test5", config));
61         suite.addTest(new TestCaseRDB("test6", config));
62         suite.addTest(new TestCaseRDB("test7", config));
63         suite.addTest(new TestCaseRDB("test8", config));
64         suite.addTest(new TestCaseRDB("test9", config));
65         suite.addTest(new TestCaseRDB("test10", config));
66         suite.addTest(new TestCaseRDB("test11", config));
67         suite.addTest(new TestCaseRDB("test12", config));
68         suite.addTest(new TestCaseRDB("test13", config));
69         suite.addTest(new TestCaseRDB("test14", config));
70         suite.addTest(new TestCaseRDB("test15", config));
71         suite.addTest(new TestCaseRDB("test16", config));
72         suite.addTest(new TestCaseRDB("test17", config));
73         suite.addTest(new TestCaseRDB("test18", config));
74         suite.addTest(new TestCaseRDB("test19", config));
75
76         return suite;
77
78     }
79
80
81
82     /** Inner class which provides config information to TestCaseRDB */
83
84     protected static class ConfigTestCaseRDB {
85
86         /** base uri for the test databases*/
87         String JavaDoc m_baseuri;
88
89         /** User name for access the databases */
90         String JavaDoc m_user;
91
92         /** Password for this user */
93         String JavaDoc m_password;
94
95         /** table layout version to test */
96         String JavaDoc m_layout;
97
98         /** database type under test */
99         String JavaDoc m_databaseType;
100
101         /** flag if this database config supports multiple models per database */
102
103         boolean supportsMultipleModels;
104
105         /** flag if this database config supports jena-style reification */
106
107         boolean supportsJenaReification;
108
109         /** flag if the tearDown code should leave the DB tables intact by doing a manual database cleanup */
110
111         boolean noReformat;
112
113         /** Database connection */
114
115         DBConnection m_dbconn = null;
116
117         /** Create config.
118          * Needs a base uri for the database, user name and login, format and database type to test.
119          * For databases which support multiple models given the whole database uri. For single model
120          * databases give a base uri to which the model names should be appended.
121          */

122
123         ConfigTestCaseRDB(String JavaDoc baseuri, String JavaDoc user, String JavaDoc password, String JavaDoc layout, String JavaDoc 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 JavaDoc e) {
134                 throw new JenaException("Unable to instantiate driver: " + TestPackage.M_DBDRIVER_CLASS);
135             }
136
137             try {
138                 DBConnection temp = new DBConnection(baseuri, user, password);
139                 IRDBDriver driver = temp.getDriver(layout, database);
140                 supportsMultipleModels = driver.supportsMultipleModels();
141                 supportsJenaReification = driver.supportsJenaReification();
142             } catch (RDFRDBException e) {
143
144                 supportsMultipleModels = false;
145             }
146             noReformat = false;
147         }
148
149         /** Create config.
150          * Needs a base uri for the database, user name and login, format and database type to test.
151          * For databases which support multiple models given the whole database uri. For single model
152          * databases give a base uri to which the model names should be appended.
153          */

154
155         ConfigTestCaseRDB(
156             String JavaDoc baseuri,
157             String JavaDoc user,
158             String JavaDoc password,
159             String JavaDoc layout,
160             String JavaDoc database,
161             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 JavaDoc name) {
169             if (supportsMultipleModels) {
170                 if (m_dbconn == null) {
171                     m_dbconn = new DBConnection(m_baseuri, m_user, m_password, m_databaseType);
172                     if (!m_dbconn.isFormatOK()) {
173                         IRDBDriver driver = m_dbconn.getDriver(m_layout, m_databaseType);
174                         driver.formatDB();
175                     }
176                 }
177                 if ( m_dbconn.containsModel(name) )
178                     ModelRDB.deleteModel(m_dbconn,name);
179                 return ModelRDB.createModel(m_dbconn, name);
180             } else {
181                 DBConnection dbcon = new DBConnection(m_baseuri + name, m_user, m_password);
182                 if ( m_dbconn.containsDefaultModel() )
183                     try {m_dbconn.cleanDB(); } catch (Exception JavaDoc e) {};
184                 return ModelRDB.create(dbcon, m_layout, m_databaseType);
185             }
186         }
187     } /// End of inner class ConfigTestCaseRDB
188

189     /** Adapt the overall jena test suite to use an RDB store */
190
191     protected static class TestCaseRDB extends TestCaseBasic {
192
193         ConfigTestCaseRDB m_config;
194
195         public TestCaseRDB(String JavaDoc name, ConfigTestCaseRDB config) {
196             super(name);
197             m_config = config;
198         }
199         
200         // Override set up to create RDB models instead of mem models
201
public void setUp() {
202             m1 = m_config.createModel("jr1");
203             m2 = m_config.createModel("jr2");
204             m3 = m_config.createModel("jr3");
205             m4 = m_config.createModel("jr4");
206         }
207
208         public void tearDown() {
209             if (m_config.supportsMultipleModels && !m_config.noReformat) {
210                 // The brute force clean deletes the entire DB so only need to do it once
211
cleanModel((ModelRDB) m1);
212                 m2.close();
213                 m3.close();
214                 m4.close();
215                 // Close connection so next time it reformats the DB
216

217                 try {
218                     m_config.m_dbconn.close();
219                 } catch (java.sql.SQLException JavaDoc e) {
220                     System.out.println("Problem during db clean up in regression test");
221                 }
222                 m_config.m_dbconn = null;
223             } else {
224                 cleanModel((ModelRDB) m1);
225                 cleanModel((ModelRDB) m2);
226                 cleanModel((ModelRDB) m3);
227                 cleanModel((ModelRDB) m4);
228             }
229         }
230
231         private void cleanModel(ModelRDB m) {
232             try {
233                 if (m_config.noReformat) {
234                     // Do a slow, brute force manual clean to avoid the database getting reformatted
235
// This is needed to supporting checking of legacy formats
236
for (StmtIterator i = m.listStatements(); i.hasNext();) {
237                         i.next();
238                         i.remove();
239                     }
240                 } else {
241                     // Turn off messages about deleting tables that aren't there any more.
242
//int l = Log.getInstance().getLevel();
243
//Log.getInstance().setLevel(Log.SEVERE);
244
//IRDBDriver driver = m.getStore().getDriver();
245
//driver.cleanDB();
246
//driver.close();
247
m.getConnection().cleanDB();
248                     m.close();
249                     //Log.getInstance().setLevel(l);
250
}
251             } catch (Exception JavaDoc e) {
252                 assertTrue("Problem clearning up regression databases: " + e, false);
253             }
254         }
255     } // End of inner class TestCaseRDB
256
}
257
258     /*
259         (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
260         All rights reserved.
261     
262         Redistribution and use in source and binary forms, with or without
263         modification, are permitted provided that the following conditions
264         are met:
265     
266         1. Redistributions of source code must retain the above copyright
267            notice, this list of conditions and the following disclaimer.
268     
269         2. Redistributions in binary form must reproduce the above copyright
270            notice, this list of conditions and the following disclaimer in the
271            documentation and/or other materials provided with the distribution.
272     
273         3. The name of the author may not be used to endorse or promote products
274            derived from this software without specific prior written permission.
275     
276         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
277         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
278         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
279         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
280         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
281         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
282         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
283         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
284         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
285         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286     */

287
Popular Tags