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.util.List;
32 
33 import testsuite.clusterj.model.IdBase;
34 import testsuite.clusterj.model.BinaryTypes;
35 
36 /** Test that Timestamps 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 binarytypes;
42 create table binarytypes (
43  id int not null primary key,
44 
45  binary1 binary(1),
46  binary2 binary(2),
47  binary4 binary(4),
48  binary8 binary(8),
49  binary16 binary(16),
50  binary32 binary(32),
51  binary64 binary(64),
52  binary128 binary(128)
53 
54 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
55 
56  */
57 public class BinaryTypesTest extends AbstractClusterJModelTest {
58 
59     static int NUMBER_OF_INSTANCES = 10;
60 
61     @Override
getDebug()62     protected boolean getDebug() {
63         return false;
64     }
65 
66     @Override
getNumberOfInstances()67     protected int getNumberOfInstances() {
68         return NUMBER_OF_INSTANCES;
69     }
70 
71     @Override
getTableName()72     protected String getTableName() {
73         return "binarytypes";
74     }
75 
76     /** Subclasses override this method to provide the model class for the test */
77     @Override
getModelClass()78     Class<? extends IdBase> getModelClass() {
79         return BinaryTypes.class;
80     }
81 
82     /** Subclasses override this method to provide values for rows (i) and columns (j) */
83     @Override
getColumnValue(int i, int j)84     protected Object getColumnValue(int i, int j) {
85         // first calculate the length of the data
86         int length = (int)Math.pow(2, j);
87         byte[] data = new byte[length];
88         // fill in the data, increasing by one for each row, column, and byte in the byte[]
89         for (int d = 0; d < length; ++d) {
90             data[d] = (byte)(i + j + d);
91         }
92         return data;
93     }
94 
95     /** Verify that the actual results match the expected results. If not, use the multiple error
96      * reporting method errorIfNotEqual defined in the superclass.
97      * @param where the location of the verification of results, normally the name of the test method
98      * @param expecteds the expected results
99      * @param actuals the actual results
100      */
101     @Override
verify(String where, List<Object[]> expecteds, List<Object[]> actuals)102     protected void verify(String where, List<Object[]> expecteds, List<Object[]> actuals) {
103         for (int i = 0; i < expecteds.size(); ++i) {
104             Object[] expected = expecteds.get(i);
105             Object[] actual = actuals.get(i);
106             errorIfNotEqual(where + " got failure on id for row " + i, i, actual[0]);
107             for (int j = 1; j < expected.length; ++j) {
108                 // verify each object in the array
109                 byte[] expectedColumn = (byte[])(expected)[j];
110                 byte[] actualColumn = (byte[])(actual)[j];
111                 errorIfNotEqual(where + " got failure on length of data for row " + i,
112                         expectedColumn.length, actualColumn.length);
113                 if (expectedColumn.length == actualColumn.length) {
114                     // now compare byte by byte
115                     for (j = 0; j < expectedColumn.length; ++j)
116                         errorIfNotEqual(where + " got failure on comparison of data for row "
117                                 + i + " column " + j,
118                                 expectedColumn[j], actualColumn[j]);
119                 }
120             }
121         }
122     }
123 
testWriteJDBCReadNDB()124     public void testWriteJDBCReadNDB() {
125         writeJDBCreadNDB();
126         failOnError();
127     }
128 
testWriteNDBReadJDBC()129     public void testWriteNDBReadJDBC() {
130         writeNDBreadJDBC();
131         failOnError();
132    }
133 
134    static ColumnDescriptor binary1 = new ColumnDescriptor
135             ("binary1", new InstanceHandler() {
136         public void setFieldValue(IdBase instance, Object value) {
137             ((BinaryTypes)instance).setBinary1((byte[])value);
138         }
139         public Object getFieldValue(IdBase instance) {
140             return ((BinaryTypes)instance).getBinary1();
141         }
142         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
143                 throws SQLException {
144             preparedStatement.setBytes(j, (byte[])value);
145         }
146         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
147             return rs.getBytes(j);
148         }
149     });
150 
151    static ColumnDescriptor binary2 = new ColumnDescriptor
152             ("binary2", new InstanceHandler() {
153         public void setFieldValue(IdBase instance, Object value) {
154             ((BinaryTypes)instance).setBinary2((byte[])value);
155         }
156         public Object getFieldValue(IdBase instance) {
157             return ((BinaryTypes)instance).getBinary2();
158         }
159         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
160                 throws SQLException {
161             preparedStatement.setBytes(j, (byte[])value);
162         }
163         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
164             return rs.getBytes(j);
165         }
166     });
167 
168    static ColumnDescriptor binary4 = new ColumnDescriptor
169             ("binary4", new InstanceHandler() {
170         public void setFieldValue(IdBase instance, Object value) {
171             ((BinaryTypes)instance).setBinary4((byte[])value);
172         }
173         public Object getFieldValue(IdBase instance) {
174             return ((BinaryTypes)instance).getBinary4();
175         }
176         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
177                 throws SQLException {
178             preparedStatement.setBytes(j, (byte[])value);
179         }
180         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
181             return rs.getBytes(j);
182         }
183     });
184 
185    static ColumnDescriptor binary8 = new ColumnDescriptor
186             ("binary8", new InstanceHandler() {
187         public void setFieldValue(IdBase instance, Object value) {
188             ((BinaryTypes)instance).setBinary8((byte[])value);
189         }
190         public Object getFieldValue(IdBase instance) {
191             return ((BinaryTypes)instance).getBinary8();
192         }
193         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
194                 throws SQLException {
195             preparedStatement.setBytes(j, (byte[])value);
196         }
197         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
198             return rs.getBytes(j);
199         }
200     });
201 
202    static ColumnDescriptor binary16 = new ColumnDescriptor
203             ("binary16", new InstanceHandler() {
204         public void setFieldValue(IdBase instance, Object value) {
205             ((BinaryTypes)instance).setBinary16((byte[])value);
206         }
207         public Object getFieldValue(IdBase instance) {
208             return ((BinaryTypes)instance).getBinary16();
209         }
210         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
211                 throws SQLException {
212             preparedStatement.setBytes(j, (byte[])value);
213         }
214         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
215             return rs.getBytes(j);
216         }
217     });
218 
219    static ColumnDescriptor binary32 = new ColumnDescriptor
220             ("binary32", new InstanceHandler() {
221         public void setFieldValue(IdBase instance, Object value) {
222             ((BinaryTypes)instance).setBinary32((byte[])value);
223         }
224         public Object getFieldValue(IdBase instance) {
225             return ((BinaryTypes)instance).getBinary32();
226         }
227         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
228                 throws SQLException {
229             preparedStatement.setBytes(j, (byte[])value);
230         }
231         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
232             return rs.getBytes(j);
233         }
234     });
235 
236    static ColumnDescriptor binary64 = new ColumnDescriptor
237             ("binary64", new InstanceHandler() {
238         public void setFieldValue(IdBase instance, Object value) {
239             ((BinaryTypes)instance).setBinary64((byte[])value);
240         }
241         public Object getFieldValue(IdBase instance) {
242             return ((BinaryTypes)instance).getBinary64();
243         }
244         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
245                 throws SQLException {
246             preparedStatement.setBytes(j, (byte[])value);
247         }
248         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
249             return rs.getBytes(j);
250         }
251     });
252 
253    static ColumnDescriptor binary128 = new ColumnDescriptor
254             ("binary128", new InstanceHandler() {
255         public void setFieldValue(IdBase instance, Object value) {
256             ((BinaryTypes)instance).setBinary128((byte[])value);
257         }
258         public Object getFieldValue(IdBase instance) {
259             return ((BinaryTypes)instance).getBinary128();
260         }
261         public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
262                 throws SQLException {
263             preparedStatement.setBytes(j, (byte[])value);
264         }
265         public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
266             return rs.getBytes(j);
267         }
268     });
269 
270     protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] {
271             binary1,
272             binary2,
273             binary4,
274             binary8,
275             binary16,
276             binary32,
277             binary64,
278             binary128
279         };
280 
281     @Override
getColumnDescriptors()282     protected ColumnDescriptor[] getColumnDescriptors() {
283         return columnDescriptors;
284     }
285 
286 }
287