[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/marshall ...
Galder Zamarreno
galder.zamarreno at jboss.com
Fri Jan 12 10:53:56 EST 2007
User: gzamarreno
Date: 07/01/12 10:53:56
Added: tests/functional/org/jboss/cache/marshall MyMap.java
CustomCollectionTest.java MyList.java MySet.java
Log:
[JBCACHE-936] Fix ported from 1.4.x branch to head
Revision Changes Path
1.2 +7 -0 JBossCache/tests/functional/org/jboss/cache/marshall/MyMap.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: MyMap.java
===================================================================
RCS file: MyMap.java
diff -N MyMap.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MyMap.java 12 Jan 2007 15:53:56 -0000 1.2
@@ -0,0 +1,7 @@
+package org.jboss.cache.marshall;
+
+import java.util.HashMap;
+
+public class MyMap extends HashMap
+{
+}
1.2 +228 -0 JBossCache/tests/functional/org/jboss/cache/marshall/CustomCollectionTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CustomCollectionTest.java
===================================================================
RCS file: CustomCollectionTest.java
diff -N CustomCollectionTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CustomCollectionTest.java 12 Jan 2007 15:53:56 -0000 1.2
@@ -0,0 +1,228 @@
+package org.jboss.cache.marshall;
+
+import junit.framework.TestCase;
+import org.jboss.cache.Cache;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.Region;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.lock.IsolationLevel;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Unit test to cover replication and marshalling of custom collections
+ *
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
+ * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
+ */
+public class CustomCollectionTest extends TestCase
+{
+ private Cache cache1 =null;
+ private Cache cache2 =null;
+ private String myListClass = MyList.class.getName();
+ private String mySetClass = MySet.class.getName();
+ private String myMapClass = MyMap.class.getName();
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ cache1 = createCache();
+ cache2 = createCache();
+ }
+
+ protected void tearDown() throws Exception
+ {
+ cache1.stop();
+ cache2.stop();
+ }
+
+ public void testMap() throws Exception
+ {
+ doMapTest(false);
+ }
+
+ public void testMapWithRegions() throws Exception
+ {
+ doMapTest(true);
+ }
+
+ public void testSet() throws Exception
+ {
+ doSetTest(false);
+ }
+
+ public void testSetWithRegions() throws Exception
+ {
+ doSetTest(true);
+ }
+
+ public void testList() throws Exception
+ {
+ doListTest(false);
+ }
+
+ public void testListWithRegions() throws Exception
+ {
+ doListTest(true);
+ }
+
+ private void doMapTest(boolean contextClassLoader) throws Exception
+ {
+
+ ClassLoader customClassLoader = getClassLoader();
+ Class mapClass = customClassLoader.loadClass(myMapClass);
+ Map map = (Map) (contextClassLoader ? mapClass.newInstance() : new MyMap());
+
+ map.put("k", "v");
+
+ if (contextClassLoader)
+ {
+ enableRegionBasedClassLoading(customClassLoader);
+ }
+
+ cache1.start();
+ cache2.start();
+
+ cache1.put(fqn("/a"), "key", map);
+ Object o = cache2.get(fqn("/a"), "key");
+ assertTrue(o instanceof Map);
+ if (contextClassLoader)
+ {
+ assertNotSame(MyMap.class, o.getClass());
+ assertSame(mapClass, o.getClass());
+ }
+ else
+ {
+ assertSame(MyMap.class, o.getClass());
+ assertNotSame(mapClass, o.getClass());
+ }
+ assertEquals("v", ((Map) o).get("k"));
+ }
+
+ private void doSetTest(boolean contextClassLoader) throws Exception
+ {
+
+ ClassLoader customClassLoader = getClassLoader();
+ Class setClass = customClassLoader.loadClass(mySetClass);
+ Set set = (Set) (contextClassLoader ? setClass.newInstance() : new MySet());
+
+ set.add("k");
+
+ if (contextClassLoader)
+ {
+ enableRegionBasedClassLoading(customClassLoader);
+ }
+
+ cache1.start();
+ cache2.start();
+
+ cache1.put(fqn("/a"), "key", set);
+ Object o = cache2.get(fqn("/a"), "key");
+ assertTrue(o instanceof Set);
+ if (contextClassLoader)
+ {
+ assertNotSame(MySet.class, o.getClass());
+ assertSame(setClass, o.getClass());
+ }
+ else
+ {
+ assertSame(MySet.class, o.getClass());
+ assertNotSame(setClass, o.getClass());
+ }
+ assertTrue(((Set) o).contains("k"));
+ }
+
+ private void doListTest(boolean contextClassLoader) throws Exception
+ {
+
+ ClassLoader customClassLoader = getClassLoader();
+ Class listClass = customClassLoader.loadClass(myListClass);
+ List list = (List) (contextClassLoader ? listClass.newInstance() : new MyList());
+
+ list.add("k");
+
+ if (contextClassLoader)
+ {
+ enableRegionBasedClassLoading(customClassLoader);
+ }
+
+ cache1.start();
+ cache2.start();
+
+ cache1.put(fqn("/a"), "key", list);
+ Object o = cache2.get(fqn("/a"), "key");
+ assertTrue(o instanceof List);
+ if (contextClassLoader)
+ {
+ assertSame(listClass, o.getClass());
+ assertNotSame(MyList.class, o.getClass());
+ }
+ else
+ {
+ assertSame(MyList.class, o.getClass());
+ assertNotSame(listClass, o.getClass());
+ }
+ assertTrue(((List) o).contains("k"));
+ }
+
+
+ public void testHarnessClassLoader() throws Exception
+ {
+ Class myListFromCustomLoader = getClassLoader().loadClass(myListClass);
+ assertNotNull(myListFromCustomLoader);
+ assertFalse(MyList.class.equals(myListFromCustomLoader));
+
+ Object customLoaderMyList = myListFromCustomLoader.newInstance();
+ try
+ {
+ MyList m = (MyList) customLoaderMyList;
+ fail("Should have barfed");
+ }
+ catch (ClassCastException cce)
+ {
+ // expected
+ }
+ }
+
+ private void enableRegionBasedClassLoading(ClassLoader customClassLoader) throws Exception
+ {
+ cache1.getConfiguration().setUseRegionBasedMarshalling(true);
+ Region region1 = cache1.getRegion(fqn("/a"), true);
+ region1.registerContextClassLoader(customClassLoader);
+// cache1.getRegionManager().createRegion("/a", customClassLoader);
+// cache1.getMarshaller().defaultMarshaller.registerClassLoader("/a", customClassLoader);
+// cache1.getMarshaller().defaultMarshaller.useRegionBasedMarshalling = true;
+ cache2.getConfiguration().setUseRegionBasedMarshalling(true);
+ Region region2 = cache2.getRegion(fqn("/a"), true);
+ region2.registerContextClassLoader(customClassLoader);
+// cache2.getRegionManager().createRegion("/a", customClassLoader);
+// cache2.getMarshaller().defaultMarshaller.registerClassLoader("/a", customClassLoader);
+// cache2.getMarshaller().defaultMarshaller.useRegionBasedMarshalling = true;
+ }
+
+ private ClassLoader getClassLoader() throws Exception
+ {
+ String[] includesClasses = { myListClass, mySetClass, myMapClass };
+ String [] excludesClasses = {};
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ return new SelectedClassnameClassLoader(includesClasses, excludesClasses, cl);
+ }
+
+ private Cache createCache()
+ {
+ Cache cache = DefaultCacheFactory.getInstance().createCache(false);
+ cache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
+ cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
+ cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
+ return cache;
+ }
+
+ private static Fqn fqn(String fqn)
+ {
+ return Fqn.fromString(fqn);
+ }
+}
\ No newline at end of file
1.2 +7 -0 JBossCache/tests/functional/org/jboss/cache/marshall/MyList.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: MyList.java
===================================================================
RCS file: MyList.java
diff -N MyList.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MyList.java 12 Jan 2007 15:53:56 -0000 1.2
@@ -0,0 +1,7 @@
+package org.jboss.cache.marshall;
+
+import java.util.ArrayList;
+
+public class MyList extends ArrayList
+{
+}
1.2 +7 -0 JBossCache/tests/functional/org/jboss/cache/marshall/MySet.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: MySet.java
===================================================================
RCS file: MySet.java
diff -N MySet.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MySet.java 12 Jan 2007 15:53:56 -0000 1.2
@@ -0,0 +1,7 @@
+package org.jboss.cache.marshall;
+
+import java.util.HashSet;
+
+public class MySet extends HashSet
+{
+}
More information about the jboss-cvs-commits
mailing list