KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > options > FailSilentlyTest


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.options;
8
9 import junit.framework.TestCase;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.config.Configuration;
13
14 import javax.transaction.Transaction JavaDoc;
15 import javax.transaction.TransactionManager JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 /**
20  * Tests passing in the failSilently option in various scenarios.
21  *
22  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
23  */

24 public class FailSilentlyTest extends TestCase
25 {
26    private CacheImpl cache;
27    private TransactionManager JavaDoc manager;
28    private Transaction JavaDoc tx;
29    private Fqn fqn = Fqn.fromString("/a");
30    private String JavaDoc key = "key";
31
32    protected void setUp() throws Exception JavaDoc
33    {
34       if (cache != null) tearDown();
35       cache = new CacheImpl();
36       // very short acquisition timeout
37
cache.getConfiguration().setLockAcquisitionTimeout(100);
38       cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
39       cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
40       cache.start();
41       manager = cache.getTransactionManager();
42    }
43
44    protected void tearDown()
45    {
46       if (tx != null)
47       {
48          try
49          {
50             manager.resume(tx);
51             manager.rollback();
52          }
53          catch (Exception JavaDoc e)
54          {
55             // who cares
56
}
57       }
58       if (cache != null)
59       {
60          cache.stop();
61          cache = null;
62       }
63    }
64
65    public void testPutKeyValue() throws Exception JavaDoc
66    {
67       manager.begin();
68       cache.put(fqn, key, "value");
69       tx = manager.suspend();
70       cache.getInvocationContext().getOptionOverrides().setFailSilently(true);
71       cache.put(fqn, key, "value2");
72    }
73
74    public void testPutData() throws Exception JavaDoc
75    {
76       Map JavaDoc<String JavaDoc, String JavaDoc> data = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
77       data.put(key, "value");
78       manager.begin();
79       cache.put(fqn, data);
80       tx = manager.suspend();
81       cache.getInvocationContext().getOptionOverrides().setFailSilently(true);
82       cache.put(fqn, data);
83    }
84
85
86    public void testRemoveNode() throws Exception JavaDoc
87    {
88       cache.put(fqn, key, "value");
89       manager.begin();
90       // get a read lock
91
cache.get(fqn, key);
92       tx = manager.suspend();
93       cache.getInvocationContext().getOptionOverrides().setFailSilently(true);
94       cache.remove(fqn);
95    }
96
97    public void testRemoveKey() throws Exception JavaDoc
98    {
99       cache.put(fqn, key, "value");
100       manager.begin();
101       // get a read lock
102
cache.get(fqn, key);
103       tx = manager.suspend();
104       cache.getInvocationContext().getOptionOverrides().setFailSilently(true);
105       cache.remove(fqn, key);
106    }
107
108    public void testGetNode() throws Exception JavaDoc
109    {
110       cache.put(fqn, key, "value");
111       manager.begin();
112       // get a WL
113
cache.put(fqn, key, "value2");
114       tx = manager.suspend();
115       cache.getInvocationContext().getOptionOverrides().setFailSilently(true);
116       cache.get(fqn);
117    }
118
119    public void testGetKey() throws Exception JavaDoc
120    {
121       cache.put(fqn, key, "value");
122       manager.begin();
123       // get a WL
124
cache.put(fqn, key, "value2");
125       tx = manager.suspend();
126       cache.getInvocationContext().getOptionOverrides().setFailSilently(true);
127       cache.get(fqn, key);
128    }
129
130    public void testGetChildrenNames() throws Exception JavaDoc
131    {
132       cache.put(fqn, key, "value");
133       manager.begin();
134       // get a WL
135
cache.put(fqn, key, "value2");
136       tx = manager.suspend();
137       cache.getInvocationContext().getOptionOverrides().setFailSilently(true);
138       cache.getChildrenNames(fqn);
139
140    }
141
142    public void testPutThatWillFail() throws Exception JavaDoc
143    {
144       manager.begin();
145       cache.put(fqn, "k", "v"); // this will get WLs on / and /a
146
tx = manager.suspend();
147
148       assertEquals(2, cache.getNumberOfLocksHeld());
149
150       // now this call WILL fail, but should fail silently - i.e., not roll back.
151
manager.begin();
152       cache.getInvocationContext().getOptionOverrides().setFailSilently(true);
153       cache.put(fqn, "x", "y");
154
155       // should not roll back, despite the cache put call failing/timing out.
156
manager.commit();
157    }
158 }
159
Popular Tags