01: /*
02: * TestStoreBlob.java
03: *
04: * Created on October 13, 2006, 5:50 PM
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09:
10: /*
11: * Licensed to the Apache Software Foundation (ASF) under one
12: * or more contributor license agreements. See the NOTICE file
13: * distributed with this work for additional information
14: * regarding copyright ownership. The ASF licenses this file
15: * to you under the Apache License, Version 2.0 (the
16: * "License"); you may not use this file except in compliance
17: * with the License. You may obtain a copy of the License at
18: *
19: * http://www.apache.org/licenses/LICENSE-2.0
20: *
21: * Unless required by applicable law or agreed to in writing,
22: * software distributed under the License is distributed on an
23: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
24: * KIND, either express or implied. See the License for the
25: * specific language governing permissions and limitations
26: * under the License.
27: */
28: package org.apache.openjpa.persistence.kernel;
29:
30: import org.apache.openjpa.persistence.kernel.common.apps.BlobTest;
31:
32: import org.apache.openjpa.persistence.OpenJPAEntityManager;
33:
34: public class TestStoreBlob extends BaseKernelTest {
35:
36: /**
37: * Creates a new instance of TestStoreBlob
38: */
39: public TestStoreBlob() {
40: }
41:
42: public TestStoreBlob(String name) {
43: super (name);
44: }
45:
46: public void testStoreBlob() {
47: OpenJPAEntityManager pm;
48:
49: pm = getPM(false, false);
50: startTx(pm);
51: BlobTest blob = new BlobTest();
52: byte[] bytes = new byte[2048];
53: for (int i = 0; i < bytes.length; i++)
54: bytes[i] = randomByte().byteValue();
55:
56: blob.setBlob(bytes);
57: pm.persist(blob);
58: int id = blob.getId();
59: endTx(pm);
60:
61: byte[] b1 = blob.getBlob();
62: endEm(pm);
63:
64: pm = getPM(false, false);
65: startTx(pm);
66: BlobTest blob2 = pm.find(BlobTest.class, id);
67:
68: byte[] b2 = blob2.getBlob();
69:
70: assertNotNull("Original blob was null", b1);
71: assertNotNull("Retrieved blob was null", b2);
72: assertEquals("Blob length was not the same", b1.length,
73: b2.length);
74: assertBytesEquals("Blob contents did not match", b1, b2);
75:
76: endTx(pm);
77: }
78:
79: private void assertBytesEquals(String str, byte[] a, byte[] b) {
80: for (int i = 0; i < a.length; i++) {
81: assertEquals(str + " [" + i + "]", a[i], b[i]);
82: }
83: }
84: }
|