[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/marshall ...
Galder Zamarreno
galder.zamarreno at jboss.com
Fri Dec 1 10:29:54 EST 2006
User: gzamarreno
Date: 06/12/01 10:29:54
Added: tests/functional/org/jboss/cache/marshall
RedeploymentEmulationTest.java
Log:
How to use JBC marshalling to get around redeployment ClassCastExceptions. QA entry added explaining the problem and resolution as well as a unit test emulating redeployment.
Revision Changes Path
1.2 +114 -0 JBossCache/tests/functional/org/jboss/cache/marshall/RedeploymentEmulationTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: RedeploymentEmulationTest.java
===================================================================
RCS file: RedeploymentEmulationTest.java
diff -N RedeploymentEmulationTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ RedeploymentEmulationTest.java 1 Dec 2006 15:29:54 -0000 1.2
@@ -0,0 +1,114 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.cache.marshall;
+
+import junit.framework.TestCase;
+import org.jboss.cache.TreeCache;
+import org.jboss.cache.Cache;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.Region;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.config.Configuration;
+import org.jgroups.Global;
+
+import java.net.URLClassLoader;
+import java.net.URL;
+import java.io.File;
+
+/**
+ * Unit test demonstrating usability of marshalling for application redeployment in application server.
+ *
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
+ */
+public class RedeploymentEmulationTest extends TestCase
+{
+ private Cache cache;
+
+ private static final String INSTANCE_LIBRARY = "jgroups.jar";
+ private static final String INSTANCE_CLASS_NAME = "org.jgroups.Global";
+ private static final String USER_DIR = System.getProperty("user.dir");
+ private static final String FILE_SEPARATOR = System.getProperty("file.separator");
+ private static final String LIB_DIR = "lib";
+
+ protected void setUp() throws Exception
+ {
+ TreeCache tc = new TreeCache();
+
+ cache = tc.getCacheSPI();
+
+ cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
+ cache.getConfiguration().setUseRegionBasedMarshalling(true);
+ }
+
+ public void testClassCastException() throws Exception
+ {
+ cache.start();
+
+ File f = new File(USER_DIR + FILE_SEPARATOR + LIB_DIR + FILE_SEPARATOR);
+ URL context = f.toURL();
+ URL jar= new URL(context, INSTANCE_LIBRARY);
+ URLClassLoader ucl1 = new URLClassLoader(new URL[]{jar}, null);
+ Thread.currentThread().setContextClassLoader(ucl1);
+
+ Class clazz1 = ucl1.loadClass(INSTANCE_CLASS_NAME);
+ cache.put(fqn("/a"), "key", clazz1.newInstance());
+
+ Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
+ try
+ {
+ Global object = (Global)cache.get(fqn("/a"), "key");
+ fail("Should have produced a ClassCastException");
+ }
+ catch(ClassCastException cce)
+ {
+ assertTrue(cce.getMessage().equals(INSTANCE_CLASS_NAME));
+ }
+ }
+
+ public void testRegisterUnregister() throws Exception
+ {
+ cache.start();
+
+ File f = new File(USER_DIR + FILE_SEPARATOR + LIB_DIR + FILE_SEPARATOR);
+ URL context = f.toURL();
+ URL jar= new URL(context, INSTANCE_LIBRARY);
+ URLClassLoader ucl1 = new URLClassLoader(new URL[]{jar}, null);
+ Thread.currentThread().setContextClassLoader(ucl1);
+
+ Region region = cache.getRegion(fqn("/"), true);
+ region.registerContextClassLoader(Thread.currentThread().getContextClassLoader());
+ region.activate();
+
+ Class clazz1 = ucl1.loadClass(INSTANCE_CLASS_NAME);
+ cache.put(fqn("/a"), "key", clazz1.newInstance());
+
+ region.deactivate();
+ region.unregisterContextClassLoader();
+
+ Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
+
+ region.registerContextClassLoader(Thread.currentThread().getContextClassLoader());
+
+ try
+ {
+ Global object = (Global)cache.get(fqn("/a"), "key");
+ assertNull(object);
+ }
+ catch(ClassCastException cce)
+ {
+ fail("Should not have produced a ClassCastException");
+ }
+
+ region.deactivate();
+ region.unregisterContextClassLoader();
+ }
+
+ private static Fqn fqn(String fqn)
+ {
+ return Fqn.fromString(fqn);
+ }
+}
More information about the jboss-cvs-commits
mailing list