MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AbstractClusterJCoreTest.java
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 package testsuite.clusterj;
19 
20 import com.mysql.clusterj.ClusterJException;
21 import com.mysql.clusterj.ClusterJHelper;
22 
23 import com.mysql.clusterj.Constants;
24 import com.mysql.clusterj.Session;
25 import com.mysql.clusterj.SessionFactory;
26 import com.mysql.clusterj.Transaction;
27 
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 
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;
49 
50 import junit.framework.TestCase;
51 
52 public abstract class AbstractClusterJCoreTest extends TestCase {
53 
58  private Throwable tearDownThrowable;
59 
64  private Collection tearDownInstances = new LinkedList();
65 
70  private Collection<Class> tearDownClasses = new LinkedList<Class>();
71 
75  private StringBuffer errorMessages;
76 
77  private String NL = "\n";
78 
80  String[] a1values = new String[]{"dc=abc", "dc=prs", "dc=xyz"};
81 
85  @Override
86  protected final void setUp() throws Exception {
87  setupDn2idPK();
88  localSetUp();
89  }
90 
96  protected void localSetUp() {}
97 
101  @Override
102  protected final void tearDown() throws Exception {
103  localTearDown();
104  // if session is null or closed, test class has already cleaned up
105  if (session != null && !(session.isClosed())) {
106  // if tx is null, get it again
107  if (tx == null) {
109  }
110  // if transaction is active (leftover), roll it back
111  if (tx.isActive()) {
112  tx.rollback();
113  }
114  // if any work to do, start a transaction and clean up
115  if (!tearDownClasses.isEmpty() | !tearDownInstances.isEmpty()) {
116  tx.begin();
117  for (Class<?> cls: tearDownClasses) {
119  }
120  for (Object o: tearDownInstances) {
122  }
123  tx.commit();
124  session.close();
125  session = null;
126  }
127  }
128  }
129 
134  protected void localTearDown() {}
135 
136  protected void addTearDownClasses(Class... classes) {
137  for (Class cls: classes) {
138  tearDownClasses.add(cls);
139  }
140  }
141 
142  protected Object[] dn2idPK = new Object[16];
143 
144  public AbstractClusterJCoreTest() {
145  }
146 
148  String PROPS_FILE_NAME = System.getProperty(
149  "clusterj.properties", "clusterj.properties");
150 
152  protected static Properties props;
153 
155  protected static String jdbcDriverName;
156 
158  protected static String jdbcURL;
159 
161  protected static String jdbcUsername;
162 
164  protected static String jdbcPassword;
165 
167  protected static Connection connection;
168 
170  protected static List<String> schemaDefinition = new ArrayList<String>();
171 
173  protected static boolean schemaInitialized = false;
174 
177 
179  protected Session session;
180 
182  protected Transaction tx;
183 
187  protected void createSessionFactory() {
188  loadProperties();
189  if (sessionFactory == null) {
191  }
192  loadSchema();
193  }
194 
196  Properties getProperties(String fileName) {
197  Properties result = null;
198  try {
199  InputStream stream = new FileInputStream(new File(fileName));
200  result = new Properties();
201  result.load(stream);
202  return result;
203  } catch (FileNotFoundException ex) {
204  } catch (IOException ex) {
205  }
206  if (result == null) {
207  try {
208  // try to load the resource from the class loader
209  ClassLoader cl = this.getClass().getClassLoader();
210  InputStream stream = cl.getResourceAsStream(fileName);
211  result = new Properties();
212  result.load(stream);
213  return result;
214  } catch (IOException ex) {
215  fail("Could not create ConnectionFactory " + ex);
216  }
217  }
218  return null; // not reached; fail will throw an exception
219  }
220 
221  protected String getA1for(int number, int index) {
222  int a1factor = 1 + number/a1values.length;
223  return a1values[index/a1factor];
224  }
225 
226  protected String getA3for(long i) {
227  return "employeenumber=100000" + i;
228  }
229 
230  protected void setupDn2idPK() {
231  dn2idPK[0] = "dc=com";
232  // pk[1] changes and is set inside loop
233  dn2idPK[1] = "dc=example";
234  dn2idPK[2] = "ou=people";
235  // pk[3] changes and is set inside loop
236  dn2idPK[4] = "";
237  dn2idPK[5] = "";
238  dn2idPK[6] = "";
239  dn2idPK[7] = "";
240  dn2idPK[8] = "";
241  dn2idPK[9] = "";
242  dn2idPK[10] = "";
243  dn2idPK[11] = "";
244  dn2idPK[12] = "";
245  dn2idPK[13] = "";
246  dn2idPK[14] = "";
247  dn2idPK[15] = "";
248  }
249 
251  protected void loadSchema() {
252  initializeJDBC();
253  if (!schemaInitialized) {
254  loadSchemaDefinition();
255  if (!testSchema()) {
257  }
258  }
259  }
260 
262  protected void initializeJDBC() {
263  loadProperties();
264  getConnection();
265  }
266 
268  protected void loadProperties() {
269  if (props == null) {
270  props = getProperties(PROPS_FILE_NAME);
271  }
272  }
273 
275  protected void getConnection() {
276  if (connection == null) {
278  jdbcURL = props.getProperty(Constants.PROPERTY_JDBC_URL);
281  if (jdbcPassword == null) jdbcPassword = "";
282  try {
283  Class.forName(jdbcDriverName, true, Thread.currentThread().getContextClassLoader());
284  connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
285  } catch (SQLException ex) {
286  throw new ClusterJException(
287  "Exception getting connection to " + jdbcURL +
288  "; username " + jdbcUsername, ex);
289  } catch (ClassNotFoundException ex) {
290  throw new ClusterJException(
291  "Exception loading JDBC driver." + jdbcDriverName, ex);
292  }
293  }
294  }
295 
296  protected void loadSchemaDefinition() {
297  InputStream inputStream = null;
298  StringBuffer buffer = new StringBuffer();
299  String line;
300  try {
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("#")) {
308  // comment line; ignore
309  continue;
310  }
311  int semi = line.indexOf(";");
312  if (semi != -1) {
313  // end of sql statement; finish this statement
314  buffer.append(line.substring(0, semi));
315  schemaDefinition.add(buffer.toString());
316  buffer = new StringBuffer();
317  } else {
318  buffer.append(line);
319  }
320  }
321  } catch (IOException ex) {
322  throw new ClusterJException(
323  "Exception reading schema.sql.", ex);
324  } finally {
325  try {
326  if (inputStream != null) {
327  inputStream.close();
328  }
329  } catch (IOException ex) {
330  // ignore this
331  }
332  }
333  }
334 
335  protected boolean testSchema() {
336  try {
337  PreparedStatement ps = connection.prepareStatement(schemaDefinition.get(0));
338  ps.execute();
339  ps.close();
340  return true;
341  } catch (SQLException ex) {
342  System.out.println(
343  "Test schema failed (normal)" + schemaDefinition.get(0));
344  return false;
345  }
346  }
347 
349  protected void initializeSchema() {
350  Iterator it = schemaDefinition.iterator();
351  it.next(); // skip past condition
352  String statement = null;
353  try {
354  while(it.hasNext()) {
355  statement = (String) it.next();
356  System.out.println("Executing statement " + statement + ".");
357  PreparedStatement s = connection.prepareStatement(statement);
358  s.execute();
359  s.close();
360  }
361  schemaInitialized = true;
362  connection.close();
363  } catch (SQLException ex) {
364  throw new ClusterJException(
365  "initializeSchema threw exception on " + statement, ex);
366  }
367  }
368 
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());
375  }
376  });
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());
380  }
381  }
382 
383  protected void initializeErrorMessages() {
384  if (errorMessages == null) {
385  errorMessages = new StringBuffer();
386  errorMessages.append(NL);
387  }
388  }
389 
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);
396  }
397  }
398 
399  protected void error(String message) {
400  initializeErrorMessages();
401  errorMessages.append(message + NL);
402  }
403 
404  protected void failOnError() {
405  if (errorMessages != null) {
406  fail(errorMessages.toString());
407  }
408  }
409 
410 }