]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
bcb62d2f947e4abe137a24eb6ff01b5600aada65
[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.TimestampAsUtilDateTypes;
28 import com.mysql.clusterj.jpatest.model.IdBase;
29
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.
33  * Schema
34  *
35 drop table if exists timestamptypes;
36 create table timestamptypes (
37  id int not null primary key,
38
39  timestamp_not_null_hash timestamp,
40  timestamp_not_null_btree timestamp,
41  timestamp_not_null_both timestamp,
42  timestamp_not_null_none timestamp
43
44 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
45
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);
49
50  */
51 public class TimestampAsUtilDateTest extends AbstractJPABaseTest {
52
53     @Override
54     public void setUp() {
55         super.setUp();
56         getConnection();
57         resetLocalSystemDefaultTimeZone(connection);
58         connection = null;
59         getConnection();
60         setAutoCommit(connection, false);
61     }
62
63     static int NUMBER_OF_INSTANCES = 10;
64
65     @Override
66     protected boolean getDebug() {
67         return false;
68     }
69
70     @Override
71     protected int getNumberOfInstances() {
72         return NUMBER_OF_INSTANCES;
73     }
74
75     @Override
76     protected String getTableName() {
77         return "datetimetypes";
78     }
79
80     /** Subclasses override this method to provide the model class for the test */
81     @Override
82     protected Class<? extends IdBase> getModelClass() {
83         return TimestampAsUtilDateTypes.class;
84     }
85
86     /** Subclasses override this method to provide values for rows (i) and columns (j) */
87     @Override
88     protected Object getColumnValue(int i, int j) {
89         return new Date(getMillisFor(1980, 0, i + 1, 0, 0, j));
90     }
91
92     @Override
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();
96     }
97
98     public void testWriteJDBCReadJPA() {
99         writeJDBCreadJPA();
100         failOnError();
101     }
102
103     public void testWriteJPAReadJDBC() {
104         writeJPAreadJDBC();
105         failOnError();
106    }
107
108     public void testWriteJDBCReadJDBC() {
109         writeJDBCreadJDBC();
110         failOnError();
111     }
112
113     public void testWriteJPAReadJPA() {
114         writeJPAreadJPA();
115         failOnError();
116    }
117
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);
122         }
123         public Object getFieldValue(IdBase instance) {
124             return ((TimestampAsUtilDateTypes)instance).getTimestamp_not_null_hash();
125         }
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);
130         }
131         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
132             return rs.getTimestamp(j);
133         }
134     });
135
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);
140         }
141         public Object getFieldValue(IdBase instance) {
142             return ((TimestampAsUtilDateTypes)instance).getTimestamp_not_null_btree();
143         }
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);
148         }
149         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
150             return rs.getTimestamp(j);
151         }
152     });
153
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);
158         }
159         public Date getFieldValue(IdBase instance) {
160             return ((TimestampAsUtilDateTypes)instance).getTimestamp_not_null_both();
161         }
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);
166         }
167         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
168             return rs.getTimestamp(j);
169         }
170     });
171
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);
176         }
177         public Date getFieldValue(IdBase instance) {
178             return ((TimestampAsUtilDateTypes)instance).getTimestamp_not_null_none();
179         }
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);
184         }
185         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
186             return rs.getTimestamp(j);
187         }
188     });
189
190     protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] {
191             not_null_hash,
192             not_null_btree,
193             not_null_both,
194             not_null_none
195         };
196
197     @Override
198     protected ColumnDescriptor[] getColumnDescriptors() {
199         return columnDescriptors;
200     }
201
202 }