View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.configuration2.test;
19  
20  import java.nio.charset.Charset;
21  import java.nio.file.Paths;
22  import java.sql.Connection;
23  import java.sql.DriverManager;
24  import java.sql.SQLException;
25  import java.sql.Statement;
26  
27  import org.apache.commons.io.file.PathUtils;
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * Sourced from Apache Turbine.
34   */
35  public class HsqlDB {
36  
37      private static final Log LOG = LogFactory.getLog(HsqlDB.class);
38  
39      private final Connection connection;
40  
41      public HsqlDB(final String uri, final String databaseDriver, final String loadFile) throws Exception {
42          Class.forName(databaseDriver);
43          this.connection = DriverManager.getConnection(uri, "sa", "");
44          if (StringUtils.isNotEmpty(loadFile)) {
45              loadSqlFile(loadFile);
46          }
47          this.connection.commit();
48      }
49  
50      public void close() {
51          try {
52              connection.close();
53          } catch (final Exception e) {
54          }
55      }
56  
57      public Connection getConnection() {
58          return connection;
59      }
60  
61      private String getFileContents(final String fileName) throws Exception {
62          return PathUtils.readString(Paths.get(fileName), Charset.defaultCharset());
63      }
64  
65      private void loadSqlFile(final String fileName) throws Exception {
66          try (Statement statement = connection.createStatement()) {
67              String commands = getFileContents(fileName);
68              for (int targetPos = commands.indexOf(';'); targetPos > -1; targetPos = commands.indexOf(';')) {
69                  final String cmd = commands.substring(0, targetPos + 1);
70                  try {
71                      statement.execute(cmd);
72                  } catch (final SQLException sqle) {
73                      LOG.warn("Statement: " + cmd + ": " + sqle.getMessage());
74                  }
75                  commands = commands.substring(targetPos + 2);
76              }
77          }
78      }
79  }