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