[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/marshall ...

Brian Stansberry brian.stansberry at jboss.com
Tue Dec 5 18:07:25 EST 2006


  User: bstansberry
  Date: 06/12/05 18:07:25

  Modified:    tests/functional/org/jboss/cache/marshall     
                        SyncReplTest.java AsyncReplTest.java
  Added:       tests/functional/org/jboss/cache/marshall     
                        FooClassLoader.java Foo.notjava Foo.clazz
  Log:
  Port JBCACHE-874 tests from 1.4.0 branch
  
  Revision  Changes    Path
  1.12      +60 -10    JBossCache/tests/functional/org/jboss/cache/marshall/SyncReplTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: SyncReplTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/marshall/SyncReplTest.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -b -r1.11 -r1.12
  --- SyncReplTest.java	13 Sep 2006 11:42:42 -0000	1.11
  +++ SyncReplTest.java	5 Dec 2006 23:07:25 -0000	1.12
  @@ -11,6 +11,8 @@
   
   import junit.framework.Test;
   import junit.framework.TestSuite;
  +
  +import org.jboss.cache.Fqn;
   import org.jboss.cache.TreeCache;
   import org.jboss.cache.config.Configuration;
   import org.jboss.cache.factories.XmlConfigurationParser;
  @@ -29,7 +31,7 @@
    * Test case for marshalling using Sync mode.
    *
    * @author Ben Wang
  - * @version $Revision: 1.11 $
  + * @version $Revision: 1.12 $
    */
   public class SyncReplTest extends RegionBasedMarshallingTestBase
   {
  @@ -215,17 +217,33 @@
      {
         ClassLoader cl = getClassLoader();
         cache1.registerClassLoader("/aop", cl);
  +      // Create an empty Person loaded by this classloader
  +      Object scopedBen1 = getPersonFromClassloader(cl);
  +      
         cl = getClassLoader();
         cache2.registerClassLoader("/aop", cl);
  +      // Create another empty Person loaded by this classloader
  +      Object scopedBen2 = getPersonFromClassloader(cl);
   
         cache1.put("/aop/1", "person", ben_);
  -      cache1.put("/aop/1", "person", ben_);
  +      cache1.put("/aop/2", "person", scopedBen1);
   
  -      Object ben2;
  +      Object ben2 = null;
  +      try
  +      {
         // Can't cast it to Person. CCE will resutl.
         ben2 = cache2.get("/aop/1", "person");
         assertEquals(ben_.toString(), ben2.toString());
   
  +         ben2 = cache2.get("/aop/2", "person");
  +         assertFalse("cache2 deserialized with scoped classloader", ben2 instanceof Person);
  +         assertFalse("cache2 deserialized with cache2 classloader", scopedBen1.equals(ben2));
  +         assertEquals("scopedBen deserialized properly", scopedBen2, ben2);
  +      } catch (Exception ex)
  +      {
  +         fail("Test fails with exception " +ex);
  +      }
  +
      }
   
      public void testMethodCall() throws Exception
  @@ -334,13 +352,45 @@
         // Need to test out if app is not registered with beforehand??
      }
   
  +   public void testCustomFqn() throws Exception
  +   {
  +      FooClassLoader cl1 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
  +      cache1.registerClassLoader("/aop", cl1);
  +      FooClassLoader cl2 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
  +      cache2.registerClassLoader("/aop", cl2); 
  +      
  +      Class clazz = cl1.loadFoo();
  +      Object custom1 = clazz.newInstance();
  +
  +      clazz = cl2.loadFoo();
  +      Object custom2 = clazz.newInstance();
  +      
  +      Fqn base = Fqn.fromString("/aop");
  +      cache1.put(new Fqn(base, custom1), "key", "value");
  +
  +      try
  +      {
  +         Object val = cache2.get(new Fqn(base, custom2), "key");
  +         assertEquals("value", val);
  +      } catch (Exception ex)
  +      {
  +         fail("Test fails with exception " +ex);
  +      }      
  +   }
  +
      Transaction beginTransaction() throws SystemException, NotSupportedException
      {
  -      DummyTransactionManager mgr = DummyTransactionManager.getInstance();
  +      DummyTransactionManager mgr=DummyTransactionManager.getInstance();
         mgr.begin();
         return mgr.getTransaction();
      }
   
  +   protected Object getPersonFromClassloader(ClassLoader cl) throws Exception
  +   {
  +      Class clazz = cl.loadClass("org.jboss.cache.marshall.Person");
  +      return clazz.newInstance();
  +   }
  +
      void log(String msg)
      {
         System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
  
  
  
  1.12      +52 -15    JBossCache/tests/functional/org/jboss/cache/marshall/AsyncReplTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: AsyncReplTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/marshall/AsyncReplTest.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -b -r1.11 -r1.12
  --- AsyncReplTest.java	6 Sep 2006 15:30:58 -0000	1.11
  +++ AsyncReplTest.java	5 Dec 2006 23:07:25 -0000	1.12
  @@ -12,6 +12,7 @@
   import junit.framework.Test;
   import junit.framework.TestSuite;
   import org.apache.commons.logging.LogFactory;
  +import org.jboss.cache.Fqn;
   import org.jboss.cache.TreeCache;
   import org.jboss.cache.factories.XmlConfigurationParser;
   import org.jboss.cache.marshall.data.Address;
  @@ -28,7 +29,7 @@
    * Test marshalling for async mode.
    *
    * @author Ben Wang
  - * @version $Revision: 1.11 $
  + * @version $Revision: 1.12 $
    */
   public class AsyncReplTest extends RegionBasedMarshallingTestBase
   {
  @@ -156,21 +157,30 @@
      {
         ClassLoader cl = getClassLoader();
         cache1.registerClassLoader("/aop", cl);
  +      // Create an empty Person loaded by this classloader
  +      Object scopedBen1 = getPersonFromClassloader(cl);
  +      
         cl = getClassLoader();
         cache2.registerClassLoader("/aop", cl);
  +      // Create another empty Person loaded by this classloader
  +      Object scopedBen2 = getPersonFromClassloader(cl);
   
         cache1.put("/aop/1", "person", ben_);
  -      cache1.put("/aop/1", "person", ben_);
  +      cache1.put("/aop/2", "person", scopedBen1);
         TestingUtil.sleepThread(1000);
   
  -      Object ben2;
  +      Object ben2 = null;
         try
         {
            // Can't cast it to Person. CCE will resutl.
            ben2 = cache2.get("/aop/1", "person");
            assertEquals(ben_.toString(), ben2.toString());
  -      }
  -      catch (Exception ex)
  +         
  +         ben2 = cache2.get("/aop/2", "person");
  +         assertFalse("cache2 deserialized with scoped classloader", ben2 instanceof Person);
  +         assertFalse("cache2 deserialized with cache2 classloader", scopedBen1.equals(ben2));
  +         assertEquals("scopedBen deserialized properly", scopedBen2, ben2);
  +      } catch (Exception ex)
         {
            fail("Test fails with exception " + ex);
         }
  @@ -251,19 +261,46 @@
         // Need to test out if app is not registered with beforehand??
      }
   
  +   public void testCustomFqn() throws Exception
  +   {
  +      // FIXME work via the Cache API
  +      
  +      FooClassLoader cl1 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
  +      cache1.registerClassLoader("/aop", cl1);
  +      FooClassLoader cl2 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
  +      cache2.registerClassLoader("/aop", cl2); 
  +      
  +      Class clazz = cl1.loadFoo();
  +      Object custom1 = clazz.newInstance();
  +
  +      clazz = cl2.loadFoo();
  +      Object custom2 = clazz.newInstance();
  +      
  +      Fqn base = Fqn.fromString("/aop");
  +      cache1.put(new Fqn(base, custom1), "key", "value");
  +      TestingUtil.sleepThread(1000);
  +
  +      try
  +      {
  +         Object val = cache2.get(new Fqn(base, custom2), "key");
  +         assertEquals("value", val);
  +      } catch (Exception ex)
  +      {
  +         fail("Test fails with exception " +ex);
  +      }      
  +   }
  +
      Transaction beginTransaction() throws SystemException, NotSupportedException
      {
  -      DummyTransactionManager mgr = DummyTransactionManager.getInstance();
  +      DummyTransactionManager mgr=DummyTransactionManager.getInstance();
         mgr.begin();
         return mgr.getTransaction();
      }
   
  -   protected ClassLoader getClassLoader() throws Exception
  +   protected Object getPersonFromClassloader(ClassLoader cl) throws Exception
      {
  -      String[] includesClasses = {PERSON_CLASSNAME, ADDRESS_CLASSNAME};
  -      String [] excludesClasses = {};
  -      ClassLoader cl = Thread.currentThread().getContextClassLoader();
  -      return new SelectedClassnameClassLoader(includesClasses, excludesClasses, cl);
  +      Class clazz = cl.loadClass(PERSON_CLASSNAME);
  +      return clazz.newInstance();
      }
   
      void log(String msg)
  
  
  
  1.2       +39 -0     JBossCache/tests/functional/org/jboss/cache/marshall/FooClassLoader.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: FooClassLoader.java
  ===================================================================
  RCS file: FooClassLoader.java
  diff -N FooClassLoader.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ FooClassLoader.java	5 Dec 2006 23:07:25 -0000	1.2
  @@ -0,0 +1,39 @@
  +package org.jboss.cache.marshall;
  +
  +import java.io.ByteArrayOutputStream;
  +import java.io.IOException;
  +import java.io.InputStream;
  +
  +public class FooClassLoader extends ClassLoader
  +{
  +   private Class foo;
  +   public FooClassLoader(ClassLoader parent)
  +   {
  +      super(parent);
  +   }
  +   
  +   public Class loadFoo() throws ClassNotFoundException
  +   {
  +      if (foo == null)
  +      {
  +         try
  +         {
  +            InputStream is = getResourceAsStream("org/jboss/cache/marshall/Foo.clazz");
  +            byte[] bytes = new byte[1024];                
  +            ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
  +            int read;
  +            while ((read = is.read(bytes)) > -1) {
  +                baos.write(bytes, 0, read);
  +            }
  +            bytes = baos.toByteArray();
  +            foo = this.defineClass("org.jboss.cache.marshall.Foo", bytes, 0, bytes.length);
  +         }
  +         catch (IOException e)
  +         {
  +            throw new ClassNotFoundException("cannot read org/jboss/cache/marshall/Foo.clazz", e);
  +         }
  +      }
  +      return foo;
  +   }
  +
  +}
  
  
  
  1.2       +37 -0     JBossCache/tests/functional/org/jboss/cache/marshall/Foo.notjava
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Foo.notjava
  ===================================================================
  RCS file: Foo.notjava
  diff -N Foo.notjava
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ Foo.notjava	5 Dec 2006 23:07:25 -0000	1.2
  @@ -0,0 +1,37 @@
  +package org.jboss.cache.marshall;
  +
  +import java.io.Serializable;
  +import java.security.SecureRandom;
  +import java.util.Random;
  +
  +/**
  + * This is the java code used to create the Foo.clazz file. File deliberately
  + * doesn't end in .java, as we don't want a Foo.class on the classpath,
  + * only the Foo.clazz file that FooClassLoader can load.
  + * 
  + * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
  + * @version $Revision: 1.2 $
  + */
  +public class Foo implements Serializable
  +{
  +   /** The serialVersionUID */
  +   private static final long serialVersionUID = 1L;
  +
  +   public boolean equals(Object obj)
  +   {      
  +      return obj instanceof Foo;
  +   }
  +
  +   public int hashCode()
  +   {
  +      return 1;
  +   }   
  +
  +   public String toString()
  +   {
  +      Random random = new SecureRandom();
  +      StringBuffer sb=new StringBuffer("org.jboss.cache.marshall.Foo[random=");
  +      sb.append(random.nextInt()).append("]");
  +      return sb.toString();
  +   }
  +}
  
  
  
  1.2       +10 -0     JBossCache/tests/functional/org/jboss/cache/marshall/Foo.clazz
  
  	<<Binary file>>
  
  



More information about the jboss-cvs-commits mailing list