2 Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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;
28 public class BlobTest extends AbstractJPABaseTest {
30 /** The size of the blob column is 2 raised to the power i. */
31 private static final int NUMBER_TO_INSERT = 16;
33 /** The blob instances for testing. */
34 protected List<BlobTypes> blobs = new ArrayList<BlobTypes>();
36 /** Subclasses can override this method to get debugging info printed to System.out */
37 protected boolean getDebug() {
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) {
61 // must be done with an active transaction
62 em.persist(blobs.get(i));
68 protected void update() {
72 for (int i = 1; i < NUMBER_TO_INSERT; ++i) {
73 // must be done with an active transaction
74 BlobTypes e = em.find(BlobTypes.class, i);
75 // see if it is the right one
76 int actualId = e.getId();
78 error("Expected BlobTypes.id " + i + " but got " + actualId);
80 byte[] bytes = e.getBlobbytes();
81 // make sure all fields were fetched properly
82 checkBlobbytes("before update", bytes, i, false);
84 int position = getBlobSizeFor(i)/2;
85 // only update if the length is correct
86 if (bytes.length == (position * 2)) {
87 // modify the byte in the middle of the blob
88 bytes[position] = (byte)(position % 128);
89 checkBlobbytes("after update", bytes, i, true);
95 for (int i = 1; i < NUMBER_TO_INSERT; ++i) {
96 // must be done with an active transaction
97 BlobTypes e = em.find(BlobTypes.class, i);
98 // see if it is the right one
99 int actualId = e.getId();
101 error("Expected BlobTypes.id " + i + " but got " + actualId);
103 byte[] bytes = e.getBlobbytes();
105 // check to see that the blob field has the right data
106 checkBlobbytes("after commit", bytes, i, true);
111 protected void createBlobInstances(int number) {
112 for (int i = 0; i < number; ++i) {
113 BlobTypes instance = new BlobTypes();
115 int length = getBlobSizeFor(i);
116 instance.setBlobbytes(getBlobbytes(length));
117 // blob streams are not yet supported
118 // instance.setBlobstream(getBlobStream(length));
123 /** Create a new byte[] of the specified size containing a pattern
124 * of bytes in which each byte is the unsigned value of the index
125 * modulo 256. This pattern is easy to test.
126 * @param size the length of the returned byte[]
127 * @return the byte[] filled with the pattern
129 protected byte[] getBlobbytes(int size) {
130 byte[] result = new byte[size];
131 for (int i = 0; i < size; ++i) {
132 result[i] = (byte)((i % 256) - 128);
137 /** Check the byte[] to be sure it matches the pattern in both size
140 * @param bytes the byte[] to check
141 * @param number the expected length of the byte[]
143 protected void checkBlobbytes(String where, byte[] bytes, int number, boolean updated) {
144 // debugging statement; comment out once test passes
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);