aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api/utils/WildcardPatternTest.java
blob: 98396d0e4a5f8ea2d2dd0d40816b48e2f732e834 (plain)
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
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2008-2011 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.api.utils;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class WildcardPatternTest {

  private boolean match(String pattern, String value, String separator) {
    return new WildcardPattern(pattern, separator).match(value);
  }

  private boolean match(String pattern, String value) {
    return new WildcardPattern(pattern, "/").match(value);
  }

  @Test
  public void javaResourcesShouldMatchWildcards() {
    assertTrue(match("Foo", "Foo", "."));
    assertFalse(match("Foo", "Bar", "."));

    assertTrue(match("org/sonar/**", "org.sonar.commons.Foo", "."));
    assertTrue(match("org/sonar/**", "org.sonar.Foo", "."));
    assertFalse(match("xxx/org/sonar/**", "org.sonar.Foo", "."));

    assertTrue(match("org/sonar/**/**", "org.sonar.commons.Foo", "."));
    assertTrue(match("org/sonar/**/**", "org.sonar.commons.sub.Foo", "."));
    assertTrue(match("org/sonar/**/Foo", "org.sonar.commons.sub.Foo", "."));
    assertTrue(match("org/sonar/**/Foo", "org.sonar.Foo", "."));

    assertTrue(match("*/foo/*", "org.foo.Bar", "."));
    assertFalse(match("*/foo/*", "foo.Bar", "."));
    assertFalse(match("*/foo/*", "foo", "."));
    assertFalse(match("*/foo/*", "org.foo.bar.Hello", "."));

    assertTrue(match("hell?", "hello", "."));
    assertFalse(match("hell?", "helloworld", "."));
    assertFalse(match("hell?", "hell", "."));

    assertTrue(match("a.b.c", "a.b.c", "."));
    assertTrue(match("*/a.b.c", "foo.a.b.c", "."));
    assertFalse(match("*/a.b.c", "foo/aabbc", "."));

    assertTrue(match("**/Reader", "java.io.Reader", "."));
    assertFalse(match("**/Reader", "org.sonar.channel.CodeReader", "."));

    assertTrue(match("**", "java.io.Reader", "."));
  }

  @Test
  public void directoriesShouldMatchWildcards() {
    assertTrue(match("Foo", "Foo"));
    assertFalse(match("Foo", "Bar"));

    assertTrue(match("org/sonar/**", "org/sonar/commons/Foo"));
    assertTrue(match("org/sonar/**", "org/sonar/Foo.java"));
    assertFalse(match("xxx/org/sonar/**", "org/sonar/Foo"));

    assertTrue(match("org/sonar/**/**", "org/sonar/commons/Foo"));
    assertTrue(match("org/sonar/**/**", "org/sonar/commons/sub/Foo.java"));
    assertTrue(match("org/sonar/**/Foo", "org/sonar/commons/sub/Foo"));
    assertTrue(match("org/sonar/**/Foo", "org/sonar/Foo"));

    assertTrue(match("*/foo/*", "org/foo/Bar"));
    assertFalse(match("*/foo/*", "foo/Bar"));
    assertFalse(match("*/foo/*", "foo"));
    assertFalse(match("*/foo/*", "org/foo/bar/Hello"));

    assertTrue(match("hell?", "hello"));
    assertFalse(match("hell?", "helloworld"));
    assertFalse(match("hell?", "hell"));

    assertTrue(match("a.b.c", "a.b.c"));
    assertTrue(match("*/a.b.c", "foo/a.b.c"));
    assertFalse(match("*/a.b.c", "foo/aabbc"));

    assertTrue(match("**/Reader", "java/io/Reader"));
    assertFalse(match("**/Reader", "org/sonar/channel/CodeReader"));

    assertTrue(match("**", "java/io/Reader"));
  }

  @Test
  public void multiplePatterns() {
    WildcardPattern[] patterns = new WildcardPattern[] { WildcardPattern.create("Foo"), WildcardPattern.create("Bar") };
    assertTrue(WildcardPattern.match(patterns, "Foo"));
    assertTrue(WildcardPattern.match(patterns, "Bar"));
    assertFalse(WildcardPattern.match(patterns, "Other"));
  }

  @Test
  public void testToString() {
    assertThat(WildcardPattern.create("foo*").toString(), is("foo*"));
  }
}