KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > marshall > RedeploymentEmulationTest


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.marshall;
8
9 import junit.framework.TestCase;
10 import org.jboss.cache.Cache;
11 import org.jboss.cache.CacheImpl;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.Region;
14 import org.jboss.cache.config.Configuration;
15 import org.jgroups.Global;
16
17 import java.io.File JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.net.URLClassLoader JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21
22 /**
23  * Unit test demonstrating usability of marshalling for application redeployment in application server.
24  *
25  * @author <a HREF="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
26  */

27 public class RedeploymentEmulationTest extends TestCase
28 {
29    private Cache cache;
30
31    private static final String JavaDoc INSTANCE_LIBRARY = "jgroups.jar";
32    private static final String JavaDoc INSTANCE_CLASS_NAME = "org.jgroups.Global";
33    private static final String JavaDoc USER_DIR = ".";//System.getProperty("user.dir");
34
private static final String JavaDoc FILE_SEPARATOR = File.separator;//System.getProperty("file.separator");
35
private static final String JavaDoc LIB_DIR_NAME = "lib";
36    private static final String JavaDoc LIB_DIR = USER_DIR + FILE_SEPARATOR + LIB_DIR_NAME + FILE_SEPARATOR;
37    private static final String JavaDoc LIB_DIR_SP = System.getProperty("lib.dir");//"lib";
38

39    protected void setUp() throws Exception JavaDoc
40    {
41       cache = new CacheImpl();
42
43       cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
44       cache.getConfiguration().setUseRegionBasedMarshalling(true);
45    }
46
47    protected void tearDown()
48    {
49       cache.stop();
50    }
51
52    public void testClassCastException() throws Exception JavaDoc
53    {
54       cache.start();
55
56       URLClassLoader JavaDoc ucl1 = createOrphanClassLoader();
57       Thread.currentThread().setContextClassLoader(ucl1);
58
59       Class JavaDoc clazz1 = ucl1.loadClass(INSTANCE_CLASS_NAME);
60       cache.put(fqn("/a"), "key", clazz1.newInstance());
61
62       Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
63       try
64       {
65          Global object = (Global) cache.get(fqn("/a"), "key");
66          fail("Should have produced a ClassCastException");
67       }
68       catch (ClassCastException JavaDoc cce)
69       {
70          assertTrue(cce.getMessage().equals(INSTANCE_CLASS_NAME));
71       }
72    }
73
74    public void testRegisterUnregister() throws Exception JavaDoc
75    {
76       cache.start();
77
78       URLClassLoader JavaDoc ucl1 = createOrphanClassLoader();
79       Thread.currentThread().setContextClassLoader(ucl1);
80
81       Region region = cache.getRegion(fqn("/"), true);
82       region.registerContextClassLoader(Thread.currentThread().getContextClassLoader());
83       region.activate();
84
85       Class JavaDoc clazz1 = ucl1.loadClass(INSTANCE_CLASS_NAME);
86       cache.put(fqn("/a"), "key", clazz1.newInstance());
87
88       region.deactivate();
89       region.unregisterContextClassLoader();
90
91       Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
92
93       region.registerContextClassLoader(Thread.currentThread().getContextClassLoader());
94
95       try
96       {
97          Global object = (Global) cache.get(fqn("/a"), "key");
98          assertNull(object);
99       }
100       catch (ClassCastException JavaDoc cce)
101       {
102          fail("Should not have produced a ClassCastException");
103       }
104
105       region.deactivate();
106       region.unregisterContextClassLoader();
107    }
108
109    private URLClassLoader JavaDoc createOrphanClassLoader() throws MalformedURLException JavaDoc
110    {
111       File JavaDoc f;
112       if (LIB_DIR_SP == null)
113       {
114          /* lib.dir system property is null, so we assume this test is being run
115          * inside an IDE, where the user dir is the root of JBossCache. We know
116          * JGroups lib is located in lib/jgroups.jar */

117          f = new File JavaDoc(USER_DIR + FILE_SEPARATOR + LIB_DIR + FILE_SEPARATOR);
118       }
119       else
120       {
121          /* lib.dir is set, so we assume that you are running from the build.xml
122          * which means that the user dir might be a completely different one. lib.dir
123          * system property allows us to know where the lib directory is independently
124          * of the user dir*/

125          f = new File JavaDoc(LIB_DIR_SP);
126       }
127
128       URL JavaDoc context = f.toURL();
129       URL JavaDoc jar = new URL JavaDoc(context, INSTANCE_LIBRARY);
130       URLClassLoader JavaDoc ucl1 = new URLClassLoader JavaDoc(new URL JavaDoc[]{jar}, null);
131
132       return ucl1;
133    }
134
135    private static Fqn fqn(String JavaDoc fqn)
136    {
137       return Fqn.fromString(fqn);
138    }
139 }
140
Popular Tags