arminw 2005/06/14 06:33:09
Modified: src/test/org/apache/ojb/odmg Tag: OJB_1_0_RELEASE
LockingTest.java
src/test/org/apache/ojb Tag: OJB_1_0_RELEASE
repository_junit_odmg.xml
Log:
add new locking test
Revision Changes Path
No revision
No revision
1.13.2.5 +211 -64 db-ojb/src/test/org/apache/ojb/odmg/LockingTest.java
Index: LockingTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/LockingTest.java,v
retrieving revision 1.13.2.4
retrieving revision 1.13.2.5
diff -u -r1.13.2.4 -r1.13.2.5
--- LockingTest.java 28 Apr 2005 23:23:50 -0000 1.13.2.4
+++ LockingTest.java 14 Jun 2005 13:33:09 -0000 1.13.2.5
@@ -1,9 +1,9 @@
package org.apache.ojb.odmg;
+import java.io.Serializable;
import java.util.Collection;
import java.util.List;
-import java.io.Serializable;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerFactory;
@@ -14,6 +14,9 @@
import org.apache.ojb.odmg.locking.LockManager;
import org.apache.ojb.odmg.locking.LockManagerFactory;
import org.apache.ojb.odmg.shared.Article;
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.EqualsBuilder;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.LockNotGrantedException;
@@ -33,7 +36,7 @@
private static String PRE = "LockingTest_" + System.currentTimeMillis()
+ "_";
- private Implementation odmg1 ;
+ private Implementation odmg1;
private Database db1;
private Implementation odmg2;
@@ -61,14 +64,114 @@
public void tearDown() throws Exception
{
- if (odmg1.currentTransaction() != null)
odmg1.currentTransaction().abort();
+ if(odmg1.currentTransaction() != null)
odmg1.currentTransaction().abort();
db1.close();
- if (odmg2.currentTransaction() != null)
odmg2.currentTransaction().abort();
+ if(odmg2.currentTransaction() != null)
odmg2.currentTransaction().abort();
db2.close();
super.tearDown();
}
+ public void testWrite_1() throws Exception
+ {
+ String name = "testWrite_1_" + System.currentTimeMillis();
+ LockObject bean = new LockObject(name + "_bean_dummy");
+
+ performSaveMethod(bean.getId(), bean);
+
+ Transaction tx = odmg1.newTransaction();
+ tx.begin();
+ OQLQuery query = odmg1.newOQLQuery();
+ query.create("select objs from " + LockObject.class.getName() + "
where value = $1");
+ query.bind(name + "_bean_dummy");
+ List result = (List) query.execute();
+ tx.commit();
+ assertEquals(1, result.size());
+ LockObject tmp = (LockObject) result.get(0);
+ assertEquals(bean, tmp);
+ }
+
+ public void testWrite_2() throws Exception
+ {
+ String name = "testWrite_2_" + System.currentTimeMillis();
+
+ Transaction tx = odmg1.newTransaction();
+ tx.begin();
+ LockObject tmp = new LockObject(name + "_temp");
+ db1.makePersistent(tmp);
+ tx.commit();
+
+ LockObject bean = new LockObject(name + "_bean_dummy");
+ bean.setId(tmp.getId());
+
+ performSaveMethod(tmp.getId(), bean);
+
+ tx = odmg1.newTransaction();
+ tx.begin();
+ OQLQuery query = odmg1.newOQLQuery();
+ query.create("select objs from " + LockObject.class.getName() + "
where value = $1");
+ query.bind(name + "_bean_dummy");
+ List result = (List) query.execute();
+ tx.commit();
+ assertEquals(1, result.size());
+ tmp = (LockObject) result.get(0);
+ assertEquals(bean, tmp);
+ }
+
+ /**
+ * This method should reproduce a problem with Cocoon and OJB1.0.3
+ */
+ private LockObject performSaveMethod(Integer testId, LockObject bean)
throws Exception
+ {
+ LockObject toBeEdited = null;
+ Transaction tx = odmg1.newTransaction();
+ tx.begin();
+ OQLQuery query = odmg1.newOQLQuery();
+ query.create("select objs from " + LockObject.class.getName() + "
where id = $1");
+ query.bind(testId);
+ List result = (List) query.execute();
+ if(result.size() != 0)
+ {
+ toBeEdited = (LockObject) result.get(0);
+ if(toBeEdited != null)
+ {
+ try
+ {
+ PropertyUtils.copyProperties(toBeEdited, bean);
+ tx.commit();
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ fail("Unexpected exception: " + e.getMessage());
+ }
+ // use new tx to check released locks for objects
+ tx = odmg1.newTransaction();
+ tx.begin();
+ tx.lock(toBeEdited, Transaction.UPGRADE);
+ tx.commit();
+ }
+ else
+ {
+ tx.abort();
+ }
+ }
+ else
+ {
+ try
+ {
+ tx.lock(bean, Transaction.WRITE);
+ tx.commit();
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ fail("Unexpected exception: " + e.getMessage());
+ }
+ }
+ return toBeEdited;
+ }
+
/**
* Test multiple locks on the same object
*/
@@ -78,7 +181,7 @@
String name = "testMultipleLocks_" + timestamp;
String nameUpdated = "testMultipleLocks_Updated_" + timestamp;
TransactionExt tx = (TransactionExt) odmg1.newTransaction();
- LockObject obj = new LockObject();
+ LockObjectOpt obj = new LockObjectOpt();
tx.begin();
tx.lock(obj, Transaction.WRITE);
obj.setValue(name);
@@ -87,7 +190,7 @@
tx.commit();
OQLQuery query = odmg1.newOQLQuery();
- query.create("select all from " + LockObject.class.getName() + "
where value like $1");
+ query.create("select all from " + LockObjectOpt.class.getName() + "
where value like $1");
query.bind(name);
Collection result = (Collection) query.execute();
assertNotNull(result);
@@ -102,7 +205,7 @@
tx.commit();
query = odmg1.newOQLQuery();
- query.create("select all from " + LockObject.class.getName() + "
where value like $1");
+ query.create("select all from " + LockObjectOpt.class.getName() + "
where value like $1");
query.bind(nameUpdated);
result = (Collection) query.execute();
assertNotNull(result);
@@ -134,7 +237,7 @@
tx1.abort();
tx2.abort();
}
- catch (Exception e)
+ catch(Exception e)
{
}
}
@@ -175,45 +278,45 @@
tx1.abort();
tx2.abort();
}
- catch (Exception e)
+ catch(Exception e)
{
}
}
- /**
- * test proper treatment of Optimistic Locking in
- * ODMG transactions
- */
+ /**
+ * test proper treatment of Optimistic Locking in
+ * ODMG transactions
+ */
public void testOptimisticLockBasics() throws Exception
{
TransactionImpl tx1 = (TransactionImpl) odmg1.newTransaction();
TransactionImpl tx2 = (TransactionImpl) odmg2.newTransaction();
- LockObject obj = new LockObject();
+ LockObjectOpt obj = new LockObjectOpt();
tx1.begin();
- tx1.lock(obj,Transaction.WRITE);
- obj.setValue("tx1");
- tx1.commit();
-
- obj.setVersion(obj.getVersion() - 1);
- tx2.begin();
- tx2.lock(obj,Transaction.WRITE);
-
- obj.setValue("tx2");
- try
- {
- tx2.commit();
- // OL exceptions should be signalled as ODMG
LockNotGrantedExceptions
- // so that users can react accordingly
+ tx1.lock(obj, Transaction.WRITE);
+ obj.setValue("tx1");
+ tx1.commit();
+
+ obj.setVersion(obj.getVersion() - 1);
+ tx2.begin();
+ tx2.lock(obj, Transaction.WRITE);
+
+ obj.setValue("tx2");
+ try
+ {
+ tx2.commit();
+// OL exceptions should be signalled as ODMG LockNotGrantedExceptions
+// so that users can react accordingly
fail("Optimistic locking exception expected");
- }
- catch (LockNotGrantedException ex)
- {
- assertTrue("expected that a OL exception is caught",
true);
- }
+ }
+ catch(LockNotGrantedException ex)
+ {
+ assertTrue("expected that a OL exception is caught", true);
+ }
catch(Exception e)
{
e.printStackTrace();
@@ -224,6 +327,7 @@
/**
* factory method that createa an PerformanceArticle
+ *
* @return the created PerformanceArticle object
*/
private Article createArticle(String name)
@@ -245,7 +349,7 @@
{
int loops = 10;
Article[] arr = new Article[loops];
- for (int i = 0; i < loops; i++)
+ for(int i = 0; i < loops; i++)
{
Article a = createArticle("testLockLoop");
arr[i] = a;
@@ -253,7 +357,7 @@
TransactionImpl tx = (TransactionImpl) odmg1.newTransaction();
tx.begin();
- for (int i = 0; i < arr.length; i++)
+ for(int i = 0; i < arr.length; i++)
{
tx.lock(arr[i], Transaction.WRITE);
}
@@ -285,65 +389,108 @@
}
+ /**
+ * Test object.
+ */
public static class LockObject implements Serializable
{
private Integer id;
private String value;
- private int version;
public LockObject()
{
}
- /**
- * Gets the id.
- * @return Returns a int
- */
+ public LockObject(String value)
+ {
+ this.value = value;
+ }
+
+ public boolean equals(Object obj)
+ {
+ if(obj == null || !(obj instanceof LockObject))
+ {
+ return false;
+ }
+ else
+ {
+ LockObject tmp = (LockObject) obj;
+ return new EqualsBuilder()
+ .append(id, tmp.id)
+ .append(value, tmp.value)
+ .isEquals();
+ }
+ }
+
+ public String toString()
+ {
+ return new ToStringBuilder(this)
+ .append("id", id)
+ .append("value", value)
+ .toString();
+ }
+
public Integer getId()
{
return id;
}
-
- /**
- * Sets the id.
- * @param id The id to set
- */
public void setId(Integer id)
{
this.id = id;
}
-
- /**
- * Gets the value.
- * @return Returns a String
- */
public String getValue()
{
return value;
}
-
- /**
- * Sets the value.
- * @param value The value to set
- */
public void setValue(String value)
{
this.value = value;
}
+ }
+
+ /**
+ * Test object with optimistic locking enabled.
+ */
+ public static class LockObjectOpt extends LockObject
+ {
+ private int version;
+
+ public LockObjectOpt()
+ {
+ }
+
+ public LockObjectOpt(String value)
+ {
+ super(value);
+ }
+
+ public boolean equals(Object obj)
+ {
+ if(obj == null || !(obj instanceof LockObjectOpt))
+ {
+ return false;
+ }
+ else
+ {
+ LockObjectOpt tmp = (LockObjectOpt) obj;
+ return new EqualsBuilder()
+ .append(version, tmp.version)
+ .isEquals() && super.equals(obj);
+ }
+ }
+
+ public String toString()
+ {
+ return new ToStringBuilder(this)
+ .append("super", super.toString())
+ .append("version", version)
+ .toString();
+ }
- /**
- * Gets the version.
- * @return Returns a long
- */
public int getVersion()
{
return version;
}
-
- /**
- * Sets the version.
- * @param version The version to set
- */
public void setVersion(int version)
{
this.version = version;
No revision
No revision
1.13.2.15 +19 -1 db-ojb/src/test/org/apache/ojb/repository_junit_odmg.xml
Index: repository_junit_odmg.xml
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit_odmg.xml,v
retrieving revision 1.13.2.14
retrieving revision 1.13.2.15
diff -u -r1.13.2.14 -r1.13.2.15
--- repository_junit_odmg.xml 4 Jun 2005 14:48:05 -0000 1.13.2.14
+++ repository_junit_odmg.xml 14 Jun 2005 13:33:09 -0000 1.13.2.15
@@ -2297,6 +2297,24 @@
column="VALUE_"
jdbc-type="VARCHAR"
/>
+</class-descriptor>
+
+<class-descriptor
+ class="org.apache.ojb.odmg.LockingTest$LockObjectOpt"
+ table="LOCKED_BY_VERSION"
+ >
+ <field-descriptor
+ name="id"
+ column="ID"
+ jdbc-type="INTEGER"
+ primarykey="true"
+ autoincrement="true"
+ />
+ <field-descriptor
+ name="value"
+ column="VALUE_"
+ jdbc-type="VARCHAR"
+ />
<field-descriptor
name="version"
column="VERSION_"
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]