import org.junit.Test;
import org.sonar.api.Plugin;
-import org.sonar.api.SonarQubeVersion;
import org.sonar.api.utils.Version;
import org.sonar.xoo.lang.CpdTokenizerSensor;
@Test
public void provide_extensions_for_5_5() {
- Plugin.Context context = new Plugin.Context(new SonarQubeVersion(V5_5));
+ Plugin.Context context = new Plugin.Context(V5_5);
new XooPlugin().define(context);
assertThat(context.getExtensions()).hasSize(39).contains(CpdTokenizerSensor.class);
- context = new Plugin.Context(new SonarQubeVersion(Version.parse("5.4")));
+ context = new Plugin.Context(Version.parse("5.4"));
new XooPlugin().define(context);
assertThat(context.getExtensions()).hasSize(38).doesNotContain(CpdTokenizerSensor.class);
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import org.sonar.api.utils.Version;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
* Entry-point for plugins to inject extensions into SonarQube.
* <p>The JAR manifest must declare the name of the implementation class in the property <code>Plugin-Class</code>.
* This property is automatically set by sonar-packaging-maven-plugin when building plugin.</p>
- * <p>Example of implementation:
+ * <p>Example of implementation</p>
* <pre>
* package com.mycompany.sonarqube;
* public class MyPlugin implements Plugin {
- * {@literal @}Override
+ * {@literal @}Override
* public void define(Context context) {
* context.addExtensions(MySensor.class, MyRules.class);
* if (context.getSonarQubeVersion().isGreaterThanOrEqual(SonarQubeVersion.V5_6)) {
* }
* }
* </pre>
- * </p>
- * <p>Example of pom.xml:</p>
+ *
+ * <p>Example of pom.xml</p>
* <pre>
* <project>
* ...
* </project>
* </pre>
*
+ * <p>Example of test</p>
+ * <pre>
+ * MyPlugin underTest = new MyPlugin();
+ *
+ *{@literal @}Test
+ * public void test_plugin_extensions_compatible_with_5_5() {
+ * Plugin.Context context = new Plugin.Context(SonarQubeVersion.V5_5);
+ * underTest.define(context);
+ * assertThat(context.getExtensions()).hasSize(4);
+ * }
+ * </pre>
+ *
* @since 5.5
*/
@Beta
public interface Plugin {
class Context {
- private final SonarQubeVersion version;
+ private final Version version;
private final List extensions = new ArrayList();
- public Context(SonarQubeVersion version) {
+ public Context(Version version) {
this.version = version;
}
- public SonarQubeVersion getSonarQubeVersion() {
+ /**
+ * Runtime version of SonarQube
+ */
+ public Version getSonarQubeVersion() {
return version;
}
* Add an extension as :
* <ul>
* <li>a Class that is annotated with {@link org.sonar.api.batch.BatchSide}, {@link org.sonar.api.server.ServerSide}
- * or {@link org.sonar.api.server.ComputeEngineSide}.</li>
- * The extension will be instantiated once. Its dependencies are injected through constructor parameters.</li>
+ * or {@link org.sonar.api.ce.ComputeEngineSide}. The extension will be instantiated once. Its dependencies are
+ * injected through constructor parameters.</li>
* <li>an instance that is annotated with {@link org.sonar.api.batch.BatchSide}, {@link org.sonar.api.server.ServerSide}
- * or {@link org.sonar.api.server.ComputeEngineSide}.</li>
+ * or {@link org.sonar.api.ce.ComputeEngineSide}.</li>
* </ul>
* Only a single component can be registered for a class. It's not allowed for example to register:
* <ul>
import javax.annotation.Nullable;
import org.sonar.api.ExtensionProvider;
import org.sonar.api.Plugin;
-import org.sonar.api.SonarPlugin;
import org.sonar.api.SonarQubeVersion;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.core.platform.ComponentContainer;
// plugin extensions
for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) {
Plugin plugin = pluginRepository.getPluginInstance(pluginInfo.getKey());
- Plugin.Context context = new Plugin.Context(sonarQubeVersion);
+ Plugin.Context context = new Plugin.Context(sonarQubeVersion.get());
plugin.define(context);
for (Object extension : context.getExtensions()) {
doInstall(container, matcher, pluginInfo, extension);