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 H2Environment extends DatabaseEnvironment
42  {
43      public static final String USERNAME_DEFAULT = "sa";
44      public static final String PASSWORD_DEFAULT = "";
45  
46      public H2Environment(DatabaseProfile profile) throws Exception
47      {
48          super(profile);
49  
50          // Creates required tables into the hypersonic in-memory database
51          File ddlFile =  TestUtils.getFile("sql/h2.sql");
52          Connection connection = getConnection().getConnection();
53  
54          executeDdlFile(ddlFile, connection);
55  
56      }
57  
58      public static void executeDdlFile(File ddlFile, Connection connection) throws Exception
59      {
60          BufferedReader sqlReader = new BufferedReader(new FileReader(ddlFile));
61          StringBuffer sqlBuffer = new StringBuffer();
62          while (sqlReader.ready())
63          {
64              String line = sqlReader.readLine();
65              if (!line.startsWith("-"))
66              {
67                  sqlBuffer.append(line);
68              }
69          }
70  
71          String sql = sqlBuffer.toString();
72          executeSql( connection, sql );
73      }
74  
75      public static void executeSql( Connection connection, String sql ) throws SQLException {
76          Statement statement = connection.createStatement();
77          try
78          {
79              statement.execute(sql);
80          }
81          finally
82          {
83              statement.close();
84          }
85      }
86  
87      public static Connection createJdbcConnection(String databaseName) throws Exception
88      {
89          return createJdbcConnection(databaseName, USERNAME_DEFAULT, PASSWORD_DEFAULT);
90      }
91  
92      public static Connection createJdbcConnection(String databaseName,
93              String username, String password) throws Exception
94      {
95          Class.forName("org.h2.Driver");
96          return DriverManager.getConnection("jdbc:h2:mem:" + databaseName,
97                  username, password);
98      }
99  
100     public void closeConnection() throws Exception
101     {
102         DatabaseOperation.DELETE_ALL.execute(getConnection(), getInitDataSet());
103     }
104 
105     public static void shutdown(Connection connection) throws SQLException {
106         executeSql( connection, "SHUTDOWN IMMEDIATELY" );      
107     }
108 
109     public static void deleteFiles(final String filename) {
110         deleteFiles(new File("."), filename);
111     }
112 
113     public static void deleteFiles(File directory, final String filename) {
114         File[] files = directory.listFiles(new FilenameFilter()
115         {
116             public boolean accept(File dir, String name)
117             {
118                 if (name.indexOf(filename) != -1)
119                 {
120                     return true;
121                 }
122                 return false;
123             }
124         });
125 
126         for (int i = 0; i < files.length; i++)
127         {
128             File file = files[i];
129             file.delete();
130         }
131 
132     }
133 }