View Javadoc

1   /*
2    * Copyright 2007 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.test;
17  
18  import java.io.File;
19  import java.sql.DriverManager;
20  
21  import org.apache.log4j.Logger;
22  import org.kuali.rice.core.api.config.ConfigurationException;
23  import org.kuali.rice.core.api.config.property.Config;
24  import org.kuali.rice.core.api.config.property.ConfigContext;
25  import org.kuali.rice.core.api.lifecycle.Lifecycle;
26  
27  public class DerbyDBCreationLifecycle implements Lifecycle {
28  	
29  	private static final Logger LOG = Logger.getLogger(DerbyDBCreationLifecycle.class);
30  	
31  	private String sqlFile;
32  	
33  	public DerbyDBCreationLifecycle(String sqlFile) {
34  		this.setSqlFile(sqlFile);
35  	}
36  
37  	public boolean isStarted() {
38  		return false;
39  	}
40  
41  	public void start() throws Exception {
42  		if (! isDoingDerby()) {
43  			LOG.info("Not using the Derby database for testing or no ddl file found");
44  			return;
45  		}
46  	
47  		//just checking that the driver's on the classpath and the url is valid
48          Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
49          DriverManager.getConnection(ConfigContext.getCurrentContextConfig().getProperty("datasource.url")).close();
50  		
51  		String dbLocation = ConfigContext.getCurrentContextConfig().getProperty("db.location");
52  		File db = new File(dbLocation);
53  		if (! db.exists()) {
54  			throw new ConfigurationException("Can't find db file " + dbLocation);
55  		}
56  		
57  		if (isDerbyDBReadyForTests()) {
58  			LOG.info("Derby ready for testing");
59  			return;
60  		}
61  		
62  		LOG.info("Setting up Derby for testing");
63  		LOG.info("Derby connection string: " + ConfigContext.getCurrentContextConfig().getProperty("datasource.url"));
64  		SQLDataLoader dataLoader = new SQLDataLoader(this.getSqlFile(), ";");
65  		dataLoader.runSql();
66  	}
67  
68  	public void stop() throws Exception {
69  		
70  	}
71  	
72  	private boolean isDerbyDBReadyForTests() {
73  		return new ClearDatabaseLifecycle().isTestTableInSchema(TestHarnessServiceLocator.getDataSource());
74  	}
75  	
76  	protected boolean isDoingDerby() {
77  		if (this.getSqlFile() == null) {
78  			return false;
79  		}
80  		String dbDriverName = ConfigContext.getCurrentContextConfig().getProperty(Config.DATASOURCE_DRIVER_NAME);
81  		if (dbDriverName == null) {
82  			throw new ConfigurationException("No property '" + Config.DATASOURCE_DRIVER_NAME + "' found");
83  		}
84  		return dbDriverName.toLowerCase().contains("derby");
85  	}
86  
87  	public String getSqlFile() {
88  		return sqlFile;
89  	}
90  
91  	public void setSqlFile(String sqlFile) {
92  		this.sqlFile = sqlFile;
93  	}
94  }