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

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;

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

    @Test
    void requireThatDollarsAreIncludedInLocation() {
        assertLocation("scheme:$foo", "scheme:$foo");
        assertLocation("scheme:foo$bar", "scheme:foo$bar");
    }

    @Test
    void requireThatCurlyBracesAreIncludedInLocation() {
        assertLocation("scheme:{foo", "scheme:{foo");
        assertLocation("scheme:foo{", "scheme:foo{");
        assertLocation("scheme:foo{bar", "scheme:foo{bar");
        assertLocation("scheme:}foo", "scheme:}foo");
        assertLocation("scheme:foo}", "scheme:foo}");
        assertLocation("scheme:foo}bar", "scheme:foo}bar");
        assertLocation("scheme:{foo}bar", "scheme:{foo}bar");
        assertLocation("scheme:foo{bar}", "scheme:foo{bar}");
    }

    @Test
    void requireThatUnterminatedPropertiesAreIncludedInLocation() {
        assertLocation("scheme:${foo", "scheme:${foo");
        assertLocation("scheme:foo${", "scheme:foo${");
        assertLocation("scheme:foo${bar", "scheme:foo${bar");
    }

    @Test
    void requireThatAllSystemPropertiesAreExpanded() throws IOException {
        assertCanonicalPath("", "${foo}");
        assertCanonicalPath("barcox", "${foo}bar${baz}cox");
        assertCanonicalPath("foobaz", "foo${bar}baz${cox}");

        System.setProperty("requireThatAllSystemPropertiesAreExpanded.foo", "FOO");
        System.setProperty("requireThatAllSystemPropertiesAreExpanded.bar", "BAR");
        System.setProperty("requireThatAllSystemPropertiesAreExpanded.baz", "BAZ");
        System.setProperty("requireThatAllSystemPropertiesAreExpanded.cox", "COX");
        assertCanonicalPath("FOO", "${requireThatAllSystemPropertiesAreExpanded.foo}");
        assertCanonicalPath("FOObarBAZcox", "${requireThatAllSystemPropertiesAreExpanded.foo}bar" +
                "${requireThatAllSystemPropertiesAreExpanded.baz}cox");
        assertCanonicalPath("fooBARbazCOX", "foo${requireThatAllSystemPropertiesAreExpanded.bar}" +
                "baz${requireThatAllSystemPropertiesAreExpanded.cox}");
    }

    @Test
    void requireThatUnschemedLocationsAreExpandedToBundleLocationProperty() throws IOException {
        assertCanonicalPath(BundleLocationResolver.BUNDLE_PATH + "foo", "foo");
    }

    @Test
    void requireThatFileSchemedLocationsAreCanonicalized() throws IOException {
        assertCanonicalPath("", "file:");
        assertCanonicalPath("foo", "file:foo");
        assertCanonicalPath("foo", "file:./foo");
        assertCanonicalPath("foo/bar", "file:foo/bar");
        assertCanonicalPath("foo/bar", "file:./foo/../foo/./bar");
        assertCanonicalPath("foo", " \f\n\r\tfile:foo");
    }

    @Test
    void requireThatOtherSchemedLocationsAreUntouched() {
        assertLocation("foo:", "foo:");
        assertLocation("foo:bar", "foo:bar");
        assertLocation("foo:bar/baz", "foo:bar/baz");
    }

    private static void assertCanonicalPath(String expected, String bundleLocation) throws IOException {
        assertLocation("file:" + new File(expected).getCanonicalPath(), bundleLocation);
    }

    private static void assertLocation(String expected, String bundleLocation) {
        assertEquals(expected, BundleLocationResolver.resolve(bundleLocation));
    }
}