01: package org.apache.ojb.otm;
02:
03: /**
04: * This is the base abstract class for all isolation TestSuites
05: * based on the ODMG locking documentation
06: */
07:
08: import junit.framework.TestCase;
09: import org.apache.ojb.broker.Article;
10: import org.apache.ojb.broker.PersistenceBrokerFactory;
11: import org.apache.ojb.otm.core.Transaction;
12: import org.apache.ojb.otm.lock.LockingException;
13: import org.apache.ojb.otm.lock.ObjectLock;
14: import org.apache.ojb.otm.lock.isolation.TransactionIsolation;
15: import org.apache.ojb.otm.lock.wait.NoWaitStrategy;
16: import org.apache.ojb.otm.lock.wait.TimeoutStrategy;
17:
18: public abstract class LockTestBase extends TestCase {
19: protected TestKit _kit;
20: protected OTMConnection _conn1;
21: protected OTMConnection _conn2;
22: protected Transaction _tx1;
23: protected Transaction _tx2;
24: protected ObjectLock _lock;
25: protected TransactionIsolation _isolation;
26:
27: public LockTestBase(String name) {
28: super (name);
29: }
30:
31: protected abstract TransactionIsolation newIsolation();
32:
33: public void setUp() {
34: _kit = TestKit.getTestInstance();
35: _kit.setLockWaitStrategy(new NoWaitStrategy());
36: _isolation = newIsolation();
37: try {
38: _conn1 = _kit.acquireConnection(PersistenceBrokerFactory
39: .getDefaultKey());
40: _tx1 = _kit.getTransaction(_conn1);
41: _tx1.begin();
42:
43: _conn2 = _kit.acquireConnection(PersistenceBrokerFactory
44: .getDefaultKey());
45: _tx2 = _kit.getTransaction(_conn2);
46: _tx2.begin();
47:
48: Article obj = Article.createInstance();
49: _lock = new ObjectLock(_conn1.getIdentity(obj));
50: } catch (Exception ex) {
51: ex.printStackTrace();
52: }
53:
54: }
55:
56: protected boolean readLock(Transaction tx) {
57: try {
58: _isolation.readLock(tx, _lock);
59: return true;
60: } catch (LockingException ex) {
61: return false;
62: }
63: }
64:
65: protected boolean writeLock(Transaction tx) {
66: try {
67: _isolation.writeLock(tx, _lock);
68: return true;
69: } catch (LockingException ex) {
70: return false;
71: }
72: }
73:
74: protected boolean releaseLock(Transaction tx) {
75: _lock.releaseLock(tx);
76: return true;
77: }
78:
79: public void tearDown() {
80: try {
81: _tx1.rollback();
82: _conn1.close();
83: _conn1 = null;
84:
85: _tx2.rollback();
86: _conn2.close();
87: _conn2 = null;
88: } catch (Throwable t) {
89: }
90: _kit.setLockWaitStrategy(new TimeoutStrategy());
91: }
92: }
|