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 java.io.BufferedReader;
25  import java.io.File;
26  import java.io.FileReader;
27  import java.sql.Connection;
28  import java.sql.SQLException;
29  import java.sql.Statement;
30  import java.util.StringTokenizer;
31  
32  import org.dbunit.testutil.TestUtils;
33  import org.dbunit.util.FileHelper;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /**
38   * @author Manuel Laflamme
39   * @version $Revision$
40   * @since Feb 18, 2002
41   */
42  public class DerbyEnvironment extends DatabaseEnvironment
43  {
44  	private static Logger logger = LoggerFactory.getLogger(DerbyEnvironment.class);
45  	
46  	
47  	public DerbyEnvironment(DatabaseProfile profile) throws Exception
48  	{
49  		super(profile);
50  
51  		// Delete the old database if exists before creating a new one in "getConnection()"
52  		// The name of the db is specified in the profile.properties and is created on the fly
53  		// when the connection is retrieved the first time
54  		FileHelper.deleteDirectory(new File("./target/derby_db"));
55  
56  		File ddlFile = TestUtils.getFile("sql/derby.sql");
57  		Connection connection = getConnection().getConnection();
58  
59  		executeDdlFile(ddlFile, connection);
60  	}
61  
62  	public static void executeDdlFile(File ddlFile, Connection connection) throws Exception
63  	{
64  		BufferedReader sqlReader = new BufferedReader(new FileReader(ddlFile));
65  		StringBuffer sqlBuffer = new StringBuffer();
66  		while (sqlReader.ready())
67  		{
68  			String line = sqlReader.readLine();
69  			if (!line.startsWith("-"))
70  			{
71  				sqlBuffer.append(line);
72  			}
73  		}
74  
75  		String sql = sqlBuffer.toString();
76  		StringTokenizer tokenizer = new StringTokenizer(sql, ";");
77  		while (tokenizer.hasMoreTokens()) {
78  			String token = tokenizer.nextToken();
79  			token = token.trim();
80  			if (token.length() > 0) {
81  				executeSql( connection, token );
82  			}
83  		}
84  		logger.info("Executed file " + ddlFile);
85  	}
86  
87  	public static void executeSql( Connection connection, String sql ) throws SQLException {
88  		Statement statement = connection.createStatement();
89  		try
90  		{
91  			statement.execute(sql);
92  		}
93  		finally
94  		{
95  			statement.close();
96  		}
97  	}
98  
99  //  public static void shutdown(Connection connection) throws SQLException {
100 //    executeSql( connection, "SHUTDOWN IMMEDIATELY" );      
101 //  }
102 
103 //  public static void deleteFiles(final String filename) {
104 //    File[] files = new File(".").listFiles(new FilenameFilter()
105 //        {
106 //            public boolean accept(File dir, String name)
107 //            {
108 //                if (name.indexOf(filename) != -1)
109 //                {
110 //                    return true;
111 //                }
112 //                return false;
113 //            }
114 //        });
115 //
116 //for (int i = 0; i < files.length; i++)
117 //{
118 //    File file = files[i];
119 //    file.delete();
120 //}
121 //  }
122   
123 }
124 
125 
126