1 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 42 public class TupleBindingTest extends TestCase { 43 44 private DatabaseEntry buffer; 45 private DatabaseEntry keyBuffer; 46 47 public static void main(String [] args) 48 throws Exception { 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 { 62 63 TestSuite suite = new TestSuite(TupleBindingTest.class); 64 return suite; 65 } 66 67 public TupleBindingTest(String 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 82 buffer = null; 83 keyBuffer = null; 84 } 85 86 public void runTest() 87 throws Throwable { 88 89 try { 90 super.runTest(); 91 } catch (Exception e) { 92 throw ExceptionUnwrapper.unwrap(e); 93 } 94 } 95 96 private void primitiveBindingTest(Class primitiveCls, Class compareCls, 97 Object val, int byteSize) { 98 99 TupleBinding binding = TupleBinding.getPrimitiveBinding(primitiveCls); 100 101 102 103 binding.objectToEntry(val, buffer); 104 assertEquals(byteSize, buffer.getSize()); 105 106 Object val2 = binding.entryToObject(buffer); 107 assertSame(compareCls, val2.getClass()); 108 assertEquals(val, val2); 109 110 Object valWithWrongCls = (primitiveCls == String .class) 111 ? ((Object ) new Integer (0)) : ((Object ) new String ("")); 112 try { 113 binding.objectToEntry(valWithWrongCls, buffer); 114 } 115 catch (ClassCastException expected) {} 116 117 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 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 .class, String .class, 137 "abc", 4); 138 139 primitiveBindingTest(Character .class, Character .class, 140 new Character ('a'), 2); 141 primitiveBindingTest(Boolean .class, Boolean .class, 142 new Boolean (true), 1); 143 primitiveBindingTest(Byte .class, Byte .class, 144 new Byte ((byte) 123), 1); 145 primitiveBindingTest(Short .class, Short .class, 146 new Short ((short) 123), 2); 147 primitiveBindingTest(Integer .class, Integer .class, 148 new Integer (123), 4); 149 primitiveBindingTest(Long .class, Long .class, 150 new Long (123), 8); 151 primitiveBindingTest(Float .class, Float .class, 152 new Float (123.123), 4); 153 primitiveBindingTest(Double .class, Double .class, 154 new Double (123.123), 8); 155 156 primitiveBindingTest(Character.TYPE, Character .class, 157 new Character ('a'), 2); 158 primitiveBindingTest(Boolean.TYPE, Boolean .class, 159 new Boolean (true), 1); 160 primitiveBindingTest(Byte.TYPE, Byte .class, 161 new Byte ((byte) 123), 1); 162 primitiveBindingTest(Short.TYPE, Short .class, 163 new Short ((short) 123), 2); 164 primitiveBindingTest(Integer.TYPE, Integer .class, 165 new Integer (123), 4); 166 primitiveBindingTest(Long.TYPE, Long .class, 167 new Long (123), 8); 168 primitiveBindingTest(Float.TYPE, Float .class, 169 new Float (123.123), 4); 170 primitiveBindingTest(Double.TYPE, Double .class, 171 new Double (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 ('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 ((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 (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 (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 ((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 (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 ((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 (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 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 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 result = binding.entryToObject(buffer); 287 assertTrue(result instanceof MarshalledObject); 288 val = (MarshalledObject) result; 289 assertEquals("abc", val.getData()); 290 } 291 292 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 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 object) { 338 TupleOutput out = super.getTupleOutput(object); 339 bufSize = out.getBufferBytes().length; 340 return out; 341 } 342 343 public Object entryToObject(TupleInput input) { 344 return input.readString(); 345 } 346 347 public void objectToEntry(Object object, TupleOutput output) { 348 assertEquals(bufSize, output.getBufferBytes().length); 349 output.writeString((String ) 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 object) { 385 out.reset(); 386 used = true; 387 return out; 388 } 389 390 public Object entryToObject(TupleInput input) { 391 return input.readString(); 392 } 393 394 public void objectToEntry(Object object, TupleOutput output) { 395 assertSame(out, output); 396 output.writeString((String ) object); 397 } 398 } 399 } 400 | Popular Tags |