/* * 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.TreeCacheMBean; 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 Galder Zamarreno */ public class RedeploymentEmulationTest extends TestCase { private TreeCacheMBean tcmb; 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(); tc.setCacheMode(TreeCache.LOCAL); tc.setUseRegionBasedMarshalling(true); tcmb = tc; } public void testClassCastException() throws Exception { tcmb.startService(); 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); tcmb.put("/a", "key", clazz1.newInstance()); Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader()); try { Global object = (Global)tcmb.get("/a", "key"); fail("Should have produced a ClassCastException"); } catch(ClassCastException cce) { assertTrue(cce.getMessage().equals(INSTANCE_CLASS_NAME)); } } public void testRegisterUnregister() throws Exception { tcmb.startService(); 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); tcmb.registerClassLoader("/", Thread.currentThread().getContextClassLoader()); Class clazz1 = ucl1.loadClass(INSTANCE_CLASS_NAME); tcmb.put("/a", "key", clazz1.newInstance()); tcmb.inactivateRegion("/"); tcmb.unregisterClassLoader("/"); Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader()); tcmb.registerClassLoader("/", Thread.currentThread().getContextClassLoader()); try { Global object = (Global)tcmb.get("/a", "key"); assertNull(object); } catch(ClassCastException cce) { fail("Should not have produced a ClassCastException"); } tcmb.inactivateRegion("/"); tcmb.unregisterClassLoader("/"); } }