import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.sonar.api.CoreProperties;
-import org.sonar.api.resources.DefaultProjectFileSystem;
+import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.resources.Project;
import org.sonar.api.test.MavenTestUtils;
public class CoberturaUtilsTest {
@Test
public void shouldGetReportPathFromProperty() throws URISyntaxException {
- DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class);
+ ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
when(fileSystem.resolvePath("foo")).thenReturn(getCoverageReport());
Project project = mock(Project.class);
public void shouldGetReportPathFromPom() {
MavenProject pom = MavenTestUtils.loadPom("/org/sonar/plugins/cobertura/CoberturaSensorTest/shouldGetReportPathFromPom/pom.xml");
- DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class);
+ ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
Project project = mock(Project.class);
when(project.getPom()).thenReturn(pom);
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.resources.DefaultProjectFileSystem;
+import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
}
private Project createProject() {
- DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class);
+ ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
when(fileSystem.hasJavaSourceFiles()).thenReturn(Boolean.TRUE);
Project project = mock(Project.class);
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.sonar.api.batch.SensorContext;
-import org.sonar.api.resources.DefaultProjectFileSystem;
+import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
import org.sonar.api.rules.Rule;
public class PmdViolationsXmlParserTest {
private void parse(SensorContext context, String xmlPath) throws URISyntaxException, XMLStreamException {
- DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class);
+ ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
when(fileSystem.getSourceDirs()).thenReturn(Arrays.asList(new File("/test/src/main/java")));
Project project = mock(Project.class);
--- /dev/null
+/*
+ * 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.batch;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.maven.project.MavenProject;
+import org.sonar.api.resources.DefaultProjectFileSystem;
+import org.sonar.api.resources.Languages;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectFileSystem;
+import org.sonar.api.utils.SonarException;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * Implementation of {@link ProjectFileSystem} based on {@link MavenProject}.
+ */
+public class MavenProjectFileSystem extends DefaultProjectFileSystem {
+
+ private MavenProject pom;
+
+ public MavenProjectFileSystem(Project project, Languages languages) {
+ super(project, languages);
+ this.pom = project.getPom();
+ }
+
+ @Override
+ public File getBasedir() {
+ return pom.getBasedir();
+ }
+
+ @Override
+ public File getBuildDir() {
+ return resolvePath(pom.getBuild().getDirectory());
+ }
+
+ @Override
+ public File getBuildOutputDir() {
+ return resolvePath(pom.getBuild().getOutputDirectory());
+ }
+
+ /**
+ * Maven can modify source directories during Sonar execution - see MavenPhaseExecutor.
+ */
+ @Override
+ public List<File> getSourceDirs() {
+ return resolvePaths(pom.getCompileSourceRoots());
+ }
+
+ @Override
+ public DefaultProjectFileSystem addSourceDir(File dir) {
+ if (dir == null) {
+ throw new IllegalArgumentException("Can not add null to project source dirs");
+ }
+ pom.getCompileSourceRoots().add(0, dir.getAbsolutePath());
+ return this;
+ }
+
+ /**
+ * Maven can modify test directories during Sonar execution - see MavenPhaseExecutor.
+ */
+ @Override
+ public List<File> getTestDirs() {
+ return resolvePaths(pom.getTestCompileSourceRoots());
+ }
+
+ /**
+ * @deprecated since 2.6, because should be immutable
+ */
+ @Override
+ public DefaultProjectFileSystem addTestDir(File dir) {
+ if (dir == null) {
+ throw new IllegalArgumentException("Can not add null to project test dirs");
+ }
+ pom.getTestCompileSourceRoots().add(0, dir.getAbsolutePath());
+ return this;
+ }
+
+ @Override
+ public File getReportOutputDir() {
+ return resolvePath(pom.getReporting().getOutputDirectory());
+ }
+
+ @Override
+ public File getSonarWorkingDirectory() {
+ try {
+ File dir = new File(getBuildDir(), "sonar");
+ FileUtils.forceMkdir(dir);
+ return dir;
+
+ } catch (IOException e) {
+ throw new SonarException("Unable to retrieve Sonar working directory.", e);
+ }
+ }
+}
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metrics;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.resources.*;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.rules.DefaultRulesManager;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.bootstrap.BatchPluginRepository;
addComponent(project);
addComponent(project.getPom());
addComponent(ProjectClasspath.class);
- addComponent(DefaultProjectFileSystem.class);
+ addComponent(MavenProjectFileSystem.class);
addComponent(project.getConfiguration());
// need to be registered after the Configuration
return file;
}
- private List<File> resolvePaths(List<String> paths) {
+ protected List<File> resolvePaths(List<String> paths) {
List<File> result = Lists.newArrayList();
if (paths != null) {
for (String path : paths) {
List<File> files = (List<File>) FileUtils.listFiles(dir, new AndFileFilter(dirFilters), HiddenFileFilter.VISIBLE);
for (File file : files) {
String relativePath = DefaultProjectFileSystem.getRelativePath(file, dir);
- result.add(new DefaultInputFile(dir, relativePath));
+ result.add(InputFileUtils.create(dir, relativePath));
}
}
}
public List<InputFile> testFiles(String... langs) {
return getFiles(getTestDirs(), false /* FIXME should be true? */, langs);
}
-
- private static final class DefaultInputFile implements InputFile {
- private File basedir;
- private String relativePath;
-
- DefaultInputFile(File basedir, String relativePath) {
- this.basedir = basedir;
- this.relativePath = relativePath;
- }
-
- public File getFileBaseDir() {
- return basedir;
- }
-
- public File getFile() {
- return new File(basedir, relativePath);
- }
-
- public String getRelativePath() {
- return relativePath;
- }
- }
}
import org.junit.Test;
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
import org.sonar.api.CoreProperties;
-import org.sonar.api.resources.DefaultProjectFileSystem;
+import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Language;
}
private void fileEncodingTest(Project project, SensorContext context, String encoding, String testFile) {
- DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class);
+ ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
when(project.getFileSystem()).thenReturn(fileSystem);
when(fileSystem.getSourceCharset()).thenReturn(Charset.forName(encoding));
when(project.getConfiguration()).thenReturn(new MapConfiguration(new HashMap<String, String>()));