aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/vespa/objects/SerializeTestCase.java
blob: 31c43f6008daeeb2b1092ede36687399561a1a29 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.objects;

import com.yahoo.io.GrowableByteBuffer;
import java.nio.ByteOrder;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * @author arnej27959
 */
public class SerializeTestCase {

    @Test
    public void testSimple() {
        SomeIdClass s = new SomeIdClass();
        BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
        s.serialize(buf);
        buf.flip();
        s.deserialize(buf);
    }

    @Test
    public void testOne() {
        SomeIdClass s = new SomeIdClass();
        BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
        s.serializeWithId(buf);
        buf.flip();
        Identifiable s2 = Identifiable.create(buf);
        assertThat((s2 instanceof SomeIdClass), is(true));
    }

    @Test
    public void testUnderflow() {
        BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
        buf.putByte(null, (byte)123);
        buf.flip();
        boolean caught = false;
        try {
            byte[] val = buf.getBytes(null, 2);
        } catch (IllegalArgumentException e) {
            // System.out.println(e);
            caught = true;
        }
        assertThat(caught, is(true));
    }

    @Test
    public void testIdNotFound() {
        BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
        buf.putInt(null, 717273);
        buf.flip();
        boolean caught = false;
        try {
            Identifiable nsi = Identifiable.create(buf);
        } catch (IllegalArgumentException e) {
            // System.out.println(e);
            caught = true;
        }
        assertThat(caught, is(true));
    }

    @Test
    public void testOrdering() {
        BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
        assertThat(buf.order(), is(ByteOrder.BIG_ENDIAN));
        buf.putInt(null, 0x11223344);
        buf.order(ByteOrder.LITTLE_ENDIAN);
        buf.putInt(null, 0x55667788);
        assertThat(buf.order(), is(ByteOrder.LITTLE_ENDIAN));
        buf.flip();
        assertThat(buf.getByte(null), is((byte)0x11));
        assertThat(buf.getByte(null), is((byte)0x22));
        assertThat(buf.getShort(null), is((short)0x4433));
        buf.order(ByteOrder.BIG_ENDIAN);
        assertThat(buf.getByte(null), is((byte)0x88));
        assertThat(buf.getByte(null), is((byte)0x77));
        assertThat(buf.getShort(null), is((short)0x6655));
    }

    @Test
    public void testBig() {
        BigIdClass dv = new BigIdClass();
        BigIdClass ov = new BigIdClass(6667666);
        BigIdClass bv = new BigIdClass(123456789);

        assertThat(BigIdClass.classId, is(42));
        assertThat(dv.getClassId(), is(42));
        assertThat(ov.getClassId(), is(42));
        assertThat(bv.getClassId(), is(42));

        assertThat(ov.equals(dv), is(false));
        assertThat(dv.equals(bv), is(false));
        assertThat(bv.equals(ov), is(false));

        BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
        ov.serialize(buf);
        buf.flip();
        dv.deserialize(buf);
        assertThat(ov, equalTo(dv));
        assertThat(dv, equalTo(ov));
        buf = new BufferSerializer(new GrowableByteBuffer());
        bv.serializeWithId(buf);
        buf.flip();
        dv.deserializeWithId(buf);
        assertThat(bv, equalTo(dv));
        assertThat(dv, equalTo(bv));

        buf = new BufferSerializer(new GrowableByteBuffer());
        SomeIdClass s = new SomeIdClass();
        assertThat(dv.equals(s), is(false));
        assertThat(ov.equals(s), is(false));
        assertThat(bv.equals(s), is(false));
        assertThat(dv.equals(new Object()), is(false));

        s.serializeWithId(buf);
        buf.flip();
        boolean caught = false;
        try {
            dv.deserializeWithId(buf);
        } catch (IllegalArgumentException ex) {
            caught = true;
            // System.out.println(ex);
        }
        assertThat(caught, is(true));
        buf = new BufferSerializer(new GrowableByteBuffer());
        buf.putLong(null, 0x7777777777777777L);
        buf.flip();
        caught = false;
        try {
            dv.deserializeWithId(buf);
        } catch (IllegalArgumentException ex) {
            caught = true;
            // System.out.println(ex);
        }
        assertThat(caught, is(true));
    }

}