aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/annotation/SpanTestCase.java
blob: da9c2e0f5febf4e6c7a81425c887de0176d144be (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.annotation;

import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.serialization.*;
import com.yahoo.io.GrowableByteBuffer;
import org.junit.Test;

import java.util.ListIterator;
import java.util.NoSuchElementException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.fail;

/**
 * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 */
public class SpanTestCase extends AbstractTypesTest {

    @Test
    public void testIteration() {
        Span span = new Span(1, 2);
        ListIterator<SpanNode> b = span.childIterator();
        assertFalse(b.hasNext());
        assertFalse(b.hasPrevious());
        assertEquals(0, b.nextIndex());
        assertEquals(0, b.previousIndex());
        try {
            b.next();
            fail();
        } catch (NoSuchElementException nsee) {
            //ok
        }
        try {
            b.previous();
            fail();
        } catch (NoSuchElementException nsee) {
            //ok
        }
        try {
            b.remove();
            fail();
        } catch (UnsupportedOperationException uoe) {
            //ok
        }
        try {
            b.set(new Span(1, 1));
            fail();
        } catch (UnsupportedOperationException uoe) {
            //ok
        }
        try {
            b.add(new Span(1, 1));
            fail();
        } catch (UnsupportedOperationException uoe) {
            //ok
        }
    }

    @Test
    public void testSerializeDeserialize() {
        {
            Span span = new Span(1, 2);
            serializeAndAssert(span);
        }
        {
            Span span = new Span(4, 15);
            serializeAndAssert(span);
        }
        {
            Span span = new Span(1, 19);
            serializeAndAssert(span);
        }
    }

    private void serializeAndAssert(Span span) {
        GrowableByteBuffer buffer;
        {
            buffer = new GrowableByteBuffer(1024);
            DocumentSerializer serializer = DocumentSerializerFactory.create6(buffer);
            StringFieldValue value = new StringFieldValue("lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lk");
            SpanTree tree = new SpanTree("bababa", span);
            value.setSpanTree(tree);
            serializer.write(null, value);
            buffer.flip();
        }
        Span span2;
        {
            DocumentDeserializer deserializer = DocumentDeserializerFactory.create6(man, buffer);
            StringFieldValue value = new StringFieldValue();
            deserializer.read(null, value);
            span2 = (Span)value.getSpanTree("bababa").getRoot();
        }

        assertEquals(span, span2);
        assertNotSame(span, span2);
    }
}