18 package com.mysql.clusterj.jpatest;
20 import java.io.IOException;
21 import java.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.List;
26 import com.mysql.clusterj.jpatest.model.BlobTypes;
31 private static final int NUMBER_TO_INSERT = 16;
34 protected List<BlobTypes>
blobs =
new ArrayList<BlobTypes>();
42 createBlobInstances(NUMBER_TO_INSERT);
49 protected void remove() {
50 removeAll(BlobTypes.class);
53 protected void insert() {
55 tx = em.getTransaction();
60 for (
int i = 0;
i < NUMBER_TO_INSERT; ++
i) {
68 protected void update() {
72 for (
int i = 1;
i < NUMBER_TO_INSERT; ++
i) {
74 BlobTypes e = em.find(BlobTypes.class,
i);
76 int actualId = e.getId();
78 error(
"Expected BlobTypes.id " +
i +
" but got " + actualId);
80 byte[] bytes = e.getBlobbytes();
84 int position = getBlobSizeFor(
i)/2;
86 if (bytes.length == (position * 2)) {
88 bytes[position] = (byte)(position % 128);
95 for (
int i = 1;
i < NUMBER_TO_INSERT; ++
i) {
97 BlobTypes e = em.find(BlobTypes.class,
i);
99 int actualId = e.getId();
101 error(
"Expected BlobTypes.id " +
i +
" but got " + actualId);
103 byte[] bytes = e.getBlobbytes();
111 protected void createBlobInstances(
int number) {
112 for (
int i = 0;
i < number; ++
i) {
113 BlobTypes instance =
new BlobTypes();
115 int length = getBlobSizeFor(
i);
130 byte[] result =
new byte[
size];
131 for (
int i = 0;
i <
size; ++
i) {
132 result[
i] = (byte)((
i % 256) - 128);
145 if (
getDebug()) dumpBlob(where, bytes);
146 int expectedSize = getBlobSizeFor(number);
147 int actualSize = bytes.length;
148 if (expectedSize != actualSize) {
150 +
" wrong size of byte[]; "
151 +
"expected: " + expectedSize
152 +
" actual: " + actualSize);
154 for (
int i = 0;
i < actualSize; ++
i) {
156 int position = expectedSize/2;
157 if (updated && (
i == position)) {
158 expected = (byte)(position % 128);
160 expected = (byte)((
i % 256) - 128);
162 byte actual = bytes[
i];
163 if (expected != actual) {
164 error(
"In " + where +
" for size: " + actualSize
165 +
" mismatch in byte[] at position " +
i
166 +
" expected: " + (
int)expected
167 +
" actual: " + (
int)actual);
173 protected InputStream getBlobStream(
final int i) {
174 return new InputStream() {
178 public int read() throws IOException {
182 return counter++ %256;
188 protected void dumpBlob(
String where, byte[] blob) {
189 System.out.println(
"In " + where +
" dumpBlob of size: " + blob.length);
191 System.out.print(
"[" + (
int)b +
"]");
193 System.out.println();
196 protected int getBlobSizeFor(
int i) {
197 int length = (int) Math.pow(2, i);