KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > otm > LockTestBase


1 package org.apache.ojb.otm;
2
3 /**
4  * This is the base abstract class for all isolation TestSuites
5  * based on the ODMG locking documentation
6  */

7
8 import junit.framework.TestCase;
9 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 {
20     protected TestKit _kit;
21     protected OTMConnection _conn1;
22     protected OTMConnection _conn2;
23     protected Transaction _tx1;
24     protected Transaction _tx2;
25     protected ObjectLock _lock;
26     protected TransactionIsolation _isolation;
27
28     public LockTestBase(String JavaDoc name)
29     {
30         super(name);
31     }
32
33     protected abstract TransactionIsolation newIsolation();
34
35     public void setUp()
36     {
37         _kit = TestKit.getTestInstance();
38         _kit.setLockWaitStrategy(new NoWaitStrategy());
39         _isolation = newIsolation();
40         try
41         {
42             _conn1 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
43             _tx1 = _kit.getTransaction(_conn1);
44             _tx1.begin();
45
46             _conn2 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
47             _tx2 = _kit.getTransaction(_conn2);
48             _tx2.begin();
49
50             Article obj = Article.createInstance();
51             _lock = new ObjectLock(_conn1.getIdentity(obj));
52         }
53         catch (Exception JavaDoc ex)
54         {
55             ex.printStackTrace();
56         }
57
58     }
59
60     protected boolean readLock(Transaction tx)
61     {
62         try
63         {
64             _isolation.readLock(tx, _lock);
65             return true;
66         }
67         catch (LockingException ex)
68         {
69             return false;
70         }
71     }
72
73     protected boolean writeLock(Transaction tx)
74     {
75         try
76         {
77             _isolation.writeLock(tx, _lock);
78             return true;
79         }
80         catch (LockingException ex)
81         {
82             return false;
83         }
84     }
85
86     protected boolean releaseLock(Transaction tx)
87     {
88         _lock.releaseLock(tx);
89         return true;
90     }
91
92     public void tearDown()
93     {
94         try
95         {
96             _tx1.rollback();
97         _conn1.close();
98             _conn1 = null;
99
100             _tx2.rollback();
101         _conn2.close();
102             _conn2 = null;
103         }
104         catch (Throwable JavaDoc t)
105         {
106         }
107         _kit.setLockWaitStrategy(new TimeoutStrategy());
108     }
109 }
110
Popular Tags