00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.bind.serial.test;
00011
00012 import java.io.Serializable;
00013
00014 import junit.framework.Test;
00015 import junit.framework.TestCase;
00016 import junit.framework.TestSuite;
00017
00018 import com.sleepycat.bind.EntityBinding;
00019 import com.sleepycat.bind.serial.ClassCatalog;
00020 import com.sleepycat.bind.serial.SerialBinding;
00021 import com.sleepycat.bind.serial.SerialSerialBinding;
00022 import com.sleepycat.bind.serial.TupleSerialMarshalledBinding;
00023 import com.sleepycat.collections.test.DbTestUtil;
00024 import com.sleepycat.db.DatabaseEntry;
00025 import com.sleepycat.util.ExceptionUnwrapper;
00026 import com.sleepycat.util.FastOutputStream;
00027
00031 public class SerialBindingTest extends TestCase {
00032
00033 private ClassCatalog catalog;
00034 private DatabaseEntry buffer;
00035 private DatabaseEntry keyBuffer;
00036 private DatabaseEntry indexKeyBuffer;
00037
00038 public static void main(String[] args)
00039 throws Exception {
00040
00041 junit.framework.TestResult tr =
00042 junit.textui.TestRunner.run(suite());
00043 if (tr.errorCount() > 0 ||
00044 tr.failureCount() > 0) {
00045 System.exit(1);
00046 } else {
00047 System.exit(0);
00048 }
00049 }
00050
00051 public static Test suite()
00052 throws Exception {
00053
00054 TestSuite suite = new TestSuite(SerialBindingTest.class);
00055 return suite;
00056 }
00057
00058 public SerialBindingTest(String name) {
00059
00060 super(name);
00061 }
00062
00063 public void setUp() {
00064
00065 DbTestUtil.printTestName("SerialBindingTest." + getName());
00066 catalog = new TestClassCatalog();
00067 buffer = new DatabaseEntry();
00068 keyBuffer = new DatabaseEntry();
00069 indexKeyBuffer = new DatabaseEntry();
00070 }
00071
00072 public void tearDown() {
00073
00074
00075 catalog = null;
00076 buffer = null;
00077 keyBuffer = null;
00078 indexKeyBuffer = null;
00079 }
00080
00081 public void runTest()
00082 throws Throwable {
00083
00084 try {
00085 super.runTest();
00086 } catch (Exception e) {
00087 throw ExceptionUnwrapper.unwrap(e);
00088 }
00089 }
00090
00091 private void primitiveBindingTest(Object val) {
00092
00093 Class cls = val.getClass();
00094 SerialBinding binding = new SerialBinding(catalog, cls);
00095
00096 binding.objectToEntry(val, buffer);
00097 assertTrue(buffer.getSize() > 0);
00098
00099 Object val2 = binding.entryToObject(buffer);
00100 assertSame(cls, val2.getClass());
00101 assertEquals(val, val2);
00102
00103 Object valWithWrongCls = (cls == String.class)
00104 ? ((Object) new Integer(0)) : ((Object) new String(""));
00105 try {
00106 binding.objectToEntry(valWithWrongCls, buffer);
00107 } catch (IllegalArgumentException expected) {}
00108 }
00109
00110 public void testPrimitiveBindings() {
00111
00112 primitiveBindingTest("abc");
00113 primitiveBindingTest(new Character('a'));
00114 primitiveBindingTest(new Boolean(true));
00115 primitiveBindingTest(new Byte((byte) 123));
00116 primitiveBindingTest(new Short((short) 123));
00117 primitiveBindingTest(new Integer(123));
00118 primitiveBindingTest(new Long(123));
00119 primitiveBindingTest(new Float(123.123));
00120 primitiveBindingTest(new Double(123.123));
00121 }
00122
00123 public void testNullObjects() {
00124
00125 SerialBinding binding = new SerialBinding(catalog, null);
00126 buffer.setSize(0);
00127 binding.objectToEntry(null, buffer);
00128 assertTrue(buffer.getSize() > 0);
00129 assertEquals(null, binding.entryToObject(buffer));
00130 }
00131
00132 public void testSerialSerialBinding() {
00133
00134 SerialBinding keyBinding = new SerialBinding(catalog, String.class);
00135 SerialBinding valueBinding = new SerialBinding(catalog, String.class);
00136 EntityBinding binding = new MySerialSerialBinding(keyBinding,
00137 valueBinding);
00138
00139 String val = "key#value?indexKey";
00140 binding.objectToData(val, buffer);
00141 assertTrue(buffer.getSize() > 0);
00142 binding.objectToKey(val, keyBuffer);
00143 assertTrue(keyBuffer.getSize() > 0);
00144
00145 Object result = binding.entryToObject(keyBuffer, buffer);
00146 assertEquals(val, result);
00147 }
00148
00149
00150
00151 public void testTupleSerialMarshalledBinding() {
00152
00153 SerialBinding valueBinding = new SerialBinding(catalog,
00154 MarshalledObject.class);
00155 EntityBinding binding =
00156 new TupleSerialMarshalledBinding(valueBinding);
00157
00158 MarshalledObject val = new MarshalledObject("abc", "primary",
00159 "index1", "index2");
00160 binding.objectToData(val, buffer);
00161 assertTrue(buffer.getSize() > 0);
00162 binding.objectToKey(val, keyBuffer);
00163 assertEquals(val.expectedKeyLength(), keyBuffer.getSize());
00164
00165 Object result = binding.entryToObject(keyBuffer, buffer);
00166 assertTrue(result instanceof MarshalledObject);
00167 val = (MarshalledObject) result;
00168 assertEquals("abc", val.getData());
00169 assertEquals("primary", val.getPrimaryKey());
00170 assertEquals("index1", val.getIndexKey1());
00171 assertEquals("index2", val.getIndexKey2());
00172 }
00173
00174 public void testBufferSize() {
00175
00176 CaptureSizeBinding binding =
00177 new CaptureSizeBinding(catalog, String.class);
00178
00179 binding.objectToEntry("x", buffer);
00180 assertEquals("x", binding.entryToObject(buffer));
00181 assertEquals(FastOutputStream.DEFAULT_INIT_SIZE, binding.bufSize);
00182
00183 binding.setSerialBufferSize(1000);
00184 binding.objectToEntry("x", buffer);
00185 assertEquals("x", binding.entryToObject(buffer));
00186 assertEquals(1000, binding.bufSize);
00187 }
00188
00189 private static class CaptureSizeBinding extends SerialBinding {
00190
00191 int bufSize;
00192
00193 CaptureSizeBinding(ClassCatalog classCatalog, Class baseClass) {
00194 super(classCatalog, baseClass);
00195 }
00196
00197 public FastOutputStream getSerialOutput(Object object) {
00198 FastOutputStream fos = super.getSerialOutput(object);
00199 bufSize = fos.getBufferBytes().length;
00200 return fos;
00201 }
00202 }
00203
00204 public void testBufferOverride() {
00205
00206 FastOutputStream out = new FastOutputStream(10);
00207 CachedOutputBinding binding =
00208 new CachedOutputBinding(catalog, String.class, out);
00209
00210 binding.used = false;
00211 binding.objectToEntry("x", buffer);
00212 assertEquals("x", binding.entryToObject(buffer));
00213 assertTrue(binding.used);
00214
00215 binding.used = false;
00216 binding.objectToEntry("aaaaaaaaaaaaaaaaaaaaaa", buffer);
00217 assertEquals("aaaaaaaaaaaaaaaaaaaaaa", binding.entryToObject(buffer));
00218 assertTrue(binding.used);
00219
00220 binding.used = false;
00221 binding.objectToEntry("x", buffer);
00222 assertEquals("x", binding.entryToObject(buffer));
00223 assertTrue(binding.used);
00224 }
00225
00226 private static class CachedOutputBinding extends SerialBinding {
00227
00228 FastOutputStream out;
00229 boolean used;
00230
00231 CachedOutputBinding(ClassCatalog classCatalog,
00232 Class baseClass,
00233 FastOutputStream out) {
00234 super(classCatalog, baseClass);
00235 this.out = out;
00236 }
00237
00238 public FastOutputStream getSerialOutput(Object object) {
00239 out.reset();
00240 used = true;
00241 return out;
00242 }
00243 }
00244
00245 private static class MySerialSerialBinding extends SerialSerialBinding {
00246
00247 private MySerialSerialBinding(SerialBinding keyBinding,
00248 SerialBinding valueBinding) {
00249
00250 super(keyBinding, valueBinding);
00251 }
00252
00253 public Object entryToObject(Object keyInput, Object valueInput) {
00254
00255 return "" + keyInput + '#' + valueInput;
00256 }
00257
00258 public Object objectToKey(Object object) {
00259
00260 String s = (String) object;
00261 int i = s.indexOf('#');
00262 if (i < 0 || i == s.length() - 1) {
00263 throw new IllegalArgumentException(s);
00264 } else {
00265 return s.substring(0, i);
00266 }
00267 }
00268
00269 public Object objectToData(Object object) {
00270
00271 String s = (String) object;
00272 int i = s.indexOf('#');
00273 if (i < 0 || i == s.length() - 1) {
00274 throw new IllegalArgumentException(s);
00275 } else {
00276 return s.substring(i + 1);
00277 }
00278 }
00279 }
00280
00286 public void testClassloaderOverride()
00287 throws Exception {
00288
00289 DatabaseEntry entry = new DatabaseEntry();
00290
00291 SerialBinding binding = new CustomLoaderBinding
00292 (catalog, null, new FailureClassLoader());
00293
00294 try {
00295 binding.objectToEntry(new MyClass(), entry);
00296 binding.entryToObject(entry);
00297 fail();
00298 } catch (RuntimeException e) {
00299 assertTrue(e.getMessage().startsWith("expect failure"));
00300 }
00301 }
00302
00303 private static class CustomLoaderBinding extends SerialBinding {
00304
00305 private ClassLoader loader;
00306
00307 CustomLoaderBinding(ClassCatalog classCatalog,
00308 Class baseClass,
00309 ClassLoader loader) {
00310
00311 super(classCatalog, baseClass);
00312 this.loader = loader;
00313 }
00314
00315 public ClassLoader getClassLoader() {
00316 return loader;
00317 }
00318 }
00319
00320 private static class FailureClassLoader extends ClassLoader {
00321
00322 public Class loadClass(String name)
00323 throws ClassNotFoundException {
00324
00325 throw new RuntimeException("expect failure: " + name);
00326 }
00327 }
00328
00329 private static class MyClass implements Serializable {
00330 }
00331 }