aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/datatypes/StringTestCase.java
blob: 9c38656a98443db576473301f4b1f738a7471751 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.datatypes;

import com.yahoo.document.DataType;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.DocumentTypeManagerConfigurer;
import com.yahoo.document.Field;
import com.yahoo.document.annotation.AbstractTypesTest;
import com.yahoo.document.annotation.Annotation;
import com.yahoo.document.annotation.AnnotationType;
import com.yahoo.document.annotation.AnnotationTypeRegistry;
import com.yahoo.document.annotation.Span;
import com.yahoo.document.annotation.SpanList;
import com.yahoo.document.annotation.SpanNode;
import com.yahoo.document.annotation.SpanTree;
import com.yahoo.document.serialization.*;
import com.yahoo.io.GrowableByteBuffer;
import com.yahoo.vespa.objects.BufferSerializer;
import org.junit.Test;

import java.util.Map;

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

/**
 * @author Einar M R Rosenvinge
 */
public class StringTestCase extends AbstractTypesTest {

    @Test
    public void testDeserialize() {
        byte[] buf = new byte[1500];
        GrowableByteBuffer data = GrowableByteBuffer.wrap(buf);
        //short string
        java.lang.String foo = "foo";

        data.put((byte)0);
        data.put((byte)(foo.length() + 1));

        data.put(foo.getBytes());
        data.put((byte)0);

        int positionAfterPut = data.position();

        data.position(0);

        StringFieldValue tmp = new StringFieldValue();
        DocumentDeserializer deser = DocumentDeserializerFactory.create6(null, data);
        tmp.deserialize(deser);
        java.lang.String foo2 = tmp.getString();

        assertTrue(foo.equals(foo2));
        assertEquals(data.position(), positionAfterPut);

        //=====================

        buf = new byte[1500];
        data = GrowableByteBuffer.wrap(buf);

        //long string
        java.lang.String blah = "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah";

        int length = blah.length() + 1;

        data.put((byte)0);
        data.putInt(length | 0x80000000);

        data.put(blah.getBytes());
        data.put((byte)0);

        positionAfterPut = data.position();

        data.position(0);

        tmp = new StringFieldValue();

        deser = DocumentDeserializerFactory.create6(null, data);
        tmp.deserialize(deser);
        java.lang.String blah2 = tmp.getString();

        assertEquals(data.position(), positionAfterPut);
        assertTrue(blah.equals(blah2));
    }

    @Test
    public void testSerializeDeserialize() {
        java.lang.String test = "Hello hello";
        BufferSerializer data = new BufferSerializer(new GrowableByteBuffer(100, 2.0f));
        StringFieldValue value = new StringFieldValue(test);
        value.serialize(data);

        data.getBuf().position(0);

        StringFieldValue tmp = new StringFieldValue();
        DocumentDeserializer deser = DocumentDeserializerFactory.create6(null, data.getBuf());
        tmp.deserialize(deser);
        java.lang.String test2 = tmp.getString();
        assertEquals(test, test2);
    }

    @Test
    public void testSerializationWithTree() {
        StringFieldValue text = getAnnotatedString();

        serializeAndAssert(text);
    }

    private void serializeAndAssert(StringFieldValue stringFieldValue) {
        Field f = new Field("text", DataType.STRING);

        GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
        DocumentSerializer serializer = DocumentSerializerFactory.create6(buffer);
        serializer.write(f, stringFieldValue);
        buffer.flip();

        DocumentDeserializer deserializer = DocumentDeserializerFactory.create6(man, buffer);
        StringFieldValue stringFieldValue2 = new StringFieldValue();
        deserializer.read(f, stringFieldValue2);

        assertEquals(stringFieldValue, stringFieldValue2);
        assertNotSame(stringFieldValue, stringFieldValue2);
    }

    @Test
    public void testNestedSpanTree() {
        AnnotationType type = new AnnotationType("ann", DataType.STRING);

        StringFieldValue outerString = new StringFieldValue("Ballooo");
        SpanTree outerTree = new SpanTree("outer");
        outerString.setSpanTree(outerTree);

        SpanList outerRoot = (SpanList)outerTree.getRoot();
        Span outerSpan = new Span(0, 1);
        outerRoot.add(outerSpan);

        StringFieldValue innerString = new StringFieldValue("innerBalloooo");

        outerTree.annotate(outerSpan, new Annotation(type, innerString));

        SpanTree innerTree = new SpanTree("inner");
        innerString.setSpanTree(innerTree);

        SpanList innerRoot = (SpanList)innerTree.getRoot();
        Span innerSpan = new Span(0, 1);
        innerRoot.add(innerSpan);
        innerTree.annotate(innerSpan, new Annotation(type));

        GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
        DocumentSerializer serializer = DocumentSerializerFactory.create6(buffer);

        try {
            serializer.write(null, outerString);
            fail("Should have failed, nested span trees are not supported.");
        } catch (SerializationException se) {
            //OK!
        }
    }

    /**
     * Test for bug 4066566. No assertions, but works if it runs without exceptions.
     */
    @Test
    public void testAnnotatorConsumer() {
        DocumentTypeManager manager = new DocumentTypeManager();
        DocumentTypeManagerConfigurer
                .configure(manager, "file:src/test/java/com/yahoo/document/datatypes/documentmanager.blog.sd");

        DocumentType blogType = manager.getDocumentType("blog");
        Document doc = new Document(blogType, "id:ns:blog::http://blogs.sun.com/praveenm");
        doc.setFieldValue("url", new StringFieldValue("http://blogs.sun.com/praveenm"));
        doc.setFieldValue("title", new StringFieldValue("Beginning JavaFX"));
        doc.setFieldValue("author", new StringFieldValue("Praveen Mohan"));
        doc.setFieldValue("body", new StringFieldValue(
                "JavaFX can expand its wings across different domains such as manufacturing, logistics, retail, etc. Many companies have adopted it - IBM, Oracle, Yahoo, Honeywell. Even the non-IT industries such as GE, WIPRO, Ford etc. So it is a success for Christopher Oliver and Richard Bair. Scott Mcnealy is happy"));

        doc = annotate(doc, manager);
        doc = serializeAndDeserialize(doc, manager);
    }

    private Document serializeAndDeserialize(Document doc, DocumentTypeManager manager) {
        GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
        DocumentSerializer serializer = DocumentSerializerFactory.create6(buffer);
        serializer.write(doc);
        buffer.flip();

        DocumentDeserializer deserializer = DocumentDeserializerFactory.create6(manager, buffer);
        return new Document(deserializer);
    }

    public Document annotate(Document document, DocumentTypeManager manager) {
        AnnotationTypeRegistry registry = manager.getAnnotationTypeRegistry();

        AnnotationType company = registry.getType("company");
        AnnotationType industry = registry.getType("industry");
        AnnotationType person = registry.getType("person");
        AnnotationType location = registry.getType("location");

        Map<String, AnnotationType> m = registry.getTypes();

        SpanTree tree = new SpanTree("testannotations");
        SpanList root = (SpanList)tree.getRoot();

        SpanNode companySpan = new Span(0, 5);
        SpanNode industrySpan = new Span(5, 10);
        SpanNode personSpan = new Span(10, 15);
        SpanNode locationSpan = new Span(15, 20);

        root.add(companySpan);
        root.add(industrySpan);
        root.add(personSpan);
        root.add(locationSpan);

        Struct companyValue = (Struct)company.getDataType().createFieldValue();
        companyValue.setFieldValue("name", new StringFieldValue("Sun"));
        companyValue.setFieldValue("ceo", new StringFieldValue("Scott Mcnealy"));
        companyValue.setFieldValue("lat", new DoubleFieldValue(37.7));
        companyValue.setFieldValue("lon", new DoubleFieldValue(-122.44));
        companyValue.setFieldValue("vertical", new StringFieldValue("software"));
        Annotation compAn = new Annotation(company, companyValue);
        tree.annotate(companySpan, compAn);

        Struct personValue = new Struct(person.getDataType());
        personValue.setFieldValue("name", new StringFieldValue("Richard Bair"));
        Annotation personAn = new Annotation(person, personValue);
        tree.annotate(personSpan, personAn);

        Struct locValue = new Struct(location.getDataType());
        locValue.setFieldValue("name", new StringFieldValue("Prinsens Gate"));
        Annotation loc = new Annotation(location, locValue);
        tree.annotate(locationSpan, loc);

        Struct locValue2 = new Struct(location.getDataType());
        locValue2.setFieldValue("name", new StringFieldValue("Kongens Gate"));
        Annotation locAn = new Annotation(location, locValue2);
        tree.annotate(locationSpan, locAn);

        SpanList branch = new SpanList();

        SpanNode span1 = new Span(0, 3);
        SpanNode span2 = new Span(1, 9);
        SpanNode span3 = new Span(12, 10);

        branch.add(span1);
        branch.add(span3);
        branch.add(span2);

        Struct industryValue = new Struct(industry.getDataType());
        industryValue.setFieldValue("vertical", new StringFieldValue("Manufacturing"));
        Annotation ind = new Annotation(industry, industryValue);
        tree.annotate(span1, ind);

        Struct pValue = new Struct(person.getDataType());
        pValue.setFieldValue("name", new StringFieldValue("Praveen Mohan"));
        Annotation pAn = new Annotation(person, pValue);
        tree.annotate(span2, pAn);

        Struct lValue = new Struct(location.getDataType());
        lValue.setFieldValue("name", new StringFieldValue("Embassy Golf Links"));
        Annotation locn = new Annotation(location, lValue);
        tree.annotate(span3, locn);

        Struct cValue = (Struct)company.getDataType().createFieldValue();
        cValue.setFieldValue("name", new StringFieldValue("Yahoo"));
        cValue.setFieldValue("ceo", new StringFieldValue("Carol Bartz"));
        cValue.setFieldValue("lat", new DoubleFieldValue(127.7));
        cValue.setFieldValue("lon", new DoubleFieldValue(-42.44));
        cValue.setFieldValue("vertical", new StringFieldValue("search"));
        Annotation cAn = new Annotation(company, cValue);
        tree.annotate(branch, cAn);

        Struct pVal = new Struct(person.getDataType());
        pVal.setFieldValue("name", new StringFieldValue("Kim Omar"));
        Annotation an = new Annotation(person, pVal);
        tree.annotate(root, an);
        root.add(branch);

        StringFieldValue body = (StringFieldValue)document.getFieldValue(document.getDataType().getField("body"));

        root.remove(branch);
        tree.cleanup();

        assertEquals(5, tree.numAnnotations());
        body.setSpanTree(tree);

        document.setFieldValue(document.getField("body"), body);

        return document;
    }

}