*/
package org.sonar.xoo;
-import java.util.Arrays;
-import java.util.List;
-import org.sonar.api.SonarPlugin;
+import org.sonar.api.Plugin;
import org.sonar.xoo.coverage.ItCoverageSensor;
import org.sonar.xoo.coverage.OverallCoverageSensor;
import org.sonar.xoo.coverage.UtCoverageSensor;
import org.sonar.xoo.test.CoveragePerTestSensor;
import org.sonar.xoo.test.TestExecutionSensor;
+import static org.sonar.api.SonarQubeVersion.V5_5;
+
/**
* Plugin entry-point, as declared in pom.xml.
*/
-public class XooPlugin extends SonarPlugin {
+public class XooPlugin implements Plugin {
- /**
- * Declares all the extensions implemented in the plugin
- */
@Override
- public List getExtensions() {
- return Arrays.asList(
+ public void define(Context context) {
+ context.addExtensions(
Xoo.class,
Xoo2.class,
XooRulesDefinition.class,
ChecksSensor.class,
RandomAccessSensor.class,
DeprecatedResourceApiSensor.class,
- CpdTokenizerSensor.class,
OneBlockerIssuePerFileSensor.class,
OneIssuePerLineSensor.class,
// Other
XooProjectBuilder.class,
XooPostJob.class);
+
+ if (context.getSonarQubeVersion().isGreaterThanOrEqual(V5_5)) {
+ context.addExtension(CpdTokenizerSensor.class);
+ }
}
}
*/
package org.sonar.xoo.rule;
-import org.sonar.xoo.Xoo2;
-
+import org.sonar.api.SonarQubeVersion;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.issue.NewIssue;
import org.sonar.api.rule.RuleKey;
import org.sonar.xoo.Xoo;
+import org.sonar.xoo.Xoo2;
public class OneIssuePerLineSensor implements Sensor {
public static final String RULE_KEY = "OneIssuePerLine";
- private static final String EFFORT_TO_FIX_PROPERTY = "sonar.oneIssuePerLine.effortToFix";
+ public static final String EFFORT_TO_FIX_PROPERTY = "sonar.oneIssuePerLine.effortToFix";
public static final String FORCE_SEVERITY_PROPERTY = "sonar.oneIssuePerLine.forceSeverity";
@Override
.on(file)
.at(file.selectLine(line))
.message("This issue is generated on each line"))
- .effortToFix(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY))
- .overrideSeverity(severity != null ? Severity.valueOf(severity) : null)
- .save();
+ .overrideSeverity(severity != null ? Severity.valueOf(severity) : null);
+ if (context.getSonarQubeVersion().isGreaterThanOrEqual(SonarQubeVersion.V5_5)) {
+ newIssue.gap(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY));
+ } else {
+ newIssue.effortToFix(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY));
+ }
+ newIssue.save();
}
}
package org.sonar.xoo;
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;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.api.SonarQubeVersion.V5_5;
public class XooPluginTest {
@Test
- public void provide_extensions() {
- assertThat(new XooPlugin().getExtensions().size()).isGreaterThan(0);
+ public void provide_extensions_for_5_5() {
+ Plugin.Context context = new Plugin.Context(new SonarQubeVersion(V5_5));
+ new XooPlugin().define(context);
+ assertThat(context.getExtensions()).hasSize(39).contains(CpdTokenizerSensor.class);
+
+ context = new Plugin.Context(new SonarQubeVersion(Version.parse("5.4")));
+ new XooPlugin().define(context);
+ assertThat(context.getExtensions()).hasSize(38).doesNotContain(CpdTokenizerSensor.class);
}
}
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.mockito.ArgumentCaptor;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.FileMetadata;
import org.sonar.api.batch.rule.Severity;
-import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
-import org.sonar.api.batch.sensor.internal.SensorStorage;
+import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.Issue;
-import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
-import org.sonar.api.config.Settings;
+import org.sonar.api.utils.Version;
import org.sonar.xoo.Xoo;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
public class OneIssuePerLineSensorTest {
@Test
public void testRule() throws IOException {
- DefaultFileSystem fs = new DefaultFileSystem(temp.newFolder().toPath());
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
.initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n")));
- fs.add(inputFile);
-
- SensorContext context = mock(SensorContext.class);
- final SensorStorage sensorStorage = mock(SensorStorage.class);
- when(context.settings()).thenReturn(new Settings());
- when(context.fileSystem()).thenReturn(fs);
- when(context.newIssue()).thenAnswer(new Answer<Issue>() {
- @Override
- public Issue answer(InvocationOnMock invocation) throws Throwable {
- return new DefaultIssue(sensorStorage);
- }
- });
+
+ SensorContextTester context = SensorContextTester.create(temp.newFolder());
+ context.fileSystem().add(inputFile);
sensor.execute(context);
- ArgumentCaptor<DefaultIssue> argCaptor = ArgumentCaptor.forClass(DefaultIssue.class);
- verify(sensorStorage, times(10)).store(argCaptor.capture());
- assertThat(argCaptor.getAllValues()).hasSize(10); // One issue per line
- assertThat(argCaptor.getValue().overriddenSeverity()).isNull();
+ assertThat(context.allIssues()).hasSize(10); // One issue per line
+ for (Issue issue : context.allIssues()) {
+ assertThat(issue.gap()).isNull();
+ }
}
@Test
public void testForceSeverity() throws IOException {
- DefaultFileSystem fs = new DefaultFileSystem(temp.newFolder().toPath());
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
.initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n")));
- fs.add(inputFile);
-
- SensorContext context = mock(SensorContext.class);
- final SensorStorage sensorStorage = mock(SensorStorage.class);
- Settings settings = new Settings();
- settings.setProperty(OneIssuePerLineSensor.FORCE_SEVERITY_PROPERTY, "MINOR");
- when(context.settings()).thenReturn(settings);
- when(context.fileSystem()).thenReturn(fs);
- when(context.newIssue()).thenAnswer(new Answer<Issue>() {
- @Override
- public Issue answer(InvocationOnMock invocation) throws Throwable {
- return new DefaultIssue(sensorStorage);
- }
- });
+
+ SensorContextTester context = SensorContextTester.create(temp.newFolder());
+ context.fileSystem().add(inputFile);
+ context.settings().setProperty(OneIssuePerLineSensor.FORCE_SEVERITY_PROPERTY, "MINOR");
+
+ sensor.execute(context);
+
+ assertThat(context.allIssues()).hasSize(10); // One issue per line
+ for (Issue issue : context.allIssues()) {
+ assertThat(issue.overriddenSeverity()).isEqualTo(Severity.MINOR);
+ }
+ }
+
+ @Test
+ public void testProvideGap() throws IOException {
+ DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
+ .initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n")));
+
+ SensorContextTester context = SensorContextTester.create(temp.newFolder());
+ context.fileSystem().add(inputFile);
+ context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2");
+
+ sensor.execute(context);
+
+ assertThat(context.allIssues()).hasSize(10); // One issue per line
+ for (Issue issue : context.allIssues()) {
+ assertThat(issue.gap()).isEqualTo(1.2d);
+ }
+ }
+
+ @Test
+ public void testProvideGap_before_5_5() throws IOException {
+ DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
+ .initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n")));
+
+ SensorContextTester context = SensorContextTester.create(temp.newFolder());
+ context.fileSystem().add(inputFile);
+ context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2");
+ context.setSonarQubeVersion(Version.parse("5.4"));
+
sensor.execute(context);
- ArgumentCaptor<DefaultIssue> argCaptor = ArgumentCaptor.forClass(DefaultIssue.class);
- verify(sensorStorage, times(10)).store(argCaptor.capture());
- assertThat(argCaptor.getAllValues()).hasSize(10); // One issue per line
- assertThat(argCaptor.getValue().overriddenSeverity()).isEqualTo(Severity.MINOR);
+ assertThat(context.allIssues()).hasSize(10); // One issue per line
+ for (Issue issue : context.allIssues()) {
+ assertThat(issue.gap()).isEqualTo(1.2d);
+ }
}
}
import java.util.List;
import javax.annotation.CheckForNull;
import org.sonar.api.config.EmailSettings;
+import org.sonar.api.internal.SonarQubeVersionFactory;
import org.sonar.api.profiles.AnnotationProfileParser;
import org.sonar.api.profiles.XMLProfileParser;
import org.sonar.api.profiles.XMLProfileSerializer;
import org.sonar.core.platform.Module;
import org.sonar.core.platform.PluginClassloaderFactory;
import org.sonar.core.platform.PluginLoader;
-import org.sonar.core.platform.SonarQubeVersionProvider;
import org.sonar.core.timemachine.Periods;
import org.sonar.core.user.DefaultUserFinder;
import org.sonar.core.user.DeprecatedUserFinder;
public class ComputeEngineContainerImpl implements ComputeEngineContainer {
private static final Object[] LEVEL_1_COMPONENTS = new Object[] {
ComputeEngineSettings.class,
- new SonarQubeVersionProvider(),
+ SonarQubeVersionFactory.create(System2.INSTANCE),
new JmxConnectionFactoryProvider(),
ServerImpl.class,
UuidFactoryImpl.INSTANCE,
import java.util.Properties;
import javax.annotation.Nullable;
+import org.sonar.api.internal.SonarQubeVersionFactory;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.internal.TempFolderCleaner;
+import org.sonar.ce.property.CePropertyDefinitions;
import org.sonar.core.config.CorePropertyDefinitions;
-import org.sonar.core.platform.SonarQubeVersionProvider;
import org.sonar.core.util.UuidFactoryImpl;
import org.sonar.db.DaoModule;
import org.sonar.db.DatabaseChecker;
import org.sonar.db.version.DatabaseVersion;
import org.sonar.db.version.MigrationStepModule;
import org.sonar.server.app.ProcessCommandWrapperImpl;
-import org.sonar.ce.property.CePropertyDefinitions;
import org.sonar.server.db.EmbeddedDatabaseFactory;
import org.sonar.server.issue.index.IssueIndex;
import org.sonar.server.platform.DatabaseServerCompatibility;
import org.sonar.server.platform.DefaultServerFileSystem;
import org.sonar.server.platform.Platform;
import org.sonar.server.platform.ServerImpl;
-import org.sonar.server.platform.WebServerSettings;
import org.sonar.server.platform.TempFolderProvider;
+import org.sonar.server.platform.WebServerSettings;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.ruby.PlatformRackBridge;
import org.sonar.server.rule.index.RuleIndex;
add(platform, properties);
addExtraRootComponents();
add(
- new SonarQubeVersionProvider(),
+ SonarQubeVersionFactory.create(System2.INSTANCE),
ProcessCommandWrapperImpl.class,
WebServerSettings.class,
ServerImpl.class,
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.core.platform;
-
-import com.google.common.io.Resources;
-import java.io.IOException;
-import java.net.URL;
-import java.nio.charset.StandardCharsets;
-import org.picocontainer.injectors.ProviderAdapter;
-import org.sonar.api.SonarQubeVersion;
-import org.sonar.api.utils.System2;
-import org.sonar.api.utils.Version;
-
-public class SonarQubeVersionProvider extends ProviderAdapter {
-
- private static final String FILE_PATH = "/sq-version.txt";
-
- private SonarQubeVersion version = null;
-
- public SonarQubeVersion provide(System2 system) {
- if (version == null) {
- try {
- URL url = system.getResource(FILE_PATH);
- String versionInFile = Resources.toString(url, StandardCharsets.UTF_8);
- version = new SonarQubeVersion(Version.parse(versionInFile));
- } catch (IOException e) {
- throw new IllegalStateException("Can not load " + FILE_PATH + " from classpath", e);
- }
- }
- return version;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.core.platform;
-
-import java.io.File;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.SonarQubeVersion;
-import org.sonar.api.utils.System2;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class SonarQubeVersionProviderTest {
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- SonarQubeVersionProvider underTest = new SonarQubeVersionProvider();
-
- @Test
- public void create() {
- SonarQubeVersion version = underTest.provide(System2.INSTANCE);
- assertThat(version).isNotNull();
- assertThat(version.get().major()).isGreaterThanOrEqualTo(5);
- }
-
- @Test
- public void cache_version() {
- SonarQubeVersion version1 = underTest.provide(System2.INSTANCE);
- SonarQubeVersion version2 = underTest.provide(System2.INSTANCE);
- assertThat(version1).isSameAs(version2);
- }
-
- @Test
- public void throw_ISE_if_fail_to_load_version() throws Exception {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Can not load /sq-version.txt from classpath");
-
- System2 system = mock(System2.class);
- when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURL());
- underTest.provide(system);
- }
-}
import javax.annotation.concurrent.Immutable;
import org.sonar.api.batch.BatchSide;
+import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.Version;
* Version of SonarQube at runtime. This component can be injected as a dependency
* of plugin extensions. The main usage for a plugin is to benefit from new APIs
* while keeping backward-compatibility with previous versions of SonarQube.
-
- * Example: a plugin needs to use an API introduced in version 5.6 ({@code AnApi} in the following
+ * <br><br>
+ *
+ * Example 1: a {@link Sensor} wants to use an API introduced in version 5.5 and still requires to support older versions
+ * at runtime.
+ * <pre>
+ * public class MySensor implements Sensor {
+ *
+ * public void execute(SensorContext context) {
+ * if (context.getSonarQubeVersion().isGreaterThanOrEqual(SonarQubeVersion.V5_5)) {
+ * context.newMethodIntroducedIn5_5();
+ * }
+ * }
+ * }
+ * </pre>
+ *
+ * Example 2: a plugin needs to use an API introduced in version 5.6 ({@code AnApi} in the following
* snippet) and still requires to support version 5.5 at runtime.
- * <p></p>
+ * <br>
* <pre>
* // Component provided by sonar-plugin-api
* // @since 5.5
* // @since 5.6
* public void bar();
* }
- *
+ *
* // Component provided by plugin
* public class MyExtension {
* private final SonarQubeVersion sonarQubeVersion;
}
public boolean isGreaterThanOrEqual(Version than) {
- return this.version.compareTo(than) >= 0;
+ return this.version.isGreaterThanOrEqual(than);
}
+
}
import org.sonar.api.batch.sensor.measure.Measure;
import org.sonar.api.batch.sensor.measure.NewMeasure;
import org.sonar.api.config.Settings;
+import org.sonar.api.utils.Version;
/**
* See {@link Sensor#execute(SensorContext)}
*/
InputModule module();
+ /**
+ * @since 5.5
+ */
+ Version getSonarQubeVersion();
+
// ----------- MEASURES --------------
/**
import com.google.common.collect.Table;
import java.io.File;
import java.io.Serializable;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
+import org.sonar.api.SonarQubeVersion;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.sensor.measure.NewMeasure;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.config.Settings;
+import org.sonar.api.internal.SonarQubeVersionFactory;
import org.sonar.api.measures.Metric;
+import org.sonar.api.utils.System2;
+import org.sonar.api.utils.Version;
import org.sonar.duplications.internal.pmd.TokensLine;
/**
private ActiveRules activeRules;
private InMemorySensorStorage sensorStorage;
private InputModule module;
+ private SonarQubeVersion sqVersion;
- private SensorContextTester(File moduleBaseDir) {
+ private SensorContextTester(Path moduleBaseDir) {
this.settings = new Settings();
this.fs = new DefaultFileSystem(moduleBaseDir);
this.activeRules = new ActiveRulesBuilder().build();
this.sensorStorage = new InMemorySensorStorage();
this.module = new DefaultInputModule("projectKey");
+ this.sqVersion = SonarQubeVersionFactory.create(System2.INSTANCE);
}
public static SensorContextTester create(File moduleBaseDir) {
+ return new SensorContextTester(moduleBaseDir.toPath());
+ }
+
+ public static SensorContextTester create(Path moduleBaseDir) {
return new SensorContextTester(moduleBaseDir);
}
return settings;
}
- public void setSettings(Settings settings) {
+ public SensorContextTester setSettings(Settings settings) {
this.settings = settings;
+ return this;
}
@Override
return fs;
}
- public void setFileSystem(DefaultFileSystem fs) {
+ public SensorContextTester setFileSystem(DefaultFileSystem fs) {
this.fs = fs;
+ return this;
}
@Override
return activeRules;
}
- public void setActiveRules(ActiveRules activeRules) {
+ public SensorContextTester setActiveRules(ActiveRules activeRules) {
this.activeRules = activeRules;
+ return this;
+ }
+
+ /**
+ * Default value is the version of this API. You can override it
+ * using {@link #setSonarQubeVersion(Version)} to test your Sensor behavior.
+ * @since 5.5
+ */
+ @Override
+ public Version getSonarQubeVersion() {
+ return sqVersion.get();
+ }
+
+ /**
+ * @since 5.5
+ */
+ public SensorContextTester setSonarQubeVersion(Version version) {
+ this.sqVersion = new SonarQubeVersion(version);
+ return this;
}
@Override
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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 com.google.common.io.Resources;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import org.sonar.api.SonarQubeVersion;
+import org.sonar.api.utils.System2;
+import org.sonar.api.utils.Version;
+
+/**
+ * For internal use only.
+ */
+public class SonarQubeVersionFactory {
+
+ private static final String FILE_PATH = "/sq-version.txt";
+
+ private SonarQubeVersionFactory() {
+ // prevents instantiation
+ }
+
+ public static SonarQubeVersion create(System2 system) {
+ try {
+ URL url = system.getResource(FILE_PATH);
+ String versionInFile = Resources.toString(url, StandardCharsets.UTF_8);
+ return new SonarQubeVersion(Version.parse(versionInFile));
+ } catch (IOException e) {
+ throw new IllegalStateException("Can not load " + FILE_PATH + " from classpath", e);
+ }
+ }
+}
return parseInt(sequence);
}
+ public boolean isGreaterThanOrEqual(Version than) {
+ return this.compareTo(than) >= 0;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
*/
package org.sonar.api;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.utils.Version;
import static org.assertj.core.api.Assertions.assertThat;
public class SonarQubeVersionTest {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
@Test
public void isGte() {
Version version = Version.parse("1.2.3");
assertThat(qubeVersion.isGreaterThanOrEqual(Version.parse("1.1"))).isTrue();
assertThat(qubeVersion.isGreaterThanOrEqual(Version.parse("1.3"))).isFalse();
}
+
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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 java.io.File;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.SonarQubeVersion;
+import org.sonar.api.utils.System2;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+public class SonarQubeVersionFactoryTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Test
+ public void create() {
+ SonarQubeVersion version = SonarQubeVersionFactory.create(System2.INSTANCE);
+ assertThat(version).isNotNull();
+ assertThat(version.get().major()).isGreaterThanOrEqualTo(5);
+ }
+
+ @Test
+ public void throw_ISE_if_fail_to_load_version() throws Exception {
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Can not load /sq-version.txt from classpath");
+
+ System2 system = spy(System2.class);
+ when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL());
+ SonarQubeVersionFactory.create(system);
+ }
+
+}
import java.util.List;
import java.util.Map;
import org.sonar.api.Plugin;
+import org.sonar.api.internal.SonarQubeVersionFactory;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.UriReader;
import org.sonar.batch.cache.GlobalPersistentCacheProvider;
import org.sonar.core.platform.PluginInfo;
import org.sonar.core.platform.PluginLoader;
import org.sonar.core.platform.PluginRepository;
-import org.sonar.core.platform.SonarQubeVersionProvider;
import org.sonar.core.util.DefaultHttpDownloader;
import org.sonar.core.util.UuidFactoryImpl;
BatchPluginPredicate.class,
ExtensionInstaller.class,
- new SonarQubeVersionProvider(),
+ SonarQubeVersionFactory.create(System2.INSTANCE),
CachesManager.class,
GlobalSettings.class,
new BatchWsClientProvider(),
import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.sonar.api.SonarQubeVersion;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.SonarIndex;
private final CoverageExclusions coverageFilter;
public DeprecatedSensorContext(InputModule module, SonarIndex index, Project project, Settings settings, FileSystem fs, ActiveRules activeRules,
- AnalysisMode analysisMode, CoverageExclusions coverageFilter,
- SensorStorage sensorStorage) {
- super(module, settings, fs, activeRules, analysisMode, sensorStorage);
+ AnalysisMode analysisMode, CoverageExclusions coverageFilter, SensorStorage sensorStorage, SonarQubeVersion sqVersion) {
+ super(module, settings, fs, activeRules, analysisMode, sensorStorage, sqVersion);
this.index = index;
this.project = project;
this.coverageFilter = coverageFilter;
package org.sonar.batch.sensor;
import java.io.Serializable;
+import org.sonar.api.SonarQubeVersion;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.batch.sensor.measure.NewMeasure;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.config.Settings;
+import org.sonar.api.utils.Version;
import org.sonar.batch.sensor.noop.NoOpNewCpdTokens;
import org.sonar.batch.sensor.noop.NoOpNewHighlighting;
private final SensorStorage sensorStorage;
private final AnalysisMode analysisMode;
private final InputModule module;
+ private final SonarQubeVersion sqVersion;
- public DefaultSensorContext(InputModule module, Settings settings, FileSystem fs, ActiveRules activeRules, AnalysisMode analysisMode, SensorStorage sensorStorage) {
+ public DefaultSensorContext(InputModule module, Settings settings, FileSystem fs, ActiveRules activeRules, AnalysisMode analysisMode, SensorStorage sensorStorage,
+ SonarQubeVersion sqVersion) {
this.module = module;
this.settings = settings;
this.fs = fs;
this.activeRules = activeRules;
this.analysisMode = analysisMode;
this.sensorStorage = sensorStorage;
+ this.sqVersion = sqVersion;
}
@Override
return module;
}
+ @Override
+ public Version getSonarQubeVersion() {
+ return sqVersion.get();
+ }
+
@Override
public <G extends Serializable> NewMeasure<G> newMeasure() {
return new DefaultMeasure<>(sensorStorage);
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
+import org.sonar.api.SonarQubeVersion;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.utils.Version;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
private Settings settings;
private SensorStorage sensorStorage;
private AnalysisMode analysisMode;
+ private SonarQubeVersion sqVersion;
@Before
public void prepare() throws Exception {
settings = new Settings();
sensorStorage = mock(SensorStorage.class);
analysisMode = mock(AnalysisMode.class);
- adaptor = new DefaultSensorContext(mock(InputModule.class), settings, fs, activeRules, analysisMode, sensorStorage);
+ sqVersion = new SonarQubeVersion(Version.parse("5.5"));
+ adaptor = new DefaultSensorContext(mock(InputModule.class), settings, fs, activeRules, analysisMode, sensorStorage, sqVersion);
}
@Test
assertThat(adaptor.activeRules()).isEqualTo(activeRules);
assertThat(adaptor.fileSystem()).isEqualTo(fs);
assertThat(adaptor.settings()).isEqualTo(settings);
+ assertThat(adaptor.getSonarQubeVersion()).isEqualTo(Version.parse("5.5"));
assertThat(adaptor.newIssue()).isNotNull();
assertThat(adaptor.newMeasure()).isNotNull();