01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.lenya.transaction;
19:
20: import org.apache.cocoon.core.container.ContainerTestCase;
21: import org.apache.lenya.ac.Identity;
22:
23: public class TransactionTest extends ContainerTestCase {
24:
25: public void testTransaction() throws TransactionException {
26:
27: Identity lenya = new Identity(getLogger());
28: lenya.addIdentifiable(new MockUser("lenya"));
29:
30: Identity alice = new Identity(getLogger());
31: alice.addIdentifiable(new MockUser("alice"));
32:
33: IdentityMap lenyaMap = new IdentityMapImpl(getLogger());
34: UnitOfWork lenyaUnit = new UnitOfWorkImpl(lenyaMap, lenya,
35: getLogger());
36: IdentifiableFactory lenyaFactory = new MockFactory(lenyaUnit);
37:
38: IdentityMap aliceMap = new IdentityMapImpl(getLogger());
39: UnitOfWork aliceUnit = new UnitOfWorkImpl(aliceMap, alice,
40: getLogger());
41: IdentifiableFactory aliceFactory = new MockFactory(aliceUnit);
42:
43: MockTransactionable lenyaT1 = (MockTransactionable) lenyaMap
44: .get(lenyaFactory, "t1");
45: MockTransactionable aliceT1 = (MockTransactionable) aliceMap
46: .get(aliceFactory, "t1");
47:
48: checkDoubleLock(lenyaT1);
49: checkLockAndModify(lenyaUnit, lenyaT1, aliceT1);
50:
51: }
52:
53: protected void checkDoubleLock(MockTransactionable t)
54: throws TransactionException {
55: t.lock();
56: try {
57: t.lock();
58: assertTrue("No exception thrown!", false);
59: } catch (LockException ignore) {
60: }
61: t.unlock();
62: }
63:
64: protected void checkLockAndModify(UnitOfWork lenyaUnit,
65: MockTransactionable lenyaT1, MockTransactionable aliceT1)
66: throws TransactionException {
67: lenyaT1.lock();
68: aliceT1.write();
69: try {
70: lenyaUnit.commit();
71: assertTrue("No exception thrown!", false);
72: } catch (ConcurrentModificationException ignore) {
73: }
74: lenyaT1.unlock();
75: }
76:
77: }
|