KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > memory > ReplicatedTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.cache.pojo.memory;
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.pojo.PojoCache;
16 import org.jboss.cache.pojo.PojoCacheFactory;
17 import org.jboss.cache.pojo.TestingUtil;
18 import org.jboss.cache.pojo.test.Address;
19 import org.jboss.cache.pojo.test.Person;
20 import org.jboss.cache.pojo.test.SerializedAddress;
21 import org.jboss.cache.Fqn;
22
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 /**
27  * @author Ben Wang
28  */

29
30 public class ReplicatedTest extends TestCase
31 {
32    Log log_ = LogFactory.getLog(ReplicatedTest.class);
33    PojoCache cache_;
34    PojoCache cache1_;
35
36    public ReplicatedTest(String JavaDoc name)
37    {
38       super(name);
39    }
40
41    protected void setUp() throws Exception JavaDoc
42    {
43       super.setUp();
44       String JavaDoc configFile = "META-INF/replSync-service.xml";
45       boolean toStart = false;
46       cache_ = PojoCacheFactory.createCache(configFile, toStart);
47       cache_.start();
48       cache1_ = PojoCacheFactory.createCache(configFile, toStart);
49       cache1_.start();
50    }
51
52    protected void tearDown() throws Exception JavaDoc
53    {
54       super.tearDown();
55       cache_.stop();
56       cache1_.stop();
57    }
58
59 // public void testDummy() {}
60

61    /**
62     * Test replication with classloaders.
63     *
64     * @throws Exception
65     */

66    public void testCLLeakageBasic() throws Exception JavaDoc
67    {
68       SerializedAddress add = new SerializedAddress();
69       add.setCity("Taipei");
70
71       ClassLoader JavaDoc cla = getClassLoader();
72       WeakReference JavaDoc refa = new WeakReference JavaDoc(cla);
73       cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(cla);
74       ClassLoader JavaDoc clb = getClassLoader();
75       WeakReference JavaDoc refb = new WeakReference JavaDoc(clb);
76       cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clb);
77
78       Fqn fqn = new Fqn("/aop");
79       cache_.getCache().put(new Fqn("/aop"), "add", add);
80
81       TestingUtil.sleepThread(100);
82       try
83       {
84          Object JavaDoc ben = cache1_.getCache().get(fqn, "add");
85          assertEquals(add.toString(), ben.toString());
86          ben = null;
87       } catch (Exception JavaDoc ex)
88       {
89          fail("Test fails with exception " + ex);
90       }
91
92       cache_.getCache().remove(fqn, "add");
93
94       ClassLoader JavaDoc clc = getClassLoader();
95       cla = null;
96       clb = null;
97       cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc);
98       cache1_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc);
99       System.gc(); // force gc
100
Thread.sleep(1000);
101       assertNull("Classloader should be gced ", refa.get());
102       assertNull("Classloader should be gced ", refb.get());
103    }
104
105    private static void forceOutOfMemoryError() throws Exception JavaDoc
106    {
107       ArrayList JavaDoc list = new ArrayList JavaDoc();
108       try
109       {
110
111          long i = 0;
112          while (true)
113          {
114             list.add("BigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBig" + (i++));
115          }
116       }
117       catch (Throwable JavaDoc ignored)
118       {
119       }
120       list.clear();
121       list = null;
122       System.gc();
123       Thread.sleep(1000);
124    }
125
126    /**
127     * Test replication with classloaders.
128     *
129     * @throws Exception
130     */

131    public void testCLLeakage() throws Exception JavaDoc
132    {
133       Person p = new Person();
134       p.setName("Ben");
135       Address add = new Address();
136       add.setCity("Taipei");
137
138       ClassLoader JavaDoc cla = getClassLoader();
139       WeakReference JavaDoc refa = new WeakReference JavaDoc(cla);
140       cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(cla);
141       ClassLoader JavaDoc clb = getClassLoader();
142       cache1_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clb);
143       WeakReference JavaDoc refb = new WeakReference JavaDoc(clb);
144
145       cache_.attach("/aop", p);
146
147       TestingUtil.sleepThread(100);
148       try
149       {
150          Object JavaDoc ben = cache1_.find("/aop");
151          assertEquals(p.toString(), ben.toString());
152          ben = null;
153       } catch (Exception JavaDoc ex)
154       {
155          fail("Test fails with exception " + ex);
156       }
157
158       cache_.detach("/aop");
159       ClassLoader JavaDoc clc = getClassLoader();
160       cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc);
161       cache1_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc);
162       cla = null;
163       clb = null;
164       forceOutOfMemoryError();
165
166       assertNull("Classloader should be gced ", refa.get());
167       assertNull("Classloader should be gced ", refb.get());
168    }
169
170    protected ClassLoader JavaDoc getClassLoader() throws Exception JavaDoc
171    {
172       String JavaDoc[] includesClasses = {"org.jboss.cache.aop.test.Person",
173               "org.jboss.cache.aop.test.Address"};
174       String JavaDoc [] excludesClasses = {};
175       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
176       return new SelectedClassnameClassLoader(includesClasses, excludesClasses, cl);
177    }
178
179    public static Test suite() throws Exception JavaDoc
180    {
181       return new TestSuite(ReplicatedTest.class);
182    }
183
184
185    public static void main(String JavaDoc[] args) throws Exception JavaDoc
186    {
187       junit.textui.TestRunner.run(ReplicatedTest.suite());
188    }
189
190 }
191
Popular Tags