KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > HypersonicEnvironment


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
26 import java.io.BufferedReader JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.DriverManager JavaDoc;
31 import java.sql.Statement JavaDoc;
32
33 /**
34  * @author Manuel Laflamme
35  * @version $Revision: 1.11 $
36  * @since Feb 18, 2002
37  */

38 public class HypersonicEnvironment extends DatabaseEnvironment
39 {
40     public HypersonicEnvironment(DatabaseProfile profile) throws Exception JavaDoc
41     {
42         super(profile);
43
44         // Creates required tables into the hypersonic in-memory database
45
File JavaDoc ddlFile = new File JavaDoc("src/sql/hypersonic.sql");
46         Connection JavaDoc connection = getConnection().getConnection();
47
48         executeDdlFile(ddlFile, connection);
49
50     }
51
52     public static void executeDdlFile(File JavaDoc ddlFile, Connection JavaDoc connection) throws Exception JavaDoc
53     {
54         BufferedReader JavaDoc sqlReader = new BufferedReader JavaDoc(new FileReader JavaDoc(ddlFile));
55         StringBuffer JavaDoc sqlBuffer = new StringBuffer JavaDoc();
56         while (sqlReader.ready())
57         {
58             String JavaDoc line = sqlReader.readLine();
59             if (!line.startsWith("-"))
60             {
61                 sqlBuffer.append(line);
62             }
63         }
64
65         String JavaDoc sql = sqlBuffer.toString();
66         Statement JavaDoc statement = connection.createStatement();
67         try
68         {
69             statement.execute(sql);
70         }
71         finally
72         {
73             statement.close();
74         }
75     }
76
77     public static Connection JavaDoc createJdbcConnection(String JavaDoc databaseName) throws Exception JavaDoc
78     {
79         Class.forName("org.hsqldb.jdbcDriver");
80         Connection JavaDoc connection = DriverManager.getConnection(
81                 "jdbc:hsqldb:" + databaseName, "sa", "");
82         return connection;
83     }
84
85     public void closeConnection() throws Exception JavaDoc
86     {
87         DatabaseOperation.DELETE_ALL.execute(getConnection(), getInitDataSet());
88     }
89 }
90
91
92
93
94
95
96
Popular Tags