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