]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
606d3a7488235e7d7aa808011312ddd5ade78521
[packages/trusty/mysql-wsrep-5.6.git] /
1 /*
2    Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3
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.
7
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.
12
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
16 */
17
18 package com.mysql.clusterj.jpatest;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import com.mysql.clusterj.jpatest.model.BlobTypes;
27
28 public class BlobTest extends AbstractJPABaseTest {
29
30     /** The size of the blob column is 2 raised to the power i. */
31     private static final int NUMBER_TO_INSERT = 16;
32
33     /** The blob instances for testing. */
34     protected List<BlobTypes> blobs = new ArrayList<BlobTypes>();
35
36     /** Subclasses can override this method to get debugging info printed to System.out */
37     protected boolean getDebug() {
38         return false;
39     }
40
41     public void test() {
42         createBlobInstances(NUMBER_TO_INSERT);
43         remove();
44         insert();
45         update();
46         failOnError();
47     }
48
49     protected void remove() {
50         removeAll(BlobTypes.class);
51     }
52
53     protected void insert() {
54         // insert instances
55         tx = em.getTransaction();
56         tx.begin();
57         
58         int count = 0;
59
60         for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
61             // must be done with an active transaction
62             em.persist(blobs.get(i));
63             ++count;
64         }
65         tx.commit();
66     }
67
68     protected void update() {
69
70         tx.begin();
71
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();
77             if (actualId != i) {
78                 error("Expected BlobTypes.id " + i + " but got " + actualId);
79             }
80             byte[] bytes = e.getBlobbytes();
81             // make sure all fields were fetched properly
82             checkBlobbytes("before update", bytes, i, false);
83
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);
90             }
91         }
92         tx.commit();
93         tx.begin();
94
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();
100             if (actualId != i) {
101                 error("Expected BlobTypes.id " + i + " but got " + actualId);
102             }
103             byte[] bytes = e.getBlobbytes();
104
105             // check to see that the blob field has the right data
106             checkBlobbytes("after commit", bytes, i, true);
107         }
108         tx.commit();
109     }
110
111     protected void createBlobInstances(int number) {
112         for (int i = 0; i < number; ++i) {
113             BlobTypes instance = new BlobTypes();
114             instance.setId(i);
115             int length = getBlobSizeFor(i);
116             instance.setBlobbytes(getBlobbytes(length));
117 //            blob streams are not yet supported
118 //            instance.setBlobstream(getBlobStream(length));
119             blobs.add(instance);
120         }
121     }
122
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
128      */
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);
133         }
134         return result;
135     }
136
137     /** Check the byte[] to be sure it matches the pattern in both size
138      * and contents.
139      * @see getBlobBytes
140      * @param bytes the byte[] to check
141      * @param number the expected length of the byte[]
142      */
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) {
149             error("In " + where
150                     + " wrong size of byte[]; "
151                     + "expected: " + expectedSize
152                     + " actual: " + actualSize);
153         }
154         for (int i = 0; i < actualSize; ++i) {
155             byte expected;
156             int position = expectedSize/2;
157             if (updated && (i == position)) {
158                 expected = (byte)(position % 128);
159             } else {
160                 expected = (byte)((i % 256) - 128);
161             }
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);
168             }
169         }
170
171     }
172
173     protected InputStream getBlobStream(final int i) {
174         return new InputStream() {
175             int size = i;
176             int counter = 0;
177             @Override
178             public int read() throws IOException {
179                 if (counter >= i) {
180                     return -1;
181                 } else {
182                     return counter++ %256;
183                 }
184             }
185         };
186     }
187
188     protected void dumpBlob(String where, byte[] blob) {
189         System.out.println("In " + where + " dumpBlob of size: " + blob.length);
190         for (byte b: blob) {
191             System.out.print("[" + (int)b + "]");
192         }
193         System.out.println();
194     }
195
196     protected int getBlobSizeFor(int i) {
197         int length = (int) Math.pow(2, i);
198         return length;
199     }
200
201 }