MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BitTypesTest.java
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
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.
7 
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.
12 
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
16 */
17 
18 package testsuite.clusterj;
19 
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.util.List;
24 
25 import testsuite.clusterj.model.BitTypes;
26 import testsuite.clusterj.model.IdBase;
27 
49 
50  static int NUMBER_OF_INSTANCES = 10;
51 
52  @Override
53  protected boolean getDebug() {
54  return false;
55  }
56 
57  @Override
58  protected int getNumberOfInstances() {
59  return NUMBER_OF_INSTANCES;
60  }
61 
62  @Override
63  protected String getTableName() {
64  return "bittypes";
65  }
66 
68  @Override
69  Class<? extends IdBase> getModelClass() {
70  return BitTypes.class;
71  }
72 
74  @Override
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);
79  switch (length) {
80  case 1: { // boolean
81  boolean data = (i % 2) == 0;
82  if (getDebug()) System.out.println("BitTypesTest.getColumnValue Column data for " + i + ", " + j
83  + " is (boolean)" + data);
84  return data;
85  }
86  case 2: { // byte
87  int data = 0;
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);
91  }
92  if (getDebug()) System.out.println("BitTypesTest.getColumnValue Column data for " + i + ", " + j
93  + " is (byte)" + data);
94  return Byte.valueOf((byte)data);
95  }
96  case 4: { // short
97  int data = 0;
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);
101  }
102  if (getDebug()) System.out.println("BitTypesTest.getColumnValue Column data for " + i + ", " + j
103  + " is (short)" + data);
104  return Short.valueOf((short)data);
105  }
106  case 8:
107  case 32: { // int
108  int data = 0;
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));
112  }
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);
118  }
119  case 16:
120  case 64: { // long
121  long data = 0;
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;
125  }
126  if (getDebug()) System.out.println("BitTypesTest.getColumnValue Column data for " + i + ", " + j
127  + " is (long)" + data);
128  return Long.valueOf(data);
129  }
130  default:
131  fail("Bad length: " + length);
132  return null;
133  }
134  }
135 
142  @Override
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]);
152  switch (j) {
153  case 1: { // boolean
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);
159  break;
160  }
161  case 2: { // byte
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));
168  break;
169  }
170  case 3: { // short
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));
177  break;
178  }
179  case 4:
180  case 6: { // int
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));
187  break;
188  }
189  case 5:
190  case 7: { // long
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));
197  break;
198  }
199  default:
200  fail("Bad value for j: " + j);
201  }
202  }
203  }
204  }
205 
206  public void testWriteJDBCReadNDB() {
208  failOnError();
209  }
210 
211  public void testWriteNDBReadJDBC() {
212 // TODO: investigate platform dependency when reading via JDBC
213 // writeNDBreadJDBC();
214 // failOnError();
215  }
216 
217  public void testWriteNDBReadNDB() {
218  writeNDBreadNDB();
219  failOnError();
220  }
221 
222  public void testWriteJDBCReadJDBC() {
223 // TODO: investigate platform dependency when reading via JDBC
224 // writeJDBCreadJDBC();
225 // failOnError();
226  }
227 
228  static ColumnDescriptor bit1 = new ColumnDescriptor
229  ("bit1", new InstanceHandler() {
230  public void setFieldValue(IdBase instance, Object value) {
231  ((BitTypes)instance).setBit1((Boolean)value);
232  }
233  public Object getFieldValue(IdBase instance) {
234  return ((BitTypes)instance).getBit1();
235  }
236  public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
237  throws SQLException {
238  preparedStatement.setBoolean(j, (Boolean)value);
239  }
240  public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
241  String value = rs.getString(j);
242  if (value.length() == 0) {
243  value = "0";
244  }
245  return (Byte.parseByte(value) == 0x01)?Boolean.TRUE:Boolean.FALSE;
246  }
247  });
248 
249  static ColumnDescriptor bit2 = new ColumnDescriptor
250  ("bit2", new InstanceHandler() {
251  public void setFieldValue(IdBase instance, Object value) {
252  ((BitTypes)instance).setBit2((Byte)value);
253  }
254  public Object getFieldValue(IdBase instance) {
255  return (Byte)((BitTypes)instance).getBit2();
256  }
257  public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
258  throws SQLException {
259  preparedStatement.setByte(j, (Byte)value);
260  }
261  public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
262  String value = rs.getString(j);
263  if (value.length() == 0) {
264  value = "0";
265  }
266  return Byte.parseByte(value);
267  }
268  });
269 
270  static ColumnDescriptor bit4 = new ColumnDescriptor
271  ("bit4", new InstanceHandler() {
272  public void setFieldValue(IdBase instance, Object value) {
273  ((BitTypes)instance).setBit4((Short)value);
274  }
275  public Object getFieldValue(IdBase instance) {
276  return (Short)((BitTypes)instance).getBit4();
277  }
278  public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
279  throws SQLException {
280  preparedStatement.setShort(j, (Short)value);
281  }
282  public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
283  String value = rs.getString(j);
284  if (value.length() == 0) {
285  value = "0";
286  }
287  return Short.parseShort(value);
288  }
289  });
290 
291  static ColumnDescriptor bit8 = new ColumnDescriptor
292  ("bit8", new InstanceHandler() {
293  public void setFieldValue(IdBase instance, Object value) {
294  ((BitTypes)instance).setBit8((Integer)value);
295  }
296  public Object getFieldValue(IdBase instance) {
297  return (Integer)((BitTypes)instance).getBit8();
298  }
299  public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
300  throws SQLException {
301  preparedStatement.setInt(j, (Integer)value);
302  }
303  public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
304  String value = rs.getString(j);
305  if (value.length() == 0) {
306  value = "0";
307  }
308  return Integer.parseInt(value) & 0xff;
309  }
310  });
311 
312  static ColumnDescriptor bit16 = new ColumnDescriptor
313  ("bit16", new InstanceHandler() {
314  public void setFieldValue(IdBase instance, Object value) {
315  ((BitTypes)instance).setBit16((Long)value);
316  }
317  public Object getFieldValue(IdBase instance) {
318  return ((BitTypes)instance).getBit16();
319  }
320  public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
321  throws SQLException {
322  preparedStatement.setLong(j, (Long)value);
323  }
324  public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
325  String value = rs.getString(j);
326  if (value.length() == 0) {
327  value = "0";
328  }
329  return Long.parseLong(value);
330  }
331  });
332 
333  static ColumnDescriptor bit32 = new ColumnDescriptor
334  ("bit32", new InstanceHandler() {
335  public void setFieldValue(IdBase instance, Object value) {
336  ((BitTypes)instance).setBit32((Integer)value);
337  }
338  public Object getFieldValue(IdBase instance) {
339  return ((BitTypes)instance).getBit32();
340  }
341  public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
342  throws SQLException {
343  preparedStatement.setInt(j, (Integer)value);
344  }
345  public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
346  String value = rs.getString(j);
347  if (value.length() == 0) {
348  value = "0";
349  }
350  return Integer.parseInt(value);
351  }
352  });
353 
354  static ColumnDescriptor bit64 = new ColumnDescriptor
355  ("bit64", new InstanceHandler() {
356  public void setFieldValue(IdBase instance, Object value) {
357  ((BitTypes)instance).setBit64((Long)value);
358  }
359  public Object getFieldValue(IdBase instance) {
360  return ((BitTypes)instance).getBit64();
361  }
362  public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
363  throws SQLException {
364  preparedStatement.setLong(j, (Long)value);
365  }
366  public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
367  String value = rs.getString(j);
368  if (value.length() == 0) {
369  value = "0";
370  }
371  return Long.parseLong(value);
372  }
373  });
374 
375  protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] {
376  bit1,
377  bit2,
378  bit4,
379  bit8,
380  bit16,
381  bit32,
382  bit64
383  };
384 
385  @Override
386  protected ColumnDescriptor[] getColumnDescriptors() {
387  return columnDescriptors;
388  }
389 
390 }