18 package testsuite.clusterj;
20 import com.mysql.clusterj.ClusterJException;
21 import com.mysql.clusterj.ClusterJHelper;
23 import com.mysql.clusterj.Constants;
24 import com.mysql.clusterj.Session;
25 import com.mysql.clusterj.SessionFactory;
26 import com.mysql.clusterj.Transaction;
28 import java.io.BufferedReader;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.sql.Connection;
37 import java.sql.DriverManager;
38 import java.sql.PreparedStatement;
39 import java.sql.SQLException;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.Comparator;
44 import java.util.Iterator;
45 import java.util.LinkedList;
46 import java.util.List;
47 import java.util.Map.Entry;
48 import java.util.Properties;
50 import junit.framework.TestCase;
58 private Throwable tearDownThrowable;
64 private Collection tearDownInstances =
new LinkedList();
70 private Collection<Class> tearDownClasses =
new LinkedList<Class>();
80 String[] a1values =
new String[]{
"dc=abc",
"dc=prs",
"dc=xyz"};
86 protected final void setUp() throws Exception {
115 if (!tearDownClasses.isEmpty() | !tearDownInstances.isEmpty()) {
117 for (Class<?> cls: tearDownClasses) {
120 for (Object o: tearDownInstances) {
136 protected void addTearDownClasses(Class... classes) {
137 for (Class cls: classes) {
138 tearDownClasses.add(cls);
142 protected Object[] dn2idPK =
new Object[16];
144 public AbstractClusterJCoreTest() {
148 String PROPS_FILE_NAME = System.getProperty(
149 "clusterj.properties",
"clusterj.properties");
196 Properties getProperties(
String fileName) {
197 Properties result = null;
199 InputStream stream =
new FileInputStream(
new File(fileName));
200 result =
new Properties();
203 }
catch (FileNotFoundException ex) {
204 }
catch (IOException ex) {
206 if (result == null) {
209 ClassLoader cl = this.getClass().getClassLoader();
210 InputStream stream = cl.getResourceAsStream(fileName);
214 }
catch (IOException ex) {
215 fail(
"Could not create ConnectionFactory " + ex);
222 int a1factor = 1 + number/a1values.length;
223 return a1values[index/a1factor];
226 protected String getA3for(
long i) {
227 return "employeenumber=100000" +
i;
230 protected void setupDn2idPK() {
231 dn2idPK[0] =
"dc=com";
233 dn2idPK[1] =
"dc=example";
234 dn2idPK[2] =
"ou=people";
254 loadSchemaDefinition();
270 props = getProperties(PROPS_FILE_NAME);
283 Class.forName(
jdbcDriverName,
true, Thread.currentThread().getContextClassLoader());
285 }
catch (SQLException ex) {
287 "Exception getting connection to " +
jdbcURL +
289 }
catch (ClassNotFoundException ex) {
296 protected void loadSchemaDefinition() {
297 InputStream inputStream = null;
301 inputStream = Thread.currentThread().getContextClassLoader()
302 .getResourceAsStream(
"schema.sql");
303 BufferedReader reader =
new BufferedReader(
304 new InputStreamReader(inputStream));
305 while (reader.ready()) {
306 line = reader.readLine();
307 if (line.contains(
"#")) {
311 int semi = line.indexOf(
";");
314 buffer.append(line.substring(0, semi));
321 }
catch (IOException ex) {
322 throw new ClusterJException(
323 "Exception reading schema.sql.", ex);
326 if (inputStream != null) {
329 }
catch (IOException ex) {
335 protected boolean testSchema() {
341 }
catch (SQLException ex) {
354 while(it.hasNext()) {
355 statement = (
String) it.next();
356 System.out.println(
"Executing statement " + statement +
".");
357 PreparedStatement s =
connection.prepareStatement(statement);
363 }
catch (SQLException ex) {
365 "initializeSchema threw exception on " + statement, ex);
369 protected void dumpSystemProperties() {
370 Properties sysprops =System.getProperties();
371 List<Entry<Object, Object>> entries =
new ArrayList<Entry<Object, Object>>(sysprops.entrySet());
372 Collections.sort(entries,
new Comparator<Entry<Object, Object>>() {
373 public int compare(Entry<Object, Object> o1, Entry<Object, Object> o2) {
374 return ((
String)o1.getKey()).compareToIgnoreCase((
String)o2.getKey());
377 for (Iterator<Entry<Object, Object>> iterator = entries.iterator();iterator.hasNext();) {
378 Entry<Object,Object>
entry = iterator.next();
379 System.out.println(
"key: " + entry.getKey() +
"; value: "+ entry.getValue());
383 protected void initializeErrorMessages() {
384 if (errorMessages == null) {
386 errorMessages.append(NL);
390 protected void errorIfNotEqual(
String message, Object expected, Object actual) {
391 if (!expected.equals(actual)) {
392 initializeErrorMessages();
393 errorMessages.append(message + NL);
394 errorMessages.append(
"Expected: " + expected.toString()
395 +
" actual: " + actual.toString() + NL);
399 protected void error(
String message) {
400 initializeErrorMessages();
401 errorMessages.append(message + NL);
404 protected void failOnError() {
405 if (errorMessages != null) {
406 fail(errorMessages.toString());