KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > replicated > ReplicationExceptionTest


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

8 package org.jboss.cache.replicated;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.jboss.cache.CacheImpl;
16 import org.jboss.cache.config.Configuration;
17 import org.jboss.cache.lock.IsolationLevel;
18 import org.jboss.cache.transaction.DummyTransactionManager;
19
20 import javax.naming.Context JavaDoc;
21 import javax.transaction.NotSupportedException JavaDoc;
22 import javax.transaction.RollbackException JavaDoc;
23 import javax.transaction.SystemException JavaDoc;
24 import javax.transaction.Transaction JavaDoc;
25 import java.io.NotSerializableException JavaDoc;
26 import java.io.Serializable JavaDoc;
27
28 /**
29  * Teting of replication exception for a Nonerislizable object
30  *
31  * @author Ben Wang
32  * @version $Revision: 1.5 $
33  */

34 public class ReplicationExceptionTest extends TestCase
35 {
36    CacheImpl cache1, cache2;
37    Configuration.CacheMode caching_mode = Configuration.CacheMode.REPL_SYNC;
38    final String JavaDoc group_name = "TreeCacheTestGroup";
39    String JavaDoc props =
40            "UDP(ip_mcast=true;ip_ttl=64;loopback=false;mcast_addr=228.1.2.3;" +
41                    "mcast_port=45566;mcast_recv_buf_size=80000;mcast_send_buf_size=150000;" +
42                    "ucast_recv_buf_size=80000;ucast_send_buf_size=150000):" +
43                    "PING(down_thread=true;num_initial_members=2;timeout=500;up_thread=true):" +
44                    "MERGE2(max_interval=20000;min_interval=10000):" +
45                    "FD(down_thread=true;shun=true;up_thread=true):" +
46                    "VERIFY_SUSPECT(down_thread=true;timeout=1500;up_thread=true):" +
47                    "pbcast.NAKACK(down_thread=true;gc_lag=50;retransmit_timeout=600,1200,2400,4800;" +
48                    "up_thread=true):" +
49                    "pbcast.STABLE(desired_avg_gossip=20000;down_thread=true;up_thread=true):" +
50                    "UNICAST(down_thread=true;min_threshold=10;timeout=600,1200,2400;window_size=100):" +
51                    "FRAG(down_thread=true;frag_size=8192;up_thread=true):" +
52                    "pbcast.GMS(join_retry_timeout=2000;join_timeout=5000;print_local_addr=true;shun=true):" +
53                    "pbcast.STATE_TRANSFER(down_thread=true;up_thread=true)";
54
55    final static Log log_ = LogFactory.getLog(ReplicationExceptionTest.class);
56    String JavaDoc old_factory = null;
57    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
58    DummyTransactionManager tx_mgr;
59    Throwable JavaDoc t1_ex, t2_ex, ex = null;
60
61    public ReplicationExceptionTest(String JavaDoc name)
62    {
63       super(name);
64    }
65
66    public void setUp() throws Exception JavaDoc
67    {
68       super.setUp();
69       old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
70       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
71       tx_mgr = DummyTransactionManager.getInstance();
72       t1_ex = t2_ex = ex = null;
73    }
74
75    public void tearDown() throws Exception JavaDoc
76    {
77       super.tearDown();
78       DummyTransactionManager.destroy();
79       destroyCaches();
80       if (old_factory != null)
81       {
82          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
83          old_factory = null;
84       }
85    }
86
87    Transaction JavaDoc beginTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc
88    {
89       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
90       mgr.begin();
91       return mgr.getTransaction();
92    }
93
94    void initCaches(Configuration.CacheMode caching_mode) throws Exception JavaDoc
95    {
96       this.caching_mode = caching_mode;
97       cache1 = new CacheImpl();
98       cache2 = new CacheImpl();
99       cache1.getConfiguration().setCacheMode(caching_mode);
100       cache2.getConfiguration().setCacheMode(caching_mode);
101       cache1.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
102       cache2.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
103
104       cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
105       cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
106       /*
107       cache1.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
108       cache2.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
109 */

110       cache1.getConfiguration().setLockAcquisitionTimeout(5000);
111       cache2.getConfiguration().setLockAcquisitionTimeout(5000);
112       cache1.start();
113       cache2.start();
114    }
115
116    void destroyCaches() throws Exception JavaDoc
117    {
118       if (cache1 != null)
119          cache1.stop();
120       if (cache2 != null)
121          cache2.stop();
122       cache1 = null;
123       cache2 = null;
124    }
125
126    public void testNonSerializableRepl() throws Exception JavaDoc
127    {
128       try
129       {
130          initCaches(Configuration.CacheMode.REPL_SYNC);
131
132          cache1.put("/a/b/c", "test", new ContainerData());
133
134          // We should not come here.
135
assertNotNull("NonSerializableData should not be null on cache2", cache2.get("/a/b/c", "test"));
136       }
137       catch (RuntimeException JavaDoc runtime)
138       {
139          Throwable JavaDoc t = runtime.getCause();
140          if (t instanceof NotSerializableException JavaDoc)
141          {
142             System.out.println("received NotSerializableException - as expected");
143          }
144          else
145          {
146             fail("should have received NotSerializableException, but received " + t.getClass());
147          }
148       }
149       catch (Exception JavaDoc exc)
150       {
151          fail("failure - we should not get here: " + exc);
152       }
153    }
154
155    public void testNonSerizlableReplWithTx() throws Exception JavaDoc
156    {
157       Transaction JavaDoc tx;
158
159       try
160       {
161          initCaches(Configuration.CacheMode.REPL_SYNC);
162
163          tx = beginTransaction();
164          cache1.put("/a/b/c", "test", new ContainerData());
165          tx.commit();
166
167          // We should not come here.
168
assertNotNull("NonSerializableData should not be null on cache2", cache2.get("/a/b/c", "test"));
169       }
170       catch (RollbackException JavaDoc rollback)
171       {
172          System.out.println("received RollbackException - as expected");
173       }
174       catch (Exception JavaDoc e)
175       {
176          // We should also examine that it is indeed throwing a NonSerilaizable exception.
177
fail(e.toString());
178       }
179    }
180
181    public static Test suite() throws Exception JavaDoc
182    {
183       return new TestSuite(ReplicationExceptionTest.class);
184    }
185
186
187    static class NonSerializabeData
188    {
189       int i;
190    }
191
192    static class ContainerData implements Serializable JavaDoc
193    {
194       int i;
195       NonSerializabeData non_serializable_data;
196       private static final long serialVersionUID = -8322197791060897247L;
197
198       public ContainerData()
199       {
200          i = 99;
201          non_serializable_data = new NonSerializabeData();
202       }
203    }
204 }
205
Popular Tags