svn commit: r492868 - /db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/lob/LOBTest.java

[email protected]
Newsgroups gmane.comp.jakarta.ojb.devel
Message-ID <[email protected]>
Author: arminw
Date: Thu Jan  4 18:32:42 2007
New Revision: 492868

URL: http://svn.apache.org/viewvc?view=rev&rev=492868
Log:
enhance tests and fix problems with Oracle

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/lob/LOBTest.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/lob/LOBTest.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/lob/LOBTest.java?view=diff&rev=492868&r1=492867&r2=492868
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/lob/LOBTest.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/lob/LOBTest.java Thu Jan  4 18:32:42 2007
@@ -29,9 +29,11 @@
 import org.apache.commons.lang.SerializationUtils;
 import org.apache.ojb.broker.Identity;
 import org.apache.ojb.broker.platforms.PlatformHsqldbImpl;
+import org.apache.ojb.broker.platforms.PlatformOracleImpl;
 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.query.Query;
 import org.apache.ojb.broker.util.ObjectModification;
 import org.apache.ojb.junit.PBTestCase;
 
@@ -103,7 +105,7 @@
 
         Criteria crit = new Criteria();
         crit.addNotNull("blob");
-        crit.addEqualTo("id", new Integer(obj.getId()));
+        crit.addEqualTo("id", obj.getId());
         ReportQueryByCriteria q = QueryFactory.newReportQuery(LobObject.class, crit);
         q.setAttributes(new String[]{"blob", "clob"});
 
@@ -271,7 +273,7 @@
 
     public void testLobAutoRefresh() throws Exception
     {
-        int size = 50;
+        int size = 2001;
 
         byte[] barr = new byte[size];
         char[] carr = new char[size];
@@ -314,7 +316,7 @@
         }
 
         broker.serviceLobHelper().refreshLob(objRead);
-        assertEquals(50, objRead.getBlob().length());
+        assertEquals(size, objRead.getBlob().length());
         broker.commitTransaction();
 
         broker.beginTransaction();
@@ -325,7 +327,7 @@
         assertNotNull("BLOB was not stored", objRead.getBlob());
         assertNotNull("CLOB was not stored", objRead.getClob());
 
-        assertEquals(50, objRead.getBlob().length());
+        assertEquals(size, objRead.getBlob().length());
         broker.commitTransaction();
     }
 
@@ -350,11 +352,10 @@
         LobObject obj = new LobObject();
         obj.setBlob(b);
         obj.setClob(c);
-
         broker.store(obj);
         broker.commitTransaction();
 
-        //broker.clearCache();
+        broker.clearCache();
         broker.beginTransaction();
         Identity oid = broker.serviceIdentity().buildIdentity(obj);
         LobObject objRead = (LobObject) broker.getObjectByIdentity(oid);
@@ -368,6 +369,39 @@
                 objRead.getClob().getSubString(1, (int) objRead.getClob().length()));
         broker.commitTransaction();
 
+        // update object using new LOB objets
+        broker.beginTransaction();
+        byte[] barr2 = new byte[size];
+        StringBuffer buf2 = new StringBuffer();
+        for(int i = 0; i < size; i++)
+        {
+            barr2[i] = (byte) 'u';
+            buf2.append('u');
+        }
+        ByteArrayInputStream in2 = new ByteArrayInputStream(barr2);
+        StringReader reader2 = new StringReader(buf2.toString());
+        Blob b_update = broker.serviceLobHelper().newBlob(in2);
+        Clob c_update = broker.serviceLobHelper().newClob(reader2);
+        obj.setBlob(b_update);
+        obj.setClob(c_update);
+        broker.store(obj, ObjectModification.UPDATE);
+        broker.commitTransaction();
+
+        broker.clearCache();
+        broker.beginTransaction();
+        oid = broker.serviceIdentity().buildIdentity(obj);
+        objRead = (LobObject) broker.getObjectByIdentity(oid);
+        broker.serviceLobHelper().refreshLob(objRead);
+        assertNotNull("BLOB was not stored", objRead.getBlob());
+        assertNotNull("CLOB was not stored", objRead.getClob());
+        result_2 = objRead.getBlob().getBytes(1, (int) objRead.getBlob().length());
+        // System.out.println("result: " + result_2.length);
+        assertTrue(ArrayUtils.isEquals(barr2, result_2));
+        assertEquals(buf2.toString(),
+                objRead.getClob().getSubString(1, (int) objRead.getClob().length()));
+        broker.commitTransaction();
+
+        // nullify LOB objects
         broker.beginTransaction();
         objRead.setBlob(null);
         objRead.setClob(null);
