2 Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 package testsuite.clusterj;
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.util.List;
25 import testsuite.clusterj.model.BitTypes;
26 import testsuite.clusterj.model.IdBase;
28 /** Test that BIT types can be read and written.
29 * case 1: Write using JDBC, read using NDB.
30 * case 2: Write using NDB, read using JDBC.
33 drop table if exists bittypes;
34 create table bittypes (
35 id int not null primary key,
45 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
48 public class BitTypesTest extends AbstractClusterJModelTest {
50 static int NUMBER_OF_INSTANCES = 10;
53 protected boolean getDebug() {
58 protected int getNumberOfInstances() {
59 return NUMBER_OF_INSTANCES;
63 protected String getTableName() {
67 /** Subclasses override this method to provide the model class for the test */
69 Class<? extends IdBase> getModelClass() {
70 return BitTypes.class;
73 /** Subclasses override this method to provide values for rows (i) and columns (j) */
75 protected Object getColumnValue(int i, int j) {
76 // Note that i and j are both 0-index here to correspond to Java semantics
77 // first calculate the length of the data
78 int length = (int)Math.pow(2, j);
81 boolean data = (i % 2) == 0;
82 if (getDebug()) System.out.println("BitTypesTest.getColumnValue Column data for " + i + ", " + j
83 + " is (boolean)" + data);
88 // fill in the data, increasing by one for each row, column, and bit in the data
89 for (int d = 0; d < length; ++d) {
90 data = (data * 2) + (int)(Math.random() * 2);
92 if (getDebug()) System.out.println("BitTypesTest.getColumnValue Column data for " + i + ", " + j
93 + " is (byte)" + data);
94 return Byte.valueOf((byte)data);
98 // fill in the data, increasing by one for each row, column, and bit in the data
99 for (int d = 0; d < length; ++d) {
100 data = (data * 2) + (int)(Math.random() * 2);
102 if (getDebug()) System.out.println("BitTypesTest.getColumnValue Column data for " + i + ", " + j
103 + " is (short)" + data);
104 return Short.valueOf((short)data);
109 // fill in the data, increasing by one for each row, column, and bit in the data
110 for (int d = 0; d < length; ++d) {
111 data = (data * 2) + ((int)(Math.random() * 2));
113 if (getDebug()) System.out.println("BitTypesTest.getColumnValue Column data for " + i + ", " + j
114 + " is (int)" + data);
115 // TODO bug in JDBC handling high bit
116 data = Math.abs(data);
117 return Integer.valueOf(data);
122 // fill in the data, increasing by one for each row, column, and bit in the data
123 for (int d = 0; d < length / 8; ++d) {
124 data = (data * 256) + (i * 16) + d;
126 if (getDebug()) System.out.println("BitTypesTest.getColumnValue Column data for " + i + ", " + j
127 + " is (long)" + data);
128 return Long.valueOf(data);
131 fail("Bad length: " + length);
136 /** Verify that the actual results match the expected results. If not, use the multiple error
137 * reporting method errorIfNotEqual defined in the superclass.
138 * @param where the location of the verification of results, normally the name of the test method
139 * @param expecteds the expected results
140 * @param actuals the actual results
143 protected void verify(String where, List<Object[]> expecteds, List<Object[]> actuals) {
144 // note that here, i is 0-index but j is 1-index to correspond to JDBC semantics
145 for (int i = 0; i < expecteds.size(); ++i) {
146 Object[] expected = expecteds.get(i);
147 Object[] actual = actuals.get(i);
148 errorIfNotEqual(where + " got failure on id for row " + i, i, actual[0]);
149 for (int j = 1; j < expected.length; ++j) {
150 if (getDebug()) System.out.println("BitTypesTest.verify for " + i + ", " + j
151 + " is (" + actual[j].getClass().getName() + ")" + actual[j]);
154 Boolean expectedColumn = (Boolean)expected[j];
155 Boolean actualColumn = (Boolean)actual[j];
156 errorIfNotEqual(where + " got failure on comparison of data for row "
157 + i + " column " + j,
158 expectedColumn, actualColumn);
162 byte expectedColumn = (Byte)expected[j];
163 byte actualColumn = (Byte)actual[j];
164 // now compare bit by bit
165 errorIfNotEqual(where + " got failure on comparison of data for row "
166 + i + " column " + j,
167 Integer.toHexString(expectedColumn), Integer.toHexString(actualColumn));
171 short expectedColumn = (Short)expected[j];
172 short actualColumn = (Short)actual[j];
173 // now compare bit by bit
174 errorIfNotEqual(where + " got failure on comparison of data for row "
175 + i + " column " + j,
176 Integer.toHexString(expectedColumn), Integer.toHexString(actualColumn));
181 int expectedColumn = (Integer)expected[j];
182 int actualColumn = (Integer)actual[j];
183 // now compare bit by bit
184 errorIfNotEqual(where + " got failure on comparison of data for row "
185 + i + " column " + j,
186 Integer.toHexString(expectedColumn), Integer.toHexString(actualColumn));
191 long expectedColumn = (Long)expected[j];
192 long actualColumn = (Long)actual[j];
193 // now compare bit by bit
194 errorIfNotEqual(where + " got failure on comparison of data for row "
195 + i + " column " + j,
196 Long.toHexString(expectedColumn), Long.toHexString(actualColumn));
200 fail("Bad value for j: " + j);
206 public void testWriteJDBCReadNDB() {
211 public void testWriteNDBReadJDBC() {
212 // TODO: investigate platform dependency when reading via JDBC
213 // writeNDBreadJDBC();
217 public void testWriteNDBReadNDB() {
222 public void testWriteJDBCReadJDBC() {
223 // TODO: investigate platform dependency when reading via JDBC
224 // writeJDBCreadJDBC();
228 static ColumnDescriptor bit1 = new ColumnDescriptor
229 ("bit1", new InstanceHandler() {
230 public void setFieldValue(IdBase instance, Object value) {
231 ((BitTypes)instance).setBit1((Boolean)value);
233 public Object getFieldValue(IdBase instance) {
234 return ((BitTypes)instance).getBit1();
236 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
237 throws SQLException {
238 preparedStatement.setBoolean(j, (Boolean)value);
240 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
241 String value = rs.getString(j);
242 if (value.length() == 0) {
245 return (Byte.parseByte(value) == 0x01)?Boolean.TRUE:Boolean.FALSE;
249 static ColumnDescriptor bit2 = new ColumnDescriptor
250 ("bit2", new InstanceHandler() {
251 public void setFieldValue(IdBase instance, Object value) {
252 ((BitTypes)instance).setBit2((Byte)value);
254 public Object getFieldValue(IdBase instance) {
255 return (Byte)((BitTypes)instance).getBit2();
257 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
258 throws SQLException {
259 preparedStatement.setByte(j, (Byte)value);
261 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
262 String value = rs.getString(j);
263 if (value.length() == 0) {
266 return Byte.parseByte(value);
270 static ColumnDescriptor bit4 = new ColumnDescriptor
271 ("bit4", new InstanceHandler() {
272 public void setFieldValue(IdBase instance, Object value) {
273 ((BitTypes)instance).setBit4((Short)value);
275 public Object getFieldValue(IdBase instance) {
276 return (Short)((BitTypes)instance).getBit4();
278 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
279 throws SQLException {
280 preparedStatement.setShort(j, (Short)value);
282 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
283 String value = rs.getString(j);
284 if (value.length() == 0) {
287 return Short.parseShort(value);
291 static ColumnDescriptor bit8 = new ColumnDescriptor
292 ("bit8", new InstanceHandler() {
293 public void setFieldValue(IdBase instance, Object value) {
294 ((BitTypes)instance).setBit8((Integer)value);
296 public Object getFieldValue(IdBase instance) {
297 return (Integer)((BitTypes)instance).getBit8();
299 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
300 throws SQLException {
301 preparedStatement.setInt(j, (Integer)value);
303 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
304 String value = rs.getString(j);
305 if (value.length() == 0) {
308 return Integer.parseInt(value) & 0xff;
312 static ColumnDescriptor bit16 = new ColumnDescriptor
313 ("bit16", new InstanceHandler() {
314 public void setFieldValue(IdBase instance, Object value) {
315 ((BitTypes)instance).setBit16((Long)value);
317 public Object getFieldValue(IdBase instance) {
318 return ((BitTypes)instance).getBit16();
320 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
321 throws SQLException {
322 preparedStatement.setLong(j, (Long)value);
324 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
325 String value = rs.getString(j);
326 if (value.length() == 0) {
329 return Long.parseLong(value);
333 static ColumnDescriptor bit32 = new ColumnDescriptor
334 ("bit32", new InstanceHandler() {
335 public void setFieldValue(IdBase instance, Object value) {
336 ((BitTypes)instance).setBit32((Integer)value);
338 public Object getFieldValue(IdBase instance) {
339 return ((BitTypes)instance).getBit32();
341 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
342 throws SQLException {
343 preparedStatement.setInt(j, (Integer)value);
345 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
346 String value = rs.getString(j);
347 if (value.length() == 0) {
350 return Integer.parseInt(value);
354 static ColumnDescriptor bit64 = new ColumnDescriptor
355 ("bit64", new InstanceHandler() {
356 public void setFieldValue(IdBase instance, Object value) {
357 ((BitTypes)instance).setBit64((Long)value);
359 public Object getFieldValue(IdBase instance) {
360 return ((BitTypes)instance).getBit64();
362 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
363 throws SQLException {
364 preparedStatement.setLong(j, (Long)value);
366 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
367 String value = rs.getString(j);
368 if (value.length() == 0) {
371 return Long.parseLong(value);
375 protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] {
386 protected ColumnDescriptor[] getColumnDescriptors() {
387 return columnDescriptors;