Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 140   Methods: 6
NCLOC: 98   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RedeploymentEmulationTest.java 50% 92.7% 100% 91.8%
coverage coverage
 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.DefaultCacheFactory;
 13    import org.jboss.cache.Fqn;
 14    import org.jboss.cache.Region;
 15    import org.jboss.cache.config.Configuration;
 16    import org.jgroups.Global;
 17   
 18    import java.io.File;
 19    import java.net.MalformedURLException;
 20    import java.net.URL;
 21    import java.net.URLClassLoader;
 22   
 23    /**
 24    * Unit test demonstrating usability of marshalling for application redeployment in application server.
 25    *
 26    * @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
 27    */
 28    public class RedeploymentEmulationTest extends TestCase
 29    {
 30    private Cache cache;
 31   
 32    private static final String INSTANCE_LIBRARY = "jgroups.jar";
 33    private static final String INSTANCE_CLASS_NAME = "org.jgroups.Global";
 34    private static final String USER_DIR = ".";//System.getProperty("user.dir");
 35    private static final String FILE_SEPARATOR = File.separator;//System.getProperty("file.separator");
 36    private static final String LIB_DIR_NAME = "lib";
 37    private static final String LIB_DIR = USER_DIR + FILE_SEPARATOR + LIB_DIR_NAME + FILE_SEPARATOR;
 38    private static final String LIB_DIR_SP = System.getProperty("lib.dir");//"lib";
 39   
 40  2 protected void setUp() throws Exception
 41    {
 42  2 cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 43   
 44  2 cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
 45  2 cache.getConfiguration().setUseRegionBasedMarshalling(true);
 46    }
 47   
 48  2 protected void tearDown()
 49    {
 50  2 cache.stop();
 51    }
 52   
 53  1 public void testClassCastException() throws Exception
 54    {
 55  1 cache.start();
 56   
 57  1 URLClassLoader ucl1 = createOrphanClassLoader();
 58  1 Thread.currentThread().setContextClassLoader(ucl1);
 59   
 60  1 Class clazz1 = ucl1.loadClass(INSTANCE_CLASS_NAME);
 61  1 cache.put(fqn("/a"), "key", clazz1.newInstance());
 62   
 63  1 Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
 64  1 try
 65    {
 66  1 Global object = (Global) cache.get(fqn("/a"), "key");
 67  0 fail("Should have produced a ClassCastException");
 68    }
 69    catch (ClassCastException cce)
 70    {
 71  1 assertTrue(cce.getMessage().equals(INSTANCE_CLASS_NAME));
 72    }
 73    }
 74   
 75  1 public void testRegisterUnregister() throws Exception
 76    {
 77  1 cache.start();
 78   
 79  1 URLClassLoader ucl1 = createOrphanClassLoader();
 80  1 Thread.currentThread().setContextClassLoader(ucl1);
 81   
 82  1 Region region = cache.getRegion(fqn("/"), true);
 83  1 region.registerContextClassLoader(Thread.currentThread().getContextClassLoader());
 84  1 region.activate();
 85   
 86  1 Class clazz1 = ucl1.loadClass(INSTANCE_CLASS_NAME);
 87  1 cache.put(fqn("/a"), "key", clazz1.newInstance());
 88   
 89  1 region.deactivate();
 90  1 region.unregisterContextClassLoader();
 91   
 92  1 Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
 93   
 94  1 region.registerContextClassLoader(Thread.currentThread().getContextClassLoader());
 95   
 96  1 try
 97    {
 98  1 Global object = (Global) cache.get(fqn("/a"), "key");
 99  1 assertNull(object);
 100    }
 101    catch (ClassCastException cce)
 102    {
 103  0 fail("Should not have produced a ClassCastException");
 104    }
 105   
 106  1 region.deactivate();
 107  1 region.unregisterContextClassLoader();
 108    }
 109   
 110  2 private URLClassLoader createOrphanClassLoader() throws MalformedURLException
 111    {
 112  2 File f;
 113  2 if (LIB_DIR_SP == null)
 114    {
 115    /* lib.dir system property is null, so we assume this test is being run
 116    * inside an IDE, where the user dir is the root of JBossCache. We know
 117    * JGroups lib is located in lib/jgroups.jar */
 118  0 f = new File(USER_DIR + FILE_SEPARATOR + LIB_DIR + FILE_SEPARATOR);
 119    }
 120    else
 121    {
 122    /* lib.dir is set, so we assume that you are running from the build.xml
 123    * which means that the user dir might be a completely different one. lib.dir
 124    * system property allows us to know where the lib directory is independently
 125    * of the user dir*/
 126  2 f = new File(LIB_DIR_SP);
 127    }
 128   
 129  2 URL context = f.toURL();
 130  2 URL jar = new URL(context, INSTANCE_LIBRARY);
 131  2 URLClassLoader ucl1 = new URLClassLoader(new URL[]{jar}, null);
 132   
 133  2 return ucl1;
 134    }
 135   
 136  5 private static Fqn fqn(String fqn)
 137    {
 138  5 return Fqn.fromString(fqn);
 139    }
 140    }