2 Copyright 2010 Sun Microsystems, Inc.
3 All rights reserved. Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package com.mysql.clusterj.jpatest;
21 import java.sql.PreparedStatement;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.sql.Timestamp;
25 import java.util.Date;
27 import com.mysql.clusterj.jpatest.model.TimestampAsUtilDateTypes;
28 import com.mysql.clusterj.jpatest.model.IdBase;
30 /** Test that Timestamps can be read and written.
31 * case 1: Write using JDBC, read using NDB.
32 * case 2: Write using NDB, read using JDBC.
35 drop table if exists timestamptypes;
36 create table timestamptypes (
37 id int not null primary key,
39 timestamp_not_null_hash timestamp,
40 timestamp_not_null_btree timestamp,
41 timestamp_not_null_both timestamp,
42 timestamp_not_null_none timestamp
44 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
46 create unique index idx_timestamp_not_null_hash using hash on timestamptypes(timestamp_not_null_hash);
47 create index idx_timestamp_not_null_btree on timestamptypes(timestamp_not_null_btree);
48 create unique index idx_timestamp_not_null_both on timestamptypes(timestamp_not_null_both);
51 public class TimestampAsUtilDateTest extends AbstractJPABaseTest {
57 resetLocalSystemDefaultTimeZone(connection);
60 setAutoCommit(connection, false);
63 static int NUMBER_OF_INSTANCES = 10;
66 protected boolean getDebug() {
71 protected int getNumberOfInstances() {
72 return NUMBER_OF_INSTANCES;
76 protected String getTableName() {
77 return "datetimetypes";
80 /** Subclasses override this method to provide the model class for the test */
82 protected Class<? extends IdBase> getModelClass() {
83 return TimestampAsUtilDateTypes.class;
86 /** Subclasses override this method to provide values for rows (i) and columns (j) */
88 protected Object getColumnValue(int i, int j) {
89 return new Date(getMillisFor(1980, 0, i + 1, 0, 0, j));
93 /** Subclasses must override this method to implement the model factory for the test */
94 protected IdBase getNewInstance(Class<? extends IdBase> modelClass) {
95 return new TimestampAsUtilDateTypes();
98 public void testWriteJDBCReadJPA() {
103 public void testWriteJPAReadJDBC() {
108 public void testWriteJDBCReadJDBC() {
113 public void testWriteJPAReadJPA() {
118 static ColumnDescriptor not_null_hash = new ColumnDescriptor
119 ("datetime_not_null_hash", new InstanceHandler() {
120 public void setFieldValue(IdBase instance, Object value) {
121 ((TimestampAsUtilDateTypes)instance).setTimestamp_not_null_hash((Date)value);
123 public Object getFieldValue(IdBase instance) {
124 return ((TimestampAsUtilDateTypes)instance).getTimestamp_not_null_hash();
126 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
127 throws SQLException {
128 Timestamp timestamp = new Timestamp(((Date)value).getTime());
129 preparedStatement.setTimestamp(j, timestamp);
131 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
132 return rs.getTimestamp(j);
136 static ColumnDescriptor not_null_btree = new ColumnDescriptor
137 ("datetime_not_null_btree", new InstanceHandler() {
138 public void setFieldValue(IdBase instance, Object value) {
139 ((TimestampAsUtilDateTypes)instance).setTimestamp_not_null_btree((Date)value);
141 public Object getFieldValue(IdBase instance) {
142 return ((TimestampAsUtilDateTypes)instance).getTimestamp_not_null_btree();
144 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
145 throws SQLException {
146 Timestamp timestamp = new Timestamp(((Date)value).getTime());
147 preparedStatement.setTimestamp(j, timestamp);
149 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
150 return rs.getTimestamp(j);
154 static ColumnDescriptor not_null_both = new ColumnDescriptor
155 ("datetime_not_null_both", new InstanceHandler() {
156 public void setFieldValue(IdBase instance, Object value) {
157 ((TimestampAsUtilDateTypes)instance).setTimestamp_not_null_both((Date)value);
159 public Date getFieldValue(IdBase instance) {
160 return ((TimestampAsUtilDateTypes)instance).getTimestamp_not_null_both();
162 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
163 throws SQLException {
164 Timestamp timestamp = new Timestamp(((Date)value).getTime());
165 preparedStatement.setTimestamp(j, timestamp);
167 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
168 return rs.getTimestamp(j);
172 static ColumnDescriptor not_null_none = new ColumnDescriptor
173 ("datetime_not_null_none", new InstanceHandler() {
174 public void setFieldValue(IdBase instance, Object value) {
175 ((TimestampAsUtilDateTypes)instance).setTimestamp_not_null_none((Date)value);
177 public Date getFieldValue(IdBase instance) {
178 return ((TimestampAsUtilDateTypes)instance).getTimestamp_not_null_none();
180 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
181 throws SQLException {
182 Timestamp timestamp = new Timestamp(((Date)value).getTime());
183 preparedStatement.setTimestamp(j, timestamp);
185 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
186 return rs.getTimestamp(j);
190 protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] {
198 protected ColumnDescriptor[] getColumnDescriptors() {
199 return columnDescriptors;