1 /*
2    Copyright 2010 Sun Microsystems, Inc.
3    All rights reserved. Use is subject to license terms.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License, version 2.0,
7    as published by the Free Software Foundation.
8 
9    This program is also distributed with certain software (including
10    but not limited to OpenSSL) that is licensed under separate terms,
11    as designated in a particular file or component or in included license
12    documentation.  The authors of MySQL hereby grant you an additional
13    permission to link the program and your derivative works with the
14    separately licensed software that they have included with MySQL.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License, version 2.0, for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 
26 package testsuite.clusterj;
27 
28 import java.util.Date;
29 import java.sql.PreparedStatement;
30 import java.sql.ResultSet;
31 import java.sql.SQLException;
32 import testsuite.clusterj.model.DateAsUtilDateTypes;
33 import testsuite.clusterj.model.IdBase;
34 
35 /** Test that Dates can be read and written.
36  * case 1: Write using JDBC, read using NDB.
37  * case 2: Write using NDB, read using JDBC.
38  * Schema
39  *
40 drop table if exists datetypes;
41 create table datetypes (
42  id int not null primary key,
43 
44  date_null_hash date,
45  date_null_btree date,
46  date_null_both date,
47  date_null_none date,
48 
49  date_not_null_hash date,
50  date_not_null_btree date,
51  date_not_null_both date,
52  date_not_null_none date
53 
54 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
55 
56 create unique index idx_date_null_hash using hash on datetypes(date_null_hash);
57 create index idx_date_null_btree on datetypes(date_null_btree);
58 create unique index idx_date_null_both on datetypes(date_null_both);
59 
60 create unique index idx_date_not_null_hash using hash on datetypes(date_not_null_hash);
61 create index idx_date_not_null_btree on datetypes(date_not_null_btree);
62 create unique index idx_date_not_null_both on datetypes(date_not_null_both);
63 
64  */
65 public class DateAsUtilDateTypesTest extends AbstractClusterJModelTest {
66 
67     static int NUMBER_OF_INSTANCES = 10;
68 
69     @Override
getDebug()70     protected boolean getDebug() {
71         return false;
72     }
73 
74     @Override
getNumberOfInstances()75     protected int getNumberOfInstances() {
76         return NUMBER_OF_INSTANCES;
77     }
78 
79     @Override
getTableName()80     protected String getTableName() {
81         return "datetypes";
82     }
83 
84     /** Subclasses override this method to provide the model class for the test */
85     @Override
getModelClass()86     Class<? extends IdBase> getModelClass() {
87         return DateAsUtilDateTypes.class;
88     }
89 
90     /** Subclasses override this method to provide values for rows (i) and columns (j) */
91     @Override
getColumnValue(int i, int j)92     protected Object getColumnValue(int i, int j) {
93         return new Date(getMillisFor(1980, i, j + 1));
94     }
95 
testWriteJDBCReadNDB()96     public void testWriteJDBCReadNDB() {
97         writeJDBCreadNDB();
98         failOnError();
99     }
100 
testWriteNDBReadNDB()101     public void testWriteNDBReadNDB() {
102         writeNDBreadNDB();
103         failOnError();
104     }
105 
testWriteJDBCReadJDBC()106     public void testWriteJDBCReadJDBC() {
107         writeJDBCreadJDBC();
108         failOnError();
109    }
110 
testWriteNDBReadJDBC()111     public void testWriteNDBReadJDBC() {
112         writeNDBreadJDBC();
113         failOnError();
114    }
115 
116    static ColumnDescriptor not_null_hash = new ColumnDescriptor
117             ("date_not_null_hash", new InstanceHandler() {
118         public void setFieldValue(IdBase instance, Object value) {
119             ((DateAsUtilDateTypes)instance).setDate_not_null_hash((Date)value);
120         }
121         public Object getFieldValue(IdBase instance) {
122             return ((DateAsUtilDateTypes)instance).getDate_not_null_hash();
123         }
124         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
125                 throws SQLException {
126             java.sql.Date date = new java.sql.Date(((Date)value).getTime());
127             preparedStatement.setDate(j, date);
128         }
129         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
130             return rs.getDate(j);
131         }
132     });
133 
134     static ColumnDescriptor not_null_btree = new ColumnDescriptor
135             ("date_not_null_btree", new InstanceHandler() {
136         public void setFieldValue(IdBase instance, Object value) {
137             ((DateAsUtilDateTypes)instance).setDate_not_null_btree((Date)value);
138         }
139         public Object getFieldValue(IdBase instance) {
140             return ((DateAsUtilDateTypes)instance).getDate_not_null_btree();
141         }
142         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
143                 throws SQLException {
144             java.sql.Date date = new java.sql.Date(((Date)value).getTime());
145             preparedStatement.setDate(j, date);
146         }
147         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
148             return rs.getDate(j);
149         }
150     });
151     static ColumnDescriptor not_null_both = new ColumnDescriptor
152             ("date_not_null_both", new InstanceHandler() {
153         public void setFieldValue(IdBase instance, Object value) {
154             ((DateAsUtilDateTypes)instance).setDate_not_null_both((Date)value);
155         }
156         public Date getFieldValue(IdBase instance) {
157             return ((DateAsUtilDateTypes)instance).getDate_not_null_both();
158         }
159         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
160                 throws SQLException {
161             java.sql.Date date = new java.sql.Date(((Date)value).getTime());
162             preparedStatement.setDate(j, date);
163         }
164         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
165             return rs.getDate(j);
166         }
167     });
168     static ColumnDescriptor not_null_none = new ColumnDescriptor
169             ("date_not_null_none", new InstanceHandler() {
170         public void setFieldValue(IdBase instance, Object value) {
171             ((DateAsUtilDateTypes)instance).setDate_not_null_none((Date)value);
172         }
173         public Date getFieldValue(IdBase instance) {
174             return ((DateAsUtilDateTypes)instance).getDate_not_null_none();
175         }
176         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
177                 throws SQLException {
178             java.sql.Date date = new java.sql.Date(((Date)value).getTime());
179             preparedStatement.setDate(j, date);
180         }
181         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
182             return rs.getDate(j);
183         }
184     });
185 
186     protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] {
187             not_null_hash,
188             not_null_btree,
189             not_null_both,
190             not_null_none
191         };
192 
193     @Override
getColumnDescriptors()194     protected ColumnDescriptor[] getColumnDescriptors() {
195         return columnDescriptors;
196     }
197 
198 }
199