01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2002,2008 Oracle. All rights reserved.
05: *
06: * $Id: LogUtilsTest.java,v 1.19.2.2 2008/01/07 15:14:29 cwl Exp $
07: */
08:
09: package com.sleepycat.je.log;
10:
11: import java.nio.ByteBuffer;
12: import java.sql.Timestamp;
13: import java.util.Arrays;
14: import java.util.Calendar;
15:
16: import junit.framework.TestCase;
17:
18: /**
19: * Test basic marshalling utilities
20: */
21: public class LogUtilsTest extends TestCase {
22:
23: public void testMarshalling() {
24: ByteBuffer dest = ByteBuffer.allocate(100);
25:
26: // unsigned ints
27: long unsignedData = 10;
28: dest.clear();
29: LogUtils.writeUnsignedInt(dest, unsignedData);
30: dest.flip();
31: assertEquals(unsignedData, LogUtils.getUnsignedInt(dest));
32:
33: unsignedData = 49249249L;
34: dest.clear();
35: LogUtils.writeUnsignedInt(dest, unsignedData);
36: dest.flip();
37: assertEquals(unsignedData, LogUtils.getUnsignedInt(dest));
38:
39: // ints
40: int intData = -1021;
41: dest.clear();
42: LogUtils.writeInt(dest, intData);
43: dest.flip();
44: assertEquals(intData, LogUtils.readInt(dest));
45:
46: intData = 257;
47: dest.clear();
48: LogUtils.writeInt(dest, intData);
49: dest.flip();
50: assertEquals(intData, LogUtils.readInt(dest));
51:
52: // longs
53: long longData = -1021;
54: dest.clear();
55: LogUtils.writeLong(dest, longData);
56: dest.flip();
57: assertEquals(longData, LogUtils.readLong(dest));
58:
59: // byte arrays
60: byte[] byteData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
61: 11, 12 };
62: dest.clear();
63: LogUtils.writeByteArray(dest, byteData);
64: dest.flip();
65: assertTrue(Arrays
66: .equals(byteData, LogUtils.readByteArray(dest)));
67:
68: // Strings
69: String stringData = "Hello world!";
70: dest.clear();
71: LogUtils.writeString(dest, stringData);
72: dest.flip();
73: assertEquals(stringData, LogUtils.readString(dest));
74:
75: // Timestamps
76: Timestamp timestampData = new Timestamp(Calendar.getInstance()
77: .getTimeInMillis());
78: dest.clear();
79: LogUtils.writeTimestamp(dest, timestampData);
80: dest.flip();
81: assertEquals(timestampData, LogUtils.readTimestamp(dest));
82:
83: // Booleans
84: boolean boolData = true;
85: dest.clear();
86: LogUtils.writeBoolean(dest, boolData);
87: dest.flip();
88: assertEquals(boolData, LogUtils.readBoolean(dest));
89: }
90: }
|