KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.tests.basic;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.CacheException;
7 import org.jboss.cache.Fqn;
8 import org.jboss.cache.TreeCache;
9 import org.jboss.cache.lock.IsolationLevel;
10 import org.jboss.cache.lock.TimeoutException;
11 import org.jboss.cache.transaction.DummyTransactionManager;
12
13 import javax.transaction.NotSupportedException JavaDoc;
14 import javax.transaction.SystemException JavaDoc;
15 import javax.transaction.Transaction JavaDoc;
16 import java.util.HashMap JavaDoc;
17
18 /**
19  * Simple functional tests for TreeCache
20  * @author Bela Ban
21  * @version $Id: TreeCacheFunctionalTest.java,v 1.2 2005/04/06 19:09:21 belaban Exp $
22  */

23 public class TreeCacheFunctionalTest extends TestCase {
24    TreeCache cache=null;
25    Transaction JavaDoc tx=null;
26    final Fqn FQN=Fqn.fromString("/myNode");
27    final String JavaDoc KEY="key";
28    final String JavaDoc VALUE="value";
29    Exception JavaDoc ex;
30
31
32    protected void setUp() throws Exception JavaDoc {
33       super.setUp();
34       cache=new TreeCache();
35       cache.setCacheMode(TreeCache.LOCAL);
36       cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
37       cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
38       cache.createService();
39       cache.startService();
40       ex=null;
41    }
42
43    protected void tearDown() throws Exception JavaDoc {
44       super.tearDown();
45       if(cache != null) {
46          cache.stopService();
47          cache.destroyService();
48          cache=null;
49       }
50       if(ex != null)
51          throw ex;
52    }
53
54
55    public void testPut() throws CacheException {
56       cache.put("/a/b/c", "age", new Integer JavaDoc(38));
57       assertEquals(cache.get("/a/b/c", "age"), new Integer JavaDoc(38));
58       assertNotNull(cache.get("/a/b/c"));
59       assertEquals(0, cache.getNumberOfLocksHeld());
60       assertEquals(0, cache.getLockTable().size());
61    }
62
63
64
65    public void testPutNullKey() throws CacheException {
66       Object JavaDoc key=null;
67       cache.put("/a/b/c", key, "val");
68       System.out.println("value of /a/b/c " + cache.print("/a/b/c"));
69    }
70
71    public void testPutNullValue() throws CacheException {
72       Object JavaDoc val=null;
73       cache.put("/a/b/c", "key", val);
74       System.out.println("value of /a/b/c " + cache.print("/a/b/c"));
75    }
76
77    public void testPutNullKeyAndValues() throws CacheException {
78       Object JavaDoc key=null, val=null;
79       cache.put("/a/b/c", key, val);
80       System.out.println("value of /a/b/c " + cache.print("/a/b/c"));
81    }
82
83    public void testPutMapsWithNullValues() throws CacheException {
84       HashMap JavaDoc map=new HashMap JavaDoc();
85       map.put("key", null);
86       map.put(null, "val");
87       map.put("a", "b");
88       map.put(null, null);
89       cache.put("/a/b/c", map);
90       System.out.println("value of /a/b/c " + cache.print("/a/b/c"));
91    }
92
93    public void testPutKeys() throws CacheException {
94       cache.put("/a/b/c", "age", new Integer JavaDoc(38));
95       cache.put("/a/b/c", "name", "Bela");
96       assertEquals(cache.get("/a/b/c", "age"), new Integer JavaDoc(38));
97       assertNotNull(cache.get("/a/b/c"));
98       assertEquals(cache.getKeys("/a/b/c").size(), 2);
99       assertEquals(cache.exists("/a/b/c"), true);
100       assertEquals(0, cache.getNumberOfLocksHeld());
101       assertEquals(0, cache.getLockTable().size());
102    }
103
104    public void testRemove() throws CacheException {
105       cache.put("/a/b/c", null);
106       cache.put("/a/b/c/1", null);
107       cache.put("/a/b/c/2", null);
108       cache.put("/a/b/c/3", null);
109       cache.put("/a/b/c/3/a/b/c", null);
110
111       cache.remove("/a/b/c");
112       assertEquals(0, cache.getNumberOfLocksHeld());
113       assertEquals(0, cache.getLockTable().size());
114    }
115
116
117    public void testFailFastPut() throws Exception JavaDoc {
118       cache.put(FQN, KEY, VALUE);
119       cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 0);
120       cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 100);
121       cache.putFailFast(Fqn.fromString("/1/2/3"), KEY, VALUE, 100);
122       cache.putFailFast(Fqn.fromString("/1/2/3"), KEY, VALUE, 0);
123       System.out.println("cache: " + cache.printLockInfo());
124    }
125
126
127    public void testFailFastWith2Transactions() throws Exception JavaDoc, NotSupportedException JavaDoc {
128       tx=startTransaction();
129       cache.put("/a/b/c", KEY, VALUE);
130
131       class Loser extends Thread JavaDoc {
132          public void run() {
133             Transaction JavaDoc trans=null;
134             try {
135                trans=startTransaction();
136                cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 500);
137                fail("this should fail as /a/b/c is held by another thread");
138             }
139             catch(TimeoutException timeoutEx) {
140                System.out.println("got TimeoutException (as expected)");
141             }
142             catch(Exception JavaDoc e) {
143                ex=e;
144             }
145          }
146       }
147
148       System.out.println("locks before loser: " + cache.printLockInfo());
149       Loser loser=new Loser();
150       loser.start();
151       loser.join();
152       tx.commit();
153    }
154
155
156    Transaction JavaDoc startTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc {
157       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
158       mgr.begin();
159       Transaction JavaDoc tmptx=mgr.getTransaction();
160       return tmptx;
161    }
162
163
164
165    public static Test suite() {
166       return new TestSuite(TreeCacheFunctionalTest.class);
167    }
168
169    //public static void main(String[] args) {
170
// junit.textui.TestRunner.run(suite());
171
//}
172

173 }
174
Popular Tags