19 package testsuite.clusterj;
 
   21 import java.io.IOException;
 
   22 import java.io.InputStream;
 
   24 import java.util.ArrayList;
 
   25 import java.util.List;
 
   27 import testsuite.clusterj.model.BlobTypes;
 
   32     private static final int NUMBER_TO_INSERT = 16;
 
   35     protected List<BlobTypes> 
blobs = 
new ArrayList<BlobTypes>();
 
   39         createSessionFactory();
 
   41         createBlobInstances(NUMBER_TO_INSERT);
 
   56     protected void insert() {
 
   63         for (
int i = 0; 
i < NUMBER_TO_INSERT; ++
i) {
 
   71     protected void update() {
 
   75         for (
int i = 1; 
i < NUMBER_TO_INSERT; ++
i) {
 
   77             BlobTypes e = session.
find(BlobTypes.class, 
i);
 
   79             int actualId = e.getId();
 
   81                 error(
"Expected BlobTypes.id " + 
i + 
" but got " + actualId);
 
   83             byte[] bytes = e.getBlobbytes();
 
   87             int position = getBlobSizeFor(
i)/2;
 
   89             if (bytes.length == (position * 2)) {
 
   91                 bytes[position] = (byte)(position % 128);
 
  104         for (
int i = 1; 
i < NUMBER_TO_INSERT; ++
i) {
 
  106             BlobTypes e = session.
find(BlobTypes.class, 
i);
 
  108             int actualId = e.getId();
 
  110                 error(
"Expected BlobTypes.id " + 
i + 
" but got " + actualId);
 
  112             byte[] bytes = e.getBlobbytes();
 
  120     protected void createBlobInstances(
int number) {
 
  121         for (
int i = 0; 
i < number; ++
i) {
 
  122             BlobTypes instance = session.
newInstance(BlobTypes.class);
 
  124             int length = getBlobSizeFor(
i);
 
  139         byte[] result = 
new byte[
size];
 
  140         for (
int i = 0; 
i < 
size; ++
i) {
 
  141             result[
i] = (byte)((
i % 256) - 128);
 
  154         dumpBlob(where, bytes);
 
  155         int expectedSize = getBlobSizeFor(number);
 
  156         int actualSize = bytes.length;
 
  157         if (expectedSize != actualSize) {
 
  159                     + 
" wrong size of byte[]; " 
  160                     + 
"expected: " + expectedSize
 
  161                     + 
" actual: " + actualSize);
 
  163         for (
int i = 0; 
i < actualSize; ++
i) {
 
  165             int position = expectedSize/2;
 
  166             if (updated && (
i == position)) {
 
  167                 expected = (byte)(position % 128);
 
  169                 expected = (byte)((
i % 256) - 128);
 
  171                 byte actual = bytes[
i];
 
  172             if (expected != actual) {
 
  173                 error(
"In " + where + 
" for size: " + actualSize
 
  174                         + 
" mismatch in byte[] at position " + 
i 
  175                         + 
" expected: " + expected
 
  176                         + 
" actual: " + actual);
 
  182     protected InputStream getBlobStream(
final int i) {
 
  183         return new InputStream() {
 
  187             public int read() throws IOException {
 
  191                     return counter++ %256;
 
  197     protected void dumpBlob(
String where, byte[] blob) {
 
  205     protected int getBlobSizeFor(
int i) {
 
  206         int length = (int) Math.pow(2, i);