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.Time; 32 import testsuite.clusterj.model.IdBase; 33 import testsuite.clusterj.model.TimeAsSqlTimeTypes; 34 35 /** Test that Times 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 timetypes; 41 create table timetypes ( 42 id int not null primary key, 43 44 time_null_hash time, 45 time_null_btree time, 46 time_null_both time, 47 time_null_none time, 48 49 time_not_null_hash time, 50 time_not_null_btree time, 51 time_not_null_both time, 52 time_not_null_none time 53 54 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1; 55 56 create unique index idx_time_null_hash using hash on timetypes(time_null_hash); 57 create index idx_time_null_btree on timetypes(time_null_btree); 58 create unique index idx_time_null_both on timetypes(time_null_both); 59 60 create unique index idx_time_not_null_hash using hash on timetypes(time_not_null_hash); 61 create index idx_time_not_null_btree on timetypes(time_not_null_btree); 62 create unique index idx_time_not_null_both on timetypes(time_not_null_both); 63 */ 64 public class TimeAsSqlTimeTypesTest extends AbstractClusterJModelTest { 65 66 static int NUMBER_OF_INSTANCES = 10; 67 68 @Override getDebug()69 protected boolean getDebug() { 70 return false; 71 } 72 73 @Override getNumberOfInstances()74 protected int getNumberOfInstances() { 75 return NUMBER_OF_INSTANCES; 76 } 77 78 @Override getTableName()79 protected String getTableName() { 80 return "timetypes"; 81 } 82 83 /** Subclasses override this method to provide the model class for the test */ 84 @Override getModelClass()85 Class<? extends IdBase> getModelClass() { 86 return TimeAsSqlTimeTypes.class; 87 } 88 89 /** Subclasses override this method to provide values for rows (i) and columns (j) */ 90 @Override getColumnValue(int i, int j)91 protected Object getColumnValue(int i, int j) { 92 return new Time(getMillisFor(0, i, i, j)); 93 } 94 testWriteJDBCReadNDB()95 public void testWriteJDBCReadNDB() { 96 writeJDBCreadNDB(); 97 failOnError(); 98 } 99 testWriteNDBReadNDB()100 public void testWriteNDBReadNDB() { 101 writeNDBreadNDB(); 102 failOnError(); 103 } 104 testWriteJDBCReadJDBC()105 public void testWriteJDBCReadJDBC() { 106 writeJDBCreadJDBC(); 107 failOnError(); 108 } 109 testWriteNDBReadJDBC()110 public void testWriteNDBReadJDBC() { 111 writeNDBreadJDBC(); 112 failOnError(); 113 } 114 115 static ColumnDescriptor not_null_hash = new ColumnDescriptor 116 ("time_not_null_hash", new InstanceHandler() { 117 public void setFieldValue(IdBase instance, Object value) { 118 ((TimeAsSqlTimeTypes)instance).setTime_not_null_hash((Time)value); 119 } 120 public Object getFieldValue(IdBase instance) { 121 return ((TimeAsSqlTimeTypes)instance).getTime_not_null_hash(); 122 } 123 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value) 124 throws SQLException { 125 preparedStatement.setTime(j, (Time)value); 126 } 127 public Object getResultSetValue(ResultSet rs, int j) throws SQLException { 128 return rs.getTime(j); 129 } 130 }); 131 132 static ColumnDescriptor not_null_btree = new ColumnDescriptor 133 ("time_not_null_btree", new InstanceHandler() { 134 public void setFieldValue(IdBase instance, Object value) { 135 ((TimeAsSqlTimeTypes)instance).setTime_not_null_btree((Time)value); 136 } 137 public Object getFieldValue(IdBase instance) { 138 return ((TimeAsSqlTimeTypes)instance).getTime_not_null_btree(); 139 } 140 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value) 141 throws SQLException { 142 preparedStatement.setTime(j, (Time)value); 143 } 144 public Object getResultSetValue(ResultSet rs, int j) throws SQLException { 145 return rs.getTime(j); 146 } 147 }); 148 149 static ColumnDescriptor not_null_both = new ColumnDescriptor 150 ("time_not_null_both", new InstanceHandler() { 151 public void setFieldValue(IdBase instance, Object value) { 152 ((TimeAsSqlTimeTypes)instance).setTime_not_null_both((Time)value); 153 } 154 public Time getFieldValue(IdBase instance) { 155 return ((TimeAsSqlTimeTypes)instance).getTime_not_null_both(); 156 } 157 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value) 158 throws SQLException { 159 preparedStatement.setTime(j, (Time)value); 160 } 161 public Object getResultSetValue(ResultSet rs, int j) throws SQLException { 162 return rs.getTime(j); 163 } 164 }); 165 166 static ColumnDescriptor not_null_none = new ColumnDescriptor 167 ("time_not_null_none", new InstanceHandler() { 168 public void setFieldValue(IdBase instance, Object value) { 169 ((TimeAsSqlTimeTypes)instance).setTime_not_null_none((Time)value); 170 } 171 public Time getFieldValue(IdBase instance) { 172 return ((TimeAsSqlTimeTypes)instance).getTime_not_null_none(); 173 } 174 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value) 175 throws SQLException { 176 preparedStatement.setTime(j, (Time)value); 177 } 178 public Object getResultSetValue(ResultSet rs, int j) throws SQLException { 179 return rs.getTime(j); 180 } 181 }); 182 183 protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] { 184 not_null_hash, 185 not_null_btree, 186 not_null_both, 187 not_null_none 188 }; 189 190 @Override getColumnDescriptors()191 protected ColumnDescriptor[] getColumnDescriptors() { 192 return columnDescriptors; 193 } 194 195 } 196