19 package testsuite.clusterj;
21 import java.nio.ByteBuffer;
22 import java.nio.CharBuffer;
23 import java.nio.charset.Charset;
24 import java.nio.charset.CharsetEncoder;
25 import java.nio.charset.CoderResult;
27 import java.sql.PreparedStatement;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Properties;
35 import testsuite.clusterj.model.CharsetLatin1;
36 import testsuite.clusterj.model.CharsetBig5;
37 import testsuite.clusterj.model.CharsetModel;
38 import testsuite.clusterj.model.CharsetSjis;
39 import testsuite.clusterj.model.CharsetUtf8;
64 createSessionFactory();
66 setAutoCommit(connection,
false);
69 public void testLatin1() {
89 public void testUtf8() {
90 writeJDBCreadJDBC(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.SMALL);
91 writeJDBCreadJDBC(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.MEDIUM);
92 writeJDBCreadJDBC(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.LARGE);
94 writeJDBCreadNDB(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.SMALL);
95 writeJDBCreadNDB(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.MEDIUM);
96 writeJDBCreadNDB(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.LARGE);
98 writeNDBreadJDBC(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.SMALL);
99 writeNDBreadJDBC(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.MEDIUM);
100 writeNDBreadJDBC(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.LARGE);
102 writeNDBreadNDB(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.SMALL);
103 writeNDBreadNDB(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.MEDIUM);
104 writeNDBreadNDB(
"UTF-8",
"charsetutf8", CharsetUtf8.class, ColumnDescriptor.LARGE);
109 public void testSjis() {
129 writeNDBreadJDBC(
"SJIS",
"charsetsjis", CharsetSjis.class, ColumnDescriptor.SMALL);
130 writeNDBreadJDBC(
"SJIS",
"charsetsjis", CharsetSjis.class, ColumnDescriptor.MEDIUM);
131 writeNDBreadJDBC(
"SJIS",
"charsetsjis", CharsetSjis.class, ColumnDescriptor.LARGE);
133 writeNDBreadNDB(
"SJIS",
"charsetsjis", CharsetSjis.class, ColumnDescriptor.SMALL);
134 writeNDBreadNDB(
"SJIS",
"charsetsjis", CharsetSjis.class, ColumnDescriptor.MEDIUM);
135 writeNDBreadNDB(
"SJIS",
"charsetsjis", CharsetSjis.class, ColumnDescriptor.LARGE);
140 public void testBig5() {
141 writeJDBCreadJDBC(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.SMALL);
142 writeJDBCreadJDBC(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.MEDIUM);
143 writeJDBCreadJDBC(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.LARGE);
145 writeJDBCreadNDB(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.SMALL);
146 writeJDBCreadNDB(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.MEDIUM);
147 writeJDBCreadNDB(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.LARGE);
149 writeNDBreadJDBC(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.SMALL);
150 writeNDBreadJDBC(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.MEDIUM);
151 writeNDBreadJDBC(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.LARGE);
153 writeNDBreadNDB(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.SMALL);
154 writeNDBreadNDB(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.MEDIUM);
155 writeNDBreadNDB(
"big5",
"charsetbig5", CharsetBig5.class, ColumnDescriptor.LARGE);
161 ColumnDescriptor columnDescriptor) {
162 removeAll(modelClass);
164 List<String> strings = generateStrings(columnDescriptor, charsetName);
166 writeToJDBC(columnDescriptor, tableName, instances);
167 result = readFromJDBC(columnDescriptor, tableName);
168 if (debug) System.out.println(
"Returned results of size " + result.size());
170 verify(
"writeJDBCreadJDBC", strings, result, columnDescriptor);
174 ColumnDescriptor columnDescriptor) {
175 removeAll(modelClass);
177 List<String> strings = generateStrings(columnDescriptor, charsetName);
178 List<CharsetModel> instances = generateInstances(columnDescriptor, modelClass, strings);
179 writeToJDBC(columnDescriptor, tableName, instances);
180 result = readFromNDB(columnDescriptor, modelClass);
181 if (debug) System.out.println(
"Returned results of size " + result.size());
183 verify(
"writeJDBCreadNDB", strings, result, columnDescriptor);
187 ColumnDescriptor columnDescriptor) {
188 removeAll(modelClass);
190 List<String> strings = generateStrings(columnDescriptor, charsetName);
191 List<CharsetModel> instances = generateInstances(columnDescriptor, modelClass, strings);
192 writeToNDB(columnDescriptor, instances);
193 result = readFromJDBC(columnDescriptor, tableName);
194 if (debug) System.out.println(
"Returned results of size " + result.size());
196 verify(
"writeNDBreadJDBC", strings, result, columnDescriptor);
200 ColumnDescriptor columnDescriptor) {
201 removeAll(modelClass);
203 List<String> strings = generateStrings(columnDescriptor, charsetName);
204 List<CharsetModel> instances = generateInstances(columnDescriptor, modelClass, strings);
205 writeToNDB(columnDescriptor, instances);
206 result = readFromNDB(columnDescriptor, modelClass);
207 if (debug) System.out.println(
"Returned results of size " + result.size());
209 verify(
"writeNDBreadNDB", strings, result, columnDescriptor);
214 for (
int i = 0;
i < expecteds.size(); ++
i) {
215 String expected = expecteds.get(
i);
216 String actual = actuals.get(
i);
217 int expectedLength = expected.length();
218 int actualLength = actual.length();
219 errorIfNotEqual(where +
" got failure on size of column data for column width " + columnDescriptor.columnWidth +
" at row " +
i, expectedLength, actualLength);
220 if (expectedLength != actualLength)
222 for (
int j = 0; j < expected.length(); ++j) {
223 if (--maxErrors > 0) {
224 errorIfNotEqual(
"Failure to match column data for column width " + columnDescriptor.columnWidth +
" at row " +
i +
" column " + j,
225 expected.codePointAt(j), actual.codePointAt(j));
231 protected List<String> generateStrings(ColumnDescriptor columnDescriptor,
234 Charset charset = Charset.forName(charsetName);
235 CharBuffer allChars = CharBuffer.allocate(65536);
236 CharsetEncoder encoder = charset.newEncoder();
240 for (
int i = 0;
i < 65536; ++
i) {
241 Character ch = (char)
i;
242 if (encoder.canEncode(ch)) {
247 if (debug) System.out.print(charsetName +
" has " + count +
" encodable characters");
250 int width = columnDescriptor.getColumnWidth();
252 boolean done =
false;
253 byte[] bytes =
new byte[width];
255 int begin = allChars.position();
257 ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
258 CoderResult coderResult = encoder.encode(allChars, byteBuffer,
false);
259 int end = allChars.position();
260 int length = end - begin;
265 char[] chars =
new char[length];
267 allChars.get(chars, 0, length);
269 result.add(encodable);
270 if (coderResult.isUnderflow()) {
274 if (debug) System.out.println(
" in " + result.size() +
" row(s) of size " + columnDescriptor.columnWidth);
279 Class<? extends CharsetModel> modelClass,
List<String> strings) {
281 for (
int i = 0;
i < strings.size(); ++
i) {
282 CharsetModel instance = session.
newInstance(modelClass);
284 columnDescriptor.set(instance, strings.get(
i));
285 result.add(instance);
287 if (debug) System.out.println(
"Created " + result.size() +
" instances of " + modelClass.getName());
291 protected void writeToJDBC(ColumnDescriptor columnDescriptor,
294 buffer.append(tableName);
295 buffer.append(
" (id, ");
296 buffer.append(columnDescriptor.getColumnName());
297 buffer.append(
") VALUES (?, ?)");
299 if (debug) System.out.println(statement);
300 PreparedStatement preparedStatement = null;
305 extraProperties.
put(
"characterEncoding",
"utf8");
307 setAutoCommit(connection,
false);
308 preparedStatement = connection.prepareStatement(statement);
309 if (debug) System.out.println(preparedStatement.toString());
310 for (i = 0; i < instances.size(); ++
i) {
311 CharsetModel instance = instances.get(i);
312 preparedStatement.setInt(1, instance.getId());
313 value = columnDescriptor.get(instance);
314 preparedStatement.setString(2, value);
317 preparedStatement.execute();
320 }
catch (SQLException e) {
321 throw new RuntimeException(
"Failed to insert " + tableName +
" at instance " + i +
" errant string: " + dump(value), e);
325 protected void writeToNDB(ColumnDescriptor columnDescriptor,
List<CharsetModel> instances) {
327 for (CharsetModel instance: instances) {
333 protected List<String> readFromNDB(ColumnDescriptor columnDescriptor,
334 Class<? extends CharsetModel> modelClass) {
338 boolean done =
false;
340 CharsetModel instance = session.
find(modelClass, i++);
341 if (instance != null) {
342 result.add(columnDescriptor.get(instance));
351 protected List<String> readFromJDBC(ColumnDescriptor columnDescriptor,
355 buffer.append(columnDescriptor.getColumnName());
356 buffer.append(
" FROM ");
357 buffer.append(tableName);
358 buffer.append(
" ORDER BY ID");
359 String statement = buffer.toString();
360 if (debug) System.out.println(statement);
361 PreparedStatement preparedStatement = null;
364 preparedStatement = connection.prepareStatement(statement);
365 ResultSet rs = preparedStatement.executeQuery();
367 String columnData = rs.getString(2);
368 result.add(columnData);
372 }
catch (SQLException e) {
373 throw new RuntimeException(
"Failed to read " + tableName +
" at instance " + i, e);
378 protected enum ColumnDescriptor {
379 SMALL(200,
"smallcolumn",
new InstanceHandler() {
381 instance.setSmallColumn(value);
384 return instance.getSmallColumn();
387 MEDIUM(500,
"mediumcolumn",
new InstanceHandler() {
389 instance.setMediumColumn(value);
392 return instance.getMediumColumn();
395 LARGE(10000,
"largecolumn",
new InstanceHandler() {
397 instance.setLargeColumn(value);
400 return instance.getLargeColumn();
404 private int columnWidth;
406 private String columnName;
408 private InstanceHandler instanceHandler;
410 public String getColumnName() {
415 return instanceHandler.get(instance);
419 this.instanceHandler.set(instance,
string);
422 public int getColumnWidth() {
426 private ColumnDescriptor(
int width,
String name, InstanceHandler instanceHandler) {
427 this.columnWidth = width;
428 this.columnName =
name;
429 this.instanceHandler = instanceHandler;
432 private interface InstanceHandler {
440 protected List<CharsetModel>
charsetTypes =
new ArrayList<CharsetModel>();