2 Copyright 2010 Sun Microsystems, Inc.
3 All rights reserved. Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package testsuite.clusterj;
21 import java.sql.PreparedStatement;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.util.List;
26 import testsuite.clusterj.model.IdBase;
27 import testsuite.clusterj.model.BinaryTypes;
29 /** Test that Timestamps can be read and written.
30 * case 1: Write using JDBC, read using NDB.
31 * case 2: Write using NDB, read using JDBC.
34 drop table if exists binarytypes;
35 create table binarytypes (
36 id int not null primary key,
47 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
50 public class BinaryTypesTest extends AbstractClusterJModelTest {
52 static int NUMBER_OF_INSTANCES = 10;
55 protected boolean getDebug() {
60 protected int getNumberOfInstances() {
61 return NUMBER_OF_INSTANCES;
65 protected String getTableName() {
69 /** Subclasses override this method to provide the model class for the test */
71 Class<? extends IdBase> getModelClass() {
72 return BinaryTypes.class;
75 /** Subclasses override this method to provide values for rows (i) and columns (j) */
77 protected Object getColumnValue(int i, int j) {
78 // first calculate the length of the data
79 int length = (int)Math.pow(2, j);
80 byte[] data = new byte[length];
81 // fill in the data, increasing by one for each row, column, and byte in the byte[]
82 for (int d = 0; d < length; ++d) {
83 data[d] = (byte)(i + j + d);
88 /** Verify that the actual results match the expected results. If not, use the multiple error
89 * reporting method errorIfNotEqual defined in the superclass.
90 * @param where the location of the verification of results, normally the name of the test method
91 * @param expecteds the expected results
92 * @param actuals the actual results
95 protected void verify(String where, List<Object[]> expecteds, List<Object[]> actuals) {
96 for (int i = 0; i < expecteds.size(); ++i) {
97 Object[] expected = expecteds.get(i);
98 Object[] actual = actuals.get(i);
99 errorIfNotEqual(where + " got failure on id for row " + i, i, actual[0]);
100 for (int j = 1; j < expected.length; ++j) {
101 // verify each object in the array
102 byte[] expectedColumn = (byte[])(expected)[j];
103 byte[] actualColumn = (byte[])(actual)[j];
104 errorIfNotEqual(where + " got failure on length of data for row " + i,
105 expectedColumn.length, actualColumn.length);
106 if (expectedColumn.length == actualColumn.length) {
107 // now compare byte by byte
108 for (j = 0; j < expectedColumn.length; ++j)
109 errorIfNotEqual(where + " got failure on comparison of data for row "
110 + i + " column " + j,
111 expectedColumn[j], actualColumn[j]);
117 public void testWriteJDBCReadNDB() {
122 public void testWriteNDBReadJDBC() {
127 static ColumnDescriptor binary1 = new ColumnDescriptor
128 ("binary1", new InstanceHandler() {
129 public void setFieldValue(IdBase instance, Object value) {
130 ((BinaryTypes)instance).setBinary1((byte[])value);
132 public Object getFieldValue(IdBase instance) {
133 return ((BinaryTypes)instance).getBinary1();
135 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
136 throws SQLException {
137 preparedStatement.setBytes(j, (byte[])value);
139 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
140 return rs.getBytes(j);
144 static ColumnDescriptor binary2 = new ColumnDescriptor
145 ("binary2", new InstanceHandler() {
146 public void setFieldValue(IdBase instance, Object value) {
147 ((BinaryTypes)instance).setBinary2((byte[])value);
149 public Object getFieldValue(IdBase instance) {
150 return ((BinaryTypes)instance).getBinary2();
152 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
153 throws SQLException {
154 preparedStatement.setBytes(j, (byte[])value);
156 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
157 return rs.getBytes(j);
161 static ColumnDescriptor binary4 = new ColumnDescriptor
162 ("binary4", new InstanceHandler() {
163 public void setFieldValue(IdBase instance, Object value) {
164 ((BinaryTypes)instance).setBinary4((byte[])value);
166 public Object getFieldValue(IdBase instance) {
167 return ((BinaryTypes)instance).getBinary4();
169 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
170 throws SQLException {
171 preparedStatement.setBytes(j, (byte[])value);
173 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
174 return rs.getBytes(j);
178 static ColumnDescriptor binary8 = new ColumnDescriptor
179 ("binary8", new InstanceHandler() {
180 public void setFieldValue(IdBase instance, Object value) {
181 ((BinaryTypes)instance).setBinary8((byte[])value);
183 public Object getFieldValue(IdBase instance) {
184 return ((BinaryTypes)instance).getBinary8();
186 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
187 throws SQLException {
188 preparedStatement.setBytes(j, (byte[])value);
190 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
191 return rs.getBytes(j);
195 static ColumnDescriptor binary16 = new ColumnDescriptor
196 ("binary16", new InstanceHandler() {
197 public void setFieldValue(IdBase instance, Object value) {
198 ((BinaryTypes)instance).setBinary16((byte[])value);
200 public Object getFieldValue(IdBase instance) {
201 return ((BinaryTypes)instance).getBinary16();
203 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
204 throws SQLException {
205 preparedStatement.setBytes(j, (byte[])value);
207 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
208 return rs.getBytes(j);
212 static ColumnDescriptor binary32 = new ColumnDescriptor
213 ("binary32", new InstanceHandler() {
214 public void setFieldValue(IdBase instance, Object value) {
215 ((BinaryTypes)instance).setBinary32((byte[])value);
217 public Object getFieldValue(IdBase instance) {
218 return ((BinaryTypes)instance).getBinary32();
220 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
221 throws SQLException {
222 preparedStatement.setBytes(j, (byte[])value);
224 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
225 return rs.getBytes(j);
229 static ColumnDescriptor binary64 = new ColumnDescriptor
230 ("binary64", new InstanceHandler() {
231 public void setFieldValue(IdBase instance, Object value) {
232 ((BinaryTypes)instance).setBinary64((byte[])value);
234 public Object getFieldValue(IdBase instance) {
235 return ((BinaryTypes)instance).getBinary64();
237 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
238 throws SQLException {
239 preparedStatement.setBytes(j, (byte[])value);
241 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
242 return rs.getBytes(j);
246 static ColumnDescriptor binary128 = new ColumnDescriptor
247 ("binary128", new InstanceHandler() {
248 public void setFieldValue(IdBase instance, Object value) {
249 ((BinaryTypes)instance).setBinary128((byte[])value);
251 public Object getFieldValue(IdBase instance) {
252 return ((BinaryTypes)instance).getBinary128();
254 public void setPreparedStatementValue(PreparedStatement preparedStatement, int j, Object value)
255 throws SQLException {
256 preparedStatement.setBytes(j, (byte[])value);
258 public Object getResultSetValue(ResultSet rs, int j) throws SQLException {
259 return rs.getBytes(j);
263 protected static ColumnDescriptor[] columnDescriptors = new ColumnDescriptor[] {
275 protected ColumnDescriptor[] getColumnDescriptors() {
276 return columnDescriptors;