KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > aop > MarshalledTreeCacheTest


1 package org.jboss.cache.tests.aop;
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.Node;
10 import org.jboss.cache.aop.MarshalledTreeCache;
11 import org.jboss.cache.tests.basic.TreeCacheFunctionalTest;
12 import org.jboss.cache.lock.IsolationLevel;
13 import org.jboss.cache.lock.TimeoutException;
14 import org.jboss.cache.transaction.DummyTransactionManager;
15
16 import javax.transaction.NotSupportedException JavaDoc;
17 import javax.transaction.SystemException JavaDoc;
18 import javax.transaction.Transaction JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.io.Serializable JavaDoc;
22
23 /**
24  * Simple functional tests for MarshalledTreeCache
25  * @author Ben Wang
26  * @version $Id: MarshalledTreeCacheTest.java,v 1.2 2005/05/15 22:37:49 bwang Exp $
27  */

28 public class MarshalledTreeCacheTest extends TestCase {
29    MarshalledTreeCache cache1_=null;
30    MarshalledTreeCache cache2_=null;
31    Exception JavaDoc ex_;
32    static final String JavaDoc FQN = "/test";
33    static final String JavaDoc KEY = "test";
34
35
36    protected void setUp() throws Exception JavaDoc {
37       super.setUp();
38       cache1_=new MarshalledTreeCache();
39       initCache(cache1_);
40       cache2_=new MarshalledTreeCache();
41       initCache(cache2_);
42       ex_=null;
43    }
44
45    protected void initCache(MarshalledTreeCache cache) throws Exception JavaDoc
46    {
47       cache.setCacheMode(TreeCache.REPL_SYNC);
48       cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
49       cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
50       cache.createService();
51       cache.startService();
52    }
53
54    protected void tearDown() throws Exception JavaDoc {
55       super.tearDown();
56       stopCache(cache1_);
57       stopCache(cache2_);
58
59       if(ex_ != null)
60          throw ex_;
61    }
62
63    protected void stopCache(MarshalledTreeCache cache) throws Exception JavaDoc
64    {
65       if(cache != null) {
66          cache.remove("/"); // Needed to remove contents so we can re-use
67
cache.stopService();
68          cache.destroyService();
69          cache=null;
70       }
71    }
72
73    public void testNodeId()
74    {
75       assertNotNull("NodeId should not be null ", cache1_.getNodeId());
76       assertNotNull("NodeId should not be null ", cache2_.getNodeId());
77       assertNotSame("NodeId should not be equal ", cache1_.getNodeId(), cache2_.getNodeId());
78    }
79
80    public void testPlainGetPutFromMyself() throws CacheException
81    {
82       Payload pl = new Payload();
83       cache1_.setMarshalling(false); // Turn off marshalling
84
cache1_.marshalledPut(FQN, KEY, pl);
85       Object JavaDoc obj = cache1_.get(FQN, KEY);
86       assertEquals("Payload should be equal ", pl, obj);
87    }
88
89    public void testMarshalledGetPutFromMyself() throws CacheException
90    {
91       Payload pl = new Payload();
92       cache1_.marshalledPut(FQN, KEY, pl);
93       Object JavaDoc obj = cache1_.marshalledGet(FQN, KEY);
94       assertEquals("Payload should be equal ", pl, obj);
95    }
96
97    public void testMarshalledRemoveFromMyself() throws CacheException
98    {
99       Payload pl = new Payload();
100       cache1_.marshalledPut(FQN, KEY, pl);
101       Object JavaDoc obj = cache1_.marshalledGet(FQN, KEY);
102       assertEquals("Payload should be equal ", pl, obj);
103       obj = cache1_.marshalledRemove(FQN, KEY);
104       assertEquals("Payload should be equal after removed ", pl, obj);
105       obj = cache1_.marshalledGet(FQN, KEY);
106       assertNull("Payload should be null ", obj);
107       Node node = cache1_.get(FQN);
108       Map JavaDoc map = node.getData();
109       assertEquals("Data should be empty ", 0, map.size());
110    }
111
112    public void testMarshalledGetPutDistributed() throws CacheException
113    {
114       Payload pl = new Payload();
115       cache1_.marshalledPut(FQN, KEY, pl);
116       Payload obj = (Payload)cache2_.marshalledGet(FQN, KEY);
117       assertNotSame("Payload should be different ", pl, obj);
118       assertTrue("Payload should be the same ", pl.compare(obj));
119    }
120
121    public void testInvalidation() throws CacheException
122    {
123       Payload pl = new Payload();
124       cache1_.marshalledPut(FQN, KEY, pl);
125       Payload obj = (Payload)cache2_.marshalledGet(FQN, KEY);
126       assertNotSame("Payload instance should be different ", pl, obj);
127       assertTrue("Payload contents should be the same ", pl.compare(obj));
128
129       pl = new Payload("RarefiedGas", 500, "Inert");
130       cache2_.marshalledPut(FQN, KEY, pl);
131       obj = (Payload)cache1_.marshalledGet(FQN, KEY);
132       assertNotSame("Payload should be different ", pl, obj);
133       assertTrue("Payload should be the same ", pl.compare(obj));
134    }
135
136    public static Test suite() {
137       return new TestSuite(MarshalledTreeCacheTest.class);
138    }
139
140    protected void _sleep(long msecs)
141    {
142       try
143       {
144          Thread.sleep(msecs);
145       }
146       catch (Exception JavaDoc ex) {}
147    }
148
149    /**
150     * Test for marshalling.
151     */

152    public static class Payload implements Serializable JavaDoc
153    {
154       String JavaDoc feul_ = "MixturedGas";
155       int weight_ = 1000;
156       String JavaDoc cargoType_ = "Explosive";
157
158       Payload()
159       {
160       }
161
162       Payload(String JavaDoc feul, int weight, String JavaDoc cargoType)
163       {
164          feul_ = feul;
165          weight_ = weight;
166          cargoType_ = cargoType;
167       }
168
169       public boolean compare(Payload pl)
170       {
171          if( feul_.equals(pl.feul_) && (weight_ == pl.weight_) && cargoType_.equals(pl.cargoType_) )
172             return true;
173          else
174             return false;
175       }
176    }
177 }
178
Popular Tags