File: LogUtilsTest.java

package info (click to toggle)
libdb-je-java 3.3.98-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,052 kB
  • sloc: java: 153,077; xml: 2,034; makefile: 3
file content (155 lines) | stat: -rw-r--r-- 5,018 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002,2010 Oracle.  All rights reserved.
 *
 * $Id: LogUtilsTest.java,v 1.24.2.2 2010/01/04 15:30:44 cwl Exp $
 */

package com.sleepycat.je.log;

import java.nio.ByteBuffer;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Calendar;

import junit.framework.TestCase;

/**
 *  Test basic marshalling utilities
 */
public class LogUtilsTest extends TestCase {

    public void testMarshalling() {
        ByteBuffer dest = ByteBuffer.allocate(100);

        // unsigned ints
        long unsignedData = 10;
        dest.clear();
        LogUtils.writeUnsignedInt(dest, unsignedData);
        assertEquals(LogUtils.UNSIGNED_INT_BYTES, dest.position());
        dest.flip();
        assertEquals(unsignedData, LogUtils.readUnsignedInt(dest));

        unsignedData = 49249249L;
        dest.clear();
        LogUtils.writeUnsignedInt(dest, unsignedData);
        assertEquals(LogUtils.UNSIGNED_INT_BYTES, dest.position());
        dest.flip();
        assertEquals(unsignedData, LogUtils.readUnsignedInt(dest));

        // ints
        int intData = -1021;
        dest.clear();
        LogUtils.writeInt(dest, intData);
        assertEquals(LogUtils.INT_BYTES, dest.position());
        dest.flip();
        assertEquals(intData, LogUtils.readInt(dest));

        intData = 257;
        dest.clear();
        LogUtils.writeInt(dest, intData);
        assertEquals(LogUtils.INT_BYTES, dest.position());
        dest.flip();
        assertEquals(intData, LogUtils.readInt(dest));

        // longs
        long longData = -1021;
        dest.clear();
        LogUtils.writeLong(dest, longData);
        assertEquals(LogUtils.LONG_BYTES, dest.position());
        dest.flip();
        assertEquals(longData, LogUtils.readLong(dest));

        // byte arrays
        byte[] byteData = new byte[] {1,2,3,4,5,6,7,8,9,10,11,12};
        dest.clear();
        LogUtils.writeByteArray(dest, byteData);
        assertEquals(LogUtils.getPackedIntLogSize(12) + 12, dest.position());
        dest.flip();
        assertTrue(Arrays.equals(byteData,
                                 LogUtils.readByteArray(dest,
                                                        false/*unpacked*/)));

        // Strings
        String stringData = "Hello world!";
        dest.clear();
        LogUtils.writeString(dest, stringData);
        assertEquals(LogUtils.INT_BYTES + 9, dest.position());
        dest.flip();
        assertEquals(stringData, LogUtils.readString(dest, false/*unpacked*/));

        // Timestamps
        Timestamp timestampData =
            new Timestamp(Calendar.getInstance().getTimeInMillis());
        dest.clear();
        LogUtils.writeTimestamp(dest, timestampData);
        assertEquals(LogUtils.getTimestampLogSize(timestampData),
                     dest.position());
        dest.flip();
        assertEquals(timestampData, LogUtils.readTimestamp(dest, false));

        // Booleans
        boolean boolData = true;
        dest.clear();
        LogUtils.writeBoolean(dest, boolData);
        assertEquals(1, dest.position());
        dest.flip();
        assertEquals(boolData, LogUtils.readBoolean(dest));

        /*
         * Test packed values with both array and direct buffers because the
         * implementation is different when there is an array available
         * (ByteBuffer.hasArray) or not.
         */
        testPacked(dest);
        testPacked(ByteBuffer.allocateDirect(100));
    }

    private void testPacked(ByteBuffer dest) {

        // packed ints
        int intValue = 119;
        dest.clear();
        LogUtils.writePackedInt(dest, intValue);
        assertEquals(1, dest.position());
        dest.flip();
        assertEquals(intValue, LogUtils.readPackedInt(dest));

        intValue = 0xFFFF + 119;
        dest.clear();
        LogUtils.writePackedInt(dest, intValue);
        assertEquals(3, dest.position());
        dest.flip();
        assertEquals(intValue, LogUtils.readPackedInt(dest));

        intValue = Integer.MAX_VALUE;
        dest.clear();
        LogUtils.writePackedInt(dest, intValue);
        assertEquals(5, dest.position());
        dest.flip();
        assertEquals(intValue, LogUtils.readPackedInt(dest));

        // packed longs
        long longValue = 119;
        dest.clear();
        LogUtils.writePackedLong(dest, longValue);
        assertEquals(1, dest.position());
        dest.flip();
        assertEquals(longValue, LogUtils.readPackedLong(dest));

        longValue = 0xFFFFFFFFL + 119;
        dest.clear();
        LogUtils.writePackedLong(dest, longValue);
        assertEquals(5, dest.position());
        dest.flip();
        assertEquals(longValue, LogUtils.readPackedLong(dest));

        longValue = Long.MAX_VALUE;
        dest.clear();
        LogUtils.writePackedLong(dest, longValue);
        assertEquals(9, dest.position());
        dest.flip();
        assertEquals(longValue, LogUtils.readPackedLong(dest));
    }
}