aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/vespaxmlparser/XMLNumericFieldErrorMsgTestCase.java
blob: 10f727d5b85ad6d43b57ecc62c481546715e074b (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespaxmlparser;

import com.yahoo.document.DataType;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.serialization.DeserializationException;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

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

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

    private static DocumentTypeManager setupTypes() {
        DocumentTypeManager dtm = new DocumentTypeManager();
        DocumentType docType = new DocumentType("doctype");
        docType.addField("bytefield", DataType.BYTE);
        docType.addField("intfield", DataType.INT);
        docType.addField("longfield", DataType.LONG);
        docType.addField("floatfield", DataType.FLOAT);
        docType.addField("doublefield", DataType.DOUBLE);
        dtm.register(docType);
        return dtm;
    }

    @Test
    public void requireDescriptiveErrorMsgForFloats() throws Exception {
        DocumentTypeManager dtm = setupTypes();
        try {
            VespaXMLDocumentReader documentReader = new VespaXMLDocumentReader(
                    new ByteArrayInputStream(("<document id=\"id:ns:doctype::bar\" type=\"doctype\">" +
                                              "  <floatfield></floatfield>" +
                                              "</document>").getBytes(StandardCharsets.UTF_8)), dtm);
            new Document(documentReader);
            fail("Sorry mac");
        } catch (DeserializationException e) {
            assertTrue(e.getMessage().contains("Field 'floatfield': Invalid float \"\""));
        }
    }

    @Test
    public void requireDescriptiveErrorMsgForDoubles() throws Exception {
        DocumentTypeManager dtm = setupTypes();
        try {
            VespaXMLDocumentReader documentReader = new VespaXMLDocumentReader(
                    new ByteArrayInputStream(("<document id=\"id:ns:doctype::bar\" type=\"doctype\">" +
                                              "  <doublefield></doublefield>" +
                                              "</document>").getBytes(StandardCharsets.UTF_8)), dtm);
            new Document(documentReader);
            fail("Sorry mac");
        } catch (DeserializationException e) {
            assertTrue(e.getMessage().contains("Field 'doublefield': Invalid double \"\""));
        }
    }

    @Test
    public void requireDescriptiveErrorMsgForLongs() throws Exception {
        DocumentTypeManager dtm = setupTypes();
        try {
            VespaXMLDocumentReader documentReader = new VespaXMLDocumentReader(
                    new ByteArrayInputStream(("<document id=\"id:ns:doctype::bar\" type=\"doctype\">" +
                                              "  <longfield></longfield>" +
                                              "</document>").getBytes(StandardCharsets.UTF_8)), dtm);
            new Document(documentReader);
            fail("Sorry mac");
        } catch (DeserializationException e) {
            assertTrue(e.getMessage().contains("Field 'longfield': Invalid long \"\""));
        }
    }

    @Test
    public void requireDescriptiveErrorMsgForIntegers() throws Exception {
        DocumentTypeManager dtm = setupTypes();
        try {
            VespaXMLDocumentReader documentReader = new VespaXMLDocumentReader(
                    new ByteArrayInputStream(("<document id=\"id:ns:doctype::bar\" type=\"doctype\">" +
                                              "  <intfield></intfield>" +
                                              "</document>").getBytes(StandardCharsets.UTF_8)), dtm);
            new Document(documentReader);
            fail("Sorry mac");
        } catch (DeserializationException e) {
            assertTrue(e.getMessage().contains("Field 'intfield': Invalid integer \"\""));
        }
    }

    @Test
    public void requireDescriptiveErrorMsgForBytes() throws Exception {
        DocumentTypeManager dtm = setupTypes();
        try {
            VespaXMLDocumentReader documentReader = new VespaXMLDocumentReader(
                    new ByteArrayInputStream(("<document id=\"id:ns:doctype::bar\" type=\"doctype\">" +
                                              "  <bytefield></bytefield>" +
                                              "</document>").getBytes(StandardCharsets.UTF_8)), dtm);
            new Document(documentReader);
            fail("Sorry mac");
        } catch (DeserializationException e) {
            assertTrue(e.getMessage().contains("Field 'bytefield': Invalid byte \"\""));
        }
    }



}