Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5d6271219112b5e603fd20d3f23329f0273ffe0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007, 2008 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal
 * </copyright>
 *
 * $Id: BaseTestDatabaseAdapter.java,v 1.4 2008/03/30 15:12:08 mtaal Exp $
 */

package org.eclipse.emf.teneo.test.stores;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.teneo.test.StoreTestException;

/**
 * Is a general test database adapter. This adapter hides details related to running the testcass
 * with different databases. The database specific actions are relatively simple, drop and create a
 * database and possibly specific such as setting lower or uppercase name handling in the database.
 * For handling other databases this class can be extended.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.4 $
 */

public class BaseTestDatabaseAdapter implements TestDatabaseAdapter {
	/** The logger */
	private static Log log = LogFactory.getLog(BaseTestDatabaseAdapter.class);

	/** The database url */
	protected String dbUrl;

	/** The database driver */
	protected String dbDriver;

	/** The database user */
	protected String dbUser;

	/** The database pwd */
	protected String dbPwd;

	/** The database name */
	protected String dbName = null;

	/** The logInfo, is used to display connection information */
	protected String logInfo = "Replace this with a meaningfull message";

	/** Use optimistic transactions or not */
	protected boolean optimistic;

	/** Initializes the database adapter */
	public void initialize(Properties props) {
		dbUrl = props.getProperty("dburl");
		dbDriver = props.getProperty("dbdriver");
		dbUser = props.getProperty("dbuser");
		dbPwd = props.getProperty("dbpassword");
		optimistic = props.getProperty("optimistic") != null && props.getProperty("optimistic").compareTo("true") == 0;
		logInfo =
				" adapter|dbName|url|user|pwd|driver " +
						this.getClass().getName().substring(this.getClass().getName().lastIndexOf(".") + 1) + "|" +
						dbName + "|" + dbUrl + "|" + dbUser + "|" + dbPwd + "|" + dbDriver;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.test.stores.TestDatabaseAdapter#isOptimistic()
	 */
	public boolean isOptimistic() {
		return optimistic;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.test.stores.TestDatabaseAdapter#getDbDriver()
	 */
	public String getDbDriver() {
		return dbDriver;
	}

	/** Returns a log messages which is placed when a testrun starts for this database adapter */
	public String getLogMessage() {
		return "Starting run for database: " + logInfo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.test.stores.TestDatabaseAdapter#getDbName()
	 */
	public String getDbName() {
		return dbName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.test.stores.TestDatabaseAdapter#setDbName(java.lang.String)
	 */
	public void setDbName(String databaseName) {
		dbName = databaseName;

		// set the loginfo
		logInfo =
				" adapter|dbName|url|user|pwd|driver " +
						this.getClass().getName().substring(this.getClass().getName().lastIndexOf(".") + 1) + "|" +
						dbName + "|" + dbUrl + "|" + dbUser + "|" + dbPwd + "|" + dbDriver;

		log.debug("Initialized database adapter: " + logInfo);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.test.stores.TestDatabaseAdapter#getDbPwd()
	 */
	public String getDbPwd() {
		return dbPwd;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.test.stores.TestDatabaseAdapter#getDbUrl()
	 */
	public String getDbUrl() {
		if (dbUrl.endsWith(":")) { // hsqldb
			return dbUrl + dbName;
		} else if (dbUrl.endsWith("/")) {
			return dbUrl + dbName;
		} else {
			return dbUrl + "/" + dbName;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.test.stores.TestDatabaseAdapter#getDbUser()
	 */
	public String getDbUser() {
		return dbUser;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.test.stores.TestDatabaseAdapter#createDatabase()
	 */
	public void createDatabase() {
		try {
			log.info("Creating database: " + logInfo);

			final Driver driver = (Driver) Class.forName(dbDriver).newInstance();
			Properties info = new Properties();
			info.put("user", dbUser);
			info.put("password", dbPwd);
			Connection conn = null;
			Statement stmt = null;
			try {
				conn = driver.connect(dbUrl, info);
				stmt = conn.createStatement();
				conn.setAutoCommit(true);
				stmt.execute("CREATE DATABASE " + dbName + ";");
			} finally {
				if (stmt != null) {
					stmt.close();
				}
				if (conn != null) {
					conn.close();
				}
			}
		} catch (Exception e) {
			throw new StoreTestException("Exception while creating database: " + logInfo, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.test.stores.TestDatabaseAdapter#dropDatabase()
	 */
	public void dropDatabase() {
		try {
			final Driver driver = (Driver) Class.forName(dbDriver).newInstance();
			Properties info = new Properties();
			info.put("user", dbUser);
			info.put("password", dbPwd);
			Connection conn = null;
			Statement stmt = null;
			try {
				conn = driver.connect(dbUrl, info);
				final ResultSet rs = conn.getMetaData().getCatalogs();
				boolean exists = false;
				while (rs.next()) {
					final String existingDb = rs.getString(1);
					exists |= existingDb.compareToIgnoreCase(dbName) == 0;
				}
				rs.close();

				if (exists) {
					stmt = conn.createStatement();
					conn.setAutoCommit(true);
					log.info("Dropping database: " + logInfo);
					stmt.execute("DROP DATABASE " + dbName + ";");
				} else {
					log.info("Database does not exist, not dropped, " + logInfo);
				}
			} finally {
				if (stmt != null) {
					stmt.close();
				}
				if (conn != null) {
					conn.close();
				}
			}
		} catch (Exception e) {
			throw new StoreTestException("Exception while dropping database: " + logInfo, e);
		}
	}

	/** Gets a database connection using the adapters connection info */
	public Connection getConnection() {
		try {
			final Driver driver = (Driver) Class.forName(dbDriver).newInstance();
			Properties info = new Properties();
			info.put("user", dbUser);
			info.put("password", dbPwd);
			return driver.connect(getDbUrl(), info);
		} catch (Exception e) {
			throw new StoreTestException("Exception while creating database: " + logInfo, e);
		}
	}

	/** Returns true if the passed database exists */
	protected boolean databaseExists(Connection conn) throws Exception {
		final String localDbName = getDbName();
		final ResultSet rs = conn.getMetaData().getCatalogs();
		boolean exists = false;
		while (rs.next()) {
			final String existingDb = rs.getString(1);
			exists = exists || existingDb.compareTo(localDbName) == 0;
		}
		rs.close();
		return exists;
	}
}

Back to the top