18 package testsuite.clusterj;
20 import com.mysql.clusterj.ClusterJException;
21 import com.mysql.clusterj.ClusterJHelper;
22 import com.mysql.clusterj.Constants;
23 import com.mysql.clusterj.Session;
24 import com.mysql.clusterj.SessionFactory;
25 import com.mysql.clusterj.Transaction;
27 import java.io.BufferedReader;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.lang.Thread.UncaughtExceptionHandler;
36 import java.sql.Connection;
37 import java.sql.DriverManager;
38 import java.sql.PreparedStatement;
39 import java.sql.SQLException;
40 import java.sql.Statement;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.Comparator;
47 import java.util.Iterator;
48 import java.util.LinkedList;
49 import java.util.List;
50 import java.util.Map.Entry;
51 import java.util.Properties;
53 import junit.framework.TestCase;
59 protected static final String JDBC_DRIVER_NAME =
"jdbc.driverName";
60 protected static final String JDBC_URL =
"jdbc.url";
61 protected static Connection connection;
62 protected static String jdbcDriverName;
63 protected static String jdbcPassword;
64 protected static String jdbcURL;
65 protected static String jdbcUsername;
66 protected static Properties props;
67 protected static List<String> schemaDefinition =
new ArrayList<String>();
70 String PROPS_FILE_NAME = System.getProperty(
"clusterj.properties",
"clusterj.properties");
84 private Collection<Class<?>> tearDownClasses =
new LinkedList<Class<?>>();
90 private Collection<Object> tearDownInstances =
new LinkedList<Object>();
101 protected boolean debug;
112 protected void addTearDownClasses(Class<?>... classes) {
113 for (Class<?> cls : classes) {
114 tearDownClasses.add(cls);
118 protected void createSessionFactory() {
119 if (sessionFactory == null) {
121 Properties modifiedProperties = modifyProperties();
122 if (debug) System.out.println(
"createSessionFactory props: " + modifiedProperties);
123 sessionFactory = ClusterJHelper.getSessionFactory(modifiedProperties);
133 public void createSession() {
134 if (session != null && !session.
isClosed()) {
145 protected void dumpSystemProperties() {
148 Collections.
sort(entries,
new Comparator<Entry<Object, Object>>() {
150 public int compare(Entry<Object, Object> o1, Entry<Object, Object> o2) {
151 return ((
String) o1.getKey()).compareToIgnoreCase((
String) o2.getKey());
154 for (Iterator<Entry<Object, Object>> iterator = entries.iterator(); iterator.hasNext();) {
155 Entry<Object, Object>
entry = iterator.next();
156 System.out.println(
"key: " + entry.getKey() +
"; value: " + entry.getValue());
161 initializeErrorMessages();
162 errorMessages.append(message + NL);
165 protected void error(
String context, Exception ex) {
166 String message = context +
" " + ex.getClass().getName() +
":" + ex.getMessage();
169 ex.printStackTrace();
173 protected void errorIfNotEqual(
String message, Object expected, Object actual) {
174 if (expected == null && actual == null) {
177 if (expected != null && expected.equals(actual)) {
180 initializeErrorMessages();
181 errorMessages.append(message + NL);
182 errorMessages.append(
183 "Expected: " + ((expected==null)?
"null":expected.toString())
184 +
" actual: " + ((actual==null)?
"null":actual.toString()) + NL);
188 protected void errorIfNotEqual(
String message,
int[] expected,
int[] actual) {
189 if (expected == null && actual == null) {
193 if (expected.length == actual.length) {
194 for (i = 0; i < expected.length; ++
i) {
195 if (expected[i] != actual[i]) {
199 if (i == expected.length) {
203 initializeErrorMessages();
204 errorMessages.append(message + NL);
205 errorMessages.append(
206 "Expected: " + ((expected==null)?
"null":Arrays.toString(expected))
207 +
" actual: " + ((actual==null)?
"null":Arrays.toString(actual)) + NL);
210 protected void errorIfEqual(
String message, Object expected, Object actual) {
211 if (expected == null && actual != null) {
214 if (expected != null && !expected.equals(actual)) {
217 initializeErrorMessages();
218 errorMessages.append(message + NL);
219 errorMessages.append(
220 "Error value: " + ((expected==null)?
"null":expected.toString()));
224 protected void failOnError() {
225 if (errorMessages != null) {
226 fail(errorMessages.toString());
235 if (connection != null) {
238 }
catch (SQLException e) {
239 throw new RuntimeException(
"Caught SQLException during close.", e);
251 Properties properties =
new Properties();
252 properties.put(
"user", jdbcUsername);
253 properties.put(
"password", jdbcPassword);
254 properties.putAll(extraProperties);
256 if (connection != null && !connection.isClosed()) {
260 if (debug) System.out.println(
"Getting new connection with properties " + properties);
261 connection = DriverManager.getConnection(jdbcURL, properties);
262 }
catch (SQLException ex) {
263 ex.printStackTrace();
264 throw new ClusterJException(
"Exception getting connection to " + jdbcURL +
"; username " + jdbcUsername, ex);
273 if (connection == null) {
275 Class.forName(jdbcDriverName,
true, Thread.currentThread().getContextClassLoader());
276 connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
277 }
catch (SQLException ex) {
278 throw new ClusterJException(
"Exception getting connection to " + jdbcURL +
"; username " + jdbcUsername, ex);
279 }
catch (ClassNotFoundException ex) {
280 throw new ClusterJException(
"Exception loading JDBC driver." + jdbcDriverName, ex);
293 String url = props.getProperty(JDBC_URL);
295 connection = DriverManager.getConnection(url);
296 setAutoCommit(connection,
false);
297 }
catch (SQLException e) {
298 throw new RuntimeException(
"Could not get Connection: " + url, e);
306 String driverName = props.getProperty(JDBC_DRIVER_NAME);
308 Class.forName(driverName);
309 }
catch (ClassNotFoundException e) {
310 throw new RuntimeException(
"Class not found: " + driverName, e);
314 protected void setAutoCommit(Connection connection,
boolean b) {
316 connection.setAutoCommit(
false);
317 }
catch (SQLException e) {
318 throw new RuntimeException(
"setAutoCommit failed", e);
329 }
catch (FileNotFoundException ex) {
331 }
catch (IOException ex) {
334 if (result == null) {
337 ClassLoader cl = this.getClass().getClassLoader();
338 InputStream stream = cl.getResourceAsStream(fileName);
342 }
catch (IOException ex) {
343 fail(
"Could not create ConnectionFactory " + ex);
344 }
catch (NullPointerException ex) {
345 fail(
"Missing properties file " + fileName);
351 protected void initializeErrorMessages() {
352 if (errorMessages == null) {
354 errorMessages.append(NL);
364 protected void initializeSchema() {
366 Iterator<String> it = schemaDefinition.iterator();
373 while (it.hasNext()) {
374 statement = it.next();
375 if (debug) System.out.println(
"Executing statement " + statement +
";");
376 PreparedStatement s = connection.prepareStatement(statement);
382 System.out.println(
"Successfully initialized schema.");
383 }
catch (SQLException ex) {
386 throw new ClusterJException(
"initializeSchema threw exception on " + statement, ex);
398 props = getProperties(propsFileName);
404 if (jdbcPassword == null) {
413 loadSchemaDefinition();
420 protected void loadSchemaDefinition() {
421 InputStream inputStream = null;
425 inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(
"schema.sql");
426 BufferedReader reader =
new BufferedReader(
new InputStreamReader(inputStream));
427 while (reader.ready()) {
428 line = reader.readLine();
429 if (line.contains(
"#")) {
433 int semi = line.indexOf(
";");
435 buffer.append(line.substring(0, semi));
436 schemaDefinition.add(buffer.toString());
442 }
catch (IOException ex) {
443 throw new ClusterJException(
"Exception reading schema.sql.", ex);
446 if (inputStream != null) {
449 }
catch (IOException ex) {
470 protected final void setUp() throws Exception {
475 protected final void tearDown() throws Exception {
478 if (session != null && !(session.
isClosed())) {
488 if (!tearDownClasses.isEmpty() | !tearDownInstances.isEmpty()) {
490 for (Class<?> cls : tearDownClasses) {
493 for (Object o : tearDownInstances) {
502 sessionFactory = null;
505 protected void removeAll(Class<?> cls) {
512 protected boolean testSchema() {
514 Statement statement = connection.createStatement();
515 statement.execute(schemaDefinition.get(1));
518 }
catch (SQLException ex) {
520 ex.printStackTrace();
522 System.out.println(
"Test schema failed (normal) " + schemaDefinition.get(1));
527 protected boolean resetSchema() {
529 Statement statement = connection.createStatement();
530 statement.execute(schemaDefinition.get(0));
533 }
catch (SQLException ex) {
534 System.out.println(
"Test schema failed (normal) " + schemaDefinition.get(0));
541 for (
int i = 0; i <
string.length(); ++
i) {
542 int theCharacter =
string.charAt(i);
543 buffer.append(theCharacter);
547 return buffer.toString();
552 for (
String string: list) {
553 result.append(dump(
string));
556 return result.toString();
562 public static class MyUncaughtExceptionHandler
implements UncaughtExceptionHandler {
563 private static List<Throwable> uncaughtExceptions =
new ArrayList<Throwable>();
565 return uncaughtExceptions;
567 public synchronized void uncaughtException(Thread t, Throwable e) {
569 uncaughtExceptions.add(e);