1 |
| package org.jboss.cache.marshall; |
2 |
| |
3 |
| import junit.framework.TestCase; |
4 |
| import org.jboss.cache.Cache; |
5 |
| import org.jboss.cache.DefaultCacheFactory; |
6 |
| import org.jboss.cache.Fqn; |
7 |
| import org.jboss.cache.Region; |
8 |
| import org.jboss.cache.config.Configuration; |
9 |
| import org.jboss.cache.lock.IsolationLevel; |
10 |
| |
11 |
| import java.util.List; |
12 |
| import java.util.Map; |
13 |
| import java.util.Set; |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class CustomCollectionTest extends TestCase |
22 |
| { |
23 |
| private Cache cache1 =null; |
24 |
| private Cache cache2 =null; |
25 |
| private String myListClass = MyList.class.getName(); |
26 |
| private String mySetClass = MySet.class.getName(); |
27 |
| private String myMapClass = MyMap.class.getName(); |
28 |
| |
29 |
7
| protected void setUp() throws Exception
|
30 |
| { |
31 |
7
| super.setUp();
|
32 |
| |
33 |
7
| cache1 = createCache();
|
34 |
7
| cache2 = createCache();
|
35 |
| } |
36 |
| |
37 |
7
| protected void tearDown() throws Exception
|
38 |
| { |
39 |
7
| cache1.stop();
|
40 |
7
| cache2.stop();
|
41 |
| } |
42 |
| |
43 |
1
| public void testMap() throws Exception
|
44 |
| { |
45 |
1
| doMapTest(false);
|
46 |
| } |
47 |
| |
48 |
1
| public void testMapWithRegions() throws Exception
|
49 |
| { |
50 |
1
| doMapTest(true);
|
51 |
| } |
52 |
| |
53 |
1
| public void testSet() throws Exception
|
54 |
| { |
55 |
1
| doSetTest(false);
|
56 |
| } |
57 |
| |
58 |
1
| public void testSetWithRegions() throws Exception
|
59 |
| { |
60 |
1
| doSetTest(true);
|
61 |
| } |
62 |
| |
63 |
1
| public void testList() throws Exception
|
64 |
| { |
65 |
1
| doListTest(false);
|
66 |
| } |
67 |
| |
68 |
1
| public void testListWithRegions() throws Exception
|
69 |
| { |
70 |
1
| doListTest(true);
|
71 |
| } |
72 |
| |
73 |
2
| private void doMapTest(boolean contextClassLoader) throws Exception
|
74 |
| { |
75 |
| |
76 |
2
| ClassLoader customClassLoader = getClassLoader();
|
77 |
2
| Class mapClass = customClassLoader.loadClass(myMapClass);
|
78 |
2
| Map map = (Map) (contextClassLoader ? mapClass.newInstance() : new MyMap());
|
79 |
| |
80 |
2
| map.put("k", "v");
|
81 |
| |
82 |
2
| if (contextClassLoader)
|
83 |
| { |
84 |
1
| enableRegionBasedClassLoading(customClassLoader);
|
85 |
| } |
86 |
| |
87 |
2
| cache1.start();
|
88 |
2
| cache2.start();
|
89 |
| |
90 |
2
| cache1.put(fqn("/a"), "key", map);
|
91 |
2
| Object o = cache2.get(fqn("/a"), "key");
|
92 |
2
| assertTrue(o instanceof Map);
|
93 |
2
| if (contextClassLoader)
|
94 |
| { |
95 |
1
| assertNotSame(MyMap.class, o.getClass());
|
96 |
1
| assertSame(mapClass, o.getClass());
|
97 |
| } |
98 |
| else |
99 |
| { |
100 |
1
| assertSame(MyMap.class, o.getClass());
|
101 |
1
| assertNotSame(mapClass, o.getClass());
|
102 |
| } |
103 |
2
| assertEquals("v", ((Map) o).get("k"));
|
104 |
| } |
105 |
| |
106 |
2
| private void doSetTest(boolean contextClassLoader) throws Exception
|
107 |
| { |
108 |
| |
109 |
2
| ClassLoader customClassLoader = getClassLoader();
|
110 |
2
| Class setClass = customClassLoader.loadClass(mySetClass);
|
111 |
2
| Set set = (Set) (contextClassLoader ? setClass.newInstance() : new MySet());
|
112 |
| |
113 |
2
| set.add("k");
|
114 |
| |
115 |
2
| if (contextClassLoader)
|
116 |
| { |
117 |
1
| enableRegionBasedClassLoading(customClassLoader);
|
118 |
| } |
119 |
| |
120 |
2
| cache1.start();
|
121 |
2
| cache2.start();
|
122 |
| |
123 |
2
| cache1.put(fqn("/a"), "key", set);
|
124 |
2
| Object o = cache2.get(fqn("/a"), "key");
|
125 |
2
| assertTrue(o instanceof Set);
|
126 |
2
| if (contextClassLoader)
|
127 |
| { |
128 |
1
| assertNotSame(MySet.class, o.getClass());
|
129 |
1
| assertSame(setClass, o.getClass());
|
130 |
| } |
131 |
| else |
132 |
| { |
133 |
1
| assertSame(MySet.class, o.getClass());
|
134 |
1
| assertNotSame(setClass, o.getClass());
|
135 |
| } |
136 |
2
| assertTrue(((Set) o).contains("k"));
|
137 |
| } |
138 |
| |
139 |
2
| private void doListTest(boolean contextClassLoader) throws Exception
|
140 |
| { |
141 |
| |
142 |
2
| ClassLoader customClassLoader = getClassLoader();
|
143 |
2
| Class listClass = customClassLoader.loadClass(myListClass);
|
144 |
2
| List list = (List) (contextClassLoader ? listClass.newInstance() : new MyList());
|
145 |
| |
146 |
2
| list.add("k");
|
147 |
| |
148 |
2
| if (contextClassLoader)
|
149 |
| { |
150 |
1
| enableRegionBasedClassLoading(customClassLoader);
|
151 |
| } |
152 |
| |
153 |
2
| cache1.start();
|
154 |
2
| cache2.start();
|
155 |
| |
156 |
2
| cache1.put(fqn("/a"), "key", list);
|
157 |
2
| Object o = cache2.get(fqn("/a"), "key");
|
158 |
2
| assertTrue(o instanceof List);
|
159 |
2
| if (contextClassLoader)
|
160 |
| { |
161 |
1
| assertSame(listClass, o.getClass());
|
162 |
1
| assertNotSame(MyList.class, o.getClass());
|
163 |
| } |
164 |
| else |
165 |
| { |
166 |
1
| assertSame(MyList.class, o.getClass());
|
167 |
1
| assertNotSame(listClass, o.getClass());
|
168 |
| } |
169 |
2
| assertTrue(((List) o).contains("k"));
|
170 |
| } |
171 |
| |
172 |
| |
173 |
1
| public void testHarnessClassLoader() throws Exception
|
174 |
| { |
175 |
1
| Class myListFromCustomLoader = getClassLoader().loadClass(myListClass);
|
176 |
1
| assertNotNull(myListFromCustomLoader);
|
177 |
1
| assertFalse(MyList.class.equals(myListFromCustomLoader));
|
178 |
| |
179 |
1
| Object customLoaderMyList = myListFromCustomLoader.newInstance();
|
180 |
1
| try
|
181 |
| { |
182 |
1
| MyList m = (MyList) customLoaderMyList;
|
183 |
0
| fail("Should have barfed");
|
184 |
| } |
185 |
| catch (ClassCastException cce) |
186 |
| { |
187 |
| |
188 |
| } |
189 |
| } |
190 |
| |
191 |
3
| private void enableRegionBasedClassLoading(ClassLoader customClassLoader) throws Exception
|
192 |
| { |
193 |
3
| cache1.getConfiguration().setUseRegionBasedMarshalling(true);
|
194 |
3
| Region region1 = cache1.getRegion(fqn("/a"), true);
|
195 |
3
| region1.registerContextClassLoader(customClassLoader);
|
196 |
| |
197 |
| |
198 |
| |
199 |
3
| cache2.getConfiguration().setUseRegionBasedMarshalling(true);
|
200 |
3
| Region region2 = cache2.getRegion(fqn("/a"), true);
|
201 |
3
| region2.registerContextClassLoader(customClassLoader);
|
202 |
| |
203 |
| |
204 |
| |
205 |
| } |
206 |
| |
207 |
7
| private ClassLoader getClassLoader() throws Exception
|
208 |
| { |
209 |
7
| String[] includesClasses = { myListClass, mySetClass, myMapClass };
|
210 |
7
| String [] excludesClasses = {};
|
211 |
7
| ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
212 |
7
| return new SelectedClassnameClassLoader(includesClasses, excludesClasses, cl);
|
213 |
| } |
214 |
| |
215 |
14
| private Cache createCache()
|
216 |
| { |
217 |
14
| Cache cache = DefaultCacheFactory.getInstance().createCache(false);
|
218 |
14
| cache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
219 |
14
| cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
220 |
14
| cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
|
221 |
14
| return cache;
|
222 |
| } |
223 |
| |
224 |
18
| private static Fqn fqn(String fqn)
|
225 |
| { |
226 |
18
| return Fqn.fromString(fqn);
|
227 |
| } |
228 |
| } |