diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-06-06 14:27:49 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-06-06 14:28:04 +0200 |
commit | d346d2313443339ded7b86bca7765a7ee850a8bb (patch) | |
tree | 9ee28f020559465a003f952ed538d3a6ddd68802 /sonar-batch/src/test | |
parent | 00d12c9382a8c4161455e2203b9b074b15278111 (diff) | |
download | sonarqube-d346d2313443339ded7b86bca7765a7ee850a8bb.tar.gz sonarqube-d346d2313443339ded7b86bca7765a7ee850a8bb.zip |
Allow the extension ProjectBuilder to change the source directories of root module
Diffstat (limited to 'sonar-batch/src/test')
7 files changed, 91 insertions, 8 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java index 687ccbbbc6b..eecec7e0113 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java @@ -19,13 +19,18 @@ */ package org.sonar.batch; +import org.apache.maven.project.MavenProject; import org.junit.Test; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.maven.MavenPlugin; import org.sonar.api.batch.maven.MavenPluginHandler; import org.sonar.api.resources.Project; +import java.io.File; + import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.junit.internal.matchers.IsCollectionContaining.hasItem; public class AbstractMavenPluginExecutorTest { @@ -34,13 +39,36 @@ public class AbstractMavenPluginExecutorTest { assertThat(AbstractMavenPluginExecutor.getGoal("group", "artifact", null, "goal"), is("group:artifact::goal")); } - static class FakeCheckstyleMavenPluginHandler implements MavenPluginHandler { + /** + * The maven plugin sometimes changes the project structure (for example mvn build-helper:add-source). These changes + * must be applied to the internal structure. + */ + @Test + public void shouldUpdateProjectAfterExecution() { + AbstractMavenPluginExecutor executor = new AbstractMavenPluginExecutor() { + @Override + public void concreteExecute(MavenProject pom, String goal) throws Exception { + pom.addCompileSourceRoot("src/java"); + } + }; + MavenProject pom = new MavenProject(); + pom.setFile(new File("target/AbstractMavenPluginExecutorTest/pom.xml")); + pom.getBuild().setDirectory("target"); + Project foo = new Project("foo"); + foo.setPom(pom); + ProjectDefinition definition = ProjectDefinition.create(); + executor.execute(foo, definition, new AddSourceMavenPluginHandler()); + + assertThat(definition.getSourceDirs(), hasItem("src/java")); + } + + static class AddSourceMavenPluginHandler implements MavenPluginHandler { public String getGroupId() { - return "org.apache.maven.plugins"; + return "fake"; } public String getArtifactId() { - return "maven-checkstyle-plugin"; + return "fake"; } public String getVersion() { @@ -52,7 +80,7 @@ public class AbstractMavenPluginExecutorTest { } public String[] getGoals() { - return new String[] { "checkstyle" }; + return new String[] { "fake" }; } public void configure(Project project, MavenPlugin plugin) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/DefaultProjectFileSystem2Test.java b/sonar-batch/src/test/java/org/sonar/batch/DefaultProjectFileSystem2Test.java new file mode 100644 index 00000000000..7fd4cb02745 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/DefaultProjectFileSystem2Test.java @@ -0,0 +1,47 @@ +/* + * 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.hamcrest.core.Is; +import org.junit.Test; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.resources.Languages; +import org.sonar.api.resources.Project; + +import java.io.File; + +import static org.junit.Assert.assertThat; +import static org.junit.internal.matchers.IsCollectionContaining.hasItem; + +public class DefaultProjectFileSystem2Test { + @Test + public void shouldIgnoreInexistingSourceDirs() { + File exists = FileUtils.toFile(getClass().getResource("/org/sonar/batch/DefaultProjectFileSystem2Test/shouldIgnoreInexistingSourceDirs")); + File notExists = new File("target/unknown"); + + ProjectDefinition definition = ProjectDefinition.create().addSourceDirs(exists, notExists); + + DefaultProjectFileSystem2 fs = new DefaultProjectFileSystem2(new Project("foo"), new Languages(), definition); + assertThat(fs.getSourceDirs().size(), Is.is(1)); + assertThat(fs.getSourceDirs(), hasItem(exists)); + + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java b/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java index 279645cb3a2..7077ddde528 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java @@ -45,9 +45,11 @@ public class MavenProjectConverterTest { public void shouldConvertModules() { MavenProject root = new MavenProject(); root.setFile(new File("/foo/pom.xml")); + root.getBuild().setDirectory("target"); root.getModules().add("module"); MavenProject module = new MavenProject(); module.setFile(new File("/foo/module/pom.xml")); + module.getBuild().setDirectory("target"); ProjectDefinition project = MavenProjectConverter.convert(Arrays.asList(root, module), root); assertThat(project.getSubProjects().size(), is(1)); @@ -61,6 +63,8 @@ public class MavenProjectConverterTest { pom.setVersion("1.0.1"); pom.setName("Test"); pom.setDescription("just test"); + pom.setFile(new File("/foo/pom.xml")); + pom.getBuild().setDirectory("target"); ProjectDefinition project = MavenProjectConverter.convert(pom); Properties properties = project.getProperties(); @@ -130,6 +134,7 @@ public class MavenProjectConverterTest { Model model = new MavenXpp3Reader().read(new StringReader(FileUtils.readFileToString(pomFile))); MavenProject pom = new MavenProject(model); pom.setFile(pomFile); + pom.getBuild().setDirectory("target"); pom.setExecutionRoot(isRoot); return pom; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java index dd765b0e100..ff62f837bbd 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java @@ -35,7 +35,7 @@ public class BootstrapModuleTest { public void execute(Project project, String goal) { } - public MavenPluginHandler execute(Project project, MavenPluginHandler handler) { + public MavenPluginHandler execute(Project project, ProjectDefinition projectDef, MavenPluginHandler handler) { return handler; } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java index fc86a6d4c27..181abf73081 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java @@ -66,7 +66,7 @@ public class DecoratorsExecutorTest { Decorator decorator = mock(Decorator.class); doThrow(new SonarException()).when(decorator).decorate(any(Resource.class), any(DecoratorContext.class)); - DecoratorsExecutor executor = new DecoratorsExecutor(mock(BatchExtensionDictionnary.class), mock(SonarIndex.class), mock(EventBus.class)); + DecoratorsExecutor executor = new DecoratorsExecutor(mock(BatchExtensionDictionnary.class), new Project("key"), mock(SonarIndex.class), mock(EventBus.class)); try { executor.executeDecorator(decorator, mock(DefaultDecoratorContext.class), new File("org/foo/Bar.java")); fail("Exception has not been thrown"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java index 2231bea710a..60efff96509 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java @@ -22,6 +22,7 @@ package org.sonar.batch.phases; import org.junit.Test; import org.sonar.api.batch.PostJob; import org.sonar.api.batch.SensorContext; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.resources.Project; import org.sonar.batch.MavenPluginExecutor; @@ -39,10 +40,11 @@ public class PostJobsExecutorTest { PostJob job2 = mock(PostJob.class); List<PostJob> jobs = Arrays.asList(job1, job2); - PostJobsExecutor executor = new PostJobsExecutor(jobs, mock(MavenPluginExecutor.class)); Project project = new Project("project"); + ProjectDefinition projectDefinition = ProjectDefinition.create(); + PostJobsExecutor executor = new PostJobsExecutor(jobs, project, projectDefinition, mock(MavenPluginExecutor.class)); SensorContext context = mock(SensorContext.class); - executor.execute(project, context); + executor.execute(context); verify(job1).executeOn(project, context); verify(job2).executeOn(project, context); diff --git a/sonar-batch/src/test/resources/org/sonar/batch/DefaultProjectFileSystem2Test/shouldIgnoreInexistingSourceDirs/fake.txt b/sonar-batch/src/test/resources/org/sonar/batch/DefaultProjectFileSystem2Test/shouldIgnoreInexistingSourceDirs/fake.txt new file mode 100644 index 00000000000..f0f877cedcc --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/DefaultProjectFileSystem2Test/shouldIgnoreInexistingSourceDirs/fake.txt @@ -0,0 +1 @@ +fake
\ No newline at end of file |