2 * Copyright (c) 2011, 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.IdBase;
26 import testsuite.clusterj.model.VarbinaryTypes;
28 /** Test that Timestamps 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 varbinarytypes;
34 create table varbinarytypes (
35 id int not null primary key,
41 binary16 varbinary(16),
42 binary32 varbinary(32),
43 binary64 varbinary(64),
44 binary128 varbinary(128),
45 binary256 varbinary(256),
46 binary512 varbinary(512),
47 binary1024 varbinary(1024),
48 binary2048 varbinary(2048)
50 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
53 public class VarbinaryTypesTest extends AbstractClusterJModelTest {
55 static int NUMBER_OF_INSTANCES = 10;
58 protected boolean getDebug() {
63 protected int getNumberOfInstances() {
64 return NUMBER_OF_INSTANCES;
68 protected String getTableName() {
69 return "varbinarytypes";
72 /** Subclasses override this method to provide the model class for the test */
74 Class<? extends IdBase> getModelClass() {
75 return VarbinaryTypes.class;
78 /** Subclasses override this method to provide values for rows (i) and columns (j) */
80 protected Object getColumnValue(int i, int j) {
81 // first calculate the length of the data
82 int length = (int)Math.pow(2, j);
83 byte[] data = new byte[length];
84 // fill in the data, increasing by one for each row, column, and byte in the byte[]
85 for (int d = 0; d < length; ++d) {
86 data[d] = (byte)(i + j + d);
91 /** Verify that the actual results match the expected results. If not, use the multiple error
92 * reporting method errorIfNotEqual defined in the superclass.
93 * @param where the location of the verification of results, normally the name of the test method
94 * @param expecteds the expected results
95 * @param actuals the actual results
98 protected void verify(String where, List<Object[]> expecteds, List<Object[]> actuals) {
99 for (int i = 0; i < expecteds.size(); ++i) {
100 Object[] expected = expecteds.get(i);
101 Object[] actual = actuals.get(i);
102 errorIfNotEqual(where + " got failure on id for row " + i, i, actual[0]);
103 for (int j = 1; j < expected.length; ++j) {
104 // verify each object in the array
105 byte[] expectedColumn = (byte[])(expected)[j];
106 byte[] actualColumn = (byte[])(actual)[j];
107 errorIfNotEqual(where + " got failure on length of data for row " + i,
108 expectedColumn.length, actualColumn.length);
109 if (expectedColumn.length == actualColumn.length) {
110 // now compare byte by byte
111 for (j = 0; j < expectedColumn.length; ++j)
112 errorIfNotEqual(where + " got failure on comparison of data for row "
113 + i + " column " + j,
114 expectedColumn[j], actualColumn[j]);
120 public void testWriteJDBCReadNDB() {
125 public void testWriteNDBReadJDBC() {
130 static ColumnDescriptor binary1 = new ColumnDescriptor
131 ("binary1", new InstanceHandler() {
132 public void setFieldValue(IdBase instance, Object value) {
133 ((VarbinaryTypes)instance).setBinary1((byte[])value);
135 public Object getFieldValue(IdBase instance) {
136 return ((VarbinaryTypes)instance).getBinary1();
138 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
139 throws SQLException {
140 preparedStatement.setBytes(j, (byte[])value);
142 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
143 return rs.getBytes(j);
147 static ColumnDescriptor binary2 = new ColumnDescriptor
148 ("binary2", new InstanceHandler() {
149 public void setFieldValue(IdBase instance, Object value) {
150 ((VarbinaryTypes)instance).setBinary2((byte[])value);
152 public Object getFieldValue(IdBase instance) {
153 return ((VarbinaryTypes)instance).getBinary2();
155 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
156 throws SQLException {
157 preparedStatement.setBytes(j, (byte[])value);
159 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
160 return rs.getBytes(j);
164 static ColumnDescriptor binary4 = new ColumnDescriptor
165 ("binary4", new InstanceHandler() {
166 public void setFieldValue(IdBase instance, Object value) {
167 ((VarbinaryTypes)instance).setBinary4((byte[])value);
169 public Object getFieldValue(IdBase instance) {
170 return ((VarbinaryTypes)instance).getBinary4();
172 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
173 throws SQLException {
174 preparedStatement.setBytes(j, (byte[])value);
176 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
177 return rs.getBytes(j);
181 static ColumnDescriptor binary8 = new ColumnDescriptor
182 ("binary8", new InstanceHandler() {
183 public void setFieldValue(IdBase instance, Object value) {
184 ((VarbinaryTypes)instance).setBinary8((byte[])value);
186 public Object getFieldValue(IdBase instance) {
187 return ((VarbinaryTypes)instance).getBinary8();
189 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
190 throws SQLException {
191 preparedStatement.setBytes(j, (byte[])value);
193 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
194 return rs.getBytes(j);
198 static ColumnDescriptor binary16 = new ColumnDescriptor
199 ("binary16", new InstanceHandler() {
200 public void setFieldValue(IdBase instance, Object value) {
201 ((VarbinaryTypes)instance).setBinary16((byte[])value);
203 public Object getFieldValue(IdBase instance) {
204 return ((VarbinaryTypes)instance).getBinary16();
206 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
207 throws SQLException {
208 preparedStatement.setBytes(j, (byte[])value);
210 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
211 return rs.getBytes(j);
215 static ColumnDescriptor binary32 = new ColumnDescriptor
216 ("binary32", new InstanceHandler() {
217 public void setFieldValue(IdBase instance, Object value) {
218 ((VarbinaryTypes)instance).setBinary32((byte[])value);
220 public Object getFieldValue(IdBase instance) {
221 return ((VarbinaryTypes)instance).getBinary32();
223 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
224 throws SQLException {
225 preparedStatement.setBytes(j, (byte[])value);
227 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
228 return rs.getBytes(j);
232 static ColumnDescriptor binary64 = new ColumnDescriptor
233 ("binary64", new InstanceHandler() {
234 public void setFieldValue(IdBase instance, Object value) {
235 ((VarbinaryTypes)instance).setBinary64((byte[])value);
237 public Object getFieldValue(IdBase instance) {
238 return ((VarbinaryTypes)instance).getBinary64();
240 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
241 throws SQLException {
242 preparedStatement.setBytes(j, (byte[])value);
244 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
245 return rs.getBytes(j);
249 static ColumnDescriptor binary128 = new ColumnDescriptor
250 ("binary128", new InstanceHandler() {
251 public void setFieldValue(IdBase instance, Object value) {
252 ((VarbinaryTypes)instance).setBinary128((byte[])value);
254 public Object getFieldValue(IdBase instance) {
255 return ((VarbinaryTypes)instance).getBinary128();
257 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
258 throws SQLException {
259 preparedStatement.setBytes(j, (byte[])value);
261 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
262 return rs.getBytes(j);
266 static ColumnDescriptor binary256 = new ColumnDescriptor
267 ("binary256", new InstanceHandler() {
268 public void setFieldValue(IdBase instance, Object value) {
269 ((VarbinaryTypes)instance).setBinary256((byte[])value);
271 public Object getFieldValue(IdBase instance) {
272 return ((VarbinaryTypes)instance).getBinary256();
274 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
275 throws SQLException {
276 preparedStatement.setBytes(j, (byte[])value);
278 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
279 return rs.getBytes(j);
283 static ColumnDescriptor binary512 = new ColumnDescriptor
284 ("binary512", new InstanceHandler() {
285 public void setFieldValue(IdBase instance, Object value) {
286 ((VarbinaryTypes)instance).setBinary512((byte[])value);
288 public Object getFieldValue(IdBase instance) {
289 return ((VarbinaryTypes)instance).getBinary512();
291 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
292 throws SQLException {
293 preparedStatement.setBytes(j, (byte[])value);
295 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
296 return rs.getBytes(j);
300 static ColumnDescriptor binary1024 = new ColumnDescriptor
301 ("binary1024", new InstanceHandler() {
302 public void setFieldValue(IdBase instance, Object value) {
303 ((VarbinaryTypes)instance).setBinary1024((byte[])value);
305 public Object getFieldValue(IdBase instance) {
306 return ((VarbinaryTypes)instance).getBinary1024();
308 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
309 throws SQLException {
310 preparedStatement.setBytes(j, (byte[])value);
312 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
313 return rs.getBytes(j);
317 static ColumnDescriptor binary2048 = new ColumnDescriptor
318 ("binary2048", new InstanceHandler() {
319 public void setFieldValue(IdBase instance, Object value) {
320 ((VarbinaryTypes)instance).setBinary2048((byte[])value);
322 public Object getFieldValue(IdBase instance) {
323 return ((VarbinaryTypes)instance).getBinary2048();
325 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
326 throws SQLException {
327 preparedStatement.setBytes(j, (byte[])value);
329 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
330 return rs.getBytes(j);
334 protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] {
350 protected ColumnDescriptor[] getColumnDescriptors() {
351 return columnDescriptors;