KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > bind > tuple > test > TupleBindingTest


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

8
9 package com.sleepycat.bind.tuple.test;
10
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14
15 import com.sleepycat.bind.EntityBinding;
16 import com.sleepycat.bind.EntryBinding;
17 import com.sleepycat.bind.tuple.BooleanBinding;
18 import com.sleepycat.bind.tuple.ByteBinding;
19 import com.sleepycat.bind.tuple.CharacterBinding;
20 import com.sleepycat.bind.tuple.DoubleBinding;
21 import com.sleepycat.bind.tuple.FloatBinding;
22 import com.sleepycat.bind.tuple.IntegerBinding;
23 import com.sleepycat.bind.tuple.LongBinding;
24 import com.sleepycat.bind.tuple.ShortBinding;
25 import com.sleepycat.bind.tuple.SortedDoubleBinding;
26 import com.sleepycat.bind.tuple.SortedFloatBinding;
27 import com.sleepycat.bind.tuple.StringBinding;
28 import com.sleepycat.bind.tuple.TupleBinding;
29 import com.sleepycat.bind.tuple.TupleInput;
30 import com.sleepycat.bind.tuple.TupleInputBinding;
31 import com.sleepycat.bind.tuple.TupleMarshalledBinding;
32 import com.sleepycat.bind.tuple.TupleOutput;
33 import com.sleepycat.bind.tuple.TupleTupleMarshalledBinding;
34 import com.sleepycat.collections.test.DbTestUtil;
35 import com.sleepycat.je.DatabaseEntry;
36 import com.sleepycat.util.FastOutputStream;
37 import com.sleepycat.util.ExceptionUnwrapper;
38
39 /**
40  * @author Mark Hayes
41  */

