1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.marshall; |
8 |
| |
9 |
| import junit.framework.TestCase; |
10 |
| import org.jboss.cache.Fqn; |
11 |
| import org.jboss.cache.RegionManager; |
12 |
| |
13 |
| import java.util.ArrayList; |
14 |
| import java.util.List; |
15 |
| |
16 |
| public abstract class CacheMarshallerTestBase extends TestCase |
17 |
| { |
18 |
| protected String currentVersion; |
19 |
| protected int currentVersionShort; |
20 |
| protected Class expectedMarshallerClass; |
21 |
| protected VersionAwareMarshaller marshaller; |
22 |
| |
23 |
| |
24 |
16
| protected void setUp() throws Exception
|
25 |
| { |
26 |
16
| marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, currentVersion);
|
27 |
| } |
28 |
| |
29 |
16
| protected void tearDown()
|
30 |
| { |
31 |
16
| marshaller = null;
|
32 |
| } |
33 |
| |
34 |
13
| protected void assertMethodCallsEquals(MethodCall call1, MethodCall call2)
|
35 |
| { |
36 |
0
| if (call1 == call2) return;
|
37 |
| |
38 |
13
| assertEquals("Method IDs should match", call1.getMethodId(), call2.getMethodId());
|
39 |
13
| assertEquals("Method names should match", call1.getName(), call2.getName());
|
40 |
13
| assertEquals("Method reflection objects should match", call1.getMethod(), call2.getMethod());
|
41 |
13
| if (call1.getArgs() == null || call2.getArgs() == null)
|
42 |
| { |
43 |
0
| assertNull("Both args should be null", call1.getArgs());
|
44 |
0
| assertNull("Both args should be null", call2.getArgs());
|
45 |
| } |
46 |
| else |
47 |
| { |
48 |
13
| Object[] call1Args = call1.getArgs();
|
49 |
13
| Object[] call2Args = call2.getArgs();
|
50 |
| |
51 |
13
| assertObjectArraysAreEqual(call1Args, call2Args);
|
52 |
| } |
53 |
| } |
54 |
| |
55 |
15
| protected void assertObjectArraysAreEqual(Object[] a1, Object[] a2)
|
56 |
| { |
57 |
15
| assertEquals("Number of args should match", a1.length, a2.length);
|
58 |
| |
59 |
15
| for (int i = 0; i < a1.length; i++)
|
60 |
| { |
61 |
41
| if (a1[i] instanceof MethodCall && a2[i] instanceof MethodCall)
|
62 |
| { |
63 |
9
| assertMethodCallsEquals((MethodCall) a1[i], (MethodCall) a2[i]);
|
64 |
| } |
65 |
32
| else if (a1[i] instanceof List && a2[i] instanceof List)
|
66 |
| { |
67 |
2
| Object[] a1Elements = ((List) a1[i]).toArray();
|
68 |
2
| Object[] a2Elements = ((List) a2[i]).toArray();
|
69 |
| |
70 |
2
| assertObjectArraysAreEqual(a1Elements, a2Elements);
|
71 |
| } |
72 |
| else |
73 |
| { |
74 |
30
| assertEquals("Argument # " + i + " should be equal", a1[i], a2[i]);
|
75 |
| } |
76 |
| } |
77 |
| |
78 |
| } |
79 |
| |
80 |
| |
81 |
1
| public void testGetMarshaller()
|
82 |
| { |
83 |
1
| assertEquals("Only one marshaller should be in the map by this stage", 1, marshaller.marshallers.size());
|
84 |
| |
85 |
1
| assertEquals(expectedMarshallerClass, marshaller.getMarshaller(currentVersionShort).getClass());
|
86 |
1
| assertEquals(CacheMarshaller200.class, marshaller.getMarshaller(15).getClass());
|
87 |
1
| assertEquals(CacheMarshaller200.class, marshaller.getMarshaller(1).getClass());
|
88 |
1
| assertEquals(CacheMarshaller200.class, marshaller.getMarshaller(-1).getClass());
|
89 |
1
| assertEquals(CacheMarshaller200.class, marshaller.getMarshaller(0).getClass());
|
90 |
| |
91 |
1
| assertEquals("One marshaller should be in the map by this stage", 1, marshaller.marshallers.size());
|
92 |
| } |
93 |
| |
94 |
1
| public void testStringBasedFqn() throws Exception
|
95 |
| { |
96 |
1
| Fqn fqn = new Fqn(new Object[]{"JSESSIONID", "1010.10.5:3000", "1234567890", "1"});
|
97 |
1
| byte[] asBytes = marshaller.objectToByteBuffer(fqn);
|
98 |
1
| System.out.println("Marshalled to " + asBytes.length + " bytes");
|
99 |
1
| Object o2 = marshaller.objectFromByteBuffer(asBytes);
|
100 |
1
| assertEquals(fqn, o2);
|
101 |
| } |
102 |
| |
103 |
1
| public void testNonStringBasedFqn() throws Exception
|
104 |
| { |
105 |
1
| Fqn fqn = new Fqn(new Object[]{3, false});
|
106 |
1
| byte[] asBytes = marshaller.objectToByteBuffer(fqn);
|
107 |
1
| Object o2 = marshaller.objectFromByteBuffer(asBytes);
|
108 |
1
| assertEquals(fqn, o2);
|
109 |
| } |
110 |
| |
111 |
1
| public void testMethodCall() throws Exception
|
112 |
| { |
113 |
1
| Fqn fqn = new Fqn(new Object[]{3, false});
|
114 |
1
| MethodCall call = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, null, fqn, "key", "value", true);
|
115 |
1
| byte[] asBytes = marshaller.objectToByteBuffer(call);
|
116 |
1
| Object o2 = marshaller.objectFromByteBuffer(asBytes);
|
117 |
| |
118 |
1
| assertTrue("Unmarshalled object should be a method call", o2 instanceof MethodCall);
|
119 |
1
| MethodCall m2 = (MethodCall) o2;
|
120 |
| |
121 |
1
| assertMethodCallsEquals(call, m2);
|
122 |
| } |
123 |
| |
124 |
1
| public void testNestedMethodCall() throws Exception
|
125 |
| { |
126 |
1
| Fqn fqn = new Fqn(new Object[]{3, false});
|
127 |
1
| MethodCall call = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, null, fqn, "key", "value", true);
|
128 |
1
| MethodCall replicateCall = MethodCallFactory.create(MethodDeclarations.replicateMethod, call);
|
129 |
1
| byte[] asBytes = marshaller.objectToByteBuffer(replicateCall);
|
130 |
1
| Object o2 = marshaller.objectFromByteBuffer(asBytes);
|
131 |
1
| assertTrue("Unmarshalled object should be a method call", o2 instanceof MethodCall);
|
132 |
1
| MethodCall m2 = (MethodCall) o2;
|
133 |
| |
134 |
1
| assertMethodCallsEquals(replicateCall, m2);
|
135 |
| } |
136 |
| |
137 |
1
| public void testLargeString() throws Exception
|
138 |
| { |
139 |
1
| doLargeStringTest(32767, false);
|
140 |
| } |
141 |
| |
142 |
1
| public void testLargerString() throws Exception
|
143 |
| { |
144 |
1
| doLargeStringTest(32768, false);
|
145 |
| } |
146 |
| |
147 |
1
| public void test64KString() throws Exception
|
148 |
| { |
149 |
1
| doLargeStringTest((2 << 15) - 10, false);
|
150 |
1
| doLargeStringTest((2 << 15) + 10, false);
|
151 |
| } |
152 |
| |
153 |
1
| public void test128KString() throws Exception
|
154 |
| { |
155 |
1
| doLargeStringTest((2 << 16) - 10, false);
|
156 |
1
| doLargeStringTest((2 << 16) + 10, false);
|
157 |
| } |
158 |
| |
159 |
1
| public void testLargeStringMultiByte() throws Exception
|
160 |
| { |
161 |
1
| doLargeStringTest(32767, true);
|
162 |
| } |
163 |
| |
164 |
1
| public void testLargerStringMultiByte() throws Exception
|
165 |
| { |
166 |
1
| doLargeStringTest(32768, true);
|
167 |
| } |
168 |
| |
169 |
1
| public void test64KStringMultiByte() throws Exception
|
170 |
| { |
171 |
1
| doLargeStringTest((2 << 15) - 10, true);
|
172 |
1
| doLargeStringTest((2 << 15) + 10, true);
|
173 |
| } |
174 |
| |
175 |
1
| public void test128KStringMultiByte() throws Exception
|
176 |
| { |
177 |
1
| doLargeStringTest((2 << 16) - 10, true);
|
178 |
1
| doLargeStringTest((2 << 16) + 10, true);
|
179 |
| } |
180 |
| |
181 |
12
| protected void doLargeStringTest(int stringSize, boolean multiByteChars) throws Exception
|
182 |
| { |
183 |
12
| StringBuilder sb = new StringBuilder();
|
184 |
| |
185 |
12
| int startingChar = multiByteChars ? 210 : 65;
|
186 |
917502
| for (int i = 0; i < stringSize; i++) sb.append((char) (startingChar + (i % 26)));
|
187 |
| |
188 |
12
| String largeString = sb.toString();
|
189 |
| |
190 |
12
| assertEquals(stringSize, largeString.length());
|
191 |
| |
192 |
12
| byte[] buf = marshaller.objectToByteBuffer(largeString);
|
193 |
| |
194 |
12
| assertEquals(largeString, marshaller.objectFromByteBuffer(buf));
|
195 |
| } |
196 |
| |
197 |
1
| public void testReplicationQueue() throws Exception
|
198 |
| { |
199 |
1
| doReplicationQueueTest();
|
200 |
| } |
201 |
| |
202 |
1
| public void testReplicationQueueWithRegionBasedMarshalling() throws Exception
|
203 |
| { |
204 |
1
| marshaller = new VersionAwareMarshaller(new RegionManager(), false, true, currentVersion);
|
205 |
1
| doReplicationQueueTest();
|
206 |
| } |
207 |
| |
208 |
2
| protected void doReplicationQueueTest() throws Exception
|
209 |
| { |
210 |
| |
211 |
2
| List<MethodCall> calls = new ArrayList<MethodCall>();
|
212 |
| |
213 |
2
| Fqn f = new Fqn(new Object[]{"BlahBlah", 3, false});
|
214 |
2
| String k = "key", v = "value";
|
215 |
| |
216 |
2
| MethodCall actualCall = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, null, f, k, v, true);
|
217 |
2
| MethodCall replicateCall = MethodCallFactory.create(MethodDeclarations.replicateMethod, actualCall);
|
218 |
| |
219 |
2
| calls.add(replicateCall);
|
220 |
| |
221 |
2
| actualCall = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, null, f, k, v, true);
|
222 |
2
| replicateCall = MethodCallFactory.create(MethodDeclarations.replicateMethod, actualCall);
|
223 |
| |
224 |
2
| calls.add(replicateCall);
|
225 |
| |
226 |
2
| MethodCall call = MethodCallFactory.create(MethodDeclarations.replicateAllMethod, calls);
|
227 |
| |
228 |
2
| byte[] buf = marshaller.objectToByteBuffer(call);
|
229 |
| |
230 |
2
| assertMethodCallsEquals(call, (MethodCall) marshaller.objectFromByteBuffer(buf));
|
231 |
| } |
232 |
| |
233 |
| } |