Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

TupleBindingTest.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: TupleBindingTest.java,v 12.2 2005/08/01 20:25:25 mark Exp $
00008  */
00009 
00010 package com.sleepycat.bind.tuple.test;
00011 
00012 import junit.framework.Test;
00013 import junit.framework.TestCase;
00014 import junit.framework.TestSuite;
00015 
00016 import com.sleepycat.bind.EntityBinding;
00017 import com.sleepycat.bind.EntryBinding;
00018 import com.sleepycat.bind.tuple.BooleanBinding;
00019 import com.sleepycat.bind.tuple.ByteBinding;
00020 import com.sleepycat.bind.tuple.CharacterBinding;
00021 import com.sleepycat.bind.tuple.DoubleBinding;
00022 import com.sleepycat.bind.tuple.FloatBinding;
00023 import com.sleepycat.bind.tuple.IntegerBinding;
00024 import com.sleepycat.bind.tuple.LongBinding;
00025 import com.sleepycat.bind.tuple.ShortBinding;
00026 import com.sleepycat.bind.tuple.StringBinding;
00027 import com.sleepycat.bind.tuple.TupleBinding;
00028 import com.sleepycat.bind.tuple.TupleInput;
00029 import com.sleepycat.bind.tuple.TupleInputBinding;
00030 import com.sleepycat.bind.tuple.TupleMarshalledBinding;
00031 import com.sleepycat.bind.tuple.TupleOutput;
00032 import com.sleepycat.bind.tuple.TupleTupleMarshalledBinding;
00033 import com.sleepycat.collections.test.DbTestUtil;
00034 import com.sleepycat.db.DatabaseEntry;
00035 import com.sleepycat.util.FastOutputStream;
00036 import com.sleepycat.util.ExceptionUnwrapper;
00037 
00041 public class TupleBindingTest extends TestCase {
00042 
00043     private DatabaseEntry buffer;
00044     private DatabaseEntry keyBuffer;
00045     private DatabaseEntry indexKeyBuffer;
00046 
00047     public static void main(String[] args)
00048         throws Exception {
00049 
00050         junit.framework.TestResult tr =
00051             junit.textui.TestRunner.run(suite());
00052         if (tr.errorCount() > 0 ||
00053             tr.failureCount() > 0) {
00054             System.exit(1);
00055         } else {
00056             System.exit(0);
00057         }
00058     }
00059 
00060     public static Test suite()
00061         throws Exception {
00062 
00063         TestSuite suite = new TestSuite(TupleBindingTest.class);
00064         return suite;
00065     }
00066 
00067     public TupleBindingTest(String name) {
00068 
00069         super(name);
00070     }
00071 
00072     public void setUp() {
00073 
00074         DbTestUtil.printTestName("TupleBindingTest." + getName());
00075         buffer = new DatabaseEntry();
00076         keyBuffer = new DatabaseEntry();
00077         indexKeyBuffer = new DatabaseEntry();
00078     }
00079 
00080     public void tearDown() {
00081 
00082         /* Ensure that GC can cleanup. */
00083         buffer = null;
00084         keyBuffer = null;
00085         indexKeyBuffer = null;
00086     }
00087 
00088     public void runTest()
00089         throws Throwable {
00090 
00091         try {
00092             super.runTest();
00093         } catch (Exception e) {
00094             throw ExceptionUnwrapper.unwrap(e);
00095         }
00096     }
00097 
00098     private void primitiveBindingTest(Class primitiveCls, Class compareCls,
00099                                       Object val, int byteSize) {
00100 
00101         TupleBinding binding = TupleBinding.getPrimitiveBinding(primitiveCls);
00102 
00103         /* Test standard object binding. */
00104 
00105         binding.objectToEntry(val, buffer);
00106         assertEquals(byteSize, buffer.getSize());
00107 
00108         Object val2 = binding.entryToObject(buffer);
00109         assertSame(compareCls, val2.getClass());
00110         assertEquals(val, val2);
00111 
00112         Object valWithWrongCls = (primitiveCls == String.class)
00113                       ? ((Object) new Integer(0)) : ((Object) new String(""));
00114         try {
00115             binding.objectToEntry(valWithWrongCls, buffer);
00116         }
00117         catch (ClassCastException expected) {}
00118 
00119         /* Test nested tuple binding. */
00120 
00121         TupleOutput output = new TupleOutput();
00122         output.writeString("abc");
00123         binding.objectToEntry(val, output);
00124         output.writeString("xyz");
00125 
00126         TupleInput input = new TupleInput(output);
00127         assertEquals("abc", input.readString());
00128         Object val3 = binding.entryToObject(input);
00129         assertEquals("xyz", input.readString());
00130 
00131         assertEquals(0, input.available());
00132         assertSame(compareCls, val3.getClass());
00133         assertEquals(val, val3);
00134     }
00135 
00136     public void testPrimitiveBindings() {
00137 
00138         primitiveBindingTest(String.class, String.class,
00139                              "abc", 4);
00140 
00141         primitiveBindingTest(Character.class, Character.class,
00142                              new Character('a'), 2);
00143         primitiveBindingTest(Boolean.class, Boolean.class,
00144                              new Boolean(true), 1);
00145         primitiveBindingTest(Byte.class, Byte.class,
00146                              new Byte((byte) 123), 1);
00147         primitiveBindingTest(Short.class, Short.class,
00148                              new Short((short) 123), 2);
00149         primitiveBindingTest(Integer.class, Integer.class,
00150                              new Integer(123), 4);
00151         primitiveBindingTest(Long.class, Long.class,
00152                              new Long(123), 8);
00153         primitiveBindingTest(Float.class, Float.class,
00154                              new Float(123.123), 4);
00155         primitiveBindingTest(Double.class, Double.class,
00156                              new Double(123.123), 8);
00157 
00158         primitiveBindingTest(Character.TYPE, Character.class,
00159                              new Character('a'), 2);
00160         primitiveBindingTest(Boolean.TYPE, Boolean.class,
00161                              new Boolean(true), 1);
00162         primitiveBindingTest(Byte.TYPE, Byte.class,
00163                              new Byte((byte) 123), 1);
00164         primitiveBindingTest(Short.TYPE, Short.class,
00165                              new Short((short) 123), 2);
00166         primitiveBindingTest(Integer.TYPE, Integer.class,
00167                              new Integer(123), 4);
00168         primitiveBindingTest(Long.TYPE, Long.class,
00169                              new Long(123), 8);
00170         primitiveBindingTest(Float.TYPE, Float.class,
00171                              new Float(123.123), 4);
00172         primitiveBindingTest(Double.TYPE, Double.class,
00173                              new Double(123.123), 8);
00174 
00175         DatabaseEntry entry = new DatabaseEntry();
00176 
00177         StringBinding.stringToEntry("abc", entry);
00178         assertEquals(4, entry.getData().length);
00179         assertEquals("abc", StringBinding.entryToString(entry));
00180 
00181         new StringBinding().objectToEntry("abc", entry);
00182         assertEquals(4, entry.getData().length);
00183 
00184         StringBinding.stringToEntry(null, entry);
00185         assertEquals(2, entry.getData().length);
00186         assertEquals(null, StringBinding.entryToString(entry));
00187 
00188         new StringBinding().objectToEntry(null, entry);
00189         assertEquals(2, entry.getData().length);
00190 
00191         CharacterBinding.charToEntry('a', entry);
00192         assertEquals(2, entry.getData().length);
00193         assertEquals('a', CharacterBinding.entryToChar(entry));
00194 
00195         new CharacterBinding().objectToEntry(new Character('a'), entry);
00196         assertEquals(2, entry.getData().length);
00197 
00198         BooleanBinding.booleanToEntry(true, entry);
00199         assertEquals(1, entry.getData().length);
00200         assertEquals(true, BooleanBinding.entryToBoolean(entry));
00201 
00202         new BooleanBinding().objectToEntry(Boolean.TRUE, entry);
00203         assertEquals(1, entry.getData().length);
00204 
00205         ByteBinding.byteToEntry((byte) 123, entry);
00206         assertEquals(1, entry.getData().length);
00207         assertEquals((byte) 123, ByteBinding.entryToByte(entry));
00208 
00209         ShortBinding.shortToEntry((short) 123, entry);
00210         assertEquals(2, entry.getData().length);
00211         assertEquals((short) 123, ShortBinding.entryToShort(entry));
00212 
00213         new ByteBinding().objectToEntry(new Byte((byte) 123), entry);
00214         assertEquals(1, entry.getData().length);
00215 
00216         IntegerBinding.intToEntry(123, entry);
00217         assertEquals(4, entry.getData().length);
00218         assertEquals(123, IntegerBinding.entryToInt(entry));
00219 
00220         new IntegerBinding().objectToEntry(new Integer(123), entry);
00221         assertEquals(4, entry.getData().length);
00222 
00223         LongBinding.longToEntry(123, entry);
00224         assertEquals(8, entry.getData().length);
00225         assertEquals(123, LongBinding.entryToLong(entry));
00226 
00227         new LongBinding().objectToEntry(new Long(123), entry);
00228         assertEquals(8, entry.getData().length);
00229 
00230         FloatBinding.floatToEntry((float) 123.123, entry);
00231         assertEquals(4, entry.getData().length);
00232         assertTrue(((float) 123.123) == FloatBinding.entryToFloat(entry));
00233 
00234         new FloatBinding().objectToEntry(new Float((float) 123.123), entry);
00235         assertEquals(4, entry.getData().length);
00236 
00237         DoubleBinding.doubleToEntry(123.123, entry);
00238         assertEquals(8, entry.getData().length);
00239         assertTrue(123.123 == DoubleBinding.entryToDouble(entry));
00240 
00241         new DoubleBinding().objectToEntry(new Double(123.123), entry);
00242         assertEquals(8, entry.getData().length);
00243     }
00244 
00245     public void testTupleInputBinding() {
00246 
00247         EntryBinding binding = new TupleInputBinding();
00248 
00249         TupleOutput out = new TupleOutput();
00250         out.writeString("abc");
00251         binding.objectToEntry(new TupleInput(out), buffer);
00252         assertEquals(4, buffer.getSize());
00253 
00254         Object result = binding.entryToObject(buffer);
00255         assertTrue(result instanceof TupleInput);
00256         TupleInput in = (TupleInput) result;
00257         assertEquals("abc", in.readString());
00258         assertEquals(0, in.available());
00259     }
00260 
00261     // also tests TupleBinding since TupleMarshalledBinding extends it
00262     public void testTupleMarshalledBinding() {
00263 
00264         EntryBinding binding =
00265             new TupleMarshalledBinding(MarshalledObject.class);
00266 
00267         MarshalledObject val = new MarshalledObject("abc", "", "", "");
00268         binding.objectToEntry(val, buffer);
00269         assertEquals(val.expectedDataLength(), buffer.getSize());
00270 
00271         Object result = binding.entryToObject(buffer);
00272         assertTrue(result instanceof MarshalledObject);
00273         val = (MarshalledObject) result;
00274         assertEquals("abc", val.getData());
00275     }
00276 
00277     // also tests TupleTupleBinding since TupleTupleMarshalledBinding extends
00278     // it
00279     public void testTupleTupleMarshalledBinding() {
00280 
00281         EntityBinding binding =
00282             new TupleTupleMarshalledBinding(MarshalledObject.class);
00283 
00284         MarshalledObject val = new MarshalledObject("abc", "primary",
00285                                                     "index1", "index2");
00286         binding.objectToData(val, buffer);
00287         assertEquals(val.expectedDataLength(), buffer.getSize());
00288         binding.objectToKey(val, keyBuffer);
00289         assertEquals(val.expectedKeyLength(), keyBuffer.getSize());
00290 
00291         Object result = binding.entryToObject(keyBuffer, buffer);
00292         assertTrue(result instanceof MarshalledObject);
00293         val = (MarshalledObject) result;
00294         assertEquals("abc", val.getData());
00295         assertEquals("primary", val.getPrimaryKey());
00296         assertEquals("index1", val.getIndexKey1());
00297         assertEquals("index2", val.getIndexKey2());
00298     }
00299 
00300     public void testBufferSize() {
00301 
00302         CaptureSizeBinding binding = new CaptureSizeBinding();
00303 
00304         binding.objectToEntry("x", buffer);
00305         assertEquals("x", binding.entryToObject(buffer));
00306         assertEquals(FastOutputStream.DEFAULT_INIT_SIZE, binding.bufSize);
00307 
00308         binding.setTupleBufferSize(1000);
00309         binding.objectToEntry("x", buffer);
00310         assertEquals("x", binding.entryToObject(buffer));
00311         assertEquals(1000, binding.bufSize);
00312     }
00313 
00314     private class CaptureSizeBinding extends TupleBinding {
00315 
00316         int bufSize;
00317 
00318         CaptureSizeBinding() {
00319             super();
00320         }
00321 
00322         public TupleOutput getTupleOutput(Object object) {
00323             TupleOutput out = super.getTupleOutput(object);
00324             bufSize = out.getBufferBytes().length;
00325             return out;
00326         }
00327 
00328         public Object entryToObject(TupleInput input) {
00329             return input.readString();
00330         }
00331 
00332         public void objectToEntry(Object object, TupleOutput output) {
00333             assertEquals(bufSize, output.getBufferBytes().length);
00334             output.writeString((String) object);
00335         }
00336     }
00337 
00338     public void testBufferOverride() {
00339 
00340         TupleOutput out = new TupleOutput(new byte[10]);
00341         CachedOutputBinding binding = new CachedOutputBinding(out);
00342 
00343         binding.used = false;
00344         binding.objectToEntry("x", buffer);
00345         assertEquals("x", binding.entryToObject(buffer));
00346         assertTrue(binding.used);
00347 
00348         binding.used = false;
00349         binding.objectToEntry("aaaaaaaaaaaaaaaaaaaaaa", buffer);
00350         assertEquals("aaaaaaaaaaaaaaaaaaaaaa", binding.entryToObject(buffer));
00351         assertTrue(binding.used);
00352 
00353         binding.used = false;
00354         binding.objectToEntry("x", buffer);
00355         assertEquals("x", binding.entryToObject(buffer));
00356         assertTrue(binding.used);
00357     }
00358 
00359     private class CachedOutputBinding extends TupleBinding {
00360 
00361         TupleOutput out;
00362         boolean used;
00363 
00364         CachedOutputBinding(TupleOutput out) {
00365             super();
00366             this.out = out;
00367         }
00368 
00369         public TupleOutput getTupleOutput(Object object) {
00370             out.reset();
00371             used = true;
00372             return out;
00373         }
00374 
00375         public Object entryToObject(TupleInput input) {
00376             return input.readString();
00377         }
00378 
00379         public void objectToEntry(Object object, TupleOutput output) {
00380             assertSame(out, output);
00381             output.writeString((String) object);
00382         }
00383     }
00384 }

Generated on Sun Dec 25 12:14:53 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2