18 package com.mysql.clusterj.jpatest;
20 import java.sql.Connection;
21 import java.sql.PreparedStatement;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.util.ArrayList;
25 import java.util.Calendar;
26 import java.util.List;
27 import java.util.TimeZone;
29 import javax.persistence.Query;
31 import org.apache.openjpa.persistence.OpenJPAEntityManager;
33 import com.mysql.clusterj.jpatest.model.Employee;
34 import com.mysql.clusterj.jpatest.model.IdBase;
51 List<IdBase> instances =
new ArrayList<IdBase>();
54 private List<Object[]> expected = null;
63 protected void getConnection() {
64 connection = (Connection) ((OpenJPAEntityManager)em).getConnection();
70 connection.setAutoCommit(
false);
71 }
catch (SQLException e) {
72 throw new RuntimeException(
"setAutoCommit failed", e);
76 public void deleteAll() {
78 em = emf.createEntityManager();
80 for (
int i = 0;
i < getNumberOfEmployees(); ++
i) {
89 public void verifyDeleteAll() {
90 em = emf.createEntityManager();
92 for (
int i = 0;
i < getNumberOfEmployees(); ++
i) {
95 error(
"Entity exists after being removed: " +
i);
102 public void createAll() {
103 em = emf.createEntityManager();
105 for (
int i = 0;
i < getNumberOfEmployees(); ++
i) {
110 e.setName(
"Employee " +
i);
117 public void findAll() {
118 em = emf.createEntityManager();
120 for (
int i = 0;
i < getNumberOfEmployees(); ++
i) {
122 verifyEmployee(e, 0);
128 public void updateThenVerifyAll() {
129 em = emf.createEntityManager();
131 for (
int i = 0;
i < getNumberOfEmployees(); ++
i) {
134 verifyEmployee(e, 1);
138 for (
int i = 0;
i < getNumberOfEmployees(); ++
i) {
140 verifyEmployee(e, 1);
146 public void deleteThenVerifyAll() {
147 em = emf.createEntityManager();
149 for (
int i = 0;
i < getNumberOfEmployees(); ++
i) {
151 verifyEmployee(e, 1);
156 for (
int i = 0;
i < getNumberOfEmployees(); ++
i) {
159 error(
"Entity exists after being removed: " +
i);
166 protected void verifyEmployee(
Employee e,
int updateOffset) {
168 errorIfNotEqual(
"Error in age", i + updateOffset, e.getAge().intValue());
169 errorIfNotEqual(
"Error in magic", i, e.getMagic());
170 errorIfNotEqual(
"Error in name",
"Employee " + i, e.getName());
172 protected int getNumberOfEmployees() {
185 protected static long getMillisFor(
int year,
int month,
int day,
int hour,
int minute,
int second) {
186 Calendar calendar = Calendar.getInstance();
188 calendar.set(Calendar.YEAR, year);
189 calendar.set(Calendar.MONTH, month);
190 calendar.set(Calendar.DATE, day);
191 calendar.set(Calendar.HOUR, hour);
192 calendar.set(Calendar.MINUTE, minute);
193 calendar.set(Calendar.SECOND, second);
194 calendar.set(Calendar.MILLISECOND, 0);
195 long result = calendar.getTimeInMillis();
207 Calendar calendar = Calendar.getInstance();
209 calendar.set(Calendar.YEAR, year);
210 calendar.set(Calendar.MONTH, month);
211 calendar.set(Calendar.DATE, day);
212 calendar.set(Calendar.HOUR, 0);
213 calendar.set(Calendar.MINUTE, 0);
214 calendar.set(Calendar.SECOND, 0);
215 calendar.set(Calendar.MILLISECOND, 0);
216 long result = calendar.getTimeInMillis();
229 protected static long getMillisFor(
int days,
int hour,
int minute,
int second) {
230 Calendar calendar = Calendar.getInstance();
232 calendar.set(Calendar.DATE, days + 1);
233 calendar.set(Calendar.HOUR, hour);
234 calendar.set(Calendar.MINUTE, minute);
235 calendar.set(Calendar.SECOND, second);
236 calendar.set(Calendar.MILLISECOND, 0);
237 long result = calendar.getTimeInMillis();
249 PreparedStatement
statement = connection.prepareStatement(
"select @@global.time_zone, @@global.system_time_zone, @@session.time_zone");
250 ResultSet rs = statement.executeQuery();
253 String globalTimeZone = rs.getString(1);
254 String globalSystemTimeZone = rs.getString(2);
255 String sessionTimeZone = rs.getString(3);
256 if (
debug) System.out.println(
"Global time zone: " + globalTimeZone +
257 " Global system time zone: " + globalSystemTimeZone +
" Session time zone: " + sessionTimeZone);
259 if (
"SYSTEM".equalsIgnoreCase(globalTimeZone)) {
260 globalTimeZone = globalSystemTimeZone;
262 globalTimeZone =
"GMT" + globalTimeZone;
265 if (
debug) System.out.println(
"Local system time zone set to: " + globalTimeZone +
"(" +
localSystemTimeZone +
")");
270 }
catch (SQLException e) {
271 throw new RuntimeException(
"setServerTimeZone failed", e);
279 protected static class ColumnDescriptor {
281 private String columnName;
283 protected InstanceHandler instanceHandler;
285 public String getColumnName() {
289 public Object getResultSetValue(ResultSet rs,
int j)
throws SQLException {
290 return instanceHandler.getResultSetValue(rs, j);
293 public Object getFieldValue(IdBase instance) {
294 return instanceHandler.getFieldValue(instance);
297 public void setFieldValue(IdBase instance, Object value) {
298 this.instanceHandler.setFieldValue(instance, value);
301 public void setPreparedStatementValue(PreparedStatement preparedStatement,
int j, Object value)
302 throws SQLException {
303 instanceHandler.setPreparedStatementValue(preparedStatement, j, value);
306 protected ColumnDescriptor(
String name, InstanceHandler instanceHandler) {
307 this.columnName =
name;
308 this.instanceHandler = instanceHandler;
313 void setFieldValue(
IdBase instance, Object value);
314 Object getResultSetValue(ResultSet rs,
int j)
316 Object getFieldValue(
IdBase instance);
317 public void setPreparedStatementValue(PreparedStatement preparedStatement,
int j, Object value)
368 expected =
new ArrayList<Object[]>();
369 instances =
new ArrayList<IdBase>();
372 for (
int i = 0; i < numberOfInstances; ++
i) {
378 for (ColumnDescriptor columnDescriptor: columnDescriptors) {
381 columnDescriptor.setFieldValue(instance, value);
383 if (
debug) System.out.println(
"generateInstances set field " + columnDescriptor.getColumnName() +
" to value " + value);
386 instances.add(instance);
387 Object[] expectedRow = createRow(columnDescriptors, instance);
388 expected.add(expectedRow);
390 if (
debug) System.out.println(
"Created " + instances.size() +
" instances.");
399 protected void verify(
String where, List<Object[]> expecteds, List<Object[]> actuals) {
400 for (
int i = 0; i < expecteds.size(); ++
i) {
401 Object[] expected = expecteds.get(i);
402 Object[] actual = actuals.get(i);
403 errorIfNotEqual(where +
" got failure on id for row " + i, i, actual[0]);
404 for (
int j = 1; j < expected.length; ++j) {
405 errorIfNotEqual(where +
" got failure to match column data for row "
406 + i +
" column " + j,
407 expected[j], actual[j]);
412 protected void removeAll(Class<? extends IdBase> modelClass) {
413 Query
query = em.createQuery(
"DELETE FROM " + modelClass.getSimpleName());
414 em.getTransaction().begin();
415 query.executeUpdate();
416 em.getTransaction().commit();
419 private void removeAll(
String tableName) {
422 PreparedStatement
statement = connection.prepareStatement(
"DELETE FROM " + tableName);
425 }
catch (SQLException e) {
435 List<Object[]> result = null;
445 List<Object[]> result = null;
455 List<Object[]> result = null;
465 List<Object[]> result = null;
472 protected void writeToJPA(ColumnDescriptor[] columnDescriptors, List<IdBase> instances) {
473 em.getTransaction().begin();
474 for (
IdBase instance: instances) {
475 em.persist(instance);
477 em.getTransaction().commit();
481 protected void writeToJDBC(ColumnDescriptor[] columnDescriptors, List<IdBase> instances) {
484 buffer.append(tableName);
485 buffer.append(
" (id");
486 for (ColumnDescriptor columnDescriptor: columnDescriptors) {
488 buffer.append(columnDescriptor.getColumnName());
490 buffer.append(
") VALUES (?");
491 for (ColumnDescriptor columnDescriptor: columnDescriptors) {
492 buffer.append(
", ?");
495 String statement = buffer.toString();
496 if (
debug) System.out.println(statement);
498 PreparedStatement preparedStatement = null;
501 preparedStatement = connection.prepareStatement(statement);
502 if (
debug) System.out.println(preparedStatement.toString());
503 for (i = 0; i < instances.size(); ++
i) {
504 IdBase instance = instances.get(i);
505 preparedStatement.setInt(1, instance.getId());
507 for (ColumnDescriptor columnDescriptor: columnDescriptors) {
508 Object value = columnDescriptor.getFieldValue(instance);
509 columnDescriptor.setPreparedStatementValue(preparedStatement, j++, value);
510 if (
debug) System.out.println(
"writeToJDBC set column: " + columnDescriptor.getColumnName() +
" to value: " + value);
512 preparedStatement.execute();
515 }
catch (SQLException e) {
516 throw new RuntimeException(
"Failed to insert " + tableName +
" at instance " + i, e);
521 protected List<Object[]>
readFromJPA(ColumnDescriptor[] columnDescriptors) {
523 List<Object[]> result =
new ArrayList<Object[]>();
524 em.getTransaction().begin();
526 IdBase instance = em.find(modelClass, i);
527 if (instance != null) {
528 Object[] row = createRow(columnDescriptors, instance);
532 em.getTransaction().commit();
533 if (
debug) System.out.println(
"readFromJPA: " + dump(result));
538 protected List<Object[]>
readFromJDBC(ColumnDescriptor[] columnDescriptors) {
540 List<Object[]> result =
new ArrayList<Object[]>();
542 for (ColumnDescriptor columnDescriptor: columnDescriptors) {
544 buffer.append(columnDescriptor.getColumnName());
546 buffer.append(
" FROM ");
547 buffer.append(tableName);
548 buffer.append(
" ORDER BY ID");
549 String statement = buffer.toString();
550 if (
debug) System.out.println(statement);
551 PreparedStatement preparedStatement = null;
554 preparedStatement = connection.prepareStatement(statement);
555 ResultSet rs = preparedStatement.executeQuery();
557 Object[] row =
new Object[columnDescriptors.length + 1];
559 row[0] = rs.getInt(1);
560 for (ColumnDescriptor columnDescriptor: columnDescriptors) {
561 row[j] = columnDescriptor.getResultSetValue(rs, j + 1);
568 }
catch (SQLException e) {
569 throw new RuntimeException(
"Failed to read " + tableName +
" at instance " + i, e);
571 if (
debug) System.out.println(
"readFromJDBC: " + dump(result));
580 private Object[] createRow(ColumnDescriptor[] columnDescriptors,
582 Object[] row =
new Object[columnDescriptors.length + 1];
583 row[0] = instance.getId();
585 for (ColumnDescriptor columnDescriptor: columnDescriptors) {
586 row[j++] = columnDescriptor.getFieldValue(instance);
594 for (Object[] row: results) {
595 result.append(
"Id: ");
596 for (Object column: row) {
597 result.append(column);
602 return result.toString();