1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.tck.util; |
12 | |
|
13 | |
import org.mule.util.ClassUtils; |
14 | |
import org.mule.util.FileUtils; |
15 | |
|
16 | |
import java.io.File; |
17 | |
import java.io.IOException; |
18 | |
import java.lang.reflect.Method; |
19 | |
import java.net.URL; |
20 | |
import java.sql.Connection; |
21 | |
import java.sql.Driver; |
22 | |
import java.sql.DriverManager; |
23 | |
import java.sql.SQLException; |
24 | |
import java.sql.Statement; |
25 | |
import java.util.Properties; |
26 | |
|
27 | |
import javax.sql.DataSource; |
28 | |
|
29 | |
public class MuleDerbyTestUtils |
30 | |
{ |
31 | |
private static final String DERBY_DRIVER_CLASS = "org.apache.derby.jdbc.EmbeddedDriver"; |
32 | |
private static final String DERBY_DATASOURCE_CLASS = "org.apache.derby.jdbc.EmbeddedDataSource"; |
33 | |
|
34 | |
|
35 | |
private MuleDerbyTestUtils() |
36 | |
{ |
37 | 0 | super(); |
38 | 0 | } |
39 | |
|
40 | |
|
41 | |
public static String setDerbyHome() |
42 | |
{ |
43 | 0 | return setDerbyHome("target"); |
44 | |
} |
45 | |
|
46 | |
public static String setDerbyHome(String path) |
47 | |
{ |
48 | 0 | File derbySystemHome = new File(System.getProperty("user.dir"), path); |
49 | 0 | System.setProperty("derby.system.home", derbySystemHome.getAbsolutePath()); |
50 | 0 | return derbySystemHome.getAbsolutePath(); |
51 | |
} |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public static void stopDatabase() throws SQLException |
60 | |
{ |
61 | |
try |
62 | |
{ |
63 | |
|
64 | |
|
65 | 0 | ClassUtils.instanciateClass(DERBY_DRIVER_CLASS); |
66 | |
|
67 | 0 | DriverManager.getConnection("jdbc:derby:;shutdown=true"); |
68 | |
} |
69 | 0 | catch (SQLException sqlex) |
70 | |
{ |
71 | |
|
72 | 0 | if (!"XJ015".equals(sqlex.getSQLState())) |
73 | |
{ |
74 | 0 | throw sqlex; |
75 | |
} |
76 | |
} |
77 | 0 | catch (Exception ex) |
78 | |
{ |
79 | |
|
80 | |
|
81 | 0 | throw new RuntimeException(ex); |
82 | 0 | } |
83 | 0 | } |
84 | |
|
85 | |
public static void cleanupDerbyDb(String databaseName) throws IOException, SQLException |
86 | |
{ |
87 | 0 | cleanupDerbyDb(setDerbyHome(), databaseName); |
88 | 0 | } |
89 | |
|
90 | |
public static void cleanupDerbyDb(String derbySystemHome, String databaseName) throws IOException, SQLException |
91 | |
{ |
92 | 0 | stopDatabase(); |
93 | 0 | FileUtils.deleteTree(new File(derbySystemHome + File.separator + databaseName)); |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public static void startDataBase(String databaseName) throws Exception |
100 | |
{ |
101 | 0 | Driver derbyDriver = (Driver) ClassUtils.instanciateClass(DERBY_DRIVER_CLASS); |
102 | |
|
103 | 0 | Method connectMethod = derbyDriver.getClass().getMethod("connect", String.class, Properties.class); |
104 | |
|
105 | 0 | String connectionName = "jdbc:derby:" + databaseName; |
106 | 0 | connectMethod.invoke(derbyDriver, connectionName, null); |
107 | 0 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public static void createDataBase(String databaseName) throws SQLException |
115 | |
{ |
116 | 0 | createDataBase(databaseName, (String[]) null); |
117 | 0 | } |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public static void createDataBase(String databaseName, String creationSql) throws SQLException |
126 | |
{ |
127 | 0 | createDataBase(databaseName, new String[] { creationSql } ); |
128 | 0 | } |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public static void createDataBase(String databaseName, String[] creationSql) throws SQLException |
137 | |
{ |
138 | 0 | createDataBase(databaseName, creationSql, null); |
139 | 0 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public static void createDataBase(String databaseName, String[] creationSql, Properties properties) throws SQLException |
148 | |
{ |
149 | |
|
150 | |
|
151 | |
try |
152 | |
{ |
153 | 0 | String connectionName = "jdbc:derby:" + databaseName + ";create=true"; |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | 0 | Driver derbyDriver = (Driver) ClassUtils.instanciateClass(DERBY_DRIVER_CLASS); |
159 | 0 | Method connectMethod = derbyDriver.getClass().getMethod("connect", String.class, Properties.class); |
160 | 0 | connectMethod.invoke(derbyDriver, connectionName, properties); |
161 | |
|
162 | 0 | if (creationSql != null) |
163 | |
{ |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | 0 | DataSource embeddedDS = (DataSource) ClassUtils.instanciateClass(DERBY_DATASOURCE_CLASS); |
169 | 0 | Method m = embeddedDS.getClass().getMethod("setDatabaseName", String.class); |
170 | 0 | m.invoke(embeddedDS, databaseName); |
171 | |
|
172 | 0 | Connection con = null; |
173 | |
try |
174 | |
{ |
175 | 0 | con = embeddedDS.getConnection(); |
176 | 0 | Statement st = con.createStatement(); |
177 | 0 | for (String aCreationSql : creationSql) |
178 | |
{ |
179 | 0 | st.execute(aCreationSql); |
180 | |
} |
181 | 0 | con.commit(); |
182 | |
} |
183 | |
finally |
184 | |
{ |
185 | 0 | if (con != null && !con.isClosed()) |
186 | |
{ |
187 | 0 | con.close(); |
188 | |
} |
189 | |
} |
190 | |
} |
191 | |
} |
192 | 0 | catch (Exception ex) |
193 | |
{ |
194 | 0 | throw new RuntimeException("Error creating the database " + databaseName, ex); |
195 | 0 | } |
196 | 0 | } |
197 | |
|
198 | |
public static String loadDatabaseName(String propertiesLocation, String propertyName) throws IOException |
199 | |
{ |
200 | 0 | Properties derbyProperties = new Properties(); |
201 | 0 | URL resource = ClassUtils.getResource(propertiesLocation, MuleDerbyTestUtils.class); |
202 | 0 | derbyProperties.load(resource.openStream()); |
203 | 0 | return derbyProperties.getProperty(propertyName); |
204 | |
} |
205 | |
|
206 | |
public static void defaultDerbyCleanAndInit(String propertiesLocation, String propertyName) throws IOException, SQLException |
207 | |
{ |
208 | 0 | String derbyHome = setDerbyHome(); |
209 | 0 | String dbName = loadDatabaseName(propertiesLocation, propertyName); |
210 | 0 | cleanupDerbyDb(derbyHome, dbName); |
211 | 0 | createDataBase(dbName); |
212 | 0 | } |
213 | |
} |
214 | |
|
215 | |
|