aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2018-02-05 14:48:50 +0100
committerJanos Gyerik <janos.gyerik@sonarsource.com>2018-02-06 09:13:51 +0100
commit3c42d5d2e6b362d389c0058e069897bf26ef65f7 (patch)
tree567be18ebbb7c081e6f06ebe5ad65fd25048bd41 /sonar-plugin-api
parentea77a805bfccb66c5943eb0bcaf36df3cb905227 (diff)
downloadsonarqube-3c42d5d2e6b362d389c0058e069897bf26ef65f7.tar.gz
sonarqube-3c42d5d2e6b362d389c0058e069897bf26ef65f7.zip
Add Java API Plugin.Context#getBootConfiguration()
That allows plugins to check configuration when providing the list of extensions.
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java30
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/internal/PluginContextImpl.java89
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java10
3 files changed, 124 insertions, 5 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java b/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java
index 9dd31141f1e..b52e628f66d 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java
@@ -22,6 +22,7 @@ package org.sonar.api;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import org.sonar.api.config.Configuration;
import org.sonar.api.utils.Version;
import static java.util.Arrays.asList;
@@ -67,18 +68,22 @@ import static java.util.Objects.requireNonNull;
* &lt;/project&gt;
* </pre>
*
- * <p>Example of test
+ * <p>Example of Test
* <pre>
*{@literal @}Test
* public void test_plugin_extensions_compatible_with_5_6() {
* SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(5, 6), SonarQubeSide.SCANNER);
- * Plugin.Context context = new Plugin.Context(runtime);
- * new MyPlugin().define(context);
+ * Plugin.Context context = new PluginContextImpl.Builder().setSonarRuntime(runtime).build();
+ * MyPlugin underTest = new MyPlugin();
+ *
+ * underTest.define(context);
+ *
* assertThat(context.getExtensions()).hasSize(4);
* }
* </pre>
*
* @since 5.5
+ * @see org.sonar.api.internal.PluginContextImpl for unit tests
*/
public interface Plugin {
@@ -86,8 +91,13 @@ public interface Plugin {
private final SonarRuntime sonarRuntime;
private final List extensions = new ArrayList();
+ /**
+ * For unit tests only. It's recommended to use {@link org.sonar.api.internal.PluginContextImpl.Builder}
+ * to create instances of {@link Plugin.Context}.
+ * The configuration returned by {@see #getBootConfiguration()} is empty.
+ */
public Context(SonarRuntime sonarRuntime) {
- this.sonarRuntime = sonarRuntime;
+ this.sonarRuntime = requireNonNull(sonarRuntime, "sonarRuntime is null");
}
/**
@@ -152,6 +162,18 @@ public interface Plugin {
return extensions;
}
+ /**
+ * The configuration that contains only the few properties required to bootstrap the process, for example:
+ * - conf/sonar.properties and persisted properties on web server and Compute Engine sides. The default values
+ * defined by plugins are ignored.
+ * - command-line arguments on scanner side. Default values or properties persisted in server are ignored.
+ *
+ * @since 7.1
+ */
+ public Configuration getBootConfiguration() {
+ throw new UnsupportedOperationException("Unit tests should create Plugin.Context with org.sonar.api.internal.PluginContextImpl#Builder");
+ }
+
}
/**
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/internal/PluginContextImpl.java b/sonar-plugin-api/src/main/java/org/sonar/api/internal/PluginContextImpl.java
new file mode 100644
index 00000000000..b82c4d8e74c
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/internal/PluginContextImpl.java
@@ -0,0 +1,89 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.internal;
+
+import org.sonar.api.Plugin;
+import org.sonar.api.SonarRuntime;
+import org.sonar.api.config.Configuration;
+import org.sonar.api.config.internal.MapSettings;
+
+/**
+ * Implementation of {@link Plugin.Context} that plugins could use in their unit tests.
+ *
+ * Example:
+ *
+ * <pre>
+ * import org.sonar.api.internal.SonarRuntimeImpl;
+ * import org.sonar.api.config.internal.MapSettings;
+ *
+ * ...
+ *
+ * SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(7, 1), SonarQubeSide.SCANNER);
+ * MapSettings settings = new MapSettings().setProperty("foo", "bar");
+ * Plugin.Context context = new PluginContextImpl.Builder()
+ * .setSonarRuntime(runtime)
+ * .setBootConfiguration(settings.asConfig());
+ * .build();
+ * </pre>
+ *
+ * @since 7.1
+ */
+public class PluginContextImpl extends Plugin.Context {
+
+ private final Configuration bootConfiguration;
+
+ private PluginContextImpl(Builder builder) {
+ super(builder.sonarRuntime);
+ this.bootConfiguration = builder.bootConfiguration != null ? builder.bootConfiguration : new MapSettings().asConfig();
+ }
+
+ @Override
+ public Configuration getBootConfiguration() {
+ return bootConfiguration;
+ }
+
+ public static class Builder {
+ private SonarRuntime sonarRuntime;
+ private Configuration bootConfiguration;
+
+ /**
+ * Required.
+ * @see SonarRuntimeImpl
+ * @return this
+ */
+ public Builder setSonarRuntime(SonarRuntime r) {
+ this.sonarRuntime = r;
+ return this;
+ }
+
+ /**
+ * If not set, then an empty configuration is used.
+ * @return this
+ */
+ public Builder setBootConfiguration(Configuration c) {
+ this.bootConfiguration = c;
+ return this;
+ }
+
+ public Plugin.Context build() {
+ return new PluginContextImpl(this);
+ }
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java
index 46bf7131b52..f8b280c40e2 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java
@@ -21,6 +21,8 @@ package org.sonar.api;
import java.util.Arrays;
import org.junit.Test;
+import org.sonar.api.config.internal.MapSettings;
+import org.sonar.api.internal.PluginContextImpl;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.utils.Version;
@@ -33,7 +35,11 @@ public class PluginTest {
@Test
public void test_context() {
SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(VERSION_5_6, SonarQubeSide.SERVER);
- Plugin.Context context = new Plugin.Context(runtime);
+ MapSettings settings = new MapSettings().setProperty("foo", "bar");
+ Plugin.Context context = new PluginContextImpl.Builder()
+ .setSonarRuntime(runtime)
+ .setBootConfiguration(settings.asConfig())
+ .build();
assertThat(context.getSonarQubeVersion()).isEqualTo(VERSION_5_6);
assertThat(context.getExtensions()).isEmpty();
@@ -46,5 +52,7 @@ public class PluginTest {
context.addExtensions("one", "two", "three", "four");
assertThat(context.getExtensions()).containsOnly("foo", "bar", "baz", "one", "two", "three", "four");
+
+ assertThat(context.getBootConfiguration().get("foo")).hasValue("bar");
}
}