View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit;
23  
24  import org.dbunit.operation.DatabaseOperation;
25  import org.dbunit.testutil.TestUtils;
26  
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.io.FileReader;
30  import java.io.FilenameFilter;
31  import java.sql.Connection;
32  import java.sql.DriverManager;
33  import java.sql.SQLException;
34  import java.sql.Statement;
35  
36  /**
37   * @author Manuel Laflamme
38   * @version $Revision$
39   * @since Feb 18, 2002
40   */
41  public class HypersonicEnvironment extends DatabaseEnvironment
42  {
43      public HypersonicEnvironment(DatabaseProfile profile) throws Exception
44      {
45          super(profile);
46  
47          // Creates required tables into the hypersonic in-memory database
48          File ddlFile = TestUtils.getFile("sql/hypersonic.sql");
49          Connection connection = getConnection().getConnection();
50  
51          executeDdlFile(ddlFile, connection);
52  
53      }
54  
55      public static void executeDdlFile(File ddlFile, Connection connection) throws Exception
56      {
57          BufferedReader sqlReader = new BufferedReader(new FileReader(ddlFile));
58          StringBuffer sqlBuffer = new StringBuffer();
59          while (sqlReader.ready())
60          {
61              String line = sqlReader.readLine();
62              if (!line.startsWith("-"))
63              {
64                  sqlBuffer.append(line);
65              }
66          }
67  
68          String sql = sqlBuffer.toString();
69          executeSql( connection, sql );
70      }
71  
72      public static void executeSql( Connection connection, String sql ) throws SQLException {
73          Statement statement = connection.createStatement();
74          try
75          {
76              statement.execute(sql);
77          }
78          finally
79          {
80              statement.close();
81          }
82      }
83  
84      public static Connection createJdbcConnection(String databaseName) throws Exception
85      {
86          Class.forName("org.hsqldb.jdbcDriver");
87          Connection connection = DriverManager.getConnection(
88                  "jdbc:hsqldb:" + databaseName, "sa", "");
89          return connection;
90      }
91  
92      public void closeConnection() throws Exception
93      {
94          DatabaseOperation.DELETE_ALL.execute(getConnection(), getInitDataSet());
95      }
96  
97      public static void shutdown(Connection connection) throws SQLException {
98          executeSql( connection, "SHUTDOWN IMMEDIATELY" );      
99      }
100 
101     public static void deleteFiles(final String filename) {
102         deleteFiles(new File("."), filename);
103     }
104 
105     public static void deleteFiles(File directory, final String filename) {
106         File[] files = directory.listFiles(new FilenameFilter()
107         {
108             public boolean accept(File dir, String name)
109             {
110                 if (name.indexOf(filename) != -1)
111                 {
112                     return true;
113                 }
114                 return false;
115             }
116         });
117 
118         for (int i = 0; i < files.length; i++)
119         {
120             File file = files[i];
121             file.delete();
122         }
123 
124     }
125 
126 }
127 
128 
129