You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2006/07/15 16:08:19 UTC
svn commit: r422224 - in /db/ojb/trunk/src/test/org/apache/ojb/broker/lob:
./ LOBTest.java
Author: arminw
Date: Sat Jul 15 07:08:19 2006
New Revision: 422224
URL: http://svn.apache.org/viewvc?rev=422224&view=rev
Log:
merge trunk with 1.0.x, lob support
Added:
db/ojb/trunk/src/test/org/apache/ojb/broker/lob/
db/ojb/trunk/src/test/org/apache/ojb/broker/lob/LOBTest.java
Added: db/ojb/trunk/src/test/org/apache/ojb/broker/lob/LOBTest.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/test/org/apache/ojb/broker/lob/LOBTest.java?rev=422224&view=auto
==============================================================================
--- db/ojb/trunk/src/test/org/apache/ojb/broker/lob/LOBTest.java (added)
+++ db/ojb/trunk/src/test/org/apache/ojb/broker/lob/LOBTest.java Sat Jul 15 07:08:19 2006
@@ -0,0 +1,987 @@
+package org.apache.ojb.broker.lob;
+
+/* Copyright 2002-2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.ByteArrayInputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.io.StringReader;
+import java.io.Writer;
+import java.sql.Blob;
+import java.sql.Clob;
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.SerializationUtils;
+import org.apache.ojb.broker.Identity;
+import org.apache.ojb.broker.platforms.PlatformHsqldbImpl;
+import org.apache.ojb.broker.query.Criteria;
+import org.apache.ojb.broker.query.QueryFactory;
+import org.apache.ojb.broker.query.ReportQueryByCriteria;
+import org.apache.ojb.broker.util.ObjectModification;
+import org.apache.ojb.junit.PBTestCase;
+
+/**
+ * This TestCase tests OJB's handling with LOB's. Persistent objects with BLOB/CLOB and
+ * byte[]/String attributes are used.
+ * Object {@link org.apache.ojb.broker.lob.LOBTest.LobObject} has Blob/Clob fields unlike class
+ * {@link org.apache.ojb.broker.lob.LOBTest.LobObjectExt} which use byte[] and String fields but
+ * BLOB/CLOB columns in database.
+ *
+ * @version $Id: LOBTest.java 365232 2005-12-21 23:36:07 +0100 (Mi, 21 Dez 2005) tomdz $
+ */
+public class LOBTest extends PBTestCase
+{
+ public static void main(String[] args)
+ {
+ String[] arr = {LOBTest.class.getName()};
+ junit.textui.TestRunner.main(arr);
+ }
+
+ public LOBTest(String name)
+ {
+ super(name);
+ }
+
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ }
+
+ public void tearDown() throws Exception
+ {
+ super.tearDown();
+ }
+
+// comment out all test when JDK1.3 was used
+
+//#ifdef JDBC30
+
+
+ public void testReportQuery() throws Exception
+ {
+ int size = 1500;
+
+ byte[] barr = new byte[size];
+ char[] carr = new char[size/10];
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ }
+
+ for(int i = 0; i < size/10; i++)
+ {
+ carr[i] = 'y';
+ }
+
+ broker.beginTransaction();
+ Blob b = broker.serviceLobHelper().newBlob();
+ b.setBytes(1, barr);
+ Clob c = broker.serviceLobHelper().newClob();
+ c.setString(1, new String(carr));
+ LobObject obj = new LobObject();
+ obj.setBlob(b);
+ obj.setClob(c);
+ broker.store(obj);
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+
+ Criteria crit = new Criteria();
+ crit.addNotNull("blob");
+ crit.addEqualTo("id", new Integer(obj.getId()));
+ ReportQueryByCriteria q = QueryFactory.newReportQuery(LobObject.class, crit);
+ q.setAttributes(new String[]{"blob", "clob"});
+
+ Iterator result = broker.getReportQueryIteratorByQuery(q);
+ while(result.hasNext())
+ {
+ Object[] arr = (Object[]) result.next();
+ assertNotNull("BLOB expected", arr[0]);
+ assertNotNull("CLOB expected", arr[1]);
+ Blob bb = (Blob) arr[0];
+ Clob cc = (Clob) arr[1];
+ assertEquals(size, bb.length());
+ assertEquals(size / 10, cc.length());
+ }
+
+ broker.clearCache();
+ result = broker.getReportQueryIteratorByQuery(q);
+ while(result.hasNext())
+ {
+ Object[] arr = (Object[]) result.next();
+ assertNotNull("BLOB expected", arr[0]);
+ assertNotNull("CLOB expected", arr[1]);
+ Blob bb = (Blob) arr[0];
+ Clob cc = (Clob) arr[1];
+ assertEquals(size, bb.length());
+ assertEquals(size / 10, cc.length());
+ }
+ broker.commitTransaction();
+
+ // without active tx it's not allowed to access LOB content
+ // except we bypass the OJB wrapper
+ result = broker.getReportQueryIteratorByQuery(q);
+ while(result.hasNext())
+ {
+ Object[] arr = (Object[]) result.next();
+ assertNotNull("BLOB expected", arr[0]);
+ assertNotNull("CLOB expected", arr[1]);
+ Blob bb = (Blob) arr[0];
+ Clob cc = (Clob) arr[1];
+ try
+ {
+ assertEquals(size, bb.length());
+ fail("Exception expected");
+ }
+ catch(LobException e)
+ {
+ // expected
+ }
+ try
+ {
+ assertEquals(size / 10, cc.length());
+ fail("Exception expected");
+ }
+ catch(LobException e)
+ {
+ // expected
+ }
+ // bypass the OJB wrapper
+ Blob realBlob = ((BlobHandle) bb).getBlob();
+ Clob realClob = ((ClobHandle) cc).getClob();
+ assertEquals(size, realBlob.length());
+ assertEquals(size / 10, realClob.length());
+ }
+ }
+
+ public void testQuery() throws Exception
+ {
+ int size = 500;
+
+ byte[] barr = new byte[size];
+ char[] carr = new char[size];
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ carr[i] = 'y';
+ }
+
+ broker.beginTransaction();
+ Blob b = broker.serviceLobHelper().newBlob();
+ b.setBytes(1, barr);
+ Clob c = broker.serviceLobHelper().newClob();
+ c.setString(1, new String(carr));
+ LobObject obj = new LobObject();
+ obj.setBlob(b);
+ obj.setClob(c);
+ broker.store(obj);
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+ Criteria crit = new Criteria();
+ crit.addNotNull("blob");
+ Collection result = broker.getCollectionByQuery(QueryFactory.newQuery(LobObject.class, crit));
+ for(Iterator iterator = result.iterator(); iterator.hasNext();)
+ {
+ LobObject objRead = (LobObject) iterator.next();
+ assertNotNull("BLOB expected", objRead.getBlob());
+ objRead.getBlob().length();
+ }
+
+ result = broker.getCollectionByQuery(QueryFactory.newQuery(LobObject.class, crit));
+ for(Iterator iterator = result.iterator(); iterator.hasNext();)
+ {
+ LobObject objRead = (LobObject) iterator.next();
+ assertNotNull("BLOB expected", objRead.getBlob());
+ objRead.getBlob().length();
+ }
+ broker.commitTransaction();
+ }
+
+ public void testUpdateWithoutRefresh() throws Exception
+ {
+ int size = 555;
+
+ byte[] barr = new byte[size];
+ char[] carr = new char[size];
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ carr[i] = 'y';
+ }
+
+ broker.beginTransaction();
+ Blob b = broker.serviceLobHelper().newBlob();
+ b.setBytes(1, barr);
+ Clob c = broker.serviceLobHelper().newClob();
+ c.setString(1, new String(carr));
+ LobObject obj = new LobObject();
+ obj.setBlob(b);
+ obj.setClob(c);
+ broker.store(obj);
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+ broker.serviceLobHelper().setLobAutoRefresh(false);
+ Identity oid = broker.serviceIdentity().buildIdentity(obj);
+
+ // if no cache is used skip this test
+ if(broker.serviceObjectCache().lookup(oid) == null) return;
+
+ LobObject objRead = (LobObject) broker.getObjectByIdentity(oid);
+ assertNotNull("BLOB was not stored", objRead.getBlob());
+ assertNotNull("CLOB was not stored", objRead.getClob());
+
+ // now update object without refreshed LOB content - autoRefresh is disabled
+ try
+ {
+ broker.store(obj, ObjectModification.UPDATE);
+ fail("LobException expected");
+ }
+ catch(LobException expected)
+ {
+ // expected exception
+ //expected.printStackTrace();
+ broker.abortTransaction();
+ broker.beginTransaction();
+ }
+
+ broker.serviceLobHelper().setLobAutoRefresh(true);
+ objRead = (LobObject) broker.getObjectByIdentity(oid);
+ assertNotNull("unexpected, BLOB was deleted", objRead.getBlob());
+ assertNotNull("unexpected, CLOB was deleted", objRead.getClob());
+
+ if(broker.isInTransaction()) broker.abortTransaction();
+ }
+
+ public void testLobAutoRefresh() throws Exception
+ {
+ int size = 50;
+
+ byte[] barr = new byte[size];
+ char[] carr = new char[size];
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ carr[i] = 'y';
+ }
+
+ broker.beginTransaction();
+ Blob b = broker.serviceLobHelper().newBlob();
+ b.setBytes(1, barr);
+ Clob c = broker.serviceLobHelper().newClob();
+ c.setString(1, new String(carr));
+ LobObject obj = new LobObject();
+ obj.setBlob(b);
+ obj.setClob(c);
+ broker.store(obj);
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+ broker.serviceLobHelper().setLobAutoRefresh(false);
+ Identity oid = broker.serviceIdentity().buildIdentity(obj);
+
+ // if no cache is used skip this test
+ if(broker.serviceObjectCache().lookup(oid) == null) return;
+
+ LobObject objRead = (LobObject) broker.getObjectByIdentity(oid);
+ assertNotNull("BLOB was not stored", objRead.getBlob());
+ assertNotNull("CLOB was not stored", objRead.getClob());
+
+ try
+ {
+ // not valid to access LOB fields
+ objRead.getBlob().length();
+ fail("Expected an LobException");
+ }
+ catch(LobException expected)
+ {
+ }
+
+ broker.serviceLobHelper().refreshLob(objRead);
+ assertEquals(50, objRead.getBlob().length());
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+ broker.serviceLobHelper().setLobAutoRefresh(true);
+ oid = broker.serviceIdentity().buildIdentity(obj);
+
+ objRead = (LobObject) broker.getObjectByIdentity(oid);
+ assertNotNull("BLOB was not stored", objRead.getBlob());
+ assertNotNull("CLOB was not stored", objRead.getClob());
+
+ assertEquals(50, objRead.getBlob().length());
+ broker.commitTransaction();
+ }
+
+ public void testLOBStream() throws Exception
+ {
+ int size = 5000;
+
+ byte[] barr = new byte[size];
+ StringBuffer buf = new StringBuffer();
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ buf.append('y');
+ }
+
+ ByteArrayInputStream in = new ByteArrayInputStream(barr);
+ StringReader reader = new StringReader(buf.toString());
+
+ broker.beginTransaction();
+ Blob b = broker.serviceLobHelper().newBlob(in);
+ Clob c = broker.serviceLobHelper().newClob(reader);
+ LobObject obj = new LobObject();
+ obj.setBlob(b);
+ obj.setClob(c);
+
+ broker.store(obj);
+ broker.commitTransaction();
+
+ //broker.clearCache();
+ broker.beginTransaction();
+ Identity oid = broker.serviceIdentity().buildIdentity(obj);
+ LobObject objRead = (LobObject) broker.getObjectByIdentity(oid);
+ broker.serviceLobHelper().refreshLob(objRead);
+ assertNotNull("BLOB was not stored", objRead.getBlob());
+ assertNotNull("CLOB was not stored", objRead.getClob());
+ byte[] result_2 = objRead.getBlob().getBytes(1, (int) objRead.getBlob().length());
+ // System.out.println("result: " + result_2.length);
+ assertTrue(ArrayUtils.isEquals(barr, result_2));
+ assertEquals(buf.toString(),
+ objRead.getClob().getSubString(1, (int) objRead.getClob().length()));
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+ objRead.setBlob(null);
+ objRead.setClob(null);
+ broker.store(objRead, ObjectModification.UPDATE);
+ broker.commitTransaction();
+ broker.clearCache();
+
+ objRead = (LobObject) broker.getObjectByIdentity(oid);
+ assertEquals(null, objRead.getBlob());
+ assertEquals(null, objRead.getClob());
+ }
+
+ public void testLOBUpdateSetBytesSetString() throws Exception
+ {
+ if(getPlatformClass().equals(PlatformHsqldbImpl.class.getName()))
+ {
+ ojbSkipTestMessage("# Skip unsupported operation for platform: " + getPlatformClass() + " #");
+ return;
+ }
+ doUpdate(UpdateSetBytesSetString);
+ }
+
+ public void testLOBUpdateSetBlobSetClob() throws Exception
+ {
+ if(getPlatformClass().equals(PlatformHsqldbImpl.class.getName()))
+ {
+ ojbSkipTestMessage("# Skip unsupported operation for platform: " + getPlatformClass() + " #");
+ return;
+ }
+ doUpdate(UpdateSetBlobSetClob);
+ }
+
+ public void testLOBUpdateSetBinaryStreamSetCharacterStream() throws Exception
+ {
+ if(getPlatformClass().equals(PlatformHsqldbImpl.class.getName()))
+ {
+ ojbSkipTestMessage("# Skip unsupported operation for platform: " + getPlatformClass() + " #");
+ return;
+ }
+ doUpdate(UpdateSetBinaryStreamSetCharacterStream);
+ }
+
+ final int UpdateSetBlobSetClob = 1;
+ final int UpdateSetBytesSetString = 2;
+ final int UpdateSetBinaryStreamSetCharacterStream = 3;
+
+ public void doUpdate(int updateType) throws Exception
+ {
+ int size = 10;
+
+ byte[] barr = new byte[size];
+ String carr = "yyyyyyyyyy";
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ }
+ byte[] updateByteArr = new byte[]{'u', 'p', 'd', 'a', 't', 'e'};
+ byte[] expectedByte = new byte[]{'u', 'p', 'd', 'a', 't', 'e', 'x', 'x', 'x', 'x'};
+ String updateString = "update";
+ String expectedString = "updateyyyy";
+
+ byte[] updateByteArr2 = new byte[]{'u', 'p', 'd', 'a', 't', 'e', '_', '2'};
+ byte[] expectedByte2 = new byte[]{'u', 'p', 'd', 'a', 't', 'e', '_', '2', 'x', 'x'};
+ String updateString2 = "update_2";
+ String expectedString2 = "update_2yy";
+
+ // insert
+ broker.beginTransaction();
+ Blob b = broker.serviceLobHelper().newBlob();
+ b.setBytes(1, barr);
+ Clob c = broker.serviceLobHelper().newClob();
+ c.setString(1, carr);
+ LobObject obj = new LobObject();
+ obj.setBlob(b);
+ obj.setClob(c);
+ broker.store(obj);
+ broker.commitTransaction();
+
+ broker.clearCache();
+ broker.beginTransaction();
+ Identity oid = broker.serviceIdentity().buildIdentity(obj);
+ LobObject objRead = (LobObject) broker.getObjectByIdentity(oid);
+ assertNotNull("BLOB was not stored", objRead.getBlob());
+ assertNotNull("CLOB was not stored", objRead.getClob());
+ byte[] resultArray = objRead.getBlob().getBytes(1, (int) objRead.getBlob().length());
+ assertTrue(ArrayUtils.isEquals(barr, resultArray));
+ assertEquals(carr,
+ objRead.getClob().getSubString(1, (int) objRead.getClob().length()));
+ broker.commitTransaction();
+
+ // update within PB-tx
+ broker.beginTransaction();
+ objRead = (LobObject) broker.getObjectByIdentity(oid);
+ broker.serviceLobHelper().refreshLob(objRead);
+ try
+ {
+ switch(updateType)
+ {
+ case UpdateSetBinaryStreamSetCharacterStream:
+ OutputStream out = objRead.getBlob().setBinaryStream(1);
+ out.write(updateByteArr);
+ out.flush();
+ out.close();
+ Writer writer = objRead.getClob().setCharacterStream(1);
+ writer.write(updateString);
+ writer.flush();
+ writer.close();
+ break;
+ case UpdateSetBlobSetClob:
+ Blob blob = broker.serviceLobHelper().newBlob(expectedByte);
+ Clob clob = broker.serviceLobHelper().newClob(expectedString);
+ objRead.setBlob(blob);
+ objRead.setClob(clob);
+ break;
+ case UpdateSetBytesSetString:
+ objRead.getBlob().setBytes(1, updateByteArr);
+ objRead.getClob().setString(1, updateString);
+ break;
+ default:
+ throw new RuntimeException("Unkown LOB update type specified");
+ }
+ }
+ catch(UnsupportedOperationException e)
+ {
+ ojbSkipTestMessage("Skip unsupported operation for platform: "
+ + getPlatformClass() + ", message: " + e.getMessage());
+ return;
+ }
+ broker.store(objRead, ObjectModification.UPDATE);
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+ oid = broker.serviceIdentity().buildIdentity(obj);
+ objRead = (LobObject) broker.getObjectByIdentity(oid);
+ broker.serviceLobHelper().refreshLob(objRead);
+ resultArray = objRead.getBlob().getBytes(1, (int) objRead.getBlob().length());
+// System.out.println("");
+// System.out.println("# exp: " + ArrayUtils.toString(expectedByte));
+// System.out.println("# get: " + ArrayUtils.toString(resultArray));
+// System.out.println("# set: " + updateString);
+// System.out.println("# get: " + objRead.getClob().getSubString(1, (int) objRead.getClob().length()));
+ assertTrue(ArrayUtils.isEquals(expectedByte, resultArray));
+ assertEquals(expectedString,
+ objRead.getClob().getSubString(1, (int) objRead.getClob().length()));
+ broker.commitTransaction();
+
+ // try update 2
+ broker.beginTransaction();
+
+ // TODO: arminw: seems that under specific conditions this test freeze
+ //here, when test testLOBUpdateSetBlobSetClob() runs against mysql
+ objRead = (LobObject) broker.getObjectByIdentity(oid);
+ broker.serviceLobHelper().refreshLob(objRead);
+
+ try
+ {
+ switch(updateType)
+ {
+ case UpdateSetBinaryStreamSetCharacterStream:
+ OutputStream out = objRead.getBlob().setBinaryStream(1);
+ out.write(updateByteArr2);
+ out.flush();
+ out.close();
+ Writer writer = objRead.getClob().setCharacterStream(1);
+ writer.write(updateString2);
+ writer.flush();
+ writer.close();
+ break;
+ case UpdateSetBlobSetClob:
+ Blob blob = broker.serviceLobHelper().newBlob(expectedByte2);
+ Clob clob = broker.serviceLobHelper().newClob(expectedString2);
+ objRead.setBlob(blob);
+ objRead.setClob(clob);
+ break;
+ case UpdateSetBytesSetString:
+ objRead.getBlob().setBytes(1, updateByteArr2);
+ objRead.getClob().setString(1, updateString2);
+ break;
+ default:
+ throw new RuntimeException("Unkown LOB update type specified");
+ }
+ }
+ catch(UnsupportedOperationException e)
+ {
+ ojbSkipTestMessage("# Skip unsupported operation for platform: " + getPlatformClass() + ", message: " + e.getMessage() + " #");
+ return;
+ }
+ broker.store(objRead, ObjectModification.UPDATE);
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+ oid = broker.serviceIdentity().buildIdentity(obj);
+ objRead = (LobObject) broker.getObjectByIdentity(oid);
+ broker.serviceLobHelper().refreshLob(objRead);
+ resultArray = objRead.getBlob().getBytes(1, (int) objRead.getBlob().length());
+ assertTrue(ArrayUtils.isEquals(expectedByte2, resultArray));
+ assertEquals(expectedString2,
+ objRead.getClob().getSubString(1, (int) objRead.getClob().length()));
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+ objRead.setBlob(null);
+ objRead.setClob(null);
+ broker.store(objRead, ObjectModification.UPDATE);
+ broker.commitTransaction();
+
+ objRead = (LobObject) broker.getObjectByIdentity(oid);
+ assertEquals(null, objRead.getBlob());
+ assertEquals(null, objRead.getClob());
+ }
+
+
+ public void testLOBInsertRead() throws Exception
+ {
+ int size = 5000;
+
+ byte[] barr = new byte[size];
+ char[] carr = new char[size];
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ carr[i] = 'y';
+ }
+
+ broker.beginTransaction();
+ Blob b = broker.serviceLobHelper().newBlob();
+ b.setBytes(1, barr);
+ Clob c = broker.serviceLobHelper().newClob();
+ c.setString(1, new String(carr));
+ LobObject obj = new LobObject();
+ obj.setBlob(b);
+ obj.setClob(c);
+
+ broker.store(obj);
+ broker.commitTransaction();
+
+ //broker.clearCache();
+ broker.beginTransaction();
+ Identity oid = broker.serviceIdentity().buildIdentity(obj);
+ LobObject objRead = (LobObject) broker.getObjectByIdentity(oid);
+ broker.serviceLobHelper().refreshLob(objRead);
+ assertNotNull("BLOB was not stored", objRead.getBlob());
+ assertNotNull("CLOB was not stored", objRead.getClob());
+ byte[] result_2 = objRead.getBlob().getBytes(1, (int) objRead.getBlob().length());
+ assertTrue(ArrayUtils.isEquals(barr, result_2));
+ assertEquals(new String(carr),
+ objRead.getClob().getSubString(1, (int) objRead.getClob().length()));
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+ objRead.setBlob(null);
+ objRead.setClob(null);
+ broker.store(objRead, ObjectModification.UPDATE);
+ broker.commitTransaction();
+ broker.clearCache();
+
+ objRead = (LobObject) broker.getObjectByIdentity(oid);
+ assertEquals(null, objRead.getBlob());
+ assertEquals(null, objRead.getClob());
+ }
+
+ public void testLOBDelete() throws Exception
+ {
+ int size = 5000;
+
+ byte[] barr = new byte[size];
+ char[] carr = new char[size];
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ carr[i] = 'y';
+ }
+
+ broker.beginTransaction();
+ Blob b = broker.serviceLobHelper().newBlob();
+ b.setBytes(1, barr);
+ Clob c = broker.serviceLobHelper().newClob();
+ c.setString(1, new String(carr));
+ LobObject obj = new LobObject();
+ obj.setBlob(b);
+ obj.setClob(c);
+
+ broker.store(obj);
+ broker.commitTransaction();
+
+ broker.clearCache();
+ broker.beginTransaction();
+ Identity oid = broker.serviceIdentity().buildIdentity(obj);
+ LobObject objRead = (LobObject) broker.getObjectByIdentity(oid);
+ //LOBHelper.refreshLob( objRead);
+ assertNotNull("BLOB was not stored", objRead.getBlob());
+ assertNotNull("CLOB was not stored", objRead.getClob());
+ byte[] result_2 = objRead.getBlob().getBytes(1, (int) objRead.getBlob().length());
+ assertTrue(ArrayUtils.isEquals(barr, result_2));
+ assertEquals(new String(carr),
+ objRead.getClob().getSubString(1, (int) objRead.getClob().length()));
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+ broker.delete(objRead);
+ broker.commitTransaction();
+
+ objRead = (LobObject) broker.getObjectByIdentity(oid);
+ assertNull(objRead);
+ }
+
+ public void testLOBFail_1() throws Exception
+ {
+ int size = 5;
+
+ byte[] barr = new byte[size];
+ char[] carr = new char[size];
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ carr[i] = 'y';
+ }
+
+ broker.beginTransaction();
+ Blob b = broker.serviceLobHelper().newBlob();
+ b.setBytes(1, barr);
+ Clob c = broker.serviceLobHelper().newClob();
+ c.setString(1, new String(carr));
+ LobObject obj = new LobObject();
+ obj.setBlob(b);
+ obj.setClob(c);
+ broker.store(obj);
+ broker.commitTransaction();
+
+ broker.beginTransaction();
+ Identity oid = broker.serviceIdentity().buildIdentity(obj);
+ // if object was in cache, we have to refresh the LOB fields
+ LobObject source = (LobObject) broker.serviceSessionCache().lookup(oid);
+ if(source != null)
+ {
+ try
+ {
+ source.getBlob().length();
+ fail("LobException expected");
+ }
+ catch(LobException expected)
+ {
+ }
+
+ broker.serviceLobHelper().refreshLob(source);
+ assertEquals(5, source.getBlob().length());
+ assertEquals(5, source.getClob().length());
+ }
+ broker.commitTransaction();
+ }
+
+ public void testLOBFail_2() throws Exception
+ {
+ int size = 5;
+
+ byte[] barr = new byte[size];
+ char[] carr = new char[size];
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ carr[i] = 'y';
+ }
+
+ broker.beginTransaction();
+ Blob b = broker.serviceLobHelper().newBlob();
+ b.setBytes(1, barr);
+ Clob c = broker.serviceLobHelper().newClob();
+ c.setString(1, new String(carr));
+ LobObject obj = new LobObject();
+ obj.setBlob(b);
+ obj.setClob(c);
+ broker.store(obj);
+ broker.commitTransaction();
+
+ broker.clearCache();
+ Identity oid = broker.serviceIdentity().buildIdentity(obj);
+ LobObject source = (LobObject) broker.getObjectByIdentity(oid);
+ try
+ {
+ source.getBlob().length();
+ fail("LobException expected");
+ }
+ catch(LobException expected)
+ {
+ }
+ broker.beginTransaction();
+ broker.serviceLobHelper().refreshLob(source);
+ assertEquals(5, source.getBlob().length());
+ broker.commitTransaction();
+ }
+
+
+ public void testByteArrayStringInsertRead() throws Exception
+ {
+ int size = 5000;
+
+ LobObjectExt obj = new LobObjectExt();
+
+ byte[] barr = new byte[size];
+ char[] carr = new char[size];
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ carr[i] = 'y';
+ }
+
+ // obj.setId(1); we use autoincrement
+ obj.setBlobArray(barr);
+ obj.setClobString(new String(carr));
+ broker.beginTransaction();
+ broker.store(obj);
+ broker.commitTransaction();
+ broker.clearCache();
+
+ Identity oid = broker.serviceIdentity().buildIdentity(obj);
+ LobObjectExt objRead = (LobObjectExt) broker.getObjectByIdentity(oid);
+ assertNotNull("BLOB was not stored", objRead.getBlobArray());
+ assertNotNull("CLOB was not stored", objRead.getClobString());
+ assertEquals(obj.getBlobArray().length, objRead.getBlobArray().length);
+ assertEquals(obj.getClobString().length(), objRead.getClobString().length());
+
+ broker.beginTransaction();
+ objRead.setBlobArray(null);
+ objRead.setClobString(null);
+ broker.store(objRead, ObjectModification.UPDATE);
+ broker.commitTransaction();
+ broker.clearCache();
+
+ objRead = (LobObjectExt) broker.getObjectByIdentity(oid);
+ assertEquals(null, objRead.getBlobArray());
+ assertEquals(null, objRead.getClobString());
+ }
+
+ public void testByteArrayStringDelete() throws Exception
+ {
+ int size = 5000;
+ LobObjectExt obj = new LobObjectExt();
+ byte[] barr = new byte[size];
+ char[] carr = new char[size];
+ for(int i = 0; i < size; i++)
+ {
+ barr[i] = (byte) 'x';
+ carr[i] = 'y';
+ }
+
+ // obj.setId(1); we use autoincrement
+ obj.setBlobArray(barr);
+ obj.setClobString(new String(carr));
+ broker.beginTransaction();
+ broker.store(obj);
+ broker.commitTransaction();
+ broker.clearCache();
+
+ Identity oid = broker.serviceIdentity().buildIdentity(obj);
+ LobObjectExt objRead = (LobObjectExt) broker.getObjectByIdentity(oid);
+ assertNotNull("BLOB was not stored", objRead.getBlobArray());
+ assertNotNull("CLOB was not stored", objRead.getClobString());
+ assertEquals(obj.getBlobArray().length, objRead.getBlobArray().length);
+ assertEquals(obj.getClobString().length(), objRead.getClobString().length());
+
+ broker.beginTransaction();
+ broker.delete(objRead);
+ broker.commitTransaction();
+
+ objRead = (LobObjectExt) broker.getObjectByIdentity(oid);
+ assertNull(objRead);
+ }
+
+ public void testLOBImplementations_1() throws Exception
+ {
+ byte[] b = new byte[]{'a', 'b', 'c', 'd', 'e'};
+ String c = "abcdefgh";
+ BlobImpl blob = new BlobImpl();
+ ClobImpl clob = new ClobImpl();
+
+ blob.setBytes(1, b);
+ clob.setString(1, c);
+
+ BlobImpl blob2 = (BlobImpl) SerializationUtils.clone(blob);
+ ClobImpl clob2 = (ClobImpl) SerializationUtils.clone(clob);
+
+ assertTrue(ArrayUtils.isEquals(blob.getBytes(), blob2.getBytes()));
+ assertTrue(ArrayUtils.isEquals(b, blob2.getBytes()));
+ assertEquals(clob.getSubString(1, (int) clob.length()), clob2.getSubString(1, (int) clob2.length()));
+ assertEquals(c, clob2.getSubString(1, (int) clob2.length()));
+ }
+
+ public void testLOBImplementations_2() throws Exception
+ {
+ byte[] b = new byte[]{'a', 'b', 'c', 'd', 'e'};
+ String c = "abcdefgh";
+ BlobImpl blob = new BlobImpl();
+ ClobImpl clob = new ClobImpl();
+
+ OutputStream out = blob.setBinaryStream(1);
+ out.write(b);
+ out.close();
+ Writer writer = clob.setCharacterStream(1);
+ writer.write(c);
+ writer.close();
+
+ BlobImpl blob2 = (BlobImpl) SerializationUtils.clone(blob);
+ ClobImpl clob2 = (ClobImpl) SerializationUtils.clone(clob);
+
+// System.out.println("# old: " + ArrayUtils.toString(blob2.getBytes()));
+// System.out.println("# old: " + clob2.getSubString(1, (int) clob2.length()));
+ assertTrue(ArrayUtils.isEquals(blob.getBytes(), blob2.getBytes()));
+ assertTrue(ArrayUtils.isEquals(b, blob2.getBytes()));
+ assertEquals(clob.getSubString(1, (int) clob.length()), clob2.getSubString(1, (int) clob2.length()));
+ assertEquals(c, clob2.getSubString(1, (int) clob2.length()));
+
+ blob2.setBytes(3, new byte[]{'C', 'D'});
+ clob2.setString(3, "CD");
+// System.out.println("# new: " + ArrayUtils.toString(blob2.getBytes()));
+// System.out.println("# new: " + ArrayUtils.toString(clob2.getSubString(1, (int) clob2.length())));
+ assertTrue(ArrayUtils.isEquals(new byte[]{'a', 'b', 'C', 'D', 'e'}, blob2.getBytes()));
+ assertEquals("abCDefgh", clob2.getSubString(1, (int) clob2.length()));
+ }
+
+
+//#else
+
+/*
+ public void testEmpty()
+ {
+ ojbSkipTestMessage("Skip all LOB tests when using JDK1.3");
+ }
+*/
+
+//#endif
+
+ //*******************************************************
+ // inner class - test class
+ //*******************************************************
+ public static class LobObject implements Serializable
+ {
+ private int id;
+ private Blob blob;
+ private Clob clob;
+
+ public LobObject()
+ {
+ }
+
+ public int getId()
+ {
+ return id;
+ }
+
+ public void setId(int id)
+ {
+ this.id = id;
+ }
+
+ public Blob getBlob()
+ {
+ return blob;
+ }
+
+ public void setBlob(Blob blob)
+ {
+ this.blob = blob;
+ }
+
+ public Clob getClob()
+ {
+ return clob;
+ }
+
+ public void setClob(Clob clob)
+ {
+ this.clob = clob;
+ }
+ }
+
+ public static class LobObjectExt implements Serializable
+ {
+ private int id;
+ private byte[] blobArray;
+ private String clobString;
+
+ public LobObjectExt()
+ {
+ }
+
+ public int getId()
+ {
+ return id;
+ }
+
+ public void setId(int id)
+ {
+ this.id = id;
+ }
+
+ public byte[] getBlobArray()
+ {
+ return blobArray;
+ }
+
+ public void setBlobArray(byte[] blobArray)
+ {
+ this.blobArray = blobArray;
+ }
+
+ public String getClobString()
+ {
+ return clobString;
+ }
+
+ public void setClobString(String clobString)
+ {
+ this.clobString = clobString;
+ }
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org