KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > TreeCacheFunctionalTest


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.cache.tests;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.TreeCache;
14 import org.jboss.cache.lock.IsolationLevel;
15 import org.jboss.cache.lock.TimeoutException;
16 import org.jboss.cache.transaction.DummyTransactionManager;
17
18 import javax.transaction.NotSupportedException JavaDoc;
19 import javax.transaction.SystemException JavaDoc;
20 import javax.transaction.Transaction JavaDoc;
21
22 /**
23  * Simple functional tests for TreeCache
24  * @author Bela Ban
25  * @version $Id: TreeCacheFunctionalTest.java,v 1.2.2.2 2005/04/06 21:07:05 starksm Exp $
26  */

27 public class TreeCacheFunctionalTest extends TestCase {
28    TreeCache cache=null;
29    Transaction JavaDoc tx=null;
30    final Fqn FQN=Fqn.fromString("/myNode");
31    final String JavaDoc KEY="key";
32    final String JavaDoc VALUE="value";
33    Exception JavaDoc ex;
34
35
36    protected void setUp() throws Exception JavaDoc {
37       super.setUp();
38       ex=null;
39    }
40
41    protected void tearDown() throws Exception JavaDoc {
42       super.tearDown();
43       if(cache != null) {
44          cache.stopService();
45          cache.destroyService();
46          cache=null;
47       }
48       if(ex != null)
49          throw ex;
50    }
51
52
53    public void testFailFastPut() throws Exception JavaDoc {
54       cache=new TreeCache();
55       cache.setCacheMode(TreeCache.LOCAL);
56       cache.startService();
57       cache.put(FQN, KEY, VALUE);
58       cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 0);
59       cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 100);
60       cache.putFailFast(Fqn.fromString("/1/2/3"), KEY, VALUE, 100);
61       cache.putFailFast(Fqn.fromString("/1/2/3"), KEY, VALUE, 0);
62       System.out.println("cache: " + cache.printLockInfo());
63    }
64
65
66    public void testFailFastWith2Transactions() throws Exception JavaDoc, NotSupportedException JavaDoc {
67       cache=new TreeCache();
68       cache.setCacheMode(TreeCache.LOCAL);
69       cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
70       cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
71       cache.startService();
72       tx=startTransaction();
73       cache.put("/a/b/c", KEY, VALUE);
74
75       class Loser extends Thread JavaDoc {
76          public void run() {
77             Transaction JavaDoc trans=null;
78             try {
79                trans=startTransaction();
80                cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 500);
81                fail("this should fail as /a/b/c is held by another thread");
82             }
83             catch(TimeoutException timeoutEx) {
84                System.out.println("got TimeoutException (as expected)");
85             }
86             catch(Exception JavaDoc e) {
87                ex=e;
88             }
89          }
90       }
91
92       System.out.println("locks before loser: " + cache.printLockInfo());
93       Loser loser=new Loser();
94       loser.start();
95       loser.join();
96       tx.commit();
97    }
98
99
100    Transaction JavaDoc startTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc {
101       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
102       mgr.begin();
103       Transaction JavaDoc tmptx=mgr.getTransaction();
104       return tmptx;
105    }
106
107
108
109    public static Test suite() {
110       return new TestSuite(TreeCacheFunctionalTest.class);
111    }
112
113    //public static void main(String[] args) {
114
// junit.textui.TestRunner.run(suite());
115
//}
116

117 }
118
Popular Tags