/*
 * 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.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * Unit test demonstrating usability of marshalling for application redeployment in application server.
 *
 * @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
 */
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 = File.separator;//System.getProperty("file.separator");
   private static final String LIB_DIR_NAME = "lib";
   private static final String LIB_DIR = USER_DIR + FILE_SEPARATOR + LIB_DIR_NAME + FILE_SEPARATOR;
   private static final String LIB_DIR_SP = System.getProperty("lib.dir");//"lib";

   protected void setUp() throws Exception
   {
      TreeCache tc = new TreeCache();
      tc.setCacheMode(TreeCache.LOCAL);
      tc.setUseRegionBasedMarshalling(true);
      tcmb = tc;
   }

   protected void tearDown()
   {
      tcmb.stop();
   }

   public void testClassCastException() throws Exception
   {
      tcmb.startService();

      URLClassLoader ucl1 = createOrphanClassLoader();
      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)
      {
         if (cce.getMessage() != null) assertTrue(cce.getMessage().equals(INSTANCE_CLASS_NAME));
      }
   }

   public void testRegisterUnregister() throws Exception
   {
      tcmb.startService();

      URLClassLoader ucl1 = createOrphanClassLoader();
      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("/");      
   }

   private URLClassLoader createOrphanClassLoader() throws MalformedURLException
   {
      File f;
      if (LIB_DIR_SP == null)
      {
         /* lib.dir system property is null, so we assume this test is being run
         * inside an IDE, where the user dir is the root of JBossCache. We know
         * JGroups lib is located in lib/jgroups.jar */
         f = new File(USER_DIR + FILE_SEPARATOR + LIB_DIR + FILE_SEPARATOR);
      }
      else
      {
         /* lib.dir is set, so we assume that you are running from the build.xml
         * which means that the user dir might be a completely different one. lib.dir
         * system property allows us to know where the lib directory is independently
         * of the user dir*/
         f = new File(LIB_DIR_SP);
      }

      URL context = f.toURL();
      URL jar = new URL(context, INSTANCE_LIBRARY);
      URLClassLoader ucl1 = new URLClassLoader(new URL[]{jar}, null);

      return ucl1;
   }
}
