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

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

/**
 * @author Ulf Lilleengen
 * @since 5.1
 */
public class PathTest {
    @Test
    public void testGetName() {
        assertEquals("baz", getAbsolutePath().getName());
        assertEquals("baz", getRelativePath().getName());
        assertEquals("baz", getWithSlashes().getName());
        assertEquals("baz", getAppended().getName());
        assertEquals("foo", getOne().getName());
    }

    @Test
    public void testEquals() {
        assertEquals(getAbsolutePath(), getAbsolutePath());
        assertEquals(getAbsolutePath(), getRelativePath());
        assertEquals(getAbsolutePath(), getWithSlashes());
        assertEquals(getAbsolutePath(), getAppended());
        assertNotEquals(getAbsolutePath(), getOne());

        assertEquals(getRelativePath(), getAbsolutePath());
        assertEquals(getRelativePath(), getRelativePath());
        assertEquals(getRelativePath(), getWithSlashes());
        assertEquals(getRelativePath(), getAppended());
        assertNotEquals(getRelativePath(), getOne());

        assertEquals(getWithSlashes(), getAbsolutePath());
        assertEquals(getWithSlashes(), getRelativePath());
        assertEquals(getWithSlashes(), getWithSlashes());
        assertEquals(getWithSlashes(), getAppended());
        assertNotEquals(getWithSlashes(), getOne());

        assertEquals(getAppended(), getAbsolutePath());
        assertEquals(getAppended(), getRelativePath());
        assertEquals(getAppended(), getWithSlashes());
        assertEquals(getAppended(), getAppended());
        assertNotEquals(getAppended(), getOne());

        assertNotEquals(getOne(), getAbsolutePath());
        assertNotEquals(getOne(), getRelativePath());
        assertNotEquals(getOne(), getWithSlashes());
        assertNotEquals(getOne(), getAppended());
        assertEquals(getOne(), getOne());
    }

    @Test
    public void testGetPath() {
        assertEquals("foo/bar/baz", getAbsolutePath().getRelative());
        assertEquals("foo/bar/baz", getRelativePath().getRelative());
        assertEquals("foo/bar/baz", getWithSlashes().getRelative());
        assertEquals("foo/bar/baz", getAppended().getRelative());
        assertEquals("foo", getOne().getRelative());
    }

    @Test
    public void testGetParentPath() {
        assertEquals("foo/bar", getAbsolutePath().getParentPath().getRelative());
        assertEquals("foo/bar", getRelativePath().getParentPath().getRelative());
        assertEquals("foo/bar", getWithSlashes().getParentPath().getRelative());
        assertEquals("foo/bar", getAppended().getParentPath().getRelative());
        assertTrue(getOne().getParentPath().getRelative().isEmpty());
    }

    @Test
    public void testGetAbsolutePath() {
        assertEquals("/foo/bar/baz", getAbsolutePath().getAbsolute());
        assertEquals("/foo/bar", getAbsolutePath().getParentPath().getAbsolute());
    }

    @Test
    public void testEmptyPath() {
        assertTrue(Path.createRoot().getName().isEmpty());
        assertTrue(Path.createRoot().getRelative().isEmpty());
        assertTrue(Path.createRoot().getParentPath().getRelative().isEmpty());
        assertTrue(Path.createRoot().isRoot());
    }

    @Test
    public void testDelimiters() {
        assertEquals("foo/bar", Path.fromString("foo/bar", ",").getName());
        assertEquals("bar", Path.fromString("foo/bar", "/").getName());
        assertEquals("foo,bar", Path.fromString("foo,bar", "/").getName());
        assertEquals("bar", Path.fromString("foo,bar", ",").getName());
        assertEquals("foo,bar", Path.createRoot(",").append("foo").append("bar").getRelative());
    }

    @Test
    public void testAppendPath() {
        Path p1 = getAbsolutePath();
        Path p2 = getAbsolutePath();
        Path p3 = p1.append(p2);
        assertEquals("/foo/bar/baz", p1.getAbsolute());
        assertEquals("/foo/bar/baz", p2.getAbsolute());
        assertEquals("/foo/bar/baz/foo/bar/baz", p3.getAbsolute());
    }

    @Test(expected = IllegalArgumentException.class)
    public void testDoubleDot() {
        Path.fromString("foo/../bar");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testLastWithDelimiter() {
        Path.fromString("foo/bar").withLast("../../baz");
    }

    private Path getRelativePath() {
        return Path.fromString("foo/bar/baz");
    }

    private Path getAbsolutePath() {
        return Path.fromString("/foo/bar/baz");
    }

    private Path getWithSlashes() {
        return Path.fromString("/foo//bar///baz/");
    }

    private Path getAppended() {
        return Path.createRoot().append("foo").append("bar").append("baz");
    }

    private Path getOne() {
        return Path.fromString("foo");
    }
}