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 33 import testsuite.clusterj.model.DatetimeAsSqlTimestampTypes; 34 import testsuite.clusterj.model.IdBase; 35 36 /** Test that Datetimes can be read and written. 37 * case 1: Write using JDBC, read using NDB. 38 * case 2: Write using NDB, read using JDBC. 39 * Schema 40 * 41 drop table if exists datetimetypes; 42 create table datetimetypes ( 43 id int not null primary key, 44 45 datetime_null_hash datetime, 46 datetime_null_btree datetime, 47 datetime_null_both datetime, 48 datetime_null_none datetime, 49 50 datetime_not_null_hash datetime, 51 datetime_not_null_btree datetime, 52 datetime_not_null_both datetime, 53 datetime_not_null_none datetime 54 55 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1; 56 57 create unique index idx_datetime_null_hash using hash on datetimetypes(datetime_null_hash); 58 create index idx_datetime_null_btree on datetimetypes(datetime_null_btree); 59 create unique index idx_datetime_null_both on datetimetypes(datetime_null_both); 60 61 create unique index idx_datetime_not_null_hash using hash on datetimetypes(datetime_not_null_hash); 62 create index idx_datetime_not_null_btree on datetimetypes(datetime_not_null_btree); 63 create unique index idx_datetime_not_null_both on datetimetypes(datetime_not_null_both); 64 */ 65 public class DatetimeAsSqlTimestampTypesTest 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 "datetimetypes"; 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 DatetimeAsSqlTimestampTypes.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 Timestamp(getMillisFor(1980, 0, i + 1, 0, 0, j)); 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 ("datetime_not_null_hash", new InstanceHandler() { 118 public void setFieldValue(IdBase instance, Object value) { 119 ((DatetimeAsSqlTimestampTypes)instance).setDatetime_not_null_hash((Timestamp)value); 120 } 121 public Object getFieldValue(IdBase instance) { 122 return ((DatetimeAsSqlTimestampTypes)instance).getDatetime_not_null_hash(); 123 } 124 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value) 125 throws SQLException { 126 preparedStatement.setTimestamp(j, (Timestamp)value); 127 } 128 public Object getResultSetValue(ResultSet rs, int j) throws SQLException { 129 return rs.getTimestamp(j); 130 } 131 }); 132 133 static ColumnDescriptor not_null_btree = new ColumnDescriptor 134 ("datetime_not_null_btree", new InstanceHandler() { 135 public void setFieldValue(IdBase instance, Object value) { 136 ((DatetimeAsSqlTimestampTypes)instance).setDatetime_not_null_btree((Timestamp)value); 137 } 138 public Object getFieldValue(IdBase instance) { 139 return ((DatetimeAsSqlTimestampTypes)instance).getDatetime_not_null_btree(); 140 } 141 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value) 142 throws SQLException { 143 preparedStatement.setTimestamp(j, (Timestamp)value); 144 } 145 public Object getResultSetValue(ResultSet rs, int j) throws SQLException { 146 return rs.getTimestamp(j); 147 } 148 }); 149 150 static ColumnDescriptor not_null_both = new ColumnDescriptor 151 ("datetime_not_null_both", new InstanceHandler() { 152 public void setFieldValue(IdBase instance, Object value) { 153 ((DatetimeAsSqlTimestampTypes)instance).setDatetime_not_null_both((Timestamp)value); 154 } 155 public Timestamp getFieldValue(IdBase instance) { 156 return ((DatetimeAsSqlTimestampTypes)instance).getDatetime_not_null_both(); 157 } 158 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value) 159 throws SQLException { 160 preparedStatement.setTimestamp(j, (Timestamp)value); 161 } 162 public Object getResultSetValue(ResultSet rs, int j) throws SQLException { 163 return rs.getTimestamp(j); 164 } 165 }); 166 167 static ColumnDescriptor not_null_none = new ColumnDescriptor 168 ("datetime_not_null_none", new InstanceHandler() { 169 public void setFieldValue(IdBase instance, Object value) { 170 ((DatetimeAsSqlTimestampTypes)instance).setDatetime_not_null_none((Timestamp)value); 171 } 172 public Timestamp getFieldValue(IdBase instance) { 173 return ((DatetimeAsSqlTimestampTypes)instance).getDatetime_not_null_none(); 174 } 175 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value) 176 throws SQLException { 177 preparedStatement.setTimestamp(j, (Timestamp)value); 178 } 179 public Object getResultSetValue(ResultSet rs, int j) throws SQLException { 180 return rs.getTimestamp(j); 181 } 182 }); 183 184 protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] { 185 not_null_hash, 186 not_null_btree, 187 not_null_both, 188 not_null_none 189 }; 190 191 @Override getColumnDescriptors()192 protected ColumnDescriptor[] getColumnDescriptors() { 193 return columnDescriptors; 194 } 195 196 } 197