summaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/processing/request/CompoundNameTestCase.java
blob: 841866f1e83f3cc8656d78f5ffe5bdc3c37a81be (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.processing.request;

import static org.junit.Assert.*;

import java.util.Iterator;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Splitter;
import com.yahoo.text.Lowercase;

/**
 * Module local test of the basic property name building block.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class CompoundNameTestCase {

    private static final String NAME = "com.yahoo.processing.request.CompoundNameTestCase";
    private CompoundName cn;

    @Before
    public void setUp() throws Exception {
        cn = new CompoundName(NAME);
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public final void testLast() {
        assertEquals(NAME.substring(NAME.lastIndexOf('.') + 1), cn.last());
    }

    @Test
    public final void testFirst() {
        assertEquals(NAME.substring(0, NAME.indexOf('.')), cn.first());
    }

    @Test
    public final void testRest() {
        assertEquals(NAME.substring(NAME.indexOf('.') + 1), cn.rest().toString());
    }

    @Test
    public final void testRestN() {
        assertEquals("a.b.c.d.e", new CompoundName("a.b.c.d.e").rest(0).toString());
        assertEquals("b.c.d.e", new CompoundName("a.b.c.d.e").rest(1).toString());
        assertEquals("c.d.e", new CompoundName("a.b.c.d.e").rest(2).toString());
        assertEquals("d.e", new CompoundName("a.b.c.d.e").rest(3).toString());
        assertEquals("e", new CompoundName("a.b.c.d.e").rest(4).toString());
        assertEquals("", new CompoundName("a.b.c.d.e").rest(5).toString());
    }

    @Test
    public final void testPrefix() {
        assertTrue(new CompoundName("a.b.c").hasPrefix(new CompoundName("")));
        assertTrue(new CompoundName("a.b.c").hasPrefix(new CompoundName("a")));
        assertTrue(new CompoundName("a.b.c").hasPrefix(new CompoundName("a.b")));
        assertTrue(new CompoundName("a.b.c").hasPrefix(new CompoundName("a.b.c")));

        assertFalse(new CompoundName("a.b.c").hasPrefix(new CompoundName("a.b.c.d")));
        assertFalse(new CompoundName("a.b.c").hasPrefix(new CompoundName("a.b.d")));
    }

    @Test
    public final void testSize() {
        Splitter s = Splitter.on('.');
        Iterable<String> i = s.split(NAME);
        int n = 0;
        for (@SuppressWarnings("unused") String x : i) {
            ++n;
        }
        assertEquals(n, cn.size());
    }

    @Test
    public final void testGet() {
        String s = cn.get(0);
        assertEquals(NAME.substring(0, NAME.indexOf('.')), s);
    }

    @Test
    public final void testIsCompound() {
        assertTrue(cn.isCompound());
    }

    @Test
    public final void testIsEmpty() {
        assertFalse(cn.isEmpty());
    }

    @Test
    public final void testAsList() {
        List<String> l = cn.asList();
        Splitter peoplesFront = Splitter.on('.');
        Iterable<String> answer = peoplesFront.split(NAME);
        Iterator<String> expected = answer.iterator();
        for (int i = 0; i < l.size(); ++i) {
            assertEquals(expected.next(), l.get(i));
        }
        assertFalse(expected.hasNext());
    }

    @Test
    public final void testEqualsObject() {
        assertFalse(cn.equals(NAME));
        assertFalse(cn.equals(null));
        assertTrue(cn.equals(cn));
        assertTrue(cn.equals(new CompoundName(NAME)));
    }

    @Test
    public final void testEmptyNonEmpty() {
        assertTrue(new CompoundName("").isEmpty());
        assertEquals(0, new CompoundName("").size());
        assertFalse(new CompoundName("a").isEmpty());
        assertEquals(1, new CompoundName("a").size());
        CompoundName empty = new CompoundName("a.b.c");
        assertTrue(empty == empty.rest(0));
        assertFalse(empty == empty.rest(1));
    }

    @Test
    public final void testGetLowerCasedName() {
        assertEquals(Lowercase.toLowerCase(NAME), cn.getLowerCasedName());
    }

    @Test
    public void testAppend() {
        assertEquals(new CompoundName("a.b.c.d"), new CompoundName("").append(new CompoundName("a.b.c.d")));
        assertEquals(new CompoundName("a.b.c.d"), new CompoundName("a").append(new CompoundName("b.c.d")));
        assertEquals(new CompoundName("a.b.c.d"), new CompoundName("a.b").append(new CompoundName("c.d")));
        assertEquals(new CompoundName("a.b.c.d"), new CompoundName("a.b.c").append(new CompoundName("d")));
        assertEquals(new CompoundName("a.b.c.d"), new CompoundName("a.b.c.d").append(new CompoundName("")));
    }

    @Test
    public void empty_CompoundName_is_prefix_of_any_CompoundName() {
        CompoundName empty = new CompoundName("");

        assertTrue(empty.hasPrefix(empty));
        assertTrue(new CompoundName("a").hasPrefix(empty));
    }

    @Test
    public void whole_components_must_match_to_be_prefix() {
        CompoundName stringPrefix = new CompoundName("a");
        CompoundName name         = new CompoundName("aa");

        assertFalse(name.hasPrefix(stringPrefix));
    }
}