MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BlobTest.java
1 /*
2  Copyright 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 package testsuite.clusterj;
20 
21 import java.io.IOException;
22 import java.io.InputStream;
23 
24 import java.util.ArrayList;
25 import java.util.List;
26 
27 import testsuite.clusterj.model.BlobTypes;
28 
29 public class BlobTest extends AbstractClusterJModelTest {
30 
32  private static final int NUMBER_TO_INSERT = 16;
33 
35  protected List<BlobTypes> blobs = new ArrayList<BlobTypes>();
36 
37  @Override
38  public void localSetUp() {
39  createSessionFactory();
40  session = sessionFactory.getSession();
41  createBlobInstances(NUMBER_TO_INSERT);
42  tx = session.currentTransaction();
43  tx.begin();
44  session.deletePersistentAll(BlobTypes.class);
45  tx.commit();
46  // the following tests the delete functionality
47  addTearDownClasses(BlobTypes.class);
48  }
49 
50  public void test() {
51  insert();
52  update();
53  failOnError();
54  }
55 
56  protected void insert() {
57  // insert instances
58  tx = session.currentTransaction();
59  tx.begin();
60 
61  int count = 0;
62 
63  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
64  // must be done with an active transaction
65  session.makePersistent(blobs.get(i));
66  ++count;
67  }
68  tx.commit();
69  }
70 
71  protected void update() {
72 
73  tx.begin();
74 
75  for (int i = 1; i < NUMBER_TO_INSERT; ++i) {
76  // must be done with an active transaction
77  BlobTypes e = session.find(BlobTypes.class, i);
78  // see if it is the right one
79  int actualId = e.getId();
80  if (actualId != i) {
81  error("Expected BlobTypes.id " + i + " but got " + actualId);
82  }
83  byte[] bytes = e.getBlobbytes();
84  // make sure all fields were fetched properly
85  checkBlobBytes("before update", bytes, i, false);
86 
87  int position = getBlobSizeFor(i)/2;
88  // only update if the length is correct
89  if (bytes.length == (position * 2)) {
90  // modify the byte in the middle of the blob
91  bytes[position] = (byte)(position % 128);
92  checkBlobBytes("after update", bytes, i, true);
93 
94  // mark the field as modified so it will be flushed
95  session.markModified(e, "blobbytes");
96 
97  // update the modified instance
98  session.updatePersistent(e);
99  }
100  }
101  tx.commit();
102  tx.begin();
103 
104  for (int i = 1; i < NUMBER_TO_INSERT; ++i) {
105  // must be done with an active transaction
106  BlobTypes e = session.find(BlobTypes.class, i);
107  // see if it is the right one
108  int actualId = e.getId();
109  if (actualId != i) {
110  error("Expected BlobTypes.id " + i + " but got " + actualId);
111  }
112  byte[] bytes = e.getBlobbytes();
113 
114  // check to see that the blob field has the right data
115  checkBlobBytes("after commit", e.getBlobbytes(), i, true);
116  }
117  tx.commit();
118  }
119 
120  protected void createBlobInstances(int number) {
121  for (int i = 0; i < number; ++i) {
122  BlobTypes instance = session.newInstance(BlobTypes.class);
123  instance.setId(i);
124  int length = getBlobSizeFor(i);
125  instance.setBlobbytes(getBlobBytes(length));
126 // blob streams are not yet supported
127 // instance.setBlobstream(getBlobStream(length));
128  blobs.add(instance);
129  }
130  }
131 
138  protected byte[] getBlobBytes(int size) {
139  byte[] result = new byte[size];
140  for (int i = 0; i < size; ++i) {
141  result[i] = (byte)((i % 256) - 128);
142  }
143  return result;
144  }
145 
152  protected void checkBlobBytes(String where, byte[] bytes, int number, boolean updated) {
153  // debugging statement; comment out once test passes
154  dumpBlob(where, bytes);
155  int expectedSize = getBlobSizeFor(number);
156  int actualSize = bytes.length;
157  if (expectedSize != actualSize) {
158  error("In " + where
159  + " wrong size of byte[]; "
160  + "expected: " + expectedSize
161  + " actual: " + actualSize);
162  }
163  for (int i = 0; i < actualSize; ++i) {
164  byte expected;
165  int position = expectedSize/2;
166  if (updated && (i == position)) {
167  expected = (byte)(position % 128);
168  } else {
169  expected = (byte)((i % 256) - 128);
170  }
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);
177  }
178  }
179 
180  }
181 
182  protected InputStream getBlobStream(final int i) {
183  return new InputStream() {
184  int size = i;
185  int counter = 0;
186  @Override
187  public int read() throws IOException {
188  if (counter >= i) {
189  return -1;
190  } else {
191  return counter++ %256;
192  }
193  }
194  };
195  }
196 
197  protected void dumpBlob(String where, byte[] blob) {
198 // System.out.println("In " + where + " dumpBlob of size: " + blob.length);
199 // for (byte b: blob) {
200 // System.out.print("[" + b + "]");
201 // }
202 // System.out.println();
203  }
204 
205  protected int getBlobSizeFor(int i) {
206  int length = (int) Math.pow(2, i);
207  return length;
208  }
209 
210 }