001    /*
002     * Copyright 2007 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.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/ecl2.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.core.api.config.ConfigurationException;
023    import org.kuali.rice.core.api.config.property.Config;
024    import org.kuali.rice.core.api.config.property.ConfigContext;
025    import org.kuali.rice.core.api.lifecycle.Lifecycle;
026    
027    public class DerbyDBCreationLifecycle implements Lifecycle {
028            
029            private static final Logger LOG = Logger.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.info("Not using the Derby database for testing or no ddl file found");
044                            return;
045                    }
046            
047                    //just checking that the driver's on the classpath and the url is valid
048            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
049            DriverManager.getConnection(ConfigContext.getCurrentContextConfig().getProperty("datasource.url")).close();
050                    
051                    String dbLocation = ConfigContext.getCurrentContextConfig().getProperty("db.location");
052                    File db = new File(dbLocation);
053                    if (! db.exists()) {
054                            throw new ConfigurationException("Can't find db file " + dbLocation);
055                    }
056                    
057                    if (isDerbyDBReadyForTests()) {
058                            LOG.info("Derby ready for testing");
059                            return;
060                    }
061                    
062                    LOG.info("Setting up Derby for testing");
063                    LOG.info("Derby connection string: " + ConfigContext.getCurrentContextConfig().getProperty("datasource.url"));
064                    SQLDataLoader dataLoader = new SQLDataLoader(this.getSqlFile(), ";");
065                    dataLoader.runSql();
066            }
067    
068            public void stop() throws Exception {
069                    
070            }
071            
072            private boolean isDerbyDBReadyForTests() {
073                    return new ClearDatabaseLifecycle().isTestTableInSchema(TestHarnessServiceLocator.getDataSource());
074            }
075            
076            protected boolean isDoingDerby() {
077                    if (this.getSqlFile() == null) {
078                            return false;
079                    }
080                    String dbDriverName = ConfigContext.getCurrentContextConfig().getProperty(Config.DATASOURCE_DRIVER_NAME);
081                    if (dbDriverName == null) {
082                            throw new ConfigurationException("No property '" + Config.DATASOURCE_DRIVER_NAME + "' found");
083                    }
084                    return dbDriverName.toLowerCase().contains("derby");
085            }
086    
087            public String getSqlFile() {
088                    return sqlFile;
089            }
090    
091            public void setSqlFile(String sqlFile) {
092                    this.sqlFile = sqlFile;
093            }
094    }