aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2013-02-12 17:56:00 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2013-02-12 17:56:23 +0100
commitb218afa5a514c2a3a7fcb856cde88d884fc129c8 (patch)
tree0ce15e51e62abf163b4aef938e11eb7e13273530 /sonar-plugin-api/src/test/java/org/sonar
parent102fe9aa9858c9371140ce66b30d48be21a24696 (diff)
downloadsonarqube-b218afa5a514c2a3a7fcb856cde88d884fc129c8.tar.gz
sonarqube-b218afa5a514c2a3a7fcb856cde88d884fc129c8.zip
SONAR-1896 SONAR-3739 improve the API of scan file system
Diffstat (limited to 'sonar-plugin-api/src/test/java/org/sonar')
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java268
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java9
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/JavaIoFileFilterTest.java48
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/ModuleExclusionsTest.java61
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/PathResolverTest.java117
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/test/MavenTestUtils.java119
6 files changed, 338 insertions, 284 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java
deleted file mode 100644
index da456d5b0b4..00000000000
--- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java
+++ /dev/null
@@ -1,268 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 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.resources;
-
-import org.apache.commons.configuration.PropertiesConfiguration;
-import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.SystemUtils;
-import org.apache.maven.project.MavenProject;
-import org.hamcrest.Description;
-import org.hamcrest.Matcher;
-import org.hamcrest.Matchers;
-import org.hamcrest.TypeSafeMatcher;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.FileFilter;
-import org.sonar.api.test.MavenTestUtils;
-
-import java.io.File;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class DefaultProjectFileSystemTest {
-
- private Project project = null;
-
- @Before
- public void before() {
- project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "sample/pom.xml");
- }
-
- /**
- * See http://jira.codehaus.org/browse/SONAR-2266
- */
- @Test
- public void shouldReturnOnlyExistingSourceAndTestDirectories() {
- // in this example : "src/main/java" is a file, "src/test/java" doesn't exists
- project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "nonexistent-dirs/pom.xml");
- DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
- assertThat(fs.getSourceDirs().size(), is(0));
- assertThat(fs.getTestDirs().size(), is(0));
- }
-
- @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));
-
- PropertiesConfiguration conf = new PropertiesConfiguration();
- conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*.java");
- project.setConfiguration(conf);
- 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() {
- PropertiesConfiguration conf = new PropertiesConfiguration();
- conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/B*.java");
- project.setConfiguration(conf);
-
- 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() {
- PropertiesConfiguration conf = new PropertiesConfiguration();
- conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*.aj");
- project.setConfiguration(conf);
-
- 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() {
- PropertiesConfiguration conf = new PropertiesConfiguration();
- conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/B*.java");
- project.setConfiguration(conf);
-
- 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));
- }
-
- /**
- * See http://jira.codehaus.org/browse/SONAR-2280
- */
- @Test
- public void resolvePathShouldReturnCanonicalFile() {
- MavenProject pom = mock(MavenProject.class);
- when(pom.getBasedir()).thenReturn(new File("/project"));
- Project project = new Project("foo").setPom(pom);
- DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project, null);
-
- assertThat(fs.resolvePath(".").getAbsolutePath(), endsWith("project"));
- assertThat(fs.resolvePath("../project").getAbsolutePath(), endsWith("project"));
- }
-
- /**
- * 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 shouldUseExtendedFilters() {
- DefaultProjectFileSystem fsWithoutFilter = newDefaultProjectFileSystem(project);
- assertThat(fsWithoutFilter.getSourceFiles().size(), is(2));
- assertThat(fsWithoutFilter.getSourceFiles(), hasItem(named("Bar.java")));
-
- FileFilter filter = new FileFilter() {
- public boolean accept(File file) {
- return !StringUtils.equals(file.getName(), "Bar.java");
- }
- };
- DefaultProjectFileSystem fsWithFilter = new DefaultProjectFileSystem(project, new Languages(Java.INSTANCE), filter);
- assertThat(fsWithFilter.getSourceFiles().size(), is(1));
- assertThat(fsWithFilter.getSourceFiles(), not(hasItem(named("Bar.java"))));
- }
-
- @Test
- public void testSelectiveFileFilter() {
- DefaultProjectFileSystem.FileSelectionFilter filter = new DefaultProjectFileSystem.FileSelectionFilter(
- Arrays.asList(new File("foo/Bar.java"), new File("hello/Bar.java"), new File("hello/World.java")));
- assertThat(filter.accept(new File("foo/Bar.java")), Matchers.is(true));
- assertThat(filter.accept(new File("hello/Bar.java")), Matchers.is(true));
- assertThat(filter.accept(new File("hello/World.java")), Matchers.is(true));
-
- assertThat(filter.accept(new File("foo/Unknown.java")), Matchers.is(false));
- assertThat(filter.accept(new File("foo/bar/Bar.java")), Matchers.is(false));
- assertThat(filter.accept(new File("foo/World.java")), Matchers.is(false));
- }
-
- /**
- * SONAR-3096
- */
- @Test
- public void shouldExcludeDirectoriesStartingWithDot() {
- List<File> dirs = Arrays.asList(new File("test-resources/org/sonar/api/resources/DefaultProjectFileSystemTest/shouldExcludeDirectoriesStartingWithDot/src"));
-
- List<InputFile> files = new DefaultProjectFileSystem(new Project("foo"), null).getFiles(dirs, Collections.<File>emptyList(), new String[0]);
- assertThat(files.size(), is(1));
- assertThat(files.get(0).getRelativePath(), is("org/sonar/Included.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());
- }
- };
- }
-} \ No newline at end of file
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java
index 81c3f209886..63789a313c4 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java
@@ -94,13 +94,4 @@ public class ProjectTest {
assertThat(project.getTestExclusionPatterns()).containsOnly("**/*Test.java", "**/*IntegrationTest.java", "**/*FunctionalTest.java");
}
-
- @Test
- public void testSetExclusionPatterns() {
- Project project = new Project("key").setConfiguration(conf);
-
- project.setExclusionPatterns(new String[] {"**/*Foo.java", "**/*Bar.java"});
-
- assertThat(project.getExclusionPatterns()).containsOnly("**/*Foo.java", "**/*Bar.java");
- }
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/JavaIoFileFilterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/JavaIoFileFilterTest.java
new file mode 100644
index 00000000000..b38dbd70c04
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/JavaIoFileFilterTest.java
@@ -0,0 +1,48 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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.scan.filesystem;
+
+import org.apache.commons.io.filefilter.IOFileFilter;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class JavaIoFileFilterTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void should_wrap_java_io_filefilter() throws IOException {
+ IOFileFilter javaIoFilter = mock(IOFileFilter.class);
+ JavaIoFileFilter filter = JavaIoFileFilter.create(javaIoFilter);
+
+ File file = temp.newFile();
+ filter.accept(file, mock(FileFilter.Context.class));
+
+ verify(javaIoFilter).accept(file);
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/ModuleExclusionsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/ModuleExclusionsTest.java
new file mode 100644
index 00000000000..25f52de601e
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/ModuleExclusionsTest.java
@@ -0,0 +1,61 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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.scan.filesystem;
+
+import org.junit.Test;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Settings;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class ModuleExclusionsTest {
+ @Test
+ public void ignore_inclusion_of_world() {
+ Settings settings = new Settings();
+ settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*");
+ settings.setProperty(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY, "**/*");
+ assertThat(new ModuleExclusions(settings).sourceInclusions()).isEmpty();
+ assertThat(new ModuleExclusions(settings).testInclusions()).isEmpty();
+ }
+
+ @Test
+ public void load_inclusions() {
+ Settings settings = new Settings();
+ settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*Foo.java");
+ settings.setProperty(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY, "**/*FooTest.java");
+ ModuleExclusions moduleExclusions = new ModuleExclusions(settings);
+
+ assertThat(moduleExclusions.sourceInclusions()).containsOnly("**/*Foo.java");
+ assertThat(moduleExclusions.testInclusions()).containsOnly("**/*FooTest.java");
+ }
+
+ @Test
+ public void load_exclusions() {
+ Settings settings = new Settings();
+ settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*Foo.java");
+ settings.setProperty(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY, "**/*FooTest.java");
+ ModuleExclusions moduleExclusions = new ModuleExclusions(settings);
+
+ assertThat(moduleExclusions.sourceInclusions()).isEmpty();
+ assertThat(moduleExclusions.sourceExclusions()).containsOnly("**/*Foo.java");
+ assertThat(moduleExclusions.testInclusions()).isEmpty();
+ assertThat(moduleExclusions.testExclusions()).containsOnly("**/*FooTest.java");
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/PathResolverTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/PathResolverTest.java
new file mode 100644
index 00000000000..f4256ca5b75
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/PathResolverTest.java
@@ -0,0 +1,117 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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.scan.filesystem;
+
+import org.apache.commons.io.FilenameUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class PathResolverTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void get_file_by_relative_path() throws IOException {
+ PathResolver resolver = new PathResolver();
+ File rootDir = temp.newFolder();
+ File file = resolver.relativeFile(rootDir, "org/foo/Bar.java");
+ assertThat(file.getName()).isEqualTo("Bar.java");
+ assertThat(FilenameUtils.separatorsToUnix(file.getCanonicalPath())).endsWith("org/foo/Bar.java");
+ assertThat(file.getParentFile().getParentFile().getParentFile().getCanonicalPath()).isEqualTo(rootDir.getCanonicalPath());
+ }
+
+ @Test
+ public void get_file_by_absolute_path() throws IOException {
+ PathResolver resolver = new PathResolver();
+ File rootDir = temp.newFolder();
+ File file = resolver.relativeFile(rootDir, new File(rootDir, "org/foo/Bar.java").getAbsolutePath());
+ assertThat(file.getName()).isEqualTo("Bar.java");
+ assertThat(FilenameUtils.separatorsToUnix(file.getCanonicalPath())).endsWith("org/foo/Bar.java");
+ assertThat(file.getParentFile().getParentFile().getParentFile().getCanonicalPath()).isEqualTo(rootDir.getCanonicalPath());
+ }
+
+ @Test
+ public void get_files_by_relative_paths() throws IOException {
+ PathResolver resolver = new PathResolver();
+ File rootDir = temp.newFolder();
+ List<File> files = resolver.relativeFiles(rootDir, Arrays.asList("org/foo/Bar.java", "org/hello/World.java"));
+ assertThat(files).hasSize(2);
+ for (File file : files) {
+ assertThat(file.getName()).endsWith(".java");
+ assertThat(file.getParentFile().getParentFile().getParentFile().getCanonicalPath()).isEqualTo(rootDir.getCanonicalPath());
+ }
+ }
+
+ @Test
+ public void relative_path_from_dir() throws IOException {
+ PathResolver resolver = new PathResolver();
+ File rootDir = temp.newFolder();
+ File org = new File(rootDir, "org");
+ File hello = new File(org, "hello");
+ File world = new File(hello, "World.java");
+
+ assertThat(resolver.relativePath(rootDir, world)).isEqualTo("org/hello/World.java");
+ }
+
+ @Test
+ public void relative_path_from_multiple_dirs() throws IOException {
+ PathResolver resolver = new PathResolver();
+ File dir1 = temp.newFolder("D1");
+ File dir2 = temp.newFolder("D2");
+
+ File org = new File(dir2, "org");
+ File hello = new File(org, "hello");
+ File world = new File(hello, "World.java");
+
+ PathResolver.RelativePath relativePath = resolver.relativePath(Arrays.asList(dir1, dir2), world);
+ assertThat(relativePath.dir().getCanonicalPath()).isEqualTo(dir2.getCanonicalPath());
+ assertThat(relativePath.path()).isEqualTo("org/hello/World.java");
+ }
+
+ @Test
+ public void cant_find_relative_path_from_multiple_dirs() throws IOException {
+ PathResolver resolver = new PathResolver();
+ File dir1 = temp.newFolder("D1");
+ File dir2 = temp.newFolder("D2");
+
+ File org = new File(dir2, "org");
+ File hello = new File(org, "hello");
+ File world = new File(hello, "World.java");
+
+ PathResolver.RelativePath relativePath = resolver.relativePath(Arrays.asList(dir1), world);
+ assertThat(relativePath).isNull();
+ }
+
+ @Test
+ public void null_relative_path_when_file_is_not_in_dir() throws IOException {
+ PathResolver resolver = new PathResolver();
+ File rootDir = temp.newFolder();
+
+ assertThat(resolver.relativePath(rootDir, new File("Elsewhere.java"))).isNull();
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/MavenTestUtils.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/MavenTestUtils.java
index 7dfc3bcf67d..0b86e3bb661 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/test/MavenTestUtils.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/MavenTestUtils.java
@@ -21,20 +21,27 @@ package org.sonar.api.test;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.MapConfiguration;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.MavenProject;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.maven.MavenUtils;
-import org.sonar.api.resources.DefaultProjectFileSystem;
-import org.sonar.api.resources.Java;
-import org.sonar.api.resources.Languages;
+import org.sonar.api.resources.InputFile;
+import org.sonar.api.resources.Language;
import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectFileSystem;
+import org.sonar.api.resources.Resource;
import org.sonar.api.utils.SonarException;
import java.io.File;
import java.io.FileReader;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -71,13 +78,13 @@ public final class MavenTestUtils {
MavenProject pom = loadPom(clazz, path);
Configuration configuration = new MapConfiguration(pom.getProperties());
Project project = new Project(pom.getGroupId() + ":" + pom.getArtifactId())
- .setPom(pom)
- .setConfiguration(configuration);
+ .setPom(pom)
+ .setConfiguration(configuration);
configuration.setProperty("sonar.java.source", MavenUtils.getJavaSourceVersion(pom));
configuration.setProperty("sonar.java.target", MavenUtils.getJavaVersion(pom));
configuration.setProperty(CoreProperties.ENCODING_PROPERTY, MavenUtils.getSourceEncoding(pom));
- DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project, new Languages(Java.INSTANCE));
- project.setFileSystem(fs);
+
+ project.setFileSystem(new MavenModuleFileSystem(pom));
return project;
}
@@ -86,4 +93,102 @@ public final class MavenTestUtils {
when(mavenProject.getPackaging()).thenReturn(packaging);
return mavenProject;
}
+
+ static class MavenModuleFileSystem implements ProjectFileSystem {
+ private MavenProject pom;
+
+ MavenModuleFileSystem(MavenProject pom) {
+ this.pom = pom;
+ }
+
+ public Charset getSourceCharset() {
+ return Charset.forName(MavenUtils.getSourceEncoding(pom));
+ }
+
+ public File getBasedir() {
+ return pom.getBasedir();
+ }
+
+ public File getBuildDir() {
+ return new File(pom.getBuild().getDirectory());
+ }
+
+ public File getBuildOutputDir() {
+ return new File(pom.getBuild().getOutputDirectory());
+ }
+
+ public List<File> getSourceDirs() {
+ return Arrays.asList(new File(pom.getBuild().getSourceDirectory()));
+ }
+
+ public ProjectFileSystem addSourceDir(File dir) {
+ throw new UnsupportedOperationException();
+ }
+
+ public List<File> getTestDirs() {
+ return null;
+ }
+
+ public ProjectFileSystem addTestDir(File dir) {
+ throw new UnsupportedOperationException();
+ }
+
+ public File getReportOutputDir() {
+ return null;
+ }
+
+ public File getSonarWorkingDirectory() {
+ File dir = new File(getBuildDir(), "sonar");
+ try {
+ FileUtils.forceMkdir(dir);
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
+ return dir;
+ }
+
+ public File resolvePath(String path) {
+ throw new UnsupportedOperationException();
+ }
+
+ public List<File> getSourceFiles(Language... langs) {
+ return new ArrayList(FileUtils.listFiles(getSourceDirs().get(0), new String[]{"java"}, true));
+ }
+
+ public List<File> getJavaSourceFiles() {
+ return getSourceFiles();
+ }
+
+ public boolean hasJavaSourceFiles() {
+ return !getJavaSourceFiles().isEmpty();
+ }
+
+ public List<File> getTestFiles(Language... langs) {
+ return new ArrayList(FileUtils.listFiles(getTestDirs().get(0), new String[]{"java"}, true));
+ }
+
+ public boolean hasTestFiles(Language lang) {
+ return !getTestFiles(lang).isEmpty();
+ }
+
+ public File writeToWorkingDirectory(String content, String fileName) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ public File getFileFromBuildDirectory(String filename) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Resource toResource(File file) {
+ throw new UnsupportedOperationException();
+ }
+
+ public List<InputFile> mainFiles(String... langs) {
+ throw new UnsupportedOperationException();
+ }
+
+ public List<InputFile> testFiles(String... langs) {
+ throw new UnsupportedOperationException();
+ }
+ }
}