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.sql.PreparedStatement; 29 import java.sql.ResultSet; 30 import java.sql.SQLException; 31 import java.sql.Timestamp; 32 import java.util.Date; 33 34 import testsuite.clusterj.model.DatetimeAsUtilDateTypes; 35 import testsuite.clusterj.model.IdBase; 36 37 /** Test that Datetimes can be read and written. 38 * case 1: Write using JDBC, read using NDB. 39 * case 2: Write using NDB, read using JDBC. 40 * Schema 41 * 42 drop table if exists datetimetypes; 43 create table datetimetypes ( 44 id int not null primary key, 45 46 datetime_null_hash datetime, 47 datetime_null_btree datetime, 48 datetime_null_both datetime, 49 datetime_null_none datetime, 50 51 datetime_not_null_hash datetime, 52 datetime_not_null_btree datetime, 53 datetime_not_null_both datetime, 54 datetime_not_null_none datetime 55 56 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1; 57 58 create unique index idx_datetime_null_hash using hash on datetimetypes(datetime_null_hash); 59 create index idx_datetime_null_btree on datetimetypes(datetime_null_btree); 60 create unique index idx_datetime_null_both on datetimetypes(datetime_null_both); 61 62 create unique index idx_datetime_not_null_hash using hash on datetimetypes(datetime_not_null_hash); 63 create index idx_datetime_not_null_btree on datetimetypes(datetime_not_null_btree); 64 create unique index idx_datetime_not_null_both on datetimetypes(datetime_not_null_both); 65 */ 66 public class DatetimeAsUtilDateTypesTest extends AbstractClusterJModelTest { 67 68 static int NUMBER_OF_INSTANCES = 10; 69 70 @Override getDebug()71 protected boolean getDebug() { 72 return false; 73 } 74 75 @Override getNumberOfInstances()76 protected int getNumberOfInstances() { 77 return NUMBER_OF_INSTANCES; 78 } 79 80 @Override getTableName()81 protected String getTableName() { 82 return "datetimetypes"; 83 } 84 85 /** Subclasses override this method to provide the model class for the test */ 86 @Override getModelClass()87 Class<? extends IdBase> getModelClass() { 88 return DatetimeAsUtilDateTypes.class; 89 } 90 91 /** Subclasses override this method to provide values for rows (i) and columns (j) */ 92 @Override getColumnValue(int i, int j)93 protected Object getColumnValue(int i, int j) { 94 return new Date(getMillisFor(1980, 0, i + 1, 0, 0, j)); 95 } 96 testWriteJDBCReadNDB()97 public void testWriteJDBCReadNDB() { 98 writeJDBCreadNDB(); 99 failOnError(); 100 } 101 testWriteNDBReadNDB()102 public void testWriteNDBReadNDB() { 103 writeNDBreadNDB(); 104 failOnError(); 105 } 106 testWriteJDBCReadJDBC()107 public void testWriteJDBCReadJDBC() { 108 writeJDBCreadJDBC(); 109 failOnError(); 110 } 111 testWriteNDBReadJDBC()112 public void testWriteNDBReadJDBC() { 113 writeNDBreadJDBC(); 114 failOnError(); 115 } 116 117 static ColumnDescriptor not_null_hash = new ColumnDescriptor 118 ("datetime_not_null_hash", new InstanceHandler() { 119 public void setFieldValue(IdBase instance, Object value) { 120 ((DatetimeAsUtilDateTypes)instance).setDatetime_not_null_hash((Date)value); 121 } 122 public Object getFieldValue(IdBase instance) { 123 return ((DatetimeAsUtilDateTypes)instance).getDatetime_not_null_hash(); 124 } 125 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value) 126 throws SQLException { 127 preparedStatement.setTimestamp(j, new Timestamp(((Date)value).getTime())); 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 preparedStatement.setTimestamp(j, new Timestamp(((Date)value).getTime())); 145 } 146 public Object getResultSetValue(ResultSet rs, int j) throws SQLException { 147 return rs.getTimestamp(j); 148 } 149 }); 150 151 static ColumnDescriptor not_null_both = new ColumnDescriptor 152 ("datetime_not_null_both", new InstanceHandler() { 153 public void setFieldValue(IdBase instance, Object value) { 154 ((DatetimeAsUtilDateTypes)instance).setDatetime_not_null_both((Date)value); 155 } 156 public Date getFieldValue(IdBase instance) { 157 return ((DatetimeAsUtilDateTypes)instance).getDatetime_not_null_both(); 158 } 159 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value) 160 throws SQLException { 161 preparedStatement.setTimestamp(j, new Timestamp(((Date)value).getTime())); 162 } 163 public Object getResultSetValue(ResultSet rs, int j) throws SQLException { 164 return rs.getTimestamp(j); 165 } 166 }); 167 168 static ColumnDescriptor not_null_none = new ColumnDescriptor 169 ("datetime_not_null_none", new InstanceHandler() { 170 public void setFieldValue(IdBase instance, Object value) { 171 ((DatetimeAsUtilDateTypes)instance).setDatetime_not_null_none((Date)value); 172 } 173 public Date getFieldValue(IdBase instance) { 174 return ((DatetimeAsUtilDateTypes)instance).getDatetime_not_null_none(); 175 } 176 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value) 177 throws SQLException { 178 preparedStatement.setTimestamp(j, new Timestamp(((Date)value).getTime())); 179 } 180 public Object getResultSetValue(ResultSet rs, int j) throws SQLException { 181 return rs.getTimestamp(j); 182 } 183 }); 184 185 protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] { 186 not_null_hash, 187 not_null_btree, 188 not_null_both, 189 not_null_none 190 }; 191 192 @Override getColumnDescriptors()193 protected ColumnDescriptor[] getColumnDescriptors() { 194 return columnDescriptors; 195 } 196 197 } 198