001package 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 022import java.io.FileReader; 023import java.sql.Connection; 024import java.sql.DriverManager; 025import java.sql.SQLException; 026import java.sql.Statement; 027 028import org.apache.commons.lang.StringUtils; 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.hsqldb.jdbcDriver; 032 033public class HsqlDB 034{ 035 private Connection connection = null; 036 private static Log log = LogFactory.getLog(HsqlDB.class); 037 038 public HsqlDB(String uri, String loadFile) 039 throws Exception 040 { 041 Class.forName(jdbcDriver.class.getName()); 042 043 this.connection = DriverManager.getConnection(uri, "sa", ""); 044 045 if (StringUtils.isNotEmpty(loadFile)) 046 { 047 loadSqlFile(loadFile); 048 } 049 } 050 051 public Connection getConnection() 052 { 053 return connection; 054 } 055 056 public void close() 057 { 058 try 059 { 060 connection.close(); 061 } 062 catch (Exception e) 063 { 064 // ignore 065 } 066 } 067 068 private void loadSqlFile(String fileName) 069 throws Exception 070 { 071 Statement statement = null; 072 try 073 { 074 statement = connection.createStatement(); 075 String commands = getFileContents(fileName); 076 077 for (int targetPos = commands.indexOf(';'); targetPos > -1; targetPos = commands.indexOf(';')) 078 { 079 String cmd = commands.substring(0, targetPos + 1).trim(); 080 081 if (cmd.startsWith("--")) 082 { 083 // comment 084 int lineend = commands.indexOf('\n'); 085 if (lineend > -1) 086 { 087 targetPos = lineend - 1; 088 } 089 } 090 else 091 { 092 try 093 { 094 statement.execute(cmd); 095 } 096 catch (SQLException sqle) 097 { 098 log.warn("Statement: " + cmd + ": " + sqle.getMessage()); 099 } 100 } 101 102 commands = commands.substring(targetPos + 2); 103 } 104 } 105 finally 106 { 107 if (statement != null) 108 { 109 statement.close(); 110 } 111 } 112 } 113 114 private String getFileContents(String fileName) 115 throws Exception 116 { 117 FileReader fr = new FileReader(fileName); 118 119 char fileBuf[] = new char[1024]; 120 StringBuffer sb = new StringBuffer(1000); 121 int res = -1; 122 123 while ((res = fr.read(fileBuf, 0, 1024)) > -1) 124 { 125 sb.append(fileBuf, 0, res); 126 } 127 fr.close(); 128 return sb.toString(); 129 } 130} 131