1 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 ; 18 import java.net.URL ; 19 import java.net.URLClassLoader ; 20 import java.net.MalformedURLException ; 21 22 27 public class RedeploymentEmulationTest extends TestCase 28 { 29 private Cache cache; 30 31 private static final String INSTANCE_LIBRARY = "jgroups.jar"; 32 private static final String INSTANCE_CLASS_NAME = "org.jgroups.Global"; 33 private static final String USER_DIR = "."; private static final String FILE_SEPARATOR = File.separator; private static final String LIB_DIR_NAME = "lib"; 36 private static final String LIB_DIR = USER_DIR + FILE_SEPARATOR + LIB_DIR_NAME + FILE_SEPARATOR; 37 private static final String LIB_DIR_SP = System.getProperty("lib.dir"); 39 protected void setUp() throws Exception 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 53 { 54 cache.start(); 55 56 URLClassLoader ucl1 = createOrphanClassLoader(); 57 Thread.currentThread().setContextClassLoader(ucl1); 58 59 Class 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 cce) 69 { 70 assertTrue(cce.getMessage().equals(INSTANCE_CLASS_NAME)); 71 } 72 } 73 74 public void testRegisterUnregister() throws Exception 75 { 76 cache.start(); 77 78 URLClassLoader 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 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 cce) 101 { 102 fail("Should not have produced a ClassCastException"); 103 } 104 105 region.deactivate(); 106 region.unregisterContextClassLoader(); 107 } 108 109 private URLClassLoader createOrphanClassLoader() throws MalformedURLException 110 { 111 File f; 112 if (LIB_DIR_SP == null) 113 { 114 117 f = new File (USER_DIR + FILE_SEPARATOR + LIB_DIR + FILE_SEPARATOR); 118 } 119 else 120 { 121 125 f = new File (LIB_DIR_SP); 126 } 127 128 URL context = f.toURL(); 129 URL jar = new URL (context, INSTANCE_LIBRARY); 130 URLClassLoader ucl1 = new URLClassLoader (new URL []{jar}, null); 131 132 return ucl1; 133 } 134 135 private static Fqn fqn(String fqn) 136 { 137 return Fqn.fromString(fqn); 138 } 139 } 140 | Popular Tags |