001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: SerialBindingTest.java,v 1.32.2.2 2008/01/07 15:14:22 cwl Exp $
007: */
008:
009: package com.sleepycat.bind.serial.test;
010:
011: import java.io.Serializable;
012:
013: import junit.framework.Test;
014: import junit.framework.TestCase;
015: import junit.framework.TestSuite;
016:
017: import com.sleepycat.bind.EntityBinding;
018: import com.sleepycat.bind.serial.ClassCatalog;
019: import com.sleepycat.bind.serial.SerialBinding;
020: import com.sleepycat.bind.serial.SerialSerialBinding;
021: import com.sleepycat.bind.serial.TupleSerialMarshalledBinding;
022: import com.sleepycat.collections.test.DbTestUtil;
023: import com.sleepycat.je.DatabaseEntry;
024: import com.sleepycat.util.ExceptionUnwrapper;
025: import com.sleepycat.util.FastOutputStream;
026:
027: /**
028: * @author Mark Hayes
029: */
030: public class SerialBindingTest extends TestCase {
031:
032: private ClassCatalog catalog;
033: private DatabaseEntry buffer;
034: private DatabaseEntry keyBuffer;
035:
036: public static void main(String[] args) throws Exception {
037:
038: junit.framework.TestResult tr = junit.textui.TestRunner
039: .run(suite());
040: if (tr.errorCount() > 0 || tr.failureCount() > 0) {
041: System.exit(1);
042: } else {
043: System.exit(0);
044: }
045: }
046:
047: public static Test suite() throws Exception {
048:
049: TestSuite suite = new TestSuite(SerialBindingTest.class);
050: return suite;
051: }
052:
053: public SerialBindingTest(String name) {
054:
055: super (name);
056: }
057:
058: public void setUp() {
059:
060: DbTestUtil.printTestName("SerialBindingTest." + getName());
061: catalog = new TestClassCatalog();
062: buffer = new DatabaseEntry();
063: keyBuffer = new DatabaseEntry();
064: }
065:
066: public void tearDown() {
067:
068: /* Ensure that GC can cleanup. */
069: catalog = null;
070: buffer = null;
071: keyBuffer = null;
072: }
073:
074: public void runTest() throws Throwable {
075:
076: try {
077: super .runTest();
078: } catch (Exception e) {
079: throw ExceptionUnwrapper.unwrap(e);
080: }
081: }
082:
083: private void primitiveBindingTest(Object val) {
084:
085: Class cls = val.getClass();
086: SerialBinding binding = new SerialBinding(catalog, cls);
087:
088: binding.objectToEntry(val, buffer);
089: assertTrue(buffer.getSize() > 0);
090:
091: Object val2 = binding.entryToObject(buffer);
092: assertSame(cls, val2.getClass());
093: assertEquals(val, val2);
094:
095: Object valWithWrongCls = (cls == String.class) ? ((Object) new Integer(
096: 0))
097: : ((Object) new String(""));
098: try {
099: binding.objectToEntry(valWithWrongCls, buffer);
100: } catch (IllegalArgumentException expected) {
101: }
102: }
103:
104: public void testPrimitiveBindings() {
105:
106: primitiveBindingTest("abc");
107: primitiveBindingTest(new Character('a'));
108: primitiveBindingTest(new Boolean(true));
109: primitiveBindingTest(new Byte((byte) 123));
110: primitiveBindingTest(new Short((short) 123));
111: primitiveBindingTest(new Integer(123));
112: primitiveBindingTest(new Long(123));
113: primitiveBindingTest(new Float(123.123));
114: primitiveBindingTest(new Double(123.123));
115: }
116:
117: public void testNullObjects() {
118:
119: SerialBinding binding = new SerialBinding(catalog, null);
120: buffer.setSize(0);
121: binding.objectToEntry(null, buffer);
122: assertTrue(buffer.getSize() > 0);
123: assertEquals(null, binding.entryToObject(buffer));
124: }
125:
126: public void testSerialSerialBinding() {
127:
128: SerialBinding keyBinding = new SerialBinding(catalog,
129: String.class);
130: SerialBinding valueBinding = new SerialBinding(catalog,
131: 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: // also tests TupleSerialBinding since TupleSerialMarshalledBinding extends
146: // it
147: public void testTupleSerialMarshalledBinding() {
148:
149: SerialBinding valueBinding = new SerialBinding(catalog,
150: MarshalledObject.class);
151: EntityBinding binding = new TupleSerialMarshalledBinding(
152: 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 = new CaptureSizeBinding(catalog,
173: String.class);
174:
175: binding.objectToEntry("x", buffer);
176: assertEquals("x", binding.entryToObject(buffer));
177: assertEquals(FastOutputStream.DEFAULT_INIT_SIZE,
178: binding.bufSize);
179:
180: binding.setSerialBufferSize(1000);
181: binding.objectToEntry("x", buffer);
182: assertEquals("x", binding.entryToObject(buffer));
183: assertEquals(1000, binding.bufSize);
184: }
185:
186: private static class CaptureSizeBinding extends SerialBinding {
187:
188: int bufSize;
189:
190: CaptureSizeBinding(ClassCatalog classCatalog, Class baseClass) {
191: super (classCatalog, baseClass);
192: }
193:
194: public FastOutputStream getSerialOutput(Object object) {
195: FastOutputStream fos = super .getSerialOutput(object);
196: bufSize = fos.getBufferBytes().length;
197: return fos;
198: }
199: }
200:
201: public void testBufferOverride() {
202:
203: FastOutputStream out = new FastOutputStream(10);
204: CachedOutputBinding binding = new CachedOutputBinding(catalog,
205: String.class, out);
206:
207: binding.used = false;
208: binding.objectToEntry("x", buffer);
209: assertEquals("x", binding.entryToObject(buffer));
210: assertTrue(binding.used);
211:
212: binding.used = false;
213: binding.objectToEntry("aaaaaaaaaaaaaaaaaaaaaa", buffer);
214: assertEquals("aaaaaaaaaaaaaaaaaaaaaa", binding
215: .entryToObject(buffer));
216: assertTrue(binding.used);
217:
218: binding.used = false;
219: binding.objectToEntry("x", buffer);
220: assertEquals("x", binding.entryToObject(buffer));
221: assertTrue(binding.used);
222: }
223:
224: private static class CachedOutputBinding extends SerialBinding {
225:
226: FastOutputStream out;
227: boolean used;
228:
229: CachedOutputBinding(ClassCatalog classCatalog, Class baseClass,
230: FastOutputStream out) {
231: super (classCatalog, baseClass);
232: this .out = out;
233: }
234:
235: public FastOutputStream getSerialOutput(Object object) {
236: out.reset();
237: used = true;
238: return out;
239: }
240: }
241:
242: private static class MySerialSerialBinding extends
243: SerialSerialBinding {
244:
245: private MySerialSerialBinding(SerialBinding keyBinding,
246: SerialBinding valueBinding) {
247:
248: super (keyBinding, valueBinding);
249: }
250:
251: public Object entryToObject(Object keyInput, Object valueInput) {
252:
253: return "" + keyInput + '#' + valueInput;
254: }
255:
256: public Object objectToKey(Object object) {
257:
258: String s = (String) object;
259: int i = s.indexOf('#');
260: if (i < 0 || i == s.length() - 1) {
261: throw new IllegalArgumentException(s);
262: } else {
263: return s.substring(0, i);
264: }
265: }
266:
267: public Object objectToData(Object object) {
268:
269: String s = (String) object;
270: int i = s.indexOf('#');
271: if (i < 0 || i == s.length() - 1) {
272: throw new IllegalArgumentException(s);
273: } else {
274: return s.substring(i + 1);
275: }
276: }
277: }
278:
279: /**
280: * Tests that overriding SerialBinding.getClassLoader is possible. This is
281: * a crude test because to create a truly working class loader is a large
282: * undertaking.
283: */
284: public void testClassloaderOverride() throws Exception {
285:
286: DatabaseEntry entry = new DatabaseEntry();
287:
288: SerialBinding binding = new CustomLoaderBinding(catalog, null,
289: new FailureClassLoader());
290:
291: try {
292: binding.objectToEntry(new MyClass(), entry);
293: binding.entryToObject(entry);
294: fail();
295: } catch (RuntimeException e) {
296: assertTrue(e.getMessage().startsWith("expect failure"));
297: }
298: }
299:
300: private static class CustomLoaderBinding extends SerialBinding {
301:
302: private ClassLoader loader;
303:
304: CustomLoaderBinding(ClassCatalog classCatalog, 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: }
|