aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2011-06-06 14:27:49 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2011-06-06 14:28:04 +0200
commitd346d2313443339ded7b86bca7765a7ee850a8bb (patch)
tree9ee28f020559465a003f952ed538d3a6ddd68802 /sonar-plugin-api
parent00d12c9382a8c4161455e2203b9b074b15278111 (diff)
downloadsonarqube-d346d2313443339ded7b86bca7765a7ee850a8bb.tar.gz
sonarqube-d346d2313443339ded7b86bca7765a7ee850a8bb.zip
Allow the extension ProjectBuilder to change the source directories of root module
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java49
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java8
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java39
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectReactorTest.java8
4 files changed, 68 insertions, 36 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java
index 1b062bc8131..cec0930c949 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java
@@ -31,7 +31,8 @@ import java.util.Properties;
/**
* Defines project metadata (key, name, source directories, ...). It's generally used by the
- * {@link org.sonar.api.batch.bootstrap.ProjectBuilder extension point}
+ * {@link org.sonar.api.batch.bootstrap.ProjectBuilder extension point} and must not be used
+ * by other standard extensions.
*
* @since 2.9
*/
@@ -48,17 +49,11 @@ public final class ProjectDefinition implements BatchComponent {
private File baseDir;
private File workDir;
- private Properties properties;
+ private Properties properties = new Properties();
private ProjectDefinition parent = null;
private List<ProjectDefinition> subProjects = Lists.newArrayList();
private List<Object> containerExtensions = Lists.newArrayList();
- public ProjectDefinition(File baseDir, File workDir, Properties properties) {
- this.baseDir = baseDir;
- this.workDir = workDir;
- this.properties = properties;
- }
-
private ProjectDefinition() {
}
@@ -66,6 +61,11 @@ public final class ProjectDefinition implements BatchComponent {
return new ProjectDefinition();
}
+ public ProjectDefinition setProperties(Properties p) {
+ this.properties = p;
+ return this;
+ }
+
public File getBaseDir() {
return baseDir;
}
@@ -151,24 +151,24 @@ public final class ProjectDefinition implements BatchComponent {
public ProjectDefinition addSourceDirs(File... dirs) {
for (File dir : dirs) {
- addSourceDirs(dir);
+ addSourceDirs(dir.getAbsolutePath());
}
return this;
}
- public ProjectDefinition setSourceDir(String path) {
- properties.setProperty(SOURCE_DIRS_PROPERTY, path);
- return this;
- }
-
public ProjectDefinition resetSourceDirs() {
properties.remove(SOURCE_DIRS_PROPERTY);
return this;
}
- public ProjectDefinition setSourceDir(File path) {
- setSourceDir(path.getAbsolutePath());
- return this;
+ public ProjectDefinition setSourceDirs(String... paths) {
+ resetSourceDirs();
+ return addSourceDirs(paths);
+ }
+
+ public ProjectDefinition setSourceDirs(File... dirs) {
+ resetSourceDirs();
+ return setSourceDirs(dirs);
}
/**
@@ -221,6 +221,16 @@ public final class ProjectDefinition implements BatchComponent {
return this;
}
+ public ProjectDefinition setTestDirs(String... paths) {
+ resetTestDirs();
+ return addTestDirs(paths);
+ }
+
+ public ProjectDefinition setTestDirs(File... dirs) {
+ resetTestDirs();
+ return setTestDirs(dirs);
+ }
+
public ProjectDefinition resetTestDirs() {
properties.remove(TEST_DIRS_PROPERTY);
return this;
@@ -269,6 +279,11 @@ public final class ProjectDefinition implements BatchComponent {
return this;
}
+ public ProjectDefinition addBinaryDir(File f) {
+ return addBinaryDir(f.getAbsolutePath());
+ }
+
+
public List<String> getLibraries() {
String sources = properties.getProperty(LIBRARIES_PROPERTY, "");
return Arrays.asList(StringUtils.split(sources, SEPARATOR));
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java
index 4a5d80b899f..af6ae6d93b8 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java
@@ -35,7 +35,7 @@ public class ProjectBuilderTest {
@Test
public void shouldChangeProject() {
// this reactor is created and injected by Sonar
- ProjectReactor projectReactor = new ProjectReactor(new ProjectDefinition(new File("."), new File("."), new Properties()));
+ ProjectReactor projectReactor = new ProjectReactor(ProjectDefinition.create());
ProjectBuilder builder = new ProjectBuilderSample(projectReactor, new PropertiesConfiguration());
builder.start();
@@ -65,11 +65,13 @@ public class ProjectBuilderTest {
// add sub-project
File baseDir = new File(root.getBaseDir(), "path/to/subproject");
- ProjectDefinition subProject = new ProjectDefinition(baseDir, new File(baseDir, "target/.sonar"), new Properties());
+ ProjectDefinition subProject = ProjectDefinition.create();
+ subProject.setBaseDir(baseDir);
+ subProject.setWorkDir(new File(baseDir, "target/.sonar"));
subProject.setKey("groupId:subProjectId");
subProject.setVersion(root.getVersion());
subProject.setName("Sub Project");
- subProject.setSourceDir("src");
+ subProject.setSourceDirs("src");
root.addSubProject(subProject);
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java
index ed13c6a719c..93b9bcdfefa 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java
@@ -19,6 +19,7 @@
*/
package org.sonar.api.batch.bootstrap;
+import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.sonar.api.CoreProperties;
@@ -29,19 +30,20 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
+import static org.junit.matchers.JUnitMatchers.hasItem;
public class ProjectDefinitionTest {
@Test
public void shouldSetKey() {
- ProjectDefinition def = new ProjectDefinition(new File("."), new File("."), new Properties());
+ ProjectDefinition def = ProjectDefinition.create();
def.setKey("mykey");
assertThat(def.getKey(), is("mykey"));
}
@Test
public void shouldSetOptionalFields() {
- ProjectDefinition def = new ProjectDefinition(new File("."), new File("."), new Properties());
+ ProjectDefinition def = ProjectDefinition.create();
def.setName("myname");
def.setDescription("desc");
assertThat(def.getName(), is("myname"));
@@ -50,7 +52,7 @@ public class ProjectDefinitionTest {
@Test
public void shouldSupportDefaultName() {
- ProjectDefinition def = new ProjectDefinition(new File("."), new File("."), new Properties());
+ ProjectDefinition def = ProjectDefinition.create();
def.setKey("myKey");
assertThat(def.getName(), is("Unnamed - myKey"));
}
@@ -58,13 +60,14 @@ public class ProjectDefinitionTest {
public void shouldGetKeyFromProperties() {
Properties props = new Properties();
props.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo");
- ProjectDefinition def = new ProjectDefinition(new File("."), new File("."), props);
+ ProjectDefinition def = ProjectDefinition.create();
+ def.setProperties(props);
assertThat(def.getKey(), is("foo"));
}
@Test
public void testDefaultValues() {
- ProjectDefinition def = new ProjectDefinition(new File("."), new File("."), new Properties());
+ ProjectDefinition def = ProjectDefinition.create();
assertThat(def.getSourceDirs().size(), is(0));
assertThat(def.getTestDirs().size(), is(0));
assertThat(def.getBinaries().size(), is(0));
@@ -72,8 +75,8 @@ public class ProjectDefinitionTest {
}
@Test
- public void shouldAddDirectories() {
- ProjectDefinition def = new ProjectDefinition(new File("."), new File("."), new Properties());
+ public void shouldAddDirectoriesAsPath() {
+ ProjectDefinition def = ProjectDefinition.create();
def.addSourceDirs("src/main/java", "src/main/java2");
def.addTestDirs("src/test/java");
def.addTestDirs("src/test/java2");
@@ -89,8 +92,20 @@ public class ProjectDefinitionTest {
}
@Test
+ public void shouldAddDirectories() {
+ ProjectDefinition def = ProjectDefinition.create();
+ def.addSourceDirs(new File("src/main/java"), new File("src/main/java2"));
+ def.addTestDirs(new File("src/test/java"), new File("src/test/java2"));
+ def.addBinaryDir(new File("target/classes"));
+
+ assertThat(def.getSourceDirs().size(), is(2));
+ assertThat(def.getTestDirs().size(), CoreMatchers.is(2));
+ assertThat(def.getBinaries().size(), CoreMatchers.is(1));
+ }
+
+ @Test
public void shouldAddFiles() {
- ProjectDefinition def = new ProjectDefinition(new File("."), new File("."), new Properties());
+ ProjectDefinition def = ProjectDefinition.create();
def.addSourceFiles("src/main/java/foo/Bar.java", "src/main/java/hello/World.java");
def.addTestFiles("src/test/java/foo/BarTest.java", "src/test/java/hello/WorldTest.java");
@@ -101,8 +116,8 @@ public class ProjectDefinitionTest {
@Test
public void shouldManageRelationships() {
- ProjectDefinition root = new ProjectDefinition(new File("."), new File("."), new Properties());
- ProjectDefinition child = new ProjectDefinition(new File("."), new File("."), new Properties());
+ ProjectDefinition root = ProjectDefinition.create();
+ ProjectDefinition child = ProjectDefinition.create();
root.addSubProject(child);
assertThat(root.getSubProjects().size(), is(1));
@@ -114,7 +129,7 @@ public class ProjectDefinitionTest {
@Test
public void shouldResetSourceDirs() {
- ProjectDefinition root = new ProjectDefinition(new File("."), new File("."), new Properties());
+ ProjectDefinition root = ProjectDefinition.create();
root.addSourceDirs("src", "src2/main");
assertThat(root.getSourceDirs().size(), is(2));
@@ -124,7 +139,7 @@ public class ProjectDefinitionTest {
@Test
public void shouldResetTestDirs() {
- ProjectDefinition root = new ProjectDefinition(new File("."), new File("."), new Properties());
+ ProjectDefinition root = ProjectDefinition.create();
root.addTestDirs("src", "src2/test");
assertThat(root.getTestDirs().size(), is(2));
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectReactorTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectReactorTest.java
index 4385cb1455a..59035cd4dd4 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectReactorTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectReactorTest.java
@@ -31,8 +31,8 @@ public class ProjectReactorTest {
@Test
public void shouldSupportMultipleProjects() {
- ProjectDefinition root = new ProjectDefinition(new File("."), new File("."), new Properties());
- ProjectDefinition child = new ProjectDefinition(new File("."), new File("."), new Properties());
+ ProjectDefinition root = ProjectDefinition.create();
+ ProjectDefinition child = ProjectDefinition.create();
root.addSubProject(child);
ProjectReactor reactor = new ProjectReactor(root);
@@ -42,8 +42,8 @@ public class ProjectReactorTest {
@Test(expected = IllegalArgumentException.class)
public void shouldFailIfNotRoot() {
- ProjectDefinition root = new ProjectDefinition(new File("."), new File("."), new Properties());
- ProjectDefinition child = new ProjectDefinition(new File("."), new File("."), new Properties());
+ ProjectDefinition root = ProjectDefinition.create();
+ ProjectDefinition child = ProjectDefinition.create();
root.addSubProject(child);
new ProjectReactor(child);