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;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import com.mysql.clusterj.jpatest.model.DateAsSqlDateTypes;
26 import com.mysql.clusterj.jpatest.model.IdBase;
28 /** Test that Dates can be read and written.
29 * case 1: Write using JDBC, read using JPA.
30 * case 2: Write using JPA, read using JDBC.
31 * case 3: Write using JDBC, read using JDBC.
32 * case 4: Write using JPA, read using JPA.
35 drop table if exists datetypes;
36 create table datetypes (
37 id int not null primary key,
44 date_not_null_hash date,
45 date_not_null_btree date,
46 date_not_null_both date,
47 date_not_null_none date
49 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
51 create unique index idx_date_null_hash using hash on datetypes(date_null_hash);
52 create index idx_date_null_btree on datetypes(date_null_btree);
53 create unique index idx_date_null_both on datetypes(date_null_both);
55 create unique index idx_date_not_null_hash using hash on datetypes(date_not_null_hash);
56 create index idx_date_not_null_btree on datetypes(date_not_null_btree);
57 create unique index idx_date_not_null_both on datetypes(date_not_null_both);
60 public class DateAsSqlDateTest extends AbstractJPABaseTest {
62 static int NUMBER_OF_INSTANCES = 10;
65 protected boolean getDebug() {
70 protected int getNumberOfInstances() {
71 return NUMBER_OF_INSTANCES;
75 protected String getTableName() {
79 /** Subclasses override this method to provide the model class for the test */
82 Class<? extends IdBase> getModelClass() {
83 return DateAsSqlDateTypes.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, i, j + 1));
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 DateAsSqlDateTypes();
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 ("date_not_null_hash", new InstanceHandler() {
120 public void setFieldValue(IdBase instance, Object value) {
121 ((DateAsSqlDateTypes)instance).setDate_not_null_hash((Date)value);
123 public Object getFieldValue(IdBase instance) {
124 return ((DateAsSqlDateTypes)instance).getDate_not_null_hash();
126 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
127 throws SQLException {
128 java.sql.Date date = new java.sql.Date(((Date)value).getTime());
129 preparedStatement.setDate(j, date);
131 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
132 return rs.getDate(j);
136 static ColumnDescriptor not_null_btree = new ColumnDescriptor
137 ("date_not_null_btree", new InstanceHandler() {
138 public void setFieldValue(IdBase instance, Object value) {
139 ((DateAsSqlDateTypes)instance).setDate_not_null_btree((Date)value);
141 public Object getFieldValue(IdBase instance) {
142 return ((DateAsSqlDateTypes)instance).getDate_not_null_btree();
144 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
145 throws SQLException {
146 java.sql.Date date = new java.sql.Date(((Date)value).getTime());
147 preparedStatement.setDate(j, date);
149 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
150 return rs.getDate(j);
153 static ColumnDescriptor not_null_both = new ColumnDescriptor
154 ("date_not_null_both", new InstanceHandler() {
155 public void setFieldValue(IdBase instance, Object value) {
156 ((DateAsSqlDateTypes)instance).setDate_not_null_both((Date)value);
158 public Date getFieldValue(IdBase instance) {
159 return ((DateAsSqlDateTypes)instance).getDate_not_null_both();
161 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
162 throws SQLException {
163 java.sql.Date date = new java.sql.Date(((Date)value).getTime());
164 preparedStatement.setDate(j, date);
166 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
167 return rs.getDate(j);
170 static ColumnDescriptor not_null_none = new ColumnDescriptor
171 ("date_not_null_none", new InstanceHandler() {
172 public void setFieldValue(IdBase instance, Object value) {
173 ((DateAsSqlDateTypes)instance).setDate_not_null_none((Date)value);
175 public Date getFieldValue(IdBase instance) {
176 return ((DateAsSqlDateTypes)instance).getDate_not_null_none();
178 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
179 throws SQLException {
180 java.sql.Date date = new java.sql.Date(((Date)value).getTime());
181 preparedStatement.setDate(j, date);
183 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
184 return rs.getDate(j);
188 protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] {
196 protected ColumnDescriptor[] getColumnDescriptors() {
197 return columnDescriptors;