Improve ProjectFileSystem

* Use ProjectFileSystem instead of DefaultProjectFileSystem in tests
* Use InputFileUtils in DefaultProjectFileSystem
* Add MavenProjectFileSystem
This commit is contained in:
Evgeny Mandrikov 2011-04-07 01:02:25 +04:00
parent 07625d01d5
commit d6693ffe16
7 changed files with 130 additions and 35 deletions

View File

@ -27,7 +27,7 @@ import static org.mockito.Mockito.when;
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;
@ -37,7 +37,7 @@ import java.net.URISyntaxException;
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);
@ -53,7 +53,7 @@ public class CoberturaUtilsTest {
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);

View File

@ -24,7 +24,7 @@ import org.junit.Test;
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;
@ -147,7 +147,7 @@ public class FindbugsSensorTest extends FindbugsTests {
}
private Project createProject() {
DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class);
ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
when(fileSystem.hasJavaSourceFiles()).thenReturn(Boolean.TRUE);
Project project = mock(Project.class);

View File

@ -39,7 +39,7 @@ import org.junit.Test;
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;
@ -50,7 +50,7 @@ import org.sonar.api.test.IsViolation;
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);

View File

@ -0,0 +1,114 @@
/*
* 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);
}
}
}

View File

@ -25,7 +25,10 @@ import org.sonar.api.measures.CoreMetrics;
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;
@ -103,7 +106,7 @@ public class ProjectBatch {
addComponent(project);
addComponent(project.getPom());
addComponent(ProjectClasspath.class);
addComponent(DefaultProjectFileSystem.class);
addComponent(MavenProjectFileSystem.class);
addComponent(project.getConfiguration());
// need to be registered after the Configuration

View File

@ -152,7 +152,7 @@ public class DefaultProjectFileSystem implements ProjectFileSystem {
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) {
@ -204,7 +204,7 @@ public class DefaultProjectFileSystem implements ProjectFileSystem {
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));
}
}
}
@ -362,26 +362,4 @@ public class DefaultProjectFileSystem implements ProjectFileSystem {
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;
}
}
}

View File

@ -53,7 +53,7 @@ import org.junit.Ignore;
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;
@ -143,7 +143,7 @@ public class AbstractSourceImporterTest {
}
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>()));