001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.rice.test;
017:
018: import java.io.File;
019: import java.sql.DriverManager;
020:
021: import org.apache.log4j.Logger;
022: import org.kuali.rice.config.ConfigurationException;
023: import org.kuali.rice.core.Core;
024: import org.kuali.rice.lifecycle.Lifecycle;
025:
026: public class DerbyDBCreationLifecycle implements Lifecycle {
027:
028: private static final Logger LOG = Logger
029: .getLogger(DerbyDBCreationLifecycle.class);
030:
031: private String sqlFile;
032:
033: public DerbyDBCreationLifecycle(String sqlFile) {
034: this .setSqlFile(sqlFile);
035: }
036:
037: public boolean isStarted() {
038: return false;
039: }
040:
041: public void start() throws Exception {
042: if (!isDoingDerby()) {
043: LOG
044: .info("Not using the Derby database for testing or no ddl file found");
045: return;
046: }
047:
048: //just checking that the driver's on the classpath and the url is valid
049: Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
050: DriverManager.getConnection(
051: Core.getCurrentContextConfig().getProperty(
052: "datasource.url")).close();
053:
054: String dbLocation = Core.getCurrentContextConfig().getProperty(
055: "db.location");
056: File db = new File(dbLocation);
057: if (!db.exists()) {
058: throw new ConfigurationException("Can't find db file "
059: + dbLocation);
060: }
061:
062: if (isDerbyDBReadyForTests()) {
063: LOG.info("Derby ready for testing");
064: return;
065: }
066:
067: LOG.info("Setting up Derby for testing");
068: LOG.info("Derby connection string: "
069: + Core.getCurrentContextConfig().getProperty(
070: "datasource.url"));
071: SQLDataLoader dataLoader = new SQLDataLoader(this .getSqlFile(),
072: ";");
073: dataLoader.runSql();
074: }
075:
076: public void stop() throws Exception {
077:
078: }
079:
080: private boolean isDerbyDBReadyForTests() {
081: return new ClearDatabaseLifecycle()
082: .isTestTableInSchema(TestHarnessServiceLocator
083: .getDataSource());
084: }
085:
086: protected boolean isDoingDerby() {
087: if (sqlFile == null) {
088: return false;
089: }
090: String dbDriverName = Core.getCurrentContextConfig()
091: .getProperty("datasource.driver.name");
092: if (dbDriverName == null) {
093: throw new ConfigurationException(
094: "No property 'datasource.driver.name' found");
095: }
096: return dbDriverName.toLowerCase().indexOf("derby") > -1;
097: }
098:
099: public String getSqlFile() {
100: return sqlFile;
101: }
102:
103: public void setSqlFile(String sqlFile) {
104: this.sqlFile = sqlFile;
105: }
106: }
|