aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java
blob: 52840b4a7ee4c3cbf2b77f7e9ef5be416c2a23b4 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2009 SonarSource SA
 * 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.resources;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.FileFilter;
import org.sonar.api.test.MavenTestUtils;

import java.io.File;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

public class DefaultProjectFileSystemTest {

  private Project project = null;

  @Before
  public void before() {
    project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "sample/pom.xml");
  }

  @Test
  public void getJavaSourceFiles() {
    final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);

    assertThat(fs.getJavaSourceFiles().size(), is(2));
    assertThat(fs.getJavaSourceFiles(), hasItem(named("Bar.java")));
    assertThat(fs.getJavaSourceFiles(), hasItem(named("Whizz.java")));
  }

  @Test
  public void hasJavaSourceFiles() {
    final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
    assertThat(fs.hasJavaSourceFiles(), is(true));

    project.setExclusionPatterns(new String[] { "**/*.java" });
    assertThat(fs.hasJavaSourceFiles(), is(false));
  }

  @Test
  public void getTestFiles() {
    final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);

    assertThat(fs.getTestFiles(Java.INSTANCE).size(), is(1));
    assertThat(fs.getTestFiles(Java.INSTANCE), hasItem(named("BarTest.java")));
  }

  @Test
  public void applyExclusionPatternsToSourceFiles() {
    project.setExclusionPatterns(new String[] { "**/B*.java" });

    final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);

    assertThat(fs.getJavaSourceFiles().size(), is(1));
    assertThat(fs.getJavaSourceFiles(), hasItem(named("Whizz.java")));
  }

  /**
   * See http://jira.codehaus.org/browse/SONAR-1449
   */
  @Test
  public void exclusionPatternOnAjFiles() {
    project.setExclusionPatterns(new String[] { "**/*.aj" });

    final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);

    assertThat(fs.getSourceFiles(Java.INSTANCE).size(), is(2));
    assertThat(fs.getSourceFiles(Java.INSTANCE), hasItem(named("Whizz.java")));
    assertThat(fs.getSourceFiles(Java.INSTANCE), hasItem(named("Bar.java")));
  }

  @Test
  public void doNotApplyExclusionPatternsToTestFiles() {
    project.setExclusionPatterns(new String[] { "**/B*.java" });

    final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);

    assertThat(fs.getTestFiles(Java.INSTANCE).size(), is(1));
    assertThat(fs.getTestFiles(Java.INSTANCE), hasItem(named("BarTest.java")));
  }

  @Test
  public void createSonarWorkingDirectory() {
    DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
    java.io.File dir = fs.getSonarWorkingDirectory();
    assertThat(dir.exists(), is(true));
    assertThat(dir.listFiles().length, is(0));
  }

  @Test
  public void getJapaneseCharSet() {
    project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "japanese-project/pom.xml");
    DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
    assertThat(fs.getSourceCharset().name(), is("Shift_JIS"));
  }

  @Test
  public void languageWithNoSpecificFileSuffixes() {
    class NoSuffixLanguage implements Language {
      public String getKey() {
        return "no-suffix";
      }

      public String getName() {
        return "no-suffix";
      }

      public String[] getFileSuffixes() {
        return new String[0];
      }
    }

    project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "sample-with-different-suffixes/pom.xml");
    ProjectFileSystem fs = newDefaultProjectFileSystem(project);
    List<File> files = fs.getSourceFiles(new NoSuffixLanguage());
    assertThat(files.size(), is(2));
  }

  /**
   * Example of hidden files/directories : .DSStore, .svn, .git
   */
  @Test
  public void hiddenFilesAreIgnored() {
    if (!SystemUtils.IS_OS_WINDOWS) {
      // hidden files/directories can not be stored in svn windows
      // On Mac/Linux it's easy, just prefix the filename by '.'
      project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "hidden-files/pom.xml");
      ProjectFileSystem fs = newDefaultProjectFileSystem(project);
      List<File> files = fs.getSourceFiles();
      assertThat(files.size(), is(1));
      assertThat(files.get(0).getName(), is("foo.sql"));
    }
  }

  @Test
  public void shouldAddExtendedFilters() {
    DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
    assertThat(fs.getSourceFiles().size(), is(2));
    assertThat(fs.getSourceFiles(), hasItem(named("Bar.java")));

    fs.addFileFilter(new FileFilter() {
      public boolean accept(File file) {
        return !StringUtils.equals(file.getName(), "Bar.java");
      }
    });
    assertThat(fs.getSourceFiles().size(), is(1));
    assertThat(fs.getSourceFiles(), not(hasItem(named("Bar.java"))));
  }

  private DefaultProjectFileSystem newDefaultProjectFileSystem(Project project) {
    return (DefaultProjectFileSystem) project.getFileSystem();
  }

  private static Matcher<java.io.File> named(final String name) {
    return new TypeSafeMatcher<java.io.File>() {
      java.io.File fileTested;

      @Override
      public boolean matchesSafely(java.io.File item) {
        fileTested = item;
        return name.equals(item.getName());
      }

      public void describeTo(Description description) {
        description.appendText(" that file ");
        description.appendValue(fileTested);
        description.appendText(" is named");
        description.appendText(name);
        description.appendText(" not ");
        description.appendValue(fileTested.getName());
      }
    };
  }
}