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 testsuite.clusterj;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import testsuite.clusterj.model.DateAsSqlDateTypes;
26 import testsuite.clusterj.model.IdBase;
28 /** Test that Dates can be read and written.
29 * case 1: Write using JDBC, read using NDB.
30 * case 2: Write using NDB, read using JDBC.
33 drop table if exists datetypes;
34 create table datetypes (
35 id int not null primary key,
42 date_not_null_hash date,
43 date_not_null_btree date,
44 date_not_null_both date,
45 date_not_null_none date
47 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
49 create unique index idx_date_null_hash using hash on datetypes(date_null_hash);
50 create index idx_date_null_btree on datetypes(date_null_btree);
51 create unique index idx_date_null_both on datetypes(date_null_both);
53 create unique index idx_date_not_null_hash using hash on datetypes(date_not_null_hash);
54 create index idx_date_not_null_btree on datetypes(date_not_null_btree);
55 create unique index idx_date_not_null_both on datetypes(date_not_null_both);
58 public class DateAsSqlDateTypesTest extends AbstractClusterJModelTest {
60 static int NUMBER_OF_INSTANCES = 10;
63 protected boolean getDebug() {
68 protected int getNumberOfInstances() {
69 return NUMBER_OF_INSTANCES;
73 protected String getTableName() {
77 /** Subclasses override this method to provide the model class for the test */
79 Class<? extends IdBase> getModelClass() {
80 return DateAsSqlDateTypes.class;
83 /** Subclasses override this method to provide values for rows (i) and columns (j) */
85 protected Object getColumnValue(int i, int j) {
86 return new Date(getMillisFor(1980, i, j + 1));
89 public void testWriteJDBCReadNDB() {
94 public void testWriteNDBReadNDB() {
99 public void testWriteJDBCReadJDBC() {
104 public void testWriteNDBReadJDBC() {
109 static ColumnDescriptor not_null_hash = new ColumnDescriptor
110 ("date_not_null_hash", new InstanceHandler() {
111 public void setFieldValue(IdBase instance, Object value) {
112 ((DateAsSqlDateTypes)instance).setDate_not_null_hash((Date)value);
114 public Object getFieldValue(IdBase instance) {
115 return ((DateAsSqlDateTypes)instance).getDate_not_null_hash();
117 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
118 throws SQLException {
119 preparedStatement.setDate(j, (Date)value);
121 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
122 return rs.getDate(j);
126 static ColumnDescriptor not_null_btree = new ColumnDescriptor
127 ("date_not_null_btree", new InstanceHandler() {
128 public void setFieldValue(IdBase instance, Object value) {
129 ((DateAsSqlDateTypes)instance).setDate_not_null_btree((Date)value);
131 public Object getFieldValue(IdBase instance) {
132 return ((DateAsSqlDateTypes)instance).getDate_not_null_btree();
134 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
135 throws SQLException {
136 preparedStatement.setDate(j, (Date)value);
138 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
139 return rs.getDate(j);
142 static ColumnDescriptor not_null_both = new ColumnDescriptor
143 ("date_not_null_both", new InstanceHandler() {
144 public void setFieldValue(IdBase instance, Object value) {
145 ((DateAsSqlDateTypes)instance).setDate_not_null_both((Date)value);
147 public Date getFieldValue(IdBase instance) {
148 return ((DateAsSqlDateTypes)instance).getDate_not_null_both();
150 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
151 throws SQLException {
152 preparedStatement.setDate(j, (Date)value);
154 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
155 return rs.getDate(j);
158 static ColumnDescriptor not_null_none = new ColumnDescriptor
159 ("date_not_null_none", new InstanceHandler() {
160 public void setFieldValue(IdBase instance, Object value) {
161 ((DateAsSqlDateTypes)instance).setDate_not_null_none((Date)value);
163 public Date getFieldValue(IdBase instance) {
164 return ((DateAsSqlDateTypes)instance).getDate_not_null_none();
166 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
167 throws SQLException {
168 preparedStatement.setDate(j, (Date)value);
170 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
171 return rs.getDate(j);
175 protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] {
183 protected ColumnDescriptor[] getColumnDescriptors() {
184 return columnDescriptors;