@@ -387,7 +421,17 @@
             ojbSkipTestMessage("Skip unsupported operation for platform: " + getPlatformClass() + ", Blob().setBytes(...)");
             return;
         }
-        doUpdate(UpdateSetBytesSetString);
+        boolean needLock = false;
+        boolean storeObject = true;
+        if(PlatformOracleImpl.class.isAssignableFrom(broker.serviceConnectionManager().getSupportedPlatform().getClass()))
+        {
+            needLock = true;
+            storeObject = false;
+        }
+        // test small object
+        doUpdate(UpdateSetBytesSetString, 50, needLock, storeObject);
+        // test large objects
+        doUpdate(UpdateSetBytesSetString, 4001, needLock, storeObject);
     }
 
     public void testLOBUpdateSetBlobSetClob() throws Exception
@@ -397,7 +441,10 @@
             ojbSkipTestMessage("Skip unsupported operation for platform: " + getPlatformClass() + ", setBlob(...)");
             return;
         }
-        doUpdate(UpdateSetBlobSetClob);
+        // test small object
+        doUpdate(UpdateSetBlobSetClob, 50, false, true);
+        // test large objects
+        doUpdate(UpdateSetBlobSetClob, 4001, false, true);
     }
 
     public void testLOBUpdateSetBinaryStreamSetCharacterStream() throws Exception
@@ -407,59 +454,90 @@
             ojbSkipTestMessage("Skip unsupported operation for platform: " + getPlatformClass() + ", Blob().setBinaryStream(...)");
             return;
         }
-        doUpdate(UpdateSetBinaryStreamSetCharacterStream);
+        boolean needLock = false;
+        boolean storeObject = true;
+        if(PlatformOracleImpl.class.isAssignableFrom(broker.serviceConnectionManager().getSupportedPlatform().getClass()))
+        {
+            needLock = true;
+            storeObject = false;
+        }
+        // test small object
+        doUpdate(UpdateSetBinaryStreamSetCharacterStream, 50, needLock, storeObject);
+        // test large objects
+        doUpdate(UpdateSetBinaryStreamSetCharacterStream, 4001, needLock,storeObject);
     }
 
     final int UpdateSetBlobSetClob = 1;
     final int UpdateSetBytesSetString = 2;
     final int UpdateSetBinaryStreamSetCharacterStream = 3;
 
-    public void doUpdate(int updateType) throws Exception
+    /**
+     * ==>> !!size value have to set >=10!!
+     */
+    void doUpdate(int updateType, int size, boolean needLock, boolean needObjectStore) throws Exception
     {
-        byte[] barr = new byte[10];
-        String carr = "yyyyyyyyyy";
-        for(int i = 0; i < 10; i++)
+        // prepare test
+        byte[] byteArr = new byte[size];
+        StringBuffer buf = new StringBuffer();
+        for(int i = 0; i < size; i++)
         {
-            barr[i] = (byte) 'x';
+            byteArr[i] = (byte) 'x';
+            buf.append('y');
         }
+        String clobString = buf.toString();
         byte[] updateByteArr = new byte[]{'u', 'p', 'd', 'a', 't', 'e'};
-        byte[] expectedByte = new byte[]{'u', 'p', 'd', 'a', 't', 'e', 'x', 'x', 'x', 'x'};
+        byte[] byteArrUpdated = ArrayUtils.addAll(updateByteArr, ArrayUtils.subarray(byteArr, 6, byteArr.length));
         String updateString = "update";
-        String expectedString = "updateyyyy";
+        String clobStringUpdated = updateString + clobString.substring(6);
 
         byte[] updateByteArr2 = new byte[]{'u', 'p', 'd', 'a', 't', 'e', '_', '2'};
-        byte[] expectedByte2 = new byte[]{'u', 'p', 'd', 'a', 't', 'e', '_', '2', 'x', 'x'};
+        byte[] byteArrUpdated2 = ArrayUtils.addAll(updateByteArr2, ArrayUtils.subarray(byteArr, 8, byteArr.length));
         String updateString2 = "update_2";
-        String expectedString2 = "update_2yy";
+        String clobStringUpdated2 = updateString2 + clobString.substring(8);
 
-        // insert
+        // start insert
         broker.beginTransaction();
         Blob b = broker.serviceLobHelper().newBlob();
-        b.setBytes(1, barr);
+        b.setBytes(1, byteArr);
         Clob c = broker.serviceLobHelper().newClob();
-        c.setString(1, carr);
+        c.setString(1, clobString);
         LobObject obj = new LobObject();
         obj.setBlob(b);
         obj.setClob(c);
         broker.store(obj);
         broker.commitTransaction();
 
+        // check insert
         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());
+        assertEquals(byteArr.length, objRead.getBlob().length());
+        assertEquals(clobString.length(), objRead.getClob().length());
         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()));
+        assertTrue(ArrayUtils.isEquals(byteArr, resultArray));
+        assertEquals(clobString, 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);
+        if(needLock)
+        {
+            objRead = (LobObject) broker.getObjectByIdentity(oid);
+            broker.serviceLobHelper().refreshLob(objRead);
+            // workaround to lock the row
+            Query q = QueryFactory.newReportQuery(
+                    LobObject.class, new String[]{"id"}, new Criteria().addEqualTo("id", objRead.getId()), false);
+            q.setSelectForUpdate(true);
+            broker.getObjectByQuery(q);
+        }
+        else
+        {
+            objRead = (LobObject) broker.getObjectByIdentity(oid);
+            broker.serviceLobHelper().refreshLob(objRead);
+        }
         try
         {
             switch(updateType)
@@ -475,8 +553,8 @@
                     writer.close();
                     break;
                 case UpdateSetBlobSetClob:
-                    Blob blob = broker.serviceLobHelper().newBlob(expectedByte);
-                    Clob clob = broker.serviceLobHelper().newClob(expectedString);
+                    Blob blob = broker.serviceLobHelper().newBlob(byteArrUpdated);
+                    Clob clob = broker.serviceLobHelper().newClob(clobStringUpdated);
                     objRead.setBlob(blob);
                     objRead.setClob(clob);
                     break;
@@ -492,34 +570,46 @@
         {
             ojbSkipTestMessage("Skip unsupported operation for platform: "
                     + getPlatformClass() + ", message: " + e.getMessage());
+            broker.abortTransaction();
             return;
         }
-        broker.store(objRead, ObjectModification.UPDATE);
+        if(needObjectStore) broker.store(objRead, ObjectModification.UPDATE);
         broker.commitTransaction();
 
+        broker.clearCache();
         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("# exp: " + ArrayUtils.toString(byteArrUpdated));
 //        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()));
+        assertEquals(byteArrUpdated.length, resultArray.length);
+        assertEquals(clobStringUpdated.length(), objRead.getClob().length());
+        assertTrue(ArrayUtils.isEquals(byteArrUpdated, resultArray));
+        assertEquals(clobStringUpdated, 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);
-
+        if(needLock)
+        {
+            objRead = (LobObject) broker.getObjectByIdentity(oid);
+            broker.serviceLobHelper().refreshLob(objRead);
+            // workaround to lock the row
+            Query q = QueryFactory.newReportQuery(
+                    LobObject.class, new String[]{"id"}, new Criteria().addEqualTo("id", objRead.getId()), false);
+            q.setSelectForUpdate(true);
+            broker.getObjectByQuery(q);
+        }
+        else
+        {
+            objRead = (LobObject) broker.getObjectByIdentity(oid);
+            broker.serviceLobHelper().refreshLob(objRead);
+        }
         try
         {
             switch(updateType)
@@ -535,8 +625,8 @@
                     writer.close();
                     break;
                 case UpdateSetBlobSetClob:
-                    Blob blob = broker.serviceLobHelper().newBlob(expectedByte2);
-                    Clob clob = broker.serviceLobHelper().newClob(expectedString2);
+                    Blob blob = broker.serviceLobHelper().newBlob(byteArrUpdated2);
+                    Clob clob = broker.serviceLobHelper().newClob(clobStringUpdated2);
                     objRead.setBlob(blob);
                     objRead.setClob(clob);
                     break;
@@ -551,19 +641,22 @@
         catch(UnsupportedOperationException e)
         {
             ojbSkipTestMessage("# Skip unsupported operation for platform: " + getPlatformClass() + ", message: " + e.getMessage() + " #");
+            broker.abortTransaction();
             return;
         }
         broker.store(objRead, ObjectModification.UPDATE);
         broker.commitTransaction();
 
+        broker.clearCache();
         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()));
+        assertEquals(byteArrUpdated2.length, resultArray.length);
+        assertEquals(clobStringUpdated2.length(), objRead.getClob().length());
+        assertTrue(ArrayUtils.isEquals(byteArrUpdated2, resultArray));
+        assertEquals(clobStringUpdated2, objRead.getClob().getSubString(1, (int) objRead.getClob().length()));
         broker.commitTransaction();
 
         broker.beginTransaction();
@@ -902,7 +995,7 @@
     //*******************************************************
     public static class LobObject implements Serializable
     {
-        private int id;
+        private Integer id;
         private Blob blob;
         private Clob clob;
 
@@ -910,12 +1003,12 @@
         {
         }
 
-        public int getId()
+        public Integer getId()
         {
             return id;
         }
 
-        public void setId(int id)
+        public void setId(Integer id)
         {
             this.id = id;
         }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.