aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api/resources/FileTest.java
blob: d1ec8111196471556c80754f9b56f7460586cba1 (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
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.api.resources;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class FileTest {

  @Test
  public void trimKeyAndName() {
    File file = File.create("   foo/bar/toto.sql  ");
    assertThat(file.getKey()).isEqualTo("foo/bar/toto.sql");
    assertThat(file.getName()).isEqualTo("toto.sql");
  }

  @Test
  public void parentIsDirectory() {
    File file = File.create("src/foo/bar/toto.sql", null, false);
    assertThat(file.getKey()).isEqualTo("src/foo/bar/toto.sql");
    assertThat(file.getLongName()).isEqualTo("src/foo/bar/toto.sql");
    assertThat(file.getName()).isEqualTo("toto.sql");
    assertThat(file.getParent().getKey()).isEqualTo("src/foo/bar");
    assertThat(ResourceUtils.isSpace(file.getParent())).isEqualTo(true);
  }

  @Test
  public void rootFilesHaveParent() {
    File file = File.create("toto.sql", null, false);
    assertThat(file.getKey()).isEqualTo("toto.sql");
    assertThat(file.getName()).isEqualTo("toto.sql");
    assertThat(file.getParent().getKey()).isEqualTo("/");
  }

  @Test
  public void setLanguage() {
    Language lang = new AbstractLanguage("java", "Java") {

      @Override
      public String[] getFileSuffixes() {
        return null;
      }
    };

    File file = File.create("Foo.java", lang, false);
    assertThat(file.getLanguage()).isEqualTo(lang);

    file = File.create("org/sonar/Foo.java", lang, false);
    assertThat(file.getLanguage()).isEqualTo(lang);
    assertThat(file.language()).isEqualTo("java");
    assertThat(file.getParent().getLanguage()).isNull();
  }

  @Test
  public void matchAntPatterns() {
    File file = File.create("src/one/two/foo.sql", null, false);
    assertThat(file.matchFilePattern("/src/one/two/*.java")).isFalse();
    assertThat(file.matchFilePattern("false")).isFalse();
    assertThat(file.matchFilePattern("two/one/**")).isFalse();
    assertThat(file.matchFilePattern("other*/**")).isFalse();

    assertThat(file.matchFilePattern("/src/one*/**/*.sql")).isTrue();
    assertThat(file.matchFilePattern("/src/one/t?o/**/*")).isTrue();
    assertThat(file.matchFilePattern("**/*")).isTrue();
    assertThat(file.matchFilePattern("src/one/two/*")).isTrue();
    assertThat(file.matchFilePattern("/src/one/two/*")).isTrue();
    assertThat(file.matchFilePattern("src/**")).isTrue();
  }
}