001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: TupleBindingTest.java,v 1.35.2.2 2008/01/07 15:14:23 cwl Exp $
007: */
008:
009: package com.sleepycat.bind.tuple.test;
010:
011: import junit.framework.Test;
012: import junit.framework.TestCase;
013: import junit.framework.TestSuite;
014:
015: import com.sleepycat.bind.EntityBinding;
016: import com.sleepycat.bind.EntryBinding;
017: import com.sleepycat.bind.tuple.BooleanBinding;
018: import com.sleepycat.bind.tuple.ByteBinding;
019: import com.sleepycat.bind.tuple.CharacterBinding;
020: import com.sleepycat.bind.tuple.DoubleBinding;
021: import com.sleepycat.bind.tuple.FloatBinding;
022: import com.sleepycat.bind.tuple.IntegerBinding;
023: import com.sleepycat.bind.tuple.LongBinding;
024: import com.sleepycat.bind.tuple.ShortBinding;
025: import com.sleepycat.bind.tuple.SortedDoubleBinding;
026: import com.sleepycat.bind.tuple.SortedFloatBinding;
027: import com.sleepycat.bind.tuple.StringBinding;
028: import com.sleepycat.bind.tuple.TupleBinding;
029: import com.sleepycat.bind.tuple.TupleInput;
030: import com.sleepycat.bind.tuple.TupleInputBinding;
031: import com.sleepycat.bind.tuple.TupleMarshalledBinding;
032: import com.sleepycat.bind.tuple.TupleOutput;
033: import com.sleepycat.bind.tuple.TupleTupleMarshalledBinding;
034: import com.sleepycat.collections.test.DbTestUtil;
035: import com.sleepycat.je.DatabaseEntry;
036: import com.sleepycat.util.FastOutputStream;
037: import com.sleepycat.util.ExceptionUnwrapper;
038:
039: /**
040: * @author Mark Hayes
041: */
042: public class TupleBindingTest extends TestCase {
043:
044: private DatabaseEntry buffer;
045: private DatabaseEntry keyBuffer;
046:
047: public static void main(String[] args) throws Exception {
048:
049: junit.framework.TestResult tr = junit.textui.TestRunner
050: .run(suite());
051: if (tr.errorCount() > 0 || tr.failureCount() > 0) {
052: System.exit(1);
053: } else {
054: System.exit(0);
055: }
056: }
057:
058: public static Test suite() throws Exception {
059:
060: TestSuite suite = new TestSuite(TupleBindingTest.class);
061: return suite;
062: }
063:
064: public TupleBindingTest(String name) {
065:
066: super (name);
067: }
068:
069: public void setUp() {
070:
071: DbTestUtil.printTestName("TupleBindingTest." + getName());
072: buffer = new DatabaseEntry();
073: keyBuffer = new DatabaseEntry();
074: }
075:
076: public void tearDown() {
077:
078: /* Ensure that GC can cleanup. */
079: buffer = null;
080: keyBuffer = null;
081: }
082:
083: public void runTest() throws Throwable {
084:
085: try {
086: super .runTest();
087: } catch (Exception e) {
088: throw ExceptionUnwrapper.unwrap(e);
089: }
090: }
091:
092: private void primitiveBindingTest(Class primitiveCls,
093: Class compareCls, Object val, int byteSize) {
094:
095: TupleBinding binding = TupleBinding
096: .getPrimitiveBinding(primitiveCls);
097:
098: /* Test standard object binding. */
099:
100: binding.objectToEntry(val, buffer);
101: assertEquals(byteSize, buffer.getSize());
102:
103: Object val2 = binding.entryToObject(buffer);
104: assertSame(compareCls, val2.getClass());
105: assertEquals(val, val2);
106:
107: Object valWithWrongCls = (primitiveCls == String.class) ? ((Object) new Integer(
108: 0))
109: : ((Object) new String(""));
110: try {
111: binding.objectToEntry(valWithWrongCls, buffer);
112: } catch (ClassCastException expected) {
113: }
114:
115: /* Test nested tuple binding. */
116:
117: TupleOutput output = new TupleOutput();
118: output.writeString("abc");
119: binding.objectToEntry(val, output);
120: output.writeString("xyz");
121:
122: TupleInput input = new TupleInput(output);
123: assertEquals("abc", input.readString());
124: Object val3 = binding.entryToObject(input);
125: assertEquals("xyz", input.readString());
126:
127: assertEquals(0, input.available());
128: assertSame(compareCls, val3.getClass());
129: assertEquals(val, val3);
130: }
131:
132: public void testPrimitiveBindings() {
133:
134: primitiveBindingTest(String.class, String.class, "abc", 4);
135:
136: primitiveBindingTest(Character.class, Character.class,
137: new Character('a'), 2);
138: primitiveBindingTest(Boolean.class, Boolean.class, new Boolean(
139: true), 1);
140: primitiveBindingTest(Byte.class, Byte.class, new Byte(
141: (byte) 123), 1);
142: primitiveBindingTest(Short.class, Short.class, new Short(
143: (short) 123), 2);
144: primitiveBindingTest(Integer.class, Integer.class, new Integer(
145: 123), 4);
146: primitiveBindingTest(Long.class, Long.class, new Long(123), 8);
147: primitiveBindingTest(Float.class, Float.class, new Float(
148: 123.123), 4);
149: primitiveBindingTest(Double.class, Double.class, new Double(
150: 123.123), 8);
151:
152: primitiveBindingTest(Character.TYPE, Character.class,
153: new Character('a'), 2);
154: primitiveBindingTest(Boolean.TYPE, Boolean.class, new Boolean(
155: true), 1);
156: primitiveBindingTest(Byte.TYPE, Byte.class,
157: new Byte((byte) 123), 1);
158: primitiveBindingTest(Short.TYPE, Short.class, new Short(
159: (short) 123), 2);
160: primitiveBindingTest(Integer.TYPE, Integer.class, new Integer(
161: 123), 4);
162: primitiveBindingTest(Long.TYPE, Long.class, new Long(123), 8);
163: primitiveBindingTest(Float.TYPE, Float.class,
164: new Float(123.123), 4);
165: primitiveBindingTest(Double.TYPE, Double.class, new Double(
166: 123.123), 8);
167:
168: DatabaseEntry entry = new DatabaseEntry();
169:
170: StringBinding.stringToEntry("abc", entry);
171: assertEquals(4, entry.getData().length);
172: assertEquals("abc", StringBinding.entryToString(entry));
173:
174: new StringBinding().objectToEntry("abc", entry);
175: assertEquals(4, entry.getData().length);
176:
177: StringBinding.stringToEntry(null, entry);
178: assertEquals(2, entry.getData().length);
179: assertEquals(null, StringBinding.entryToString(entry));
180:
181: new StringBinding().objectToEntry(null, entry);
182: assertEquals(2, entry.getData().length);
183:
184: CharacterBinding.charToEntry('a', entry);
185: assertEquals(2, entry.getData().length);
186: assertEquals('a', CharacterBinding.entryToChar(entry));
187:
188: new CharacterBinding().objectToEntry(new Character('a'), entry);
189: assertEquals(2, entry.getData().length);
190:
191: BooleanBinding.booleanToEntry(true, entry);
192: assertEquals(1, entry.getData().length);
193: assertEquals(true, BooleanBinding.entryToBoolean(entry));
194:
195: new BooleanBinding().objectToEntry(Boolean.TRUE, entry);
196: assertEquals(1, entry.getData().length);
197:
198: ByteBinding.byteToEntry((byte) 123, entry);
199: assertEquals(1, entry.getData().length);
200: assertEquals((byte) 123, ByteBinding.entryToByte(entry));
201:
202: ShortBinding.shortToEntry((short) 123, entry);
203: assertEquals(2, entry.getData().length);
204: assertEquals((short) 123, ShortBinding.entryToShort(entry));
205:
206: new ByteBinding().objectToEntry(new Byte((byte) 123), entry);
207: assertEquals(1, entry.getData().length);
208:
209: IntegerBinding.intToEntry(123, entry);
210: assertEquals(4, entry.getData().length);
211: assertEquals(123, IntegerBinding.entryToInt(entry));
212:
213: new IntegerBinding().objectToEntry(new Integer(123), entry);
214: assertEquals(4, entry.getData().length);
215:
216: LongBinding.longToEntry(123, entry);
217: assertEquals(8, entry.getData().length);
218: assertEquals(123, LongBinding.entryToLong(entry));
219:
220: new LongBinding().objectToEntry(new Long(123), entry);
221: assertEquals(8, entry.getData().length);
222:
223: FloatBinding.floatToEntry((float) 123.123, entry);
224: assertEquals(4, entry.getData().length);
225: assertTrue(((float) 123.123) == FloatBinding
226: .entryToFloat(entry));
227:
228: new FloatBinding().objectToEntry(new Float((float) 123.123),
229: entry);
230: assertEquals(4, entry.getData().length);
231:
232: DoubleBinding.doubleToEntry(123.123, entry);
233: assertEquals(8, entry.getData().length);
234: assertTrue(123.123 == DoubleBinding.entryToDouble(entry));
235:
236: new DoubleBinding().objectToEntry(new Double(123.123), entry);
237: assertEquals(8, entry.getData().length);
238:
239: SortedFloatBinding.floatToEntry((float) 123.123, entry);
240: assertEquals(4, entry.getData().length);
241: assertTrue(((float) 123.123) == SortedFloatBinding
242: .entryToFloat(entry));
243:
244: new SortedFloatBinding().objectToEntry(new Float(
245: (float) 123.123), entry);
246: assertEquals(4, entry.getData().length);
247:
248: SortedDoubleBinding.doubleToEntry(123.123, entry);
249: assertEquals(8, entry.getData().length);
250: assertTrue(123.123 == SortedDoubleBinding.entryToDouble(entry));
251:
252: new SortedDoubleBinding().objectToEntry(new Double(123.123),
253: entry);
254: assertEquals(8, entry.getData().length);
255: }
256:
257: public void testTupleInputBinding() {
258:
259: EntryBinding binding = new TupleInputBinding();
260:
261: TupleOutput out = new TupleOutput();
262: out.writeString("abc");
263: binding.objectToEntry(new TupleInput(out), buffer);
264: assertEquals(4, buffer.getSize());
265:
266: Object result = binding.entryToObject(buffer);
267: assertTrue(result instanceof TupleInput);
268: TupleInput in = (TupleInput) result;
269: assertEquals("abc", in.readString());
270: assertEquals(0, in.available());
271: }
272:
273: // also tests TupleBinding since TupleMarshalledBinding extends it
274: public void testTupleMarshalledBinding() {
275:
276: EntryBinding binding = new TupleMarshalledBinding(
277: MarshalledObject.class);
278:
279: MarshalledObject val = new MarshalledObject("abc", "", "", "");
280: binding.objectToEntry(val, buffer);
281: assertEquals(val.expectedDataLength(), buffer.getSize());
282:
283: Object result = binding.entryToObject(buffer);
284: assertTrue(result instanceof MarshalledObject);
285: val = (MarshalledObject) result;
286: assertEquals("abc", val.getData());
287: }
288:
289: // also tests TupleTupleBinding since TupleTupleMarshalledBinding extends
290: // it
291: public void testTupleTupleMarshalledBinding() {
292:
293: EntityBinding binding = new TupleTupleMarshalledBinding(
294: MarshalledObject.class);
295:
296: MarshalledObject val = new MarshalledObject("abc", "primary",
297: "index1", "index2");
298: binding.objectToData(val, buffer);
299: assertEquals(val.expectedDataLength(), buffer.getSize());
300: binding.objectToKey(val, keyBuffer);
301: assertEquals(val.expectedKeyLength(), keyBuffer.getSize());
302:
303: Object result = binding.entryToObject(keyBuffer, buffer);
304: assertTrue(result instanceof MarshalledObject);
305: val = (MarshalledObject) result;
306: assertEquals("abc", val.getData());
307: assertEquals("primary", val.getPrimaryKey());
308: assertEquals("index1", val.getIndexKey1());
309: assertEquals("index2", val.getIndexKey2());
310: }
311:
312: public void testBufferSize() {
313:
314: CaptureSizeBinding binding = new CaptureSizeBinding();
315:
316: binding.objectToEntry("x", buffer);
317: assertEquals("x", binding.entryToObject(buffer));
318: assertEquals(FastOutputStream.DEFAULT_INIT_SIZE,
319: binding.bufSize);
320:
321: binding.setTupleBufferSize(1000);
322: binding.objectToEntry("x", buffer);
323: assertEquals("x", binding.entryToObject(buffer));
324: assertEquals(1000, binding.bufSize);
325: }
326:
327: private class CaptureSizeBinding extends TupleBinding {
328:
329: int bufSize;
330:
331: CaptureSizeBinding() {
332: super ();
333: }
334:
335: public TupleOutput getTupleOutput(Object object) {
336: TupleOutput out = super .getTupleOutput(object);
337: bufSize = out.getBufferBytes().length;
338: return out;
339: }
340:
341: public Object entryToObject(TupleInput input) {
342: return input.readString();
343: }
344:
345: public void objectToEntry(Object object, TupleOutput output) {
346: assertEquals(bufSize, output.getBufferBytes().length);
347: output.writeString((String) object);
348: }
349: }
350:
351: public void testBufferOverride() {
352:
353: TupleOutput out = new TupleOutput(new byte[10]);
354: CachedOutputBinding binding = new CachedOutputBinding(out);
355:
356: binding.used = false;
357: binding.objectToEntry("x", buffer);
358: assertEquals("x", binding.entryToObject(buffer));
359: assertTrue(binding.used);
360:
361: binding.used = false;
362: binding.objectToEntry("aaaaaaaaaaaaaaaaaaaaaa", buffer);
363: assertEquals("aaaaaaaaaaaaaaaaaaaaaa", binding
364: .entryToObject(buffer));
365: assertTrue(binding.used);
366:
367: binding.used = false;
368: binding.objectToEntry("x", buffer);
369: assertEquals("x", binding.entryToObject(buffer));
370: assertTrue(binding.used);
371: }
372:
373: private class CachedOutputBinding extends TupleBinding {
374:
375: TupleOutput out;
376: boolean used;
377:
378: CachedOutputBinding(TupleOutput out) {
379: super ();
380: this .out = out;
381: }
382:
383: public TupleOutput getTupleOutput(Object object) {
384: out.reset();
385: used = true;
386: return out;
387: }
388:
389: public Object entryToObject(TupleInput input) {
390: return input.readString();
391: }
392:
393: public void objectToEntry(Object object, TupleOutput output) {
394: assertSame(out, output);
395: output.writeString((String) object);
396: }
397: }
398: }
|