From 65dc85cf661879f9289b93e1410a93a1a113a1a8 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Fri, 21 Jan 2011 15:21:15 +0300 Subject: [PATCH] SONAR-2126: Change api to work with project file system * Introduce InputFile (file and reference to base directory), which can be retrieved from ProjectFileSystem * Reduce dependencies on MavenProject - don't use project.getPom() in DefaultProjectFileSystem and MavenUtils in ProjectUtils --- .../org/sonar/batch/MavenProjectBuilder.java | 26 ++- .../resources/DefaultProjectFileSystem.java | 203 ++++++++++-------- .../org/sonar/api/resources/InputFile.java | 33 +++ .../java/org/sonar/api/resources/Project.java | 14 +- .../api/resources/ProjectFileSystem.java | 62 +++++- .../org/sonar/api/resources/ProjectUtils.java | 20 +- .../DefaultProjectFileSystemTest.java | 37 ++-- .../org/sonar/api/test/MavenTestUtils.java | 11 +- .../api/test/SimpleProjectFileSystem.java | 26 ++- 9 files changed, 296 insertions(+), 136 deletions(-) create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/MavenProjectBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/MavenProjectBuilder.java index 3feb2258463..e05a557bbee 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/MavenProjectBuilder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/MavenProjectBuilder.java @@ -23,6 +23,7 @@ import org.apache.commons.configuration.*; import org.apache.commons.lang.time.DateUtils; import org.apache.maven.project.MavenProject; import org.sonar.api.CoreProperties; +import org.sonar.api.batch.maven.MavenUtils; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.ResourceModel; import org.sonar.api.database.model.Snapshot; @@ -31,6 +32,7 @@ import org.sonar.api.resources.Java; import org.sonar.api.resources.Project; import org.sonar.api.utils.SonarException; +import java.io.File; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -68,23 +70,35 @@ public class MavenProjectBuilder { return configuration.getString(CoreProperties.PROJECT_BRANCH_PROPERTY, configuration.getString("branch" /* deprecated property */)); } - public void configure(Project project) { ProjectConfiguration projectConfiguration = new ProjectConfiguration(databaseSession, project); configure(project, projectConfiguration); } - void configure(Project project, Configuration projectConfiguration) { Date analysisDate = loadAnalysisDate(projectConfiguration); + DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project); + MavenProject pom = project.getPom(); + if (pom != null) { + for (File dir : fs.resolvePaths(pom.getCompileSourceRoots())) { + fs.addSourceDir(dir); + } + for (File dir : fs.resolvePaths(pom.getTestCompileSourceRoots())) { + fs.addTestDir(dir); + } + fs.setBaseDir(pom.getBasedir()); + fs.setBuildDir(pom.getBuild().getDirectory()); + projectConfiguration.setProperty("sonar.java.sourceVersion", MavenUtils.getJavaSourceVersion(pom)); + projectConfiguration.setProperty("sonar.java.targetVersion", MavenUtils.getJavaVersion(pom)); + } project.setConfiguration(projectConfiguration) .setExclusionPatterns(loadExclusionPatterns(projectConfiguration)) .setAnalysisDate(analysisDate) .setLatestAnalysis(isLatestAnalysis(project.getKey(), analysisDate)) - .setAnalysisVersion(loadAnalysisVersion(projectConfiguration, project.getPom())) + .setAnalysisVersion(loadAnalysisVersion(projectConfiguration, pom)) .setAnalysisType(loadAnalysisType(projectConfiguration)) .setLanguageKey(loadLanguageKey(projectConfiguration)) - .setFileSystem(new DefaultProjectFileSystem(project)); + .setFileSystem(fs); } static String[] loadExclusionPatterns(Configuration configuration) { @@ -104,7 +118,6 @@ public class MavenProjectBuilder { return true; } - Date loadAnalysisDate(Configuration configuration) { String formattedDate = configuration.getString(CoreProperties.PROJECT_DATE_PROPERTY); if (formattedDate == null) { @@ -118,7 +131,8 @@ public class MavenProjectBuilder { return DateUtils.setMinutes(date, 1); } catch (ParseException e) { - throw new SonarException("The property " + CoreProperties.PROJECT_DATE_PROPERTY + " does not respect the format yyyy-MM-dd (for example 2008-05-23) : " + formattedDate, e); + throw new SonarException("The property " + CoreProperties.PROJECT_DATE_PROPERTY + + " does not respect the format yyyy-MM-dd (for example 2008-05-23) : " + formattedDate, e); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java index d66011d11a1..45c1bbcabec 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java @@ -26,7 +26,7 @@ import org.apache.commons.io.filefilter.*; import org.apache.commons.lang.CharEncoding; import org.apache.commons.lang.StringUtils; import org.sonar.api.batch.FileFilter; -import org.sonar.api.batch.maven.MavenUtils; +import org.sonar.api.utils.Logs; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.WildcardPattern; @@ -38,32 +38,39 @@ import java.util.Arrays; import java.util.List; /** - * An implementation of ProjectFileSystem - * + * An implementation of {@link ProjectFileSystem}. + * For internal use only. + * * @since 1.10 + * @TODO inject into container */ public class DefaultProjectFileSystem implements ProjectFileSystem { private Project project; private List filters = Lists.newArrayList(); - /** - * Creates a DefaultProjectFileSystem based on a project - * - * @param project - */ + private File basedir; + private File buildDir; + private List sourceDirs = Lists.newArrayList(); + private List testDirs = Lists.newArrayList(); + public DefaultProjectFileSystem(Project project) { this.project = project; } - /** - * Source encoding. Never null, it returns the default plateform charset if it is not defined in project. - */ public Charset getSourceCharset() { - return MavenUtils.getSourceCharset(project.getPom()); + // TODO was return MavenUtils.getSourceCharset(project.getPom()); + String encoding = project.getConfiguration().getString("project.build.sourceEncoding"); + if (StringUtils.isNotEmpty(encoding)) { + try { + return Charset.forName(encoding); + } catch (Exception e) { + Logs.INFO.warn("Can not get project charset", e); + } + } + return Charset.defaultCharset(); } - public DefaultProjectFileSystem addFileFilters(List l) { for (FileFilter fileFilter : l) { addFileFilter(fileFilter); @@ -76,80 +83,53 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { return this; } - /** - * Basedir is the project root directory. - */ public File getBasedir() { - return project.getPom().getBasedir(); + // TODO was return project.getPom().getBasedir(); + return basedir; } - /** - * Build directory is by default "target" in maven projects. - */ public File getBuildDir() { - return resolvePath(project.getPom().getBuild().getDirectory()); + // TODO was return resolvePath(project.getPom().getBuild().getDirectory()); + return buildDir; } - /** - * Directory where classes are placed. By default "target/classes" in maven projects. - */ public File getBuildOutputDir() { - return resolvePath(project.getPom().getBuild().getOutputDirectory()); + // TODO was return resolvePath(project.getPom().getBuild().getOutputDirectory()); + return resolvePath(project.getConfiguration().getString("project.build.outputDirectory")); } - /** - * The list of directories for sources - */ public List getSourceDirs() { - return resolvePaths(project.getPom().getCompileSourceRoots()); + return sourceDirs; } - /** - * Adds a source directory - * - * @return the current object - */ public DefaultProjectFileSystem addSourceDir(File dir) { if (dir == null) { throw new IllegalArgumentException("Can not add null to project source dirs"); } - project.getPom().getCompileSourceRoots().add(0, dir.getAbsolutePath()); + sourceDirs.add(dir); return this; } - /** - * The list of directories for tests - */ public List getTestDirs() { - return resolvePaths(project.getPom().getTestCompileSourceRoots()); + return testDirs; } - /** - * Adds a test directory - * - * @return the current object - */ public DefaultProjectFileSystem addTestDir(File dir) { if (dir == null) { throw new IllegalArgumentException("Can not add null to project test dirs"); } - project.getPom().getTestCompileSourceRoots().add(0, dir.getAbsolutePath()); + testDirs.add(dir); return this; } - /** - * @return the directory where reporting is placed. Default is target/sites - */ public File getReportOutputDir() { - return resolvePath(project.getPom().getReporting().getOutputDirectory()); + // TODO was return resolvePath(project.getPom().getReporting().getOutputDirectory()); + return resolvePath(project.getConfiguration().getString("project.reporting.outputDirectory")); } - /** - * @return the Sonar working directory. Default is "target/sonar" - */ public File getSonarWorkingDirectory() { try { - File dir = new File(project.getPom().getBuild().getDirectory(), "sonar"); + File dir = new File(getBuildDir(), "sonar"); FileUtils.forceMkdir(dir); return dir; @@ -161,63 +141,47 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { public File resolvePath(String path) { File file = new File(path); if (!file.isAbsolute()) { - file = new File(project.getPom().getBasedir(), path); + file = new File(getBasedir(), path); } return file; } - private List resolvePaths(List paths) { - List result = new ArrayList(); + // TODO was private + public List resolvePaths(List paths) { + List result = Lists.newArrayList(); if (paths != null) { for (String path : paths) { result.add(resolvePath(path)); } } - return result; } - /** - * Gets the list of source files for given languages - * - * @param langs language filter. If null or empty, will return empty list - */ + @Deprecated public List getSourceFiles(Language... langs) { - return getFiles(getSourceDirs(), true, langs); + return toFiles(mainFiles(langs)); } - /** - * Gets the list of java source files - */ + @Deprecated public List getJavaSourceFiles() { return getSourceFiles(Java.INSTANCE); } - /** - * @return whether there are java source - */ public boolean hasJavaSourceFiles() { - return !getJavaSourceFiles().isEmpty(); + return !mainFiles(Java.INSTANCE).isEmpty(); } - /** - * Gets the list of test files for given languages - * - * @param langs language filter. If null or empty, will return empty list - */ + @Deprecated public List getTestFiles(Language... langs) { - return getFiles(getTestDirs(), false, langs); + return toFiles(testFiles(langs)); } - /** - * @return whether there are tests files - */ public boolean hasTestFiles(Language lang) { - return !getTestFiles(lang).isEmpty(); + return !testFiles(lang).isEmpty(); } - private List getFiles(List directories, boolean applyExclusionPatterns, Language... langs) { - List result = new ArrayList(); + private List getFiles(List directories, boolean applyExclusionPatterns, Language... langs) { + List result = Lists.newArrayList(); if (directories == null) { return result; } @@ -229,9 +193,12 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { if (dir.exists()) { IOFileFilter exclusionFilter = new ExclusionFilter(dir, exclusionPatterns); IOFileFilter visibleFileFilter = HiddenFileFilter.VISIBLE; - List dirFilters = Lists.newArrayList(visibleFileFilter, suffixFilter, exclusionFilter); + List dirFilters = Lists.newArrayList(visibleFileFilter, suffixFilter, exclusionFilter); dirFilters.addAll(this.filters); - result.addAll(FileUtils.listFiles(dir, new AndFileFilter(dirFilters), HiddenFileFilter.VISIBLE)); + List files = (List) FileUtils.listFiles(dir, new AndFileFilter(dirFilters), HiddenFileFilter.VISIBLE); + for (File file : files) { + result.add(new DefaultInputFile(dir, file)); + } } } return result; @@ -291,11 +258,6 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { } } - /** - * Save data into a new file of Sonar working directory. - * - * @return the created file - */ public File writeToWorkingDirectory(String content, String fileName) throws IOException { return writeToFile(content, getSonarWorkingDirectory(), fileName); } @@ -308,7 +270,7 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { /** * getRelativePath("c:/foo/src/my/package/Hello.java", "c:/foo/src") is "my/package/Hello.java" - * + * * @return null if file is not in dir (including recursive subdirectories) */ public static String getRelativePath(File file, File dir) { @@ -317,9 +279,10 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { /** * getRelativePath("c:/foo/src/my/package/Hello.java", ["c:/bar", "c:/foo/src"]) is "my/package/Hello.java". - *

- *

Relative path is composed of slashes. Windows backslaches are replaced by /

- * + *

+ * Relative path is composed of slashes. Windows backslaches are replaced by / + *

+ * * @return null if file is not in dir (including recursive subdirectories) */ public static String getRelativePath(File file, List dirs) { @@ -362,4 +325,58 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { } return false; } + + private static List toFiles(List files) { + List result = Lists.newArrayList(); + for (InputFile file : files) { + result.add(file.getFile()); + } + return result; + } + + /** + * @since 2.6 + */ + public List mainFiles(Language... langs) { + return getFiles(getSourceDirs(), true, langs); + } + + /** + * @since 2.6 + */ + public List testFiles(Language... langs) { + return getFiles(getTestDirs(), false /* FIXME should be true? */, langs); + } + + private class DefaultInputFile implements InputFile { + private File basedir; + private File file; + + public DefaultInputFile(File basedir, File file) { + this.basedir = basedir; + this.file = file; + } + + public File getBaseDir() { + return basedir; + } + + public File getFile() { + return file; + } + } + + /** + * @since 2.6 + */ + public void setBaseDir(File basedir) { + this.basedir = basedir; + } + + /** + * @since 2.6 + */ + public void setBuildDir(String path) { + this.buildDir = path == null ? resolvePath("target") : resolvePath(path); + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java new file mode 100644 index 00000000000..5f53317b464 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java @@ -0,0 +1,33 @@ +/* + * 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 java.io.File; + +/** + * @since 2.6 + */ +public interface InputFile { + + File getBaseDir(); + + File getFile(); + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java index c891e90395c..bfecdbd3eba 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java @@ -30,7 +30,7 @@ import java.util.Date; import java.util.List; /** - * A class that manipulates Projects in the Sonar way, i.e. mixing MavenProjects with the way it should be analyzed + * A class that manipulates Projects in the Sonar way. * * @since 1.10 */ @@ -42,37 +42,37 @@ public class Project extends Resource { * @deprecated since version 1.11. Constant moved to CoreProperties */ @Deprecated - public static final String PARAM_VERSION = "sonar.projectVersion"; + public static final String PARAM_VERSION = CoreProperties.PROJECT_VERSION_PROPERTY; /** * @deprecated since version 1.11. Constant moved to CoreProperties */ @Deprecated - public static final String PARAM_DATE = "sonar.projectDate"; + public static final String PARAM_DATE = CoreProperties.PROJECT_DATE_PROPERTY; /** * @deprecated since version 1.11. Constant moved to CoreProperties */ @Deprecated - public static final String PARAM_LANGUAGE = "sonar.language"; + public static final String PARAM_LANGUAGE = CoreProperties.PROJECT_LANGUAGE_PROPERTY; /** * @deprecated since version 1.11. Constant moved to CoreProperties */ @Deprecated - public static final String PARAM_DYNAMIC_ANALYSIS = "sonar.dynamicAnalysis"; + public static final String PARAM_DYNAMIC_ANALYSIS = CoreProperties.DYNAMIC_ANALYSIS_PROPERTY; /** * @deprecated since version 1.11. Constant moved to CoreProperties */ @Deprecated - public static final String PARAM_EXCLUSIONS = "sonar.exclusions"; + public static final String PARAM_EXCLUSIONS = CoreProperties.PROJECT_EXCLUSIONS_PROPERTY; /** * @deprecated since version 1.11. Constant moved to CoreProperties */ @Deprecated - public static final String PARAM_REUSE_RULES_CONFIG = "sonar.reuseExistingRulesConfiguration"; + public static final String PARAM_REUSE_RULES_CONFIG = CoreProperties.REUSE_RULES_CONFIGURATION_PROPERTY; /** * Enumerates the type of possible analysis diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java index 9440b37daf7..f64f98d4f9a 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java @@ -19,6 +19,8 @@ */ package org.sonar.api.resources; +import org.sonar.api.BatchExtension; + import java.io.File; import java.io.IOException; import java.nio.charset.Charset; @@ -26,11 +28,12 @@ import java.util.List; /** * @since 1.10 - * @deprecated since 2.6 */ -public interface ProjectFileSystem { +public interface ProjectFileSystem extends BatchExtension { /** - * Source encoding. It's the default platform charset if it is not defined in the project (Maven property 'project.build.sourceEncoding'). + * Source encoding. + * Never null, it returns the default platform charset if it is not defined in project. + * (Maven property 'project.build.sourceEncoding'). */ Charset getSourceCharset(); @@ -44,18 +47,43 @@ public interface ProjectFileSystem { */ File getBuildDir(); + /** + * Directory where classes are placed. It's "${basedir}/target/classes" by default in Maven projects. + */ File getBuildOutputDir(); + /** + * The list of directories for sources + */ List getSourceDirs(); + /** + * Adds a source directory + * + * @return the current object + */ ProjectFileSystem addSourceDir(File dir); + /** + * The list of directories for tests + */ List getTestDirs(); + /** + * Adds a test directory + * + * @return the current object + */ ProjectFileSystem addTestDir(File dir); + /** + * @return the directory where reporting is placed. Default is target/sites + */ File getReportOutputDir(); + /** + * @return the Sonar working directory. Default is "target/sonar" + */ File getSonarWorkingDirectory(); /** @@ -68,12 +96,19 @@ public interface ProjectFileSystem { * Source files, excluding unit tests and files matching project exclusion patterns. * * @param langs language filter. Check all files, whatever their language, if null or empty. + * @deprecated since 2.6 use {@link #mainFiles(Language...)} instead. + * See http://jira.codehaus.org/browse/SONAR-2126 */ + @Deprecated List getSourceFiles(Language... langs); /** * Java source files, excluding unit tests and files matching project exclusion patterns. Shortcut for getSourceFiles(Java.INSTANCE) + * + * @deprecated since 2.6 use {@link #mainFiles(Language...)} instead. + * See http://jira.codehaus.org/browse/SONAR-2126 */ + @Deprecated List getJavaSourceFiles(); /** @@ -83,7 +118,11 @@ public interface ProjectFileSystem { /** * Unit test files, excluding files matching project exclusion patterns. + * + * @deprecated since 2.6 use {@link #testFiles(Language...)} instead. + * See http://jira.codehaus.org/browse/SONAR-2126 */ + @Deprecated List getTestFiles(Language... langs); /** @@ -101,4 +140,21 @@ public interface ProjectFileSystem { File getFileFromBuildDirectory(String filename); Resource toResource(File file); + + /** + * Source files, excluding unit tests and files matching project exclusion patterns. + * + * @param langs language filter. If null or empty, will return empty list + * @since 2.6 + */ + List mainFiles(Language... langs); + + /** + * TODO comment me + * + * @param langs language filter. If null or empty, will return empty list + * @since 2.6 + */ + List testFiles(Language... langs); + } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectUtils.java index 1ad585acbe3..1998fbbdaf1 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectUtils.java @@ -19,9 +19,11 @@ */ package org.sonar.api.resources; -import org.sonar.api.batch.maven.MavenUtils; - /** + * FIXME + * Actually this class incorrectly named, because provides information not about project, but about Java project. + * And seems that only core plugins use this class. + * * @since 1.10 */ public final class ProjectUtils { @@ -34,10 +36,20 @@ public final class ProjectUtils { * Java version as defined in maven-compiler-plugin */ public static String getJavaVersion(Project project) { - return MavenUtils.getJavaVersion(project.getPom()); + // target version + // TODO was return MavenUtils.getJavaVersion(project.getPom()); + if (project.getConfiguration() != null) { + return project.getConfiguration().getString("sonar.java.targetVersion"); + } + return null; } public static String getJavaSourceVersion(Project project) { - return MavenUtils.getJavaSourceVersion(project.getPom()); + // source version + // TODO was return MavenUtils.getJavaSourceVersion(project.getPom()); + if (project.getConfiguration() != null) { + return project.getConfiguration().getString("sonar.java.sourceVersion"); + } + return null; } } 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 index 4aa606b97e4..52840b4a7ee 100644 --- 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 @@ -48,7 +48,7 @@ public class DefaultProjectFileSystemTest { @Test public void getJavaSourceFiles() { - final DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); assertThat(fs.getJavaSourceFiles().size(), is(2)); assertThat(fs.getJavaSourceFiles(), hasItem(named("Bar.java"))); @@ -57,16 +57,16 @@ public class DefaultProjectFileSystemTest { @Test public void hasJavaSourceFiles() { - final DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); assertThat(fs.hasJavaSourceFiles(), is(true)); - project.setExclusionPatterns(new String[]{"**/*.java"}); + project.setExclusionPatterns(new String[] { "**/*.java" }); assertThat(fs.hasJavaSourceFiles(), is(false)); } @Test public void getTestFiles() { - final DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); assertThat(fs.getTestFiles(Java.INSTANCE).size(), is(1)); assertThat(fs.getTestFiles(Java.INSTANCE), hasItem(named("BarTest.java"))); @@ -74,9 +74,9 @@ public class DefaultProjectFileSystemTest { @Test public void applyExclusionPatternsToSourceFiles() { - project.setExclusionPatterns(new String[]{"**/B*.java"}); + project.setExclusionPatterns(new String[] { "**/B*.java" }); - final DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); assertThat(fs.getJavaSourceFiles().size(), is(1)); assertThat(fs.getJavaSourceFiles(), hasItem(named("Whizz.java"))); @@ -87,9 +87,9 @@ public class DefaultProjectFileSystemTest { */ @Test public void exclusionPatternOnAjFiles() { - project.setExclusionPatterns(new String[]{"**/*.aj"}); + project.setExclusionPatterns(new String[] { "**/*.aj" }); - final DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); assertThat(fs.getSourceFiles(Java.INSTANCE).size(), is(2)); assertThat(fs.getSourceFiles(Java.INSTANCE), hasItem(named("Whizz.java"))); @@ -98,9 +98,9 @@ public class DefaultProjectFileSystemTest { @Test public void doNotApplyExclusionPatternsToTestFiles() { - project.setExclusionPatterns(new String[]{"**/B*.java"}); + project.setExclusionPatterns(new String[] { "**/B*.java" }); - final DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); assertThat(fs.getTestFiles(Java.INSTANCE).size(), is(1)); assertThat(fs.getTestFiles(Java.INSTANCE), hasItem(named("BarTest.java"))); @@ -108,7 +108,7 @@ public class DefaultProjectFileSystemTest { @Test public void createSonarWorkingDirectory() { - DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project); + DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); java.io.File dir = fs.getSonarWorkingDirectory(); assertThat(dir.exists(), is(true)); assertThat(dir.listFiles().length, is(0)); @@ -117,7 +117,7 @@ public class DefaultProjectFileSystemTest { @Test public void getJapaneseCharSet() { project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "japanese-project/pom.xml"); - DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project); + DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); assertThat(fs.getSourceCharset().name(), is("Shift_JIS")); } @@ -138,7 +138,7 @@ public class DefaultProjectFileSystemTest { } project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "sample-with-different-suffixes/pom.xml"); - ProjectFileSystem fs = new DefaultProjectFileSystem(project); + ProjectFileSystem fs = newDefaultProjectFileSystem(project); List files = fs.getSourceFiles(new NoSuffixLanguage()); assertThat(files.size(), is(2)); } @@ -152,29 +152,32 @@ public class DefaultProjectFileSystemTest { // 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 = new DefaultProjectFileSystem(project); + ProjectFileSystem fs = newDefaultProjectFileSystem(project); List files = fs.getSourceFiles(); assertThat(files.size(), is(1)); assertThat(files.get(0).getName(), is("foo.sql")); } } - @Test public void shouldAddExtendedFilters() { - DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project); + 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"); + 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 named(final String name) { return new TypeSafeMatcher() { java.io.File fileTested; 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 b2aa52cb469..7b4d9dcf186 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 @@ -67,7 +67,16 @@ public final class MavenTestUtils { Project project = new Project(pom.getGroupId() + ":" + pom.getArtifactId()) .setPom(pom) .setConfiguration(new MapConfiguration(pom.getProperties())); - project.setFileSystem(new DefaultProjectFileSystem(project)); + DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project); + project.setFileSystem(fs); + for (File dir : fs.resolvePaths(project.getPom().getCompileSourceRoots())) { + fs.addSourceDir(dir); + } + for (File dir : fs.resolvePaths(project.getPom().getTestCompileSourceRoots())) { + fs.addTestDir(dir); + } + fs.setBaseDir(project.getPom().getBasedir()); + fs.setBuildDir(project.getPom().getBuild().getDirectory()); return project; } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/SimpleProjectFileSystem.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/SimpleProjectFileSystem.java index d219faee187..6bf7acad63a 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/test/SimpleProjectFileSystem.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/SimpleProjectFileSystem.java @@ -19,11 +19,7 @@ */ package org.sonar.api.test; -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.util.Arrays; -import java.util.List; +import org.sonar.api.resources.InputFile; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.CharEncoding; @@ -33,6 +29,12 @@ 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.IOException; +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.List; + public class SimpleProjectFileSystem implements ProjectFileSystem { private File basedir; @@ -129,4 +131,18 @@ public class SimpleProjectFileSystem implements ProjectFileSystem { public Resource toResource(File file) { return null; } + + /** + * @since 2.6 + */ + public List mainFiles(Language... lang) { + return null; + } + + /** + * @since 2.6 + */ + public List testFiles(Language... lang) { + return null; + } } -- 2.39.5