42 public class TupleBindingTest extends TestCase {
43
44     private DatabaseEntry buffer;
45     private DatabaseEntry keyBuffer;
46
47     public static void main(String JavaDoc[] args)
48         throws Exception JavaDoc {
49
50         junit.framework.TestResult tr =
51             junit.textui.TestRunner.run(suite());
52         if (tr.errorCount() > 0 ||
53             tr.failureCount() > 0) {
54             System.exit(1);
55         } else {
56             System.exit(0);
57         }
58     }
59
60     public static Test suite()
61         throws Exception JavaDoc {
62
63         TestSuite suite = new TestSuite(TupleBindingTest.class);
64         return suite;
65     }
66
67     public TupleBindingTest(String JavaDoc name) {
68
69         super(name);
70     }
71
72     public void setUp() {
73
74         DbTestUtil.printTestName("TupleBindingTest." + getName());
75         buffer = new DatabaseEntry();
76         keyBuffer = new DatabaseEntry();
77     }
78
79     public void tearDown() {
80
81         /* Ensure that GC can cleanup. */
82         buffer = null;
83         keyBuffer = null;
84     }
85
86     public void runTest()
87         throws Throwable JavaDoc {
88
89         try {
90             super.runTest();
91         } catch (Exception JavaDoc e) {
92             throw ExceptionUnwrapper.unwrap(e);
93         }
94     }
95
96     private void primitiveBindingTest(Class JavaDoc primitiveCls, Class JavaDoc compareCls,
97                                       Object JavaDoc val, int byteSize) {
98
99         TupleBinding binding = TupleBinding.getPrimitiveBinding(primitiveCls);
100
101         /* Test standard object binding. */
102
103         binding.objectToEntry(val, buffer);
104         assertEquals(byteSize, buffer.getSize());
105
106         Object JavaDoc val2 = binding.entryToObject(buffer);
107         assertSame(compareCls, val2.getClass());
108         assertEquals(val, val2);
109
110         Object JavaDoc valWithWrongCls = (primitiveCls == String JavaDoc.class)
111                       ? ((Object JavaDoc) new Integer JavaDoc(0)) : ((Object JavaDoc) new String JavaDoc(""));
112         try {
113             binding.objectToEntry(valWithWrongCls, buffer);
114         }
115         catch (ClassCastException JavaDoc expected) {}
116
117         /* Test nested tuple binding. */
118
119         TupleOutput output = new TupleOutput();
120         output.writeString("abc");
121         binding.objectToEntry(val, output);
122         output.writeString("xyz");
123
124         TupleInput input = new TupleInput(output);
125         assertEquals("abc", input.readString());
126         Object JavaDoc val3 = binding.entryToObject(input);
127         assertEquals("xyz", input.readString());
128
129         assertEquals(0, input.available());
130         assertSame(compareCls, val3.getClass());
131         assertEquals(val, val3);
132     }
133
134     public void testPrimitiveBindings() {
135
136         primitiveBindingTest(String JavaDoc.class, String JavaDoc.class,
137                              "abc", 4);
138
139         primitiveBindingTest(Character JavaDoc.class, Character JavaDoc.class,
140                              new Character JavaDoc('a'), 2);
141         primitiveBindingTest(Boolean JavaDoc.class, Boolean JavaDoc.class,
142                              new Boolean JavaDoc(true), 1);
143         primitiveBindingTest(Byte JavaDoc.class, Byte JavaDoc.class,
144                              new Byte JavaDoc((byte) 123), 1);
145         primitiveBindingTest(Short JavaDoc.class, Short JavaDoc.class,
146                              new Short JavaDoc((short) 123), 2);
147         primitiveBindingTest(Integer JavaDoc.class, Integer JavaDoc.class,
148                              new Integer JavaDoc(123), 4);
149         primitiveBindingTest(Long JavaDoc.class, Long JavaDoc.class,
150                              new Long JavaDoc(123), 8);
151         primitiveBindingTest(Float JavaDoc.class, Float JavaDoc.class,
152                              new Float JavaDoc(123.123), 4);
153         primitiveBindingTest(Double JavaDoc.class, Double JavaDoc.class,
154                              new Double JavaDoc(123.123), 8);
155
156         primitiveBindingTest(Character.TYPE, Character JavaDoc.class,
157                              new Character JavaDoc('a'), 2);
158         primitiveBindingTest(Boolean.TYPE, Boolean JavaDoc.class,
159                              new Boolean JavaDoc(true), 1);
160         primitiveBindingTest(Byte.TYPE, Byte JavaDoc.class,
161                              new Byte JavaDoc((byte) 123), 1);
162         primitiveBindingTest(Short.TYPE, Short JavaDoc.class,
163                              new Short JavaDoc((short) 123), 2);
164         primitiveBindingTest(Integer.TYPE, Integer JavaDoc.class,
165                              new Integer JavaDoc(123), 4);
166         primitiveBindingTest(Long.TYPE, Long JavaDoc.class,
167                              new Long JavaDoc(123), 8);
168         primitiveBindingTest(Float.TYPE, Float JavaDoc.class,
169                              new Float JavaDoc(123.123), 4);
170         primitiveBindingTest(Double.TYPE, Double JavaDoc.class,
171                              new Double JavaDoc(123.123), 8);
172
173         DatabaseEntry entry = new DatabaseEntry();
174
175         StringBinding.stringToEntry("abc", entry);
176     assertEquals(4, entry.getData().length);
177         assertEquals("abc", StringBinding.entryToString(entry));
178
179         new StringBinding().objectToEntry("abc", entry);
180     assertEquals(4, entry.getData().length);
181
182         StringBinding.stringToEntry(null, entry);
183     assertEquals(2, entry.getData().length);
184         assertEquals(null, StringBinding.entryToString(entry));
185
186         new StringBinding().objectToEntry(null, entry);
187     assertEquals(2, entry.getData().length);
188
189         CharacterBinding.charToEntry('a', entry);
190     assertEquals(2, entry.getData().length);
191         assertEquals('a', CharacterBinding.entryToChar(entry));
192
193         new CharacterBinding().objectToEntry(new Character JavaDoc('a'), entry);
194     assertEquals(2, entry.getData().length);
195
196         BooleanBinding.booleanToEntry(true, entry);
197     assertEquals(1, entry.getData().length);
198         assertEquals(true, BooleanBinding.entryToBoolean(entry));
199
200         new BooleanBinding().objectToEntry(Boolean.TRUE, entry);
201     assertEquals(1, entry.getData().length);
202
203         ByteBinding.byteToEntry((byte) 123, entry);
204     assertEquals(1, entry.getData().length);
205         assertEquals((byte) 123, ByteBinding.entryToByte(entry));
206
207         ShortBinding.shortToEntry((short) 123, entry);
208     assertEquals(2, entry.getData().length);
209         assertEquals((short) 123, ShortBinding.entryToShort(entry));
210
211         new ByteBinding().objectToEntry(new Byte JavaDoc((byte) 123), entry);
212     assertEquals(1, entry.getData().length);
213
214         IntegerBinding.intToEntry(123, entry);
215     assertEquals(4, entry.getData().length);
216         assertEquals(123, IntegerBinding.entryToInt(entry));
217
218         new IntegerBinding().objectToEntry(new Integer JavaDoc(123), entry);
219     assertEquals(4, entry.getData().length);
220
221         LongBinding.longToEntry(123, entry);
222     assertEquals(8, entry.getData().length);
223         assertEquals(123, LongBinding.entryToLong(entry));
224
225         new LongBinding().objectToEntry(new Long JavaDoc(123), entry);
226     assertEquals(8, entry.getData().length);
227
228         FloatBinding.floatToEntry((float) 123.123, entry);
229     assertEquals(4, entry.getData().length);
230         assertTrue(((float) 123.123) == FloatBinding.entryToFloat(entry));
231
232         new FloatBinding().objectToEntry(new Float JavaDoc((float) 123.123), entry);
233     assertEquals(4, entry.getData().length);
234
235         DoubleBinding.doubleToEntry(123.123, entry);
236     assertEquals(8, entry.getData().length);
237         assertTrue(123.123 == DoubleBinding.entryToDouble(entry));
238
239         new DoubleBinding().objectToEntry(new Double JavaDoc(123.123), entry);
240     assertEquals(8, entry.getData().length);
241
242
243         SortedFloatBinding.floatToEntry((float) 123.123, entry);
244     assertEquals(4, entry.getData().length);
245         assertTrue(((float) 123.123) ==
246                    SortedFloatBinding.entryToFloat(entry));
247
248         new SortedFloatBinding().objectToEntry
249             (new Float JavaDoc((float) 123.123), entry);
250     assertEquals(4, entry.getData().length);
251
252         SortedDoubleBinding.doubleToEntry(123.123, entry);
253     assertEquals(8, entry.getData().length);
254         assertTrue(123.123 == SortedDoubleBinding.entryToDouble(entry));
255
256         new SortedDoubleBinding().objectToEntry(new Double JavaDoc(123.123), entry);
257     assertEquals(8, entry.getData().length);
258     }
259
260     public void testTupleInputBinding() {
261
262         EntryBinding binding = new TupleInputBinding();
263
264         TupleOutput out = new TupleOutput();
265         out.writeString("abc");
266         binding.objectToEntry(new TupleInput(out), buffer);
267         assertEquals(4, buffer.getSize());
268
269         Object JavaDoc result = binding.entryToObject(buffer);
270         assertTrue(result instanceof TupleInput);
271         TupleInput in = (TupleInput) result;
272         assertEquals("abc", in.readString());
273         assertEquals(0, in.available());
274     }
275
276     // also tests TupleBinding since TupleMarshalledBinding extends it
277
public void testTupleMarshalledBinding() {
278
279         EntryBinding binding =
280             new TupleMarshalledBinding(MarshalledObject.class);
281
282         MarshalledObject val = new MarshalledObject("abc", "", "", "");
283         binding.objectToEntry(val, buffer);
284         assertEquals(val.expectedDataLength(), buffer.getSize());
285
286         Object JavaDoc result = binding.entryToObject(buffer);
287         assertTrue(result instanceof MarshalledObject);
288         val = (MarshalledObject) result;
289         assertEquals("abc", val.getData());
290     }
291
292     // also tests TupleTupleBinding since TupleTupleMarshalledBinding extends
293
// it
294
public void testTupleTupleMarshalledBinding() {
295
296         EntityBinding binding =
297             new TupleTupleMarshalledBinding(MarshalledObject.class);
298
299         MarshalledObject val = new MarshalledObject("abc", "primary",
300                                                     "index1", "index2");
301         binding.objectToData(val, buffer);
302         assertEquals(val.expectedDataLength(), buffer.getSize());
303         binding.objectToKey(val, keyBuffer);
304         assertEquals(val.expectedKeyLength(), keyBuffer.getSize());
305
306         Object JavaDoc result = binding.entryToObject(keyBuffer, buffer);
307         assertTrue(result instanceof MarshalledObject);
308         val = (MarshalledObject) result;
309         assertEquals("abc", val.getData());
310         assertEquals("primary", val.getPrimaryKey());
311         assertEquals("index1", val.getIndexKey1());
312         assertEquals("index2", val.getIndexKey2());
313     }
314
315     public void testBufferSize() {
316
317         CaptureSizeBinding binding = new CaptureSizeBinding();
318
319         binding.objectToEntry("x", buffer);
320         assertEquals("x", binding.entryToObject(buffer));
321         assertEquals(FastOutputStream.DEFAULT_INIT_SIZE, binding.bufSize);
322
323         binding.setTupleBufferSize(1000);
324         binding.objectToEntry("x", buffer);
325         assertEquals("x", binding.entryToObject(buffer));
326         assertEquals(1000, binding.bufSize);
327     }
328
329     private class CaptureSizeBinding extends TupleBinding {
330
331         int bufSize;
332
333         CaptureSizeBinding() {
334             super();
335         }
336
337         public TupleOutput getTupleOutput(Object JavaDoc object) {
338             TupleOutput out = super.getTupleOutput(object);
339             bufSize = out.getBufferBytes().length;
340             return out;
341         }
342
343         public Object JavaDoc entryToObject(TupleInput input) {
344             return input.readString();
345         }
346
347         public void objectToEntry(Object JavaDoc object, TupleOutput output) {
348             assertEquals(bufSize, output.getBufferBytes().length);
349             output.writeString((String JavaDoc) object);
350         }
351     }
352
353     public void testBufferOverride() {
354
355         TupleOutput out = new TupleOutput(new byte[10]);
356         CachedOutputBinding binding = new CachedOutputBinding(out);
357
358         binding.used = false;
359         binding.objectToEntry("x", buffer);
360         assertEquals("x", binding.entryToObject(buffer));
361         assertTrue(binding.used);
362
363         binding.used = false;
364         binding.objectToEntry("aaaaaaaaaaaaaaaaaaaaaa", buffer);
365         assertEquals("aaaaaaaaaaaaaaaaaaaaaa", binding.entryToObject(buffer));
366         assertTrue(binding.used);
367
368         binding.used = false;
369         binding.objectToEntry("x", buffer);
370         assertEquals("x", binding.entryToObject(buffer));
371         assertTrue(binding.used);
372     }
373
374     private class CachedOutputBinding extends TupleBinding {
375
376         TupleOutput out;
377         boolean used;
378
379         CachedOutputBinding(TupleOutput out) {
380             super();
381             this.out = out;
382         }
383
384         public TupleOutput getTupleOutput(Object JavaDoc object) {
385             out.reset();
386             used = true;
387             return out;
388         }
389
390         public Object JavaDoc entryToObject(TupleInput input) {
391             return input.readString();
392         }
393
394         public void objectToEntry(Object JavaDoc object, TupleOutput output) {
395             assertSame(out, output);
396             output.writeString((String JavaDoc) object);
397         }
398     }
399 }
400
Popular Tags