KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > buddyreplication > BuddyReplicationWithTransactionsTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.buddyreplication;
8
9 import org.jboss.cache.CacheImpl;
10 import org.jboss.cache.Fqn;
11
12 import javax.transaction.TransactionManager JavaDoc;
13
14
15 /**
16  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
17  */

18 public class BuddyReplicationWithTransactionsTest extends BuddyReplicationTestsBase
19 {
20    private Fqn fqn = Fqn.fromString("test");
21    private String JavaDoc key = "key";
22    private String JavaDoc value = "value";
23    private CacheImpl[] caches;
24
25    protected void tearDown() throws Exception JavaDoc
26    {
27       if (caches != null)
28       {
29          cleanup(caches);
30          caches = null;
31       }
32    }
33
34    public void testTransactionsCommit() throws Exception JavaDoc
35    {
36       caches = createCaches(3, false, true, false);
37
38       caches[0].put(fqn, key, value);
39       Fqn oldBackupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + "/test");
40       Fqn newBackupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress()) + "/test");
41
42       dumpCacheContents(caches);
43
44       TransactionManager JavaDoc txman = caches[2].getTransactionManager();
45
46
47       assertTrue(caches[0].exists(fqn));
48       assertTrue(!caches[1].exists(fqn));
49       assertTrue(!caches[2].exists(fqn));
50       assertTrue(!caches[0].exists(oldBackupFqn));
51       assertTrue(caches[1].exists(oldBackupFqn));
52       assertTrue(!caches[2].exists(oldBackupFqn));
53       assertTrue(!caches[0].exists(newBackupFqn));
54       assertTrue(!caches[1].exists(newBackupFqn));
55       assertTrue(!caches[2].exists(newBackupFqn));
56
57
58       txman.begin();
59
60       dumpCacheContents(caches);
61       caches[2].get(fqn, key);
62       dumpCacheContents(caches);
63
64       assertTrue(caches[0].exists(fqn));
65       assertTrue(!caches[1].exists(fqn));
66       assertTrue(caches[2].exists(fqn));
67       assertTrue(!caches[0].exists(oldBackupFqn));
68       assertTrue(caches[1].exists(oldBackupFqn));
69       assertTrue(!caches[2].exists(oldBackupFqn));
70       assertTrue(!caches[0].exists(newBackupFqn));
71       assertTrue(!caches[1].exists(newBackupFqn));
72       assertTrue(!caches[2].exists(newBackupFqn));
73
74       txman.commit();
75
76       dumpCacheContents(caches);
77
78       assertTrue(!caches[0].exists(fqn));
79       assertTrue(!caches[1].exists(fqn));
80       assertTrue(caches[2].exists(fqn));
81       assertTrue(!caches[0].exists(oldBackupFqn));
82       assertTrue(!caches[1].exists(oldBackupFqn));
83       assertTrue(!caches[2].exists(oldBackupFqn));
84       assertTrue(caches[0].exists(newBackupFqn));
85       assertTrue(!caches[1].exists(newBackupFqn));
86       assertTrue(!caches[2].exists(newBackupFqn));
87
88       dumpCacheContents(caches);
89
90       assertNoLocks(caches);
91    }
92
93    public void testTransactionsRollback() throws Exception JavaDoc
94    {
95       caches = createCaches(3, false, true, false);
96
97       caches[0].put(fqn, key, value);
98       Fqn oldBackupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + "/test");
99       Fqn newBackupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress()) + "/test");
100
101       dumpCacheContents(caches);
102
103       TransactionManager JavaDoc txman = caches[2].getTransactionManager();
104
105
106       assertTrue(caches[0].exists(fqn));
107       assertTrue(!caches[1].exists(fqn));
108       assertTrue(!caches[2].exists(fqn));
109       assertTrue(!caches[0].exists(oldBackupFqn));
110       assertTrue(caches[1].exists(oldBackupFqn));
111       assertTrue(!caches[2].exists(oldBackupFqn));
112       assertTrue(!caches[0].exists(newBackupFqn));
113       assertTrue(!caches[1].exists(newBackupFqn));
114       assertTrue(!caches[2].exists(newBackupFqn));
115
116
117       txman.begin();
118
119       dumpCacheContents(caches);
120       caches[2].get(fqn, key);
121       dumpCacheContents(caches);
122
123       assertTrue(caches[0].exists(fqn));
124       assertTrue(!caches[1].exists(fqn));
125       assertTrue(caches[2].exists(fqn));
126       assertTrue(!caches[0].exists(oldBackupFqn));
127       assertTrue(caches[1].exists(oldBackupFqn));
128       assertTrue(!caches[2].exists(oldBackupFqn));
129       assertTrue(!caches[0].exists(newBackupFqn));
130       assertTrue(!caches[1].exists(newBackupFqn));
131       assertTrue(!caches[2].exists(newBackupFqn));
132
133       txman.rollback();
134
135       dumpCacheContents(caches);
136
137       assertTrue(caches[0].exists(fqn));
138       assertTrue(!caches[1].exists(fqn));
139       assertTrue(!caches[2].exists(fqn));
140       assertTrue(!caches[0].exists(oldBackupFqn));
141       assertTrue(caches[1].exists(oldBackupFqn));
142       assertTrue(!caches[2].exists(oldBackupFqn));
143       assertTrue(!caches[0].exists(newBackupFqn));
144       assertTrue(!caches[1].exists(newBackupFqn));
145       assertTrue(!caches[2].exists(newBackupFqn));
146
147       assertNoLocks(caches);
148    }
149
150 }
151
Popular Tags