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

import org.junit.Test;
import org.w3c.dom.Element;

import java.util.List;

import static org.junit.Assert.*;

/**
 * @author Simon Thoresen Hult
 */
public class XmlHelperTestCase {

    @Test
    public void requireThatAttributeTextCanBeRetrieved() throws Exception {
        Element node = XmlHelper.parseXml("<element a1='v1' a2='v2' />");
        assertEquals("v1", XmlHelper.getAttributeText(node, "a1"));
        assertEquals("v2", XmlHelper.getAttributeText(node, "a2"));
    }

    @Test
    public void requireThatMissingAttributeTextThrowsIllegalArgument() throws Exception {
        try {
            XmlHelper.getAttributeText(XmlHelper.parseXml("<element />"), "a1");
            fail();
        } catch (IllegalArgumentException e) {

        }
        try {
            XmlHelper.getAttributeText(XmlHelper.parseXml("<element a1='' />"), "a1");
            fail();
        } catch (IllegalArgumentException e) {

        }
    }

    @Test
    public void requireThatSingleElementCanBeRetrieved() throws Exception {
        String xml = "<parent>" +
                     "    <child id='a' />" +
                     "</parent>";
        Element element = XmlHelper.getSingleElement(XmlHelper.parseXml(xml), null);
        assertNotNull(element);
        assertEquals("a", XmlHelper.getAttributeText(element, "id"));
    }

    @Test
    public void requireThatNamedSingleElementCanBeRetrieved() throws Exception {
        String xml = "<parent>" +
                     "    <bastard id='a' />" +
                     "    <child id='b' />" +
                     "    <bastard id='c' />" +
                     "</parent>";
        Element element = XmlHelper.getSingleElement(XmlHelper.parseXml(xml), "child");
        assertNotNull(element);
        assertEquals("b", XmlHelper.getAttributeText(element, "id"));
    }

    @Test
    public void requireThatMissingSingleElementThrowsIllegalArgument() throws Exception {
        try {
            XmlHelper.getSingleElement(XmlHelper.parseXml("<parent />"), null);
            fail();
        } catch (IllegalArgumentException e) {

        }
    }

    @Test
    public void requireThatMissingNamedSingleElementThrowsIllegalArgument() throws Exception {
        String xml = "<parent>" +
                     "    <bastard id='a' />" +
                     "</parent>";
        try {
            XmlHelper.getSingleElement(XmlHelper.parseXml(xml), "child");
            fail();
        } catch (IllegalArgumentException e) {

        }
    }

    @Test
    public void requireThatAmbigousSingleElementThrowsIllegalArgument() throws Exception {
        String xml = "<parent>" +
                     "    <child id='a' />" +
                     "    <child id='b' />" +
                     "</parent>";
        try {
            XmlHelper.getSingleElement(XmlHelper.parseXml(xml), null);
            fail();
        } catch (IllegalArgumentException e) {

        }
    }

    @Test
    public void requireThatAmbigousNamedSingleElementThrowsIllegalArgument() throws Exception {
        String xml = "<parent>" +
                     "    <child id='a' />" +
                     "    <bastard id='b' />" +
                     "    <child id='c' />" +
                     "</parent>";
        try {
            XmlHelper.getSingleElement(XmlHelper.parseXml(xml), "child");
            fail();
        } catch (IllegalArgumentException e) {

        }
    }

    @Test
    public void requireThatChildElementsCanBeRetrieved() throws Exception {
        String xml = "<parent>" +
                     "    <child id='a' />" +
                     "    <child id='b' />" +
                     "</parent>";
        List<Element> lst = XmlHelper.getChildElements(XmlHelper.parseXml(xml), null);
        assertNotNull(lst);
        assertEquals(2, lst.size());
        assertEquals("a", XmlHelper.getAttributeText(lst.get(0), "id"));
        assertEquals("b", XmlHelper.getAttributeText(lst.get(1), "id"));
    }

    @Test
    public void requireThatNamedChildElementsCanBeRetrieved() throws Exception {
        String xml = "<parent>" +
                     "    <child id='a' />" +
                     "    <bastard id='b' />" +
                     "    <child id='c' />" +
                     "</parent>";
        List<Element> lst = XmlHelper.getChildElements(XmlHelper.parseXml(xml), "child");
        assertNotNull(lst);
        assertEquals(2, lst.size());
        assertEquals("a", XmlHelper.getAttributeText(lst.get(0), "id"));
        assertEquals("c", XmlHelper.getAttributeText(lst.get(1), "id"));
    }

    @Test
    public void requireThatChildElementsAreNeverNull() throws Exception {
        List<Element> lst = XmlHelper.getChildElements(XmlHelper.parseXml("<parent />"), null);
        assertNotNull(lst);
        assertTrue(lst.isEmpty());
    }

    @Test
    public void requireThatNamedChildElementsAreNeverNull() throws Exception {
        List<Element> lst = XmlHelper.getChildElements(XmlHelper.parseXml("<parent />"), "child");
        assertNotNull(lst);
        assertTrue(lst.isEmpty());
    }
}