KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > marshall > CacheMarshallerTestBase


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