KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > bind > serial > test > SerialBindingTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: SerialBindingTest.java,v 1.32 2006/10/30 21:14:36 bostic Exp $
7  */

8
9 package com.sleepycat.bind.serial.test;
10
11 import java.io.Serializable JavaDoc;
12
13 import junit.framework.Test;
14 import junit.framework.TestCase;
15 import junit.framework.TestSuite;
16
17 import com.sleepycat.bind.EntityBinding;
18 import com.sleepycat.bind.serial.ClassCatalog;
19 import com.sleepycat.bind.serial.SerialBinding;
20 import com.sleepycat.bind.serial.SerialSerialBinding;
21 import com.sleepycat.bind.serial.TupleSerialMarshalledBinding;
22 import com.sleepycat.collections.test.DbTestUtil;
23 import com.sleepycat.je.DatabaseEntry;
24 import com.sleepycat.util.ExceptionUnwrapper;
25 import com.sleepycat.util.FastOutputStream;
26
27 /**
28  * @author Mark Hayes
29  */

30 public class SerialBindingTest extends TestCase {
31
32     private ClassCatalog catalog;
33     private DatabaseEntry buffer;
34     private DatabaseEntry keyBuffer;
35
36     public static void main(String JavaDoc[] args)
37         throws Exception JavaDoc {
38
39         junit.framework.TestResult tr =
40             junit.textui.TestRunner.run(suite());
41         if (tr.errorCount() > 0 ||
42             tr.failureCount() > 0) {
43             System.exit(1);
44         } else {
45             System.exit(0);
46         }
47     }
48
49     public static Test suite()
50         throws Exception JavaDoc {
51
52         TestSuite suite = new TestSuite(SerialBindingTest.class);
53         return suite;
54     }
55
56     public SerialBindingTest(String JavaDoc name) {
57
58         super(name);
59     }
60
61     public void setUp() {
62
63         DbTestUtil.printTestName("SerialBindingTest." + getName());
64         catalog = new TestClassCatalog();
65         buffer = new DatabaseEntry();
66         keyBuffer = new DatabaseEntry();
67     }
68
69     public void tearDown() {
70
71         /* Ensure that GC can cleanup. */
72         catalog = null;
73         buffer = null;
74         keyBuffer = null;
75     }
76
77     public void runTest()
78         throws Throwable JavaDoc {
79
80         try {
81             super.runTest();
82         } catch (Exception JavaDoc e) {
83             throw ExceptionUnwrapper.unwrap(e);
84         }
85     }
86
87     private void primitiveBindingTest(Object JavaDoc val) {
88
89         Class JavaDoc cls = val.getClass();
90         SerialBinding binding = new SerialBinding(catalog, cls);
91
92         binding.objectToEntry(val, buffer);
93         assertTrue(buffer.getSize() > 0);
94
95         Object JavaDoc val2 = binding.entryToObject(buffer);
96         assertSame(cls, val2.getClass());
97         assertEquals(val, val2);
98
99         Object JavaDoc valWithWrongCls = (cls == String JavaDoc.class)
100                       ? ((Object JavaDoc) new Integer JavaDoc(0)) : ((Object JavaDoc) new String JavaDoc(""));
101         try {
102             binding.objectToEntry(valWithWrongCls, buffer);
103         } catch (IllegalArgumentException JavaDoc expected) {}
104     }
105
106     public void testPrimitiveBindings() {
107
108         primitiveBindingTest("abc");
109         primitiveBindingTest(new Character JavaDoc('a'));
110         primitiveBindingTest(new Boolean JavaDoc(true));
111         primitiveBindingTest(new Byte JavaDoc((byte) 123));
112         primitiveBindingTest(new Short JavaDoc((short) 123));
113         primitiveBindingTest(new Integer JavaDoc(123));
114         primitiveBindingTest(new Long JavaDoc(123));
115         primitiveBindingTest(new Float JavaDoc(123.123));
116         primitiveBindingTest(new Double JavaDoc(123.123));
117     }
118
119     public void testNullObjects() {
120
121         SerialBinding binding = new SerialBinding(catalog, null);
122         buffer.setSize(0);
123         binding.objectToEntry(null, buffer);
124         assertTrue(buffer.getSize() > 0);
125         assertEquals(null, binding.entryToObject(buffer));
126     }
127
128     public void testSerialSerialBinding() {
129
130         SerialBinding keyBinding = new SerialBinding(catalog, String JavaDoc.class);
131         SerialBinding valueBinding = new SerialBinding(catalog, String JavaDoc.class);
132         EntityBinding binding = new MySerialSerialBinding(keyBinding,
133                                                           valueBinding);
134
135         String JavaDoc val = "key#value?indexKey";
136         binding.objectToData(val, buffer);
137         assertTrue(buffer.getSize() > 0);
138         binding.objectToKey(val, keyBuffer);
139         assertTrue(keyBuffer.getSize() > 0);
140
141         Object JavaDoc result = binding.entryToObject(keyBuffer, buffer);
142         assertEquals(val, result);
143     }
144
145     // also tests TupleSerialBinding since TupleSerialMarshalledBinding extends
146
// it
147
public void testTupleSerialMarshalledBinding() {
148
149         SerialBinding valueBinding = new SerialBinding(catalog,
150                                                     MarshalledObject.class);
151         EntityBinding binding =
152             new TupleSerialMarshalledBinding(valueBinding);
153
154         MarshalledObject val = new MarshalledObject("abc", "primary",
155                                                     "index1", "index2");
156         binding.objectToData(val, buffer);
157         assertTrue(buffer.getSize() > 0);
158         binding.objectToKey(val, keyBuffer);
159         assertEquals(val.expectedKeyLength(), keyBuffer.getSize());
160
161         Object JavaDoc result = binding.entryToObject(keyBuffer, buffer);
162         assertTrue(result instanceof MarshalledObject);
163         val = (MarshalledObject) result;
164         assertEquals("abc", val.getData());
165         assertEquals("primary", val.getPrimaryKey());
166         assertEquals("index1", val.getIndexKey1());
167         assertEquals("index2", val.getIndexKey2());
168     }
169
170     public void testBufferSize() {
171
172         CaptureSizeBinding binding =
173             new CaptureSizeBinding(catalog, String JavaDoc.class);
174
175         binding.objectToEntry("x", buffer);
176         assertEquals("x", binding.entryToObject(buffer));
177         assertEquals(FastOutputStream.DEFAULT_INIT_SIZE, binding.bufSize);
178
179         binding.setSerialBufferSize(1000);
180         binding.objectToEntry("x", buffer);
181         assertEquals("x", binding.entryToObject(buffer));
182         assertEquals(1000, binding.bufSize);
183     }
184
185     private static class CaptureSizeBinding extends SerialBinding {
186
187         int bufSize;
188
189         CaptureSizeBinding(ClassCatalog classCatalog, Class JavaDoc baseClass) {
190             super(classCatalog, baseClass);
191         }
192
193         public FastOutputStream getSerialOutput(Object JavaDoc object) {
194             FastOutputStream fos = super.getSerialOutput(object);
195             bufSize = fos.getBufferBytes().length;
196             return fos;
197         }
198     }
199
200     public void testBufferOverride() {
201
202         FastOutputStream out = new FastOutputStream(10);
203         CachedOutputBinding binding =
204             new CachedOutputBinding(catalog, String JavaDoc.class, out);
205
206         binding.used = false;
207         binding.objectToEntry("x", buffer);
208         assertEquals("x", binding.entryToObject(buffer));
209         assertTrue(binding.used);
210
211         binding.used = false;
212         binding.objectToEntry("aaaaaaaaaaaaaaaaaaaaaa", buffer);
213         assertEquals("aaaaaaaaaaaaaaaaaaaaaa", binding.entryToObject(buffer));
214         assertTrue(binding.used);
215
216         binding.used = false;
217         binding.objectToEntry("x", buffer);
218         assertEquals("x", binding.entryToObject(buffer));
219         assertTrue(binding.used);
220     }
221
222     private static class CachedOutputBinding extends SerialBinding {
223
224         FastOutputStream out;
225         boolean used;
226
227         CachedOutputBinding(ClassCatalog classCatalog,
228                             Class JavaDoc baseClass,
229                             FastOutputStream out) {
230             super(classCatalog, baseClass);
231             this.out = out;
232         }
233
234         public FastOutputStream getSerialOutput(Object JavaDoc object) {
235             out.reset();
236             used = true;
237             return out;
238         }
239     }
240
241     private static class MySerialSerialBinding extends SerialSerialBinding {
242
243         private MySerialSerialBinding(SerialBinding keyBinding,
244                                       SerialBinding valueBinding) {
245
246             super(keyBinding, valueBinding);
247         }
248
249         public Object JavaDoc entryToObject(Object JavaDoc keyInput, Object JavaDoc valueInput) {
250
251             return "" + keyInput + '#' + valueInput;
252         }
253
254         public Object JavaDoc objectToKey(Object JavaDoc object) {
255
256             String JavaDoc s = (String JavaDoc) object;
257             int i = s.indexOf('#');
258             if (i < 0 || i == s.length() - 1) {
259                 throw new IllegalArgumentException JavaDoc(s);
260             } else {
261                 return s.substring(0, i);
262             }
263         }
264
265         public Object JavaDoc objectToData(Object JavaDoc object) {
266
267             String JavaDoc s = (String JavaDoc) object;
268             int i = s.indexOf('#');
269             if (i < 0 || i == s.length() - 1) {
270                 throw new IllegalArgumentException JavaDoc(s);
271             } else {
272                 return s.substring(i + 1);
273             }
274         }
275     }
276
277     /**
278      * Tests that overriding SerialBinding.getClassLoader is possible. This is
279      * a crude test because to create a truly working class loader is a large
280      * undertaking.
281      */

282     public void testClassloaderOverride()
283         throws Exception JavaDoc {
284
285         DatabaseEntry entry = new DatabaseEntry();
286
287         SerialBinding binding = new CustomLoaderBinding
288             (catalog, null, new FailureClassLoader());
289
290         try {
291             binding.objectToEntry(new MyClass(), entry);
292             binding.entryToObject(entry);
293             fail();
294         } catch (RuntimeException JavaDoc e) {
295             assertTrue(e.getMessage().startsWith("expect failure"));
296         }
297     }
298
299     private static class CustomLoaderBinding extends SerialBinding {
300
301         private ClassLoader JavaDoc loader;
302
303         CustomLoaderBinding(ClassCatalog classCatalog,
304                             Class JavaDoc baseClass,
305                             ClassLoader JavaDoc loader) {
306
307             super(classCatalog, baseClass);
308             this.loader = loader;
309         }
310
311         public ClassLoader JavaDoc getClassLoader() {
312             return loader;
313         }
314     }
315
316     private static class FailureClassLoader extends ClassLoader JavaDoc {
317
318         public Class JavaDoc loadClass(String JavaDoc name)
319             throws ClassNotFoundException JavaDoc {
320
321             throw new RuntimeException JavaDoc("expect failure: " + name);
322         }
323     }
324
325     private static class MyClass implements Serializable JavaDoc {
326     }
327 }
328
Popular Tags