LocalTest.java :  » JBoss » JBossCache » org » jboss » cache » marshall » Java Open Source

Java Open Source » JBoss » JBossCache 
JBossCache » org » jboss » cache » marshall » LocalTest.java
package org.jboss.cache.marshall;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.jboss.cache.CacheImpl;
import org.jboss.cache.DefaultCacheFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.lock.IsolationLevel;
import org.jboss.cache.marshall.data.Debug;

import javax.transaction.Transaction;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * Simple functional tests for LegacyTreeCacheMarshaller
 *
 * @author Ben Wang
 * @version $Id: LocalTest.java,v 1.15 2007/02/08 14:52:41 genman Exp $
 */
public class LocalTest extends RegionBasedMarshallingTestBase
{
   CacheImpl cache = null;
   Transaction tx = null;
   final Fqn FQN = Fqn.fromString("/myNode");
   final String KEY = "key";
   final String VALUE = "value";
   Exception ex;


   protected void setUp() throws Exception
   {
      super.setUp();
      cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
      cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
      cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
      cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
      cache.create();
      cache.start();
      ex = null;
   }

   protected void tearDown() throws Exception
   {
      super.tearDown();
      if (cache != null)
      {
         cache.stop();
         cache.destroy();
         cache = null;
      }
      if (ex != null)
      {
         throw ex;
      }
   }

   public void testClassloader() throws Exception
   {
      String jarDir = System.getProperty("test.jar.dir");
      File jar0 = new File(jarDir + "/testMarshall.jar");
      URL[] cp0 = {jar0.toURL()};
      URLClassLoader ucl0 = new URLClassLoader(cp0);
      Thread.currentThread().setContextClassLoader(ucl0);
      Class clasz1 = ucl0.loadClass(PERSON_CLASSNAME);
      StringBuffer buffer = new StringBuffer("Person Info");
      Debug.displayClassInfo(clasz1, buffer, false);
      log(buffer.toString());
      Object ben = clasz1.newInstance();
      Object value;
      try
      {
         {
            Class[] types = {String.class};
            Method setValue = clasz1.getMethod("setName", types);
            Object[] margs = {"Ben"};
            value = setValue.invoke(ben, margs);
         }
         Class[] types = {};
         Object[] margs = {};
         Method getValue = clasz1.getMethod("getLastElementAsString", types);
         value = getValue.invoke(ben, margs);
         buffer.setLength(0);
         buffer.append("main.obj.CodeSource: ");
         Debug.displayClassInfo(value.getClass(), buffer, false);
         log(buffer.toString());
      }
      catch (Exception e)
      {
         e.printStackTrace();
         log("Failed to invoke getLastElementAsString: " + e);
      }

      cache.put("/a/b/c", "ben", ben);
      assertNotNull(cache.get("/a/b/c"));
      assertEquals(cache.get("/a/b/c", "ben"), ben);
      Object obj = cache.get("/a/b/c", "ben");

      Class claszAddr = ucl0.loadClass(ADDRESS_CLASSNAME);
      buffer = new StringBuffer("Address Info");
      Debug.displayClassInfo(claszAddr, buffer, false);
      log(buffer.toString());
      Object addr = claszAddr.newInstance();
      try
      {
         {
            Class[] types = {String.class};
            Method setValue = claszAddr.getMethod("setCity", types);
            Object[] margs = {"SF"};
            value = setValue.invoke(addr, margs);
         }

         {
            Class[] types = {claszAddr};
            Method setValue = clasz1.getMethod("setAddress", types);
            Object[] margs = {addr};
            value = setValue.invoke(obj, margs);
         }

         Class[] types = {};
         Object[] margs = {};
         Method getValue = clasz1.getMethod("getAddress", types);
         value = getValue.invoke(obj, margs);
         buffer.setLength(0);
         buffer.append("main.obj.CodeSource: ");
         Debug.displayClassInfo(value.getClass(), buffer, false);
         log(buffer.toString());
      }
      catch (Exception e)
      {
         e.printStackTrace();
         log("Failed to invoke: " + e);
         throw e;
      }

   }

   void log(String msg)
   {
      System.out.println("-- " + msg);
   }

   public static Test suite()
   {
      return new TestSuite(LocalTest.class);
   }

   //public static void main(String[] args) {
   // junit.textui.TestRunner.run(suite());
   //}

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.