001: package org.apache.ojb.broker;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.junit.PBTestCase;
019: import org.apache.ojb.broker.platforms.PlatformHsqldbImpl;
020: import org.apache.ojb.broker.platforms.PlatformPostgreSQLImpl;
021:
022: import java.io.Serializable;
023:
024: /**
025: * This TestClass tests storing and retrieving Objects with BLOB/CLOB Attributes.
026: * @version $Id: BlobTest.java,v 1.9.2.6 2005/12/21 22:31:23 tomdz Exp $
027: */
028: public class BlobTest extends PBTestCase {
029: private static final String SKIP_MESSAGE = "# Skip "
030: + BlobTest.class.getName()
031: + ", DB does not support Blob/Clob #";
032:
033: /**
034: * Known issue (to be fixed, warn in setup)
035: * or DB lack of features (will never be fixed)?
036: */
037: private boolean knownIssue;
038: private boolean skipTest;
039:
040: public BlobTest(String name) {
041: super (name);
042: }
043:
044: public static void main(String[] args) {
045: String[] arr = { BlobTest.class.getName() };
046: junit.textui.TestRunner.main(arr);
047: }
048:
049: public void setUp() throws Exception {
050: super .setUp();
051:
052: String platformClass = getPlatformClass();
053: /*
054: hsqldb<1.7.2 does not support Blob/Clob, so we skip test for this DB
055: */
056: knownIssue = false;
057: if (platformClass.equals(PlatformHsqldbImpl.class.getName())) {
058: skipTest = true;
059: } else if (platformClass.equals(PlatformPostgreSQLImpl.class
060: .getName())
061: && ojbSkipKnownIssueProblem("LOB handling is not yet implemented for PostgreSQL")) {
062: knownIssue = true;
063: skipTest = true;
064: }
065: }
066:
067: public void testBlobInsertion() throws Exception {
068: if (skipTest) {
069: if (!knownIssue) {
070: System.out.println(SKIP_MESSAGE);
071: }
072: return;
073: }
074:
075: int size = 5000;
076:
077: ObjectWithBlob obj = new ObjectWithBlob();
078:
079: byte[] barr = new byte[size];
080: char[] carr = new char[size];
081: for (int i = 0; i < size; i++) {
082: barr[i] = (byte) 'x';
083: carr[i] = 'y';
084: }
085:
086: // obj.setId(1); we use autoincrement
087: obj.setBlob(barr);
088: obj.setClob(new String(carr));
089: broker.beginTransaction();
090: broker.store(obj);
091: broker.commitTransaction();
092: broker.clearCache();
093:
094: Identity oid = new Identity(obj, broker);
095: ObjectWithBlob obj1 = (ObjectWithBlob) broker
096: .getObjectByIdentity(oid);
097: assertNotNull("BLOB was not stored", obj1.getBlob());
098: assertNotNull("CLOB was not stored", obj1.getClob());
099: assertEquals(obj.getBlob().length, obj1.getBlob().length);
100: assertEquals(obj.getClob().length(), obj1.getClob().length());
101:
102: }
103:
104: public void testReadNullBlob() {
105: if (skipTest) {
106: if (!knownIssue) {
107: System.out.println(SKIP_MESSAGE);
108: }
109: return;
110: }
111:
112: ObjectWithBlob obj = new ObjectWithBlob();
113:
114: // obj.setId(1); we use autoincrement
115: obj.setBlob(null);
116: obj.setClob(null);
117: broker.beginTransaction();
118: broker.store(obj);
119: broker.commitTransaction();
120: broker.clearCache();
121:
122: Identity oid = new Identity(obj, broker);
123: ObjectWithBlob obj1 = (ObjectWithBlob) broker
124: .getObjectByIdentity(oid);
125:
126: assertEquals(null, obj1.getBlob());
127: assertEquals(null, obj1.getClob());
128: }
129:
130: //*******************************************************
131: // inner class - test class
132: //*******************************************************
133: public static class ObjectWithBlob implements Serializable {
134: private int id;
135:
136: private byte[] blob;
137:
138: private String clob;
139:
140: /**
141: * Gets the blob.
142: * @return Returns a byte[]
143: */
144: public byte[] getBlob() {
145: return blob;
146: }
147:
148: /**
149: * Sets the blob.
150: * @param blob The blob to set
151: */
152: public void setBlob(byte[] blob) {
153: this .blob = blob;
154: }
155:
156: /**
157: * Gets the clob.
158: * @return Returns a char[]
159: */
160: public String getClob() {
161: return clob;
162: }
163:
164: /**
165: * Sets the clob.
166: * @param clob The clob to set
167: */
168: public void setClob(String clob) {
169: this .clob = clob;
170: }
171:
172: /**
173: * Gets the id.
174: * @return Returns a int
175: */
176: public int getId() {
177: return id;
178: }
179:
180: /**
181: * Sets the id.
182: * @param id The id to set
183: */
184: public void setId(int id) {
185: this.id = id;
186: }
187: }
188:
189: }
|