1 |
| package org.jboss.cache.marshall; |
2 |
| |
3 |
| import junit.framework.TestCase; |
4 |
| import org.jboss.cache.CacheImpl; |
5 |
| import org.jboss.cache.DefaultCacheFactory; |
6 |
| import org.jboss.cache.Fqn; |
7 |
| import org.jboss.cache.Region; |
8 |
| import org.jboss.cache.buddyreplication.GravitateResult; |
9 |
| import org.jboss.cache.config.Configuration; |
10 |
| |
11 |
| import java.util.List; |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| public class ReturnValueMarshallingTest extends TestCase |
20 |
| { |
21 |
| private CacheImpl cache1, cache2; |
22 |
| private Fqn fqn = Fqn.fromString("/a"); |
23 |
| private ClassLoader classLoader; |
24 |
| private Object key = "key", value; |
25 |
| private String className = "org.jboss.cache.marshall.MyList"; |
26 |
| private Class listClass; |
27 |
| |
28 |
2
| protected void setUp() throws Exception
|
29 |
| { |
30 |
2
| cache1 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
31 |
2
| cache1.getConfiguration().setUseRegionBasedMarshalling(true);
|
32 |
2
| cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
33 |
2
| cache1.getConfiguration().setSyncReplTimeout(60000);
|
34 |
2
| cache1.start();
|
35 |
| |
36 |
2
| cache2 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
37 |
2
| cache2.getConfiguration().setUseRegionBasedMarshalling(true);
|
38 |
2
| cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
39 |
2
| cache2.getConfiguration().setSyncReplTimeout(60000);
|
40 |
2
| cache2.start();
|
41 |
| |
42 |
2
| classLoader = getClassLoader();
|
43 |
2
| Region r1 = cache1.getRegion(fqn, true);
|
44 |
2
| r1.setActive(true);
|
45 |
2
| r1.registerContextClassLoader(classLoader);
|
46 |
| |
47 |
2
| Region r2 = cache2.getRegion(fqn, true);
|
48 |
2
| r2.setActive(true);
|
49 |
2
| r2.registerContextClassLoader(classLoader);
|
50 |
| |
51 |
2
| listClass = classLoader.loadClass(className);
|
52 |
2
| value = listClass.newInstance();
|
53 |
| |
54 |
2
| cache1.put(fqn, key, value);
|
55 |
| } |
56 |
| |
57 |
2
| protected void tearDown()
|
58 |
| { |
59 |
2
| cache1.stop();
|
60 |
2
| cache2.stop();
|
61 |
| } |
62 |
| |
63 |
2
| private ClassLoader getClassLoader() throws Exception
|
64 |
| { |
65 |
2
| String[] includesClasses = {className};
|
66 |
2
| String[] excludesClasses = {};
|
67 |
2
| ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
68 |
2
| return new SelectedClassnameClassLoader(includesClasses, excludesClasses, cl);
|
69 |
| } |
70 |
| |
71 |
1
| public void testClusteredGet() throws Exception
|
72 |
| { |
73 |
1
| assertNotNull(cache1.get(fqn, key));
|
74 |
1
| assertNotSame(MyList.class, cache1.get(fqn, key).getClass());
|
75 |
1
| assertSame(listClass, cache1.get(fqn, key).getClass());
|
76 |
| |
77 |
1
| assertNotNull(cache2.get(fqn, key));
|
78 |
1
| assertNotSame(MyList.class, cache2.get(fqn, key).getClass());
|
79 |
1
| assertSame(listClass, cache2.get(fqn, key).getClass());
|
80 |
| |
81 |
| |
82 |
| |
83 |
1
| MethodCall call = MethodCallFactory.create(MethodDeclarations.replicateMethod,
|
84 |
| MethodCallFactory.create(MethodDeclarations.clusteredGetMethod, |
85 |
| MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, fqn, key, false), |
86 |
| false |
87 |
| ) |
88 |
| ); |
89 |
| |
90 |
1
| List responses = cache1.getRPCManager().callRemoteMethods(null, call, true, true, 15000);
|
91 |
1
| List response1 = (List) responses.get(0);
|
92 |
| |
93 |
1
| Boolean found = (Boolean) response1.get(0);
|
94 |
1
| assertTrue("Should have found remote data", found);
|
95 |
| |
96 |
1
| Object data = response1.get(1);
|
97 |
| |
98 |
| |
99 |
1
| assertNotNull(data);
|
100 |
1
| assertNotSame(MyList.class, data.getClass());
|
101 |
1
| assertSame(listClass, data.getClass());
|
102 |
| } |
103 |
| |
104 |
1
| public void testDataGravitation() throws Exception
|
105 |
| { |
106 |
1
| assertNotNull(cache1.get(fqn, key));
|
107 |
1
| assertNotSame(MyList.class, cache1.get(fqn, key).getClass());
|
108 |
1
| assertSame(listClass, cache1.get(fqn, key).getClass());
|
109 |
| |
110 |
1
| assertNotNull(cache2.get(fqn, key));
|
111 |
1
| assertNotSame(MyList.class, cache2.get(fqn, key).getClass());
|
112 |
1
| assertSame(listClass, cache2.get(fqn, key).getClass());
|
113 |
| |
114 |
| |
115 |
| |
116 |
1
| MethodCall call = MethodCallFactory.create(MethodDeclarations.replicateMethod,
|
117 |
| MethodCallFactory.create(MethodDeclarations.dataGravitationMethod, |
118 |
| fqn, false |
119 |
| ) |
120 |
| ); |
121 |
| |
122 |
1
| List responses = cache1.getRPCManager().callRemoteMethods(null, call, true, true, 15000);
|
123 |
1
| GravitateResult data = (GravitateResult) responses.get(0);
|
124 |
| |
125 |
1
| assertTrue("Should have found remote data", data.isDataFound());
|
126 |
1
| assertNotNull(data.getNodeData());
|
127 |
1
| Object value = data.getNodeData().get(0).getAttributes().get(key);
|
128 |
| |
129 |
1
| assertNotNull(value);
|
130 |
1
| assertNotSame(MyList.class, value.getClass());
|
131 |
1
| assertSame(listClass, value.getClass());
|
132 |
| } |
133 |
| |
134 |
| } |