From bbfce797275cf13e8c7a0e43aa29406175c89bfc Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Mon, 7 Feb 2011 13:20:26 +0300 Subject: SONAR-2172: New extension point - Initializer * Rename AbstractInitializer to Initializer * Execute Initializers before Sensors --- .../org/sonar/api/batch/AbstractInitializer.java | 46 -------------------- .../main/java/org/sonar/api/batch/Initializer.java | 50 ++++++++++++++++++++++ .../src/main/java/org/sonar/api/batch/Sensor.java | 43 ++++++++++--------- .../api/batch/maven/DependsUponMavenPlugin.java | 8 +++- .../java/org/sonar/api/batch/InitializerTest.java | 43 +++++++++++++++++++ 5 files changed, 123 insertions(+), 67 deletions(-) delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractInitializer.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/batch/Initializer.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/batch/InitializerTest.java (limited to 'sonar-plugin-api') diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractInitializer.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractInitializer.java deleted file mode 100644 index 9c34f2869c9..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractInitializer.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.api.batch; - -import org.sonar.api.batch.Phase.Name; -import org.sonar.api.resources.Project; - -/** - * @since 2.6 - */ -@Phase(name = Name.PRE) -public abstract class AbstractInitializer implements Sensor { - - public boolean shouldExecuteOnProject(Project project) { - return true; - } - - public void analyse(Project project, SensorContext context) { - prepare(project); - } - - public abstract void prepare(Project project); - - @Override - public String toString() { - return getClass().getSimpleName(); - } - -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/Initializer.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/Initializer.java new file mode 100644 index 00000000000..fa52021df9f --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/Initializer.java @@ -0,0 +1,50 @@ +/* + * 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.api.batch; + +import org.sonar.api.BatchExtension; +import org.sonar.api.resources.Project; + +/** + *

+ * Initializer can execute external tool (like a Maven plugin), change project configuration. For example CoberturaMavenInitializer invokes + * the Codehaus Cobertura Mojo and sets path to Cobertura report according to Maven POM. + *

+ * + *

+ * Initializers are executed first and once during project analysis. + *

+ * + * @since 2.6 + */ +public abstract class Initializer implements BatchExtension, CheckProject { + + public boolean shouldExecuteOnProject(Project project) { + return true; + } + + public abstract void execute(Project project); + + @Override + public String toString() { + return getClass().getSimpleName(); + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/Sensor.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/Sensor.java index ee50d58e49e..e8d9791d2cb 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/Sensor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/Sensor.java @@ -23,38 +23,41 @@ import org.sonar.api.BatchExtension; import org.sonar.api.resources.Project; /** - *

A Sensor is invoked once during the analysis of a project. The sensor can invoke a maven plugin, - * parse a flat file, connect to a web server... For example the Cobertura Sensor invokes the Codehaus Cobertura MOJO. - * Then the generated XML file is parsed and used to save the first-level of measures on resources - * (project, package or class).

- * - *

Sensors are executed first during project analysis. Sensor are generally used to add measure at the - * lowest level of the resource tree. A sensor can access and save measures on the whole tree of resources.

- * - *

A particular attention should be given to resource exclusion. Sonar already manages exclusions at file level : if - * you try to save a measure on a resource that is excluded in the settings, then Sonar will not save the measure. - * When handling a plugin or an external tool, you should make sure that exclusions are passed if you are going to get - * back consolidated data.

- * + *

+ * A Sensor is invoked once during the analysis of a project. The sensor can parse a flat file, connect to a web server... Sensor are + * generally used to add measure at the lowest level of the resource tree. A sensor can access and save measures on the whole tree of + * resources. + *

+ * + *

+ * For example the Cobertura Sensor parses Cobertura report and saves the first-level of measures on resources. + *

+ * + *

+ * A particular attention should be given to resource exclusion. Sonar already manages exclusions at file level : if you try to save a + * measure on a resource that is excluded in the settings, then Sonar will not save the measure. When handling a plugin or an external tool, + * you should make sure that exclusions are passed if you are going to get back consolidated data. + *

+ * * @since 1.10 */ public interface Sensor extends BatchExtension, CheckProject { /** * Sensors that depend upon Squid must declare the following method : - * - * - * @DependsUpon public String dependsUponSquidAnalysis() { - * return Sensor.FLAG_SQUID_ANALYSIS; - * } - * + * + *
+   * @DependsUpon
+   * public String dependsUponSquidAnalysis() {
+   *   return Sensor.FLAG_SQUID_ANALYSIS;
    * }
+   * 
*/ String FLAG_SQUID_ANALYSIS = "squid"; /** * The method that is going to be run when the sensor is called - * + * * @param project the project the sensor runs on * @param context the context */ diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/DependsUponMavenPlugin.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/DependsUponMavenPlugin.java index 11ea8d00f75..f58e5f7dc30 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/DependsUponMavenPlugin.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/DependsUponMavenPlugin.java @@ -24,7 +24,13 @@ import org.sonar.api.batch.SupportedEnvironment; import org.sonar.api.resources.Project; /** - * Used for Sensors and PostJobs only. + * Can be used only for {@link org.sonar.api.batch.Initializer Initializers}, {@link org.sonar.api.batch.Sensor Sensors} and + * {@link org.sonar.api.batch.PostJob PostJobs}. + * + *

+ * If extension implements this interface, then it would be available only when Sonar executed from Maven. So we recommend to use this + * interface for Initializers instead of Sensors. + *

* * @since 1.10 */ diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/InitializerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/InitializerTest.java new file mode 100644 index 00000000000..7dd85e996dc --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/InitializerTest.java @@ -0,0 +1,43 @@ +/* + * 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.api.batch; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + +import org.junit.Test; +import org.sonar.api.resources.Project; + +public class InitializerTest { + + @Test + public void shouldBeExecutedByDefault() { + Project project = mock(Project.class); + assertThat(new FakeInitializer().shouldExecuteOnProject(project), is(true)); + } + + private class FakeInitializer extends Initializer { + @Override + public void execute(Project project) { + } + } + +} -- cgit v1.2.3