18 package com.mysql.clusterj.jpatest;
 
   20 import java.io.IOException;
 
   21 import java.io.InputStream;
 
   22 import java.nio.charset.Charset;
 
   23 import java.nio.charset.CharsetEncoder;
 
   25 import java.util.ArrayList;
 
   26 import java.util.Arrays;
 
   27 import java.util.List;
 
   29 import com.mysql.clusterj.jpatest.model.ClobTypes;
 
   46     private static final int NUMBER_TO_INSERT = 16;
 
   49     protected List<ClobTypes> 
clobs = 
new ArrayList<ClobTypes>();
 
   52     private char[] chars = getChars(Charset.forName(
"latin1"));
 
   63     private char[] getChars(Charset charset) {
 
   64         CharsetEncoder encoder = charset.newEncoder();
 
   65         char[] result = 
new char[128];
 
   66         char current = (char)0;
 
   67         for (
int i = 0; 
i < result.length; ++
i) {
 
   68             current = nextChar(encoder, current);
 
   79     char nextChar(CharsetEncoder encoder, 
char current) {
 
   81         if (encoder.canEncode(current)) {
 
   84             return nextChar(encoder, current);
 
   89         createClobInstances(NUMBER_TO_INSERT);
 
   96     protected void remove() {
 
   97         removeAll(ClobTypes.class);
 
  100     protected void insert() {
 
  102         tx = em.getTransaction();
 
  107         for (
int i = 0; 
i < NUMBER_TO_INSERT; ++
i) {
 
  115     protected void update() {
 
  119         for (
int i = 1; 
i < NUMBER_TO_INSERT; ++
i) {
 
  121             ClobTypes e = em.find(ClobTypes.class, 
i);
 
  123             int actualId = e.getId();
 
  125                 error(
"Expected ClobTypes.id " + 
i + 
" but got " + actualId);
 
  127             String string = e.getLarge10000();
 
  131             int position = getClobSizeFor(
i)/2;
 
  133             if (
string.length() == (position * 2)) {
 
  134                 StringBuilder sb = 
new StringBuilder(
string);
 
  136                 sb.replace(position, position + 1, 
"!");
 
  137                 string = sb.toString();
 
  140             e.setLarge10000(
string);
 
  145         for (
int i = 1; 
i < NUMBER_TO_INSERT; ++
i) {
 
  147             ClobTypes e = em.find(ClobTypes.class, 
i);
 
  149             int actualId = e.getId();
 
  151                 error(
"Expected ClobTypes.id " + 
i + 
" but got " + actualId);
 
  153             String string = e.getLarge10000();
 
  161     protected void createClobInstances(
int number) {
 
  162         for (
int i = 0; 
i < number; ++
i) {
 
  163             ClobTypes instance = 
new ClobTypes();
 
  165             int length = getClobSizeFor(
i);
 
  166             instance.setLarge10000(
getString(length));
 
  180         char[] result = 
new char[
size];
 
  181         for (
int i = 0; 
i < 
size; ++
i) {
 
  182             result[
i] = chars [
i%128];
 
  184         return new String(result);
 
  195         if (
getDebug()) dumpClob(where, 
string);
 
  196         int expectedSize = getClobSizeFor(number);
 
  197         int actualSize = 
string.length();
 
  198         if (expectedSize != actualSize) {
 
  200                     + 
" wrong size of string; " 
  201                     + 
"expected: " + expectedSize
 
  202                     + 
" actual: " + actualSize);
 
  204         for (
int i = 0; 
i < actualSize; ++
i) {
 
  206             int position = expectedSize/2;
 
  207             if (updated && (
i == position)) {
 
  210                 expected = chars[
i%128];
 
  212                 char actual = 
string.charAt(
i);
 
  213             if (expected != actual) {
 
  214                 error(
"In " + where + 
" for size: " + actualSize
 
  215                         + 
" mismatch in string at position " + 
i 
  216                         + 
" expected: " + (
int)expected
 
  217                         + 
" actual: " + (
int)actual);
 
  223     protected InputStream getClobStream(
final int i) {
 
  224         return new InputStream() {
 
  228             public int read() throws IOException {
 
  232                     return counter++ %256;
 
  239         System.out.println(
"In " + where + 
" dumpClob of size: " + 
string.length() + 
" " + Arrays.toString(
string.getBytes()));
 
  242     protected int getClobSizeFor(
int i) {
 
  243         int length = (int) Math.pow(2, i);