aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/tests/com/yahoo/jrt/slobrok/api/MirrorTest.java
blob: ab314f00ba590938ffd34855f06c9e2f66ed2a99 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt.slobrok.api;

import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

public class MirrorTest {

    static void mustMatch(String name, String pattern) {
        assertTrue(Mirror.match(name.toCharArray(), pattern.toCharArray()));
    }

    static void mustNotMatch(String name, String pattern) {
        assertFalse(Mirror.match(name.toCharArray(), pattern.toCharArray()));
    }

    @Test public void requireThatPatternMatchesSameString() {
        String pattern = "foo/bar*zot/qux?foo**bar*/*nop*";
        mustMatch(pattern, pattern);
    }

    @Test public void requireThatStarIsPrefixMatch() {
        String pattern = "foo/bar.*/qux.*/bar*/nop*";
        String matches = "foo/bar.foo/qux.bar/bar123/nop000";
        mustMatch(matches, pattern);

        matches = "foo/bar.bar/qux.qux/bar.bar/nop.nop";
        mustMatch(matches, pattern);

        matches = "foo/bar.1/qux.3/bar.4/nop.5";
        mustMatch(matches, pattern);
   }

    @Test public void requireThatStarMatchesEmptyString() {
        String pattern = "foo/bar.*/qux.*/bar*/nop*";
        String matches = "foo/bar./qux./bar/nop";
        mustMatch(matches, pattern);
    }

    @Test public void requireThatExtraBeforeSlashIsNotMatch() {
        String pattern = "foo/*";
        String nomatch = "foo1/bar";
        mustNotMatch(nomatch, pattern);
    }

    @Test public void requireThatStarDoesNotMatchMultipleLevels() {
        String pattern = "foo/*/qux";
        String matches = "foo/bar/qux";
        String nomatch = "foo/bar/bar/qux";
        mustMatch(matches, pattern);
        mustNotMatch(nomatch, pattern);

        pattern = "*";
        nomatch = "foo/bar.foo/qux.bar/bar123/nop000";
        mustNotMatch(nomatch, pattern);
    }

    @Test public void requireThatDoubleStarMatchesMultipleLevels() {
        String pattern = "**";
        String matches = "foo/bar.foo/qux.bar/bar123/nop000";
        mustMatch(matches, pattern);

        pattern = "foo/**";
        matches = "foo/bar.foo/qux.bar/bar123/nop000";
        mustMatch(matches, pattern);

        pattern = "foo**";
        matches = "foo/bar.foo/qux.bar/bar123/nop000";
        mustMatch(matches, pattern);

        pattern = "f**";
        matches = "foo/bar.foo/qux.bar/bar123/nop000";
        mustMatch(matches, pattern);
    }

    @Test public void requireThatDoubleStarMatchesNothing() {
        String pattern = "A**";
        String matches = "A";
        mustMatch(matches, pattern);
    }

    @Test public void requireThatDoubleStarEatsRestOfName() {
        String pattern = "foo/**/suffix";
        String nomatch = "foo/bar/baz/suffix";
        mustNotMatch(nomatch, pattern);
    }

}