1 8 9 package com.sleepycat.bind.serial.test; 10 11 import java.io.Serializable ; 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 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 [] args) 37 throws Exception { 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 { 51 52 TestSuite suite = new TestSuite(SerialBindingTest.class); 53 return suite; 54 } 55 56 public SerialBindingTest(String 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 72 catalog = null; 73 buffer = null; 74 keyBuffer = null; 75 } 76 77 public void runTest() 78 throws Throwable { 79 80 try { 81 super.runTest(); 82 } catch (Exception e) { 83 throw ExceptionUnwrapper.unwrap(e); 84 } 85 } 86 87 private void primitiveBindingTest(Object val) { 88 89 Class cls = val.getClass(); 90 SerialBinding binding = new SerialBinding(catalog, cls); 91 92 binding.objectToEntry(val, buffer); 93 assertTrue(buffer.getSize() > 0); 94 95 Object val2 = binding.entryToObject(buffer); 96 assertSame(cls, val2.getClass()); 97 assertEquals(val, val2); 98 99 Object valWithWrongCls = (cls == String .class) 100 ? ((Object ) new Integer (0)) : ((Object ) new String ("")); 101 try { 102 binding.objectToEntry(valWithWrongCls, buffer); 103 } catch (IllegalArgumentException expected) {} 104 } 105 106 public void testPrimitiveBindings() { 107 108 primitiveBindingTest("abc"); 109 primitiveBindingTest(new Character ('a')); 110 primitiveBindingTest(new Boolean (true)); 111 primitiveBindingTest(new Byte ((byte) 123)); 112 primitiveBindingTest(new Short ((short) 123)); 113 primitiveBindingTest(new Integer (123)); 114 primitiveBindingTest(new Long (123)); 115 primitiveBindingTest(new Float (123.123)); 116 primitiveBindingTest(new Double (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 .class); 131 SerialBinding valueBinding = new SerialBinding(catalog, String .class); 132 EntityBinding binding = new MySerialSerialBinding(keyBinding, 133 valueBinding); 134 135 String 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 result = binding.entryToObject(keyBuffer, buffer); 142 assertEquals(val, result); 143 } 144 145 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 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 .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 baseClass) { 190 super(classCatalog, baseClass); 191 } 192 193 public FastOutputStream getSerialOutput(Object 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 .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 baseClass, 229 FastOutputStream out) { 230 super(classCatalog, baseClass); 231 this.out = out; 232 } 233 234 public FastOutputStream getSerialOutput(Object 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 entryToObject(Object keyInput, Object valueInput) { 250 251 return "" + keyInput + '#' + valueInput; 252 } 253 254 public Object objectToKey(Object object) { 255 256 String s = (String ) object; 257 int i = s.indexOf('#'); 258 if (i < 0 || i == s.length() - 1) { 259 throw new IllegalArgumentException (s); 260 } else { 261 return s.substring(0, i); 262 } 263 } 264 265 public Object objectToData(Object object) { 266 267 String s = (String ) object; 268 int i = s.indexOf('#'); 269 if (i < 0 || i == s.length() - 1) { 270 throw new IllegalArgumentException (s); 271 } else { 272 return s.substring(i + 1); 273 } 274 } 275 } 276 277 282 public void testClassloaderOverride() 283 throws Exception { 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 e) { 295 assertTrue(e.getMessage().startsWith("expect failure")); 296 } 297 } 298 299 private static class CustomLoaderBinding extends SerialBinding { 300 301 private ClassLoader loader; 302 303 CustomLoaderBinding(ClassCatalog classCatalog, 304 Class baseClass, 305 ClassLoader loader) { 306 307 super(classCatalog, baseClass); 308 this.loader = loader; 309 } 310 311 public ClassLoader getClassLoader() { 312 return loader; 313 } 314 } 315 316 private static class FailureClassLoader extends ClassLoader { 317 318 public Class loadClass(String name) 319 throws ClassNotFoundException { 320 321 throw new RuntimeException ("expect failure: " + name); 322 } 323 } 324 325 private static class MyClass implements Serializable { 326 } 327 } 328 | Popular Tags |