]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
7d9c9dc60f1d151d88860eb4aa2570642b42dad7
[packages/trusty/mysql-wsrep-5.6.git] /
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 as published by
7    the Free Software Foundation; version 2 of the License.
8
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.
13
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
17 */
18
19 package com.mysql.clusterj.jpatest;
20
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;
26
27 import com.mysql.clusterj.jpatest.model.DatetimeAsUtilDateTypes;
28 import com.mysql.clusterj.jpatest.model.IdBase;
29
30 /** Test that Datetimes can be read and written. 
31  * case 1: Write using JDBC, read using NDB.
32  * case 2: Write using NDB, read using JDBC.
33  * Schema
34  *
35 drop table if exists datetimetypes;
36 create table datetimetypes (
37  id int not null primary key,
38
39  datetime_null_hash datetime,
40  datetime_null_btree datetime,
41  datetime_null_both datetime,
42  datetime_null_none datetime,
43
44  datetime_not_null_hash datetime,
45  datetime_not_null_btree datetime,
46  datetime_not_null_both datetime,
47  datetime_not_null_none datetime
48
49 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
50
51 create unique index idx_datetime_null_hash using hash on datetimetypes(datetime_null_hash);
52 create index idx_datetime_null_btree on datetimetypes(datetime_null_btree);
53 create unique index idx_datetime_null_both on datetimetypes(datetime_null_both);
54
55 create unique index idx_datetime_not_null_hash using hash on datetimetypes(datetime_not_null_hash);
56 create index idx_datetime_not_null_btree on datetimetypes(datetime_not_null_btree);
57 create unique index idx_datetime_not_null_both on datetimetypes(datetime_not_null_both);
58  */
59 public class DatetimeAsUtilDateTest extends AbstractJPABaseTest {
60
61     static int NUMBER_OF_INSTANCES = 10;
62
63     @Override
64     protected boolean getDebug() {
65         return false;
66     }
67
68     @Override
69     protected int getNumberOfInstances() {
70         return NUMBER_OF_INSTANCES;
71     }
72
73     @Override
74     protected String getTableName() {
75         return "datetimetypes";
76     }
77
78     /** Subclasses override this method to provide the model class for the test */
79     @Override
80     protected Class<? extends IdBase> getModelClass() {
81         return DatetimeAsUtilDateTypes.class;
82     }
83
84     /** Subclasses override this method to provide values for rows (i) and columns (j) */
85     @Override
86     protected Object getColumnValue(int i, int j) {
87         return new Date(getMillisFor(1980, 0, i + 1, 0, 0, j));
88     }
89
90     @Override
91     /** Subclasses must override this method to implement the model factory for the test */
92     protected IdBase getNewInstance(Class<? extends IdBase> modelClass) {
93         return new DatetimeAsUtilDateTypes();
94     }
95
96     public void testWriteJDBCReadJPA() {
97         writeJDBCreadJPA();
98         failOnError();
99     }
100
101     public void testWriteJPAReadJDBC() {
102         writeJPAreadJDBC();
103         failOnError();
104    }
105
106     public void testWriteJDBCReadJDBC() {
107         writeJDBCreadJDBC();
108         failOnError();
109     }
110
111     public void testWriteJPAReadJPA() {
112         writeJPAreadJPA();
113         failOnError();
114    }
115
116    static ColumnDescriptor not_null_hash = new ColumnDescriptor
117             ("datetime_not_null_hash", new InstanceHandler() {
118         public void setFieldValue(IdBase instance, Object value) {
119             ((DatetimeAsUtilDateTypes)instance).setDatetime_not_null_hash((Date)value);
120         }
121         public Object getFieldValue(IdBase instance) {
122             return ((DatetimeAsUtilDateTypes)instance).getDatetime_not_null_hash();
123         }
124         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
125                 throws SQLException {
126             Timestamp timestamp = new Timestamp(((Date)value).getTime());
127             preparedStatement.setTimestamp(j, timestamp);
128         }
129         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
130             return rs.getTimestamp(j);
131         }
132     });
133
134     static ColumnDescriptor not_null_btree = new ColumnDescriptor
135             ("datetime_not_null_btree", new InstanceHandler() {
136         public void setFieldValue(IdBase instance, Object value) {
137             ((DatetimeAsUtilDateTypes)instance).setDatetime_not_null_btree((Date)value);
138         }
139         public Object getFieldValue(IdBase instance) {
140             return ((DatetimeAsUtilDateTypes)instance).getDatetime_not_null_btree();
141         }
142         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
143                 throws SQLException {
144             Timestamp timestamp = new Timestamp(((Date)value).getTime());
145             preparedStatement.setTimestamp(j, timestamp);
146         }
147         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
148             return rs.getTimestamp(j);
149         }
150     });
151
152     static ColumnDescriptor not_null_both = new ColumnDescriptor
153             ("datetime_not_null_both", new InstanceHandler() {
154         public void setFieldValue(IdBase instance, Object value) {
155             ((DatetimeAsUtilDateTypes)instance).setDatetime_not_null_both((Date)value);
156         }
157         public Date getFieldValue(IdBase instance) {
158             return ((DatetimeAsUtilDateTypes)instance).getDatetime_not_null_both();
159         }
160         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
161                 throws SQLException {
162             Timestamp timestamp = new Timestamp(((Date)value).getTime());
163             preparedStatement.setTimestamp(j, timestamp);
164         }
165         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
166             return rs.getTimestamp(j);
167         }
168     });
169
170     static ColumnDescriptor not_null_none = new ColumnDescriptor
171             ("datetime_not_null_none", new InstanceHandler() {
172         public void setFieldValue(IdBase instance, Object value) {
173             ((DatetimeAsUtilDateTypes)instance).setDatetime_not_null_none((Date)value);
174         }
175         public Date getFieldValue(IdBase instance) {
176             return ((DatetimeAsUtilDateTypes)instance).getDatetime_not_null_none();
177         }
178         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
179                 throws SQLException {
180             Timestamp timestamp = new Timestamp(((Date)value).getTime());
181             preparedStatement.setTimestamp(j, timestamp);
182         }
183         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
184             return rs.getTimestamp(j);
185         }
186     });
187
188     protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] {
189             not_null_hash,
190             not_null_btree,
191             not_null_both,
192             not_null_none
193         };
194
195     @Override
196     protected ColumnDescriptor[] getColumnDescriptors() {
197         return columnDescriptors;
198     }
199
200 }