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 { 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 } 065 } 066 067 private void loadSqlFile(String fileName) 068 throws Exception 069 { 070 Statement statement = null; 071 try 072 { 073 statement = connection.createStatement(); 074 String commands = getFileContents(fileName); 075 076 for (int targetPos = commands.indexOf(';'); targetPos > -1; targetPos = commands.indexOf(';')) 077 { 078 String cmd = commands.substring(0, targetPos + 1).trim(); 079 080 if (cmd.startsWith("--")) 081 { 082 // comment 083 int lineend = commands.indexOf('\n'); 084 if (lineend > -1) 085 { 086 targetPos = lineend - 1; 087 } 088 } 089 else 090 { 091 try 092 { 093 statement.execute(cmd); 094 } 095 catch (SQLException sqle) 096 { 097 log.warn("Statement: " + cmd + ": " + sqle.getMessage()); 098 } 099 } 100 101 commands = commands.substring(targetPos + 2); 102 } 103 } 104 finally 105 { 106 if (statement != null) 107 { 108 statement.close(); 109 } 110 } 111 } 112 113 private String getFileContents(String fileName) 114 throws Exception 115 { 116 FileReader fr = new FileReader(fileName); 117 118 char fileBuf[] = new char[1024]; 119 StringBuffer sb = new StringBuffer(1000); 120 int res = -1; 121 122 while ((res = fr.read(fileBuf, 0, 1024)) > -1) 123 { 124 sb.append(fileBuf, 0, res); 125 } 126 fr.close(); 127 return sb.toString(); 128 } 129 } 130