001: package org.apache.turbine.test;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.FileReader;
023: import java.sql.Connection;
024: import java.sql.DriverManager;
025: import java.sql.SQLException;
026: import java.sql.Statement;
027:
028: import org.apache.commons.lang.StringUtils;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.hsqldb.jdbcDriver;
032:
033: public class HsqlDB {
034: private Connection connection = null;
035: private static Log log = LogFactory.getLog(HsqlDB.class);
036:
037: public HsqlDB(String uri, String loadFile) throws Exception {
038: Class.forName(jdbcDriver.class.getName());
039:
040: this .connection = DriverManager.getConnection(uri, "sa", "");
041:
042: if (StringUtils.isNotEmpty(loadFile)) {
043: loadSqlFile(loadFile);
044: }
045: }
046:
047: public Connection getConnection() {
048: return connection;
049: }
050:
051: public void close() {
052: try {
053: connection.close();
054: } catch (Exception e) {
055: }
056: }
057:
058: private void loadSqlFile(String fileName) throws Exception {
059: Statement statement = null;
060: try {
061: statement = connection.createStatement();
062: String commands = getFileContents(fileName);
063:
064: for (int targetPos = commands.indexOf(';'); targetPos > -1; targetPos = commands
065: .indexOf(';')) {
066: String cmd = commands.substring(0, targetPos + 1)
067: .trim();
068:
069: if (cmd.startsWith("--")) {
070: // comment
071: int lineend = commands.indexOf('\n');
072: if (lineend > -1) {
073: targetPos = lineend - 1;
074: }
075: } else {
076: try {
077: statement.execute(cmd);
078: } catch (SQLException sqle) {
079: log.warn("Statement: " + cmd + ": "
080: + sqle.getMessage());
081: }
082: }
083:
084: commands = commands.substring(targetPos + 2);
085: }
086: } finally {
087: if (statement != null) {
088: statement.close();
089: }
090: }
091: }
092:
093: private String getFileContents(String fileName) throws Exception {
094: FileReader fr = new FileReader(fileName);
095:
096: char fileBuf[] = new char[1024];
097: StringBuffer sb = new StringBuffer(1000);
098: int res = -1;
099:
100: while ((res = fr.read(fileBuf, 0, 1024)) > -1) {
101: sb.append(fileBuf, 0, res);
102: }
103: fr.close();
104: return sb.toString();
105: }
106: }
|