Browse Source

SONAR-11476 Remove scanner task support

tags/7.6
Julien HENRY 5 years ago
parent
commit
e20ec8f128

+ 23
- 44
sonar-scanner-engine/src/main/java/org/sonar/batch/bootstrapper/Batch.java View File

@@ -21,6 +21,7 @@ package org.sonar.batch.bootstrapper;

import com.google.common.base.Throwables;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -38,8 +39,7 @@ public final class Batch {

private LoggingConfiguration loggingConfig;
private List<Object> components;
private Map<String, String> scannerProperties = new HashMap<>();
private GlobalContainer bootstrapContainer;
private Map<String, String> globalProperties = new HashMap<>();

private Batch(Builder builder) {
components = new ArrayList<>();
@@ -47,11 +47,11 @@ public final class Batch {
if (builder.environment != null) {
components.add(builder.environment);
}
if (builder.scannerProperties != null) {
scannerProperties.putAll(builder.scannerProperties);
if (builder.globalProperties != null) {
globalProperties.putAll(builder.globalProperties);
}
if (builder.isEnableLoggingConfiguration()) {
loggingConfig = new LoggingConfiguration(builder.environment).setProperties(scannerProperties);
loggingConfig = new LoggingConfiguration(builder.environment).setProperties(globalProperties);

if (builder.logOutput != null) {
loggingConfig.setLogOutput(builder.logOutput);
@@ -64,12 +64,15 @@ public final class Batch {
}

public synchronized Batch execute() {
return doExecute(this.globalProperties, this.components);
}

public synchronized Batch doExecute(Map<String, String> scannerProperties, List<Object> components) {
configureLogging();
doStart();
try {
doExecute();
} finally {
doStop();
GlobalContainer.create(scannerProperties, components).execute();
} catch (RuntimeException e) {
throw handleException(e);
}
return this;
}
@@ -83,33 +86,17 @@ public final class Batch {
return this;
}

private Batch doStart() {
try {
bootstrapContainer = GlobalContainer.create(scannerProperties, components);
bootstrapContainer.startComponents();
} catch (RuntimeException e) {
throw handleException(e);
}

return this;
}

/**
* @since 4.4
* @deprecated since 6.6 use {@link #execute()}
*/
@Deprecated
public Batch executeTask(Map<String, String> analysisProperties, Object... components) {
return execute();
}

private Batch doExecute(Object... components) {
try {
bootstrapContainer.executeTask(scannerProperties, components);
} catch (RuntimeException e) {
throw handleException(e);
}
return this;
Map<String, String> mergedProps = new HashMap<>(this.globalProperties);
mergedProps.putAll(analysisProperties);
List<Object> mergedComponents = new ArrayList<>(this.components);
mergedComponents.addAll(Arrays.asList(components));
return doExecute(mergedProps, mergedComponents);
}

private RuntimeException handleException(RuntimeException t) {
@@ -134,17 +121,9 @@ public final class Batch {
public synchronized void stop() {
}

private void doStop() {
try {
bootstrapContainer.stopComponents();
} catch (RuntimeException e) {
throw handleException(e);
}
}

private void configureLogging() {
if (loggingConfig != null) {
loggingConfig.setProperties(scannerProperties);
loggingConfig.setProperties(globalProperties);
LoggingConfigurator.apply(loggingConfig);
}
}
@@ -154,7 +133,7 @@ public final class Batch {
}

public static final class Builder {
private Map<String, String> scannerProperties;
private Map<String, String> globalProperties;
private EnvironmentInformation environment;
private List<Object> components = new ArrayList<>();
private boolean enableLoggingConfiguration = true;
@@ -178,17 +157,17 @@ public final class Batch {
return this;
}

public Builder setScannerProperties(Map<String, String> scannerProperties) {
this.scannerProperties = scannerProperties;
public Builder setGlobalProperties(Map<String, String> globalProperties) {
this.globalProperties = globalProperties;
return this;
}

/**
* @deprecated since 6.6 use {@link #setScannerProperties(Map)}
* @deprecated since 6.6 use {@link #setGlobalProperties(Map)}
*/
@Deprecated
public Builder setBootstrapProperties(Map<String, String> bootstrapProperties) {
this.scannerProperties = bootstrapProperties;
this.globalProperties = bootstrapProperties;
return this;
}


+ 1
- 14
sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/BatchComponents.java View File

@@ -34,10 +34,6 @@ import org.sonar.scanner.scan.report.JSONReport;
import org.sonar.scanner.scm.ScmConfiguration;
import org.sonar.scanner.scm.ScmPublisher;
import org.sonar.scanner.source.ZeroCoverageSensor;
import org.sonar.scanner.task.ListTask;
import org.sonar.scanner.task.ScanTask;
import org.sonar.scanner.task.Tasks;
import org.sonar.scanner.task.ViewsTask;

public class BatchComponents {
private BatchComponents() {
@@ -46,16 +42,7 @@ public class BatchComponents {

public static Collection<Object> all(GlobalAnalysisMode analysisMode) {
List<Object> components = Lists.newArrayList(
DefaultResourceTypes.get(),

// Tasks
Tasks.class,
ListTask.DEFINITION,
ListTask.class,
ScanTask.DEFINITION,
ScanTask.class,
ViewsTask.DEFINITION,
ViewsTask.class);
DefaultResourceTypes.get());
components.addAll(CorePropertyDefinitions.all());
if (!analysisMode.isIssues()) {
// SCM

+ 16
- 8
sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java View File

@@ -22,11 +22,14 @@ package org.sonar.scanner.bootstrap;
import java.time.Clock;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties;
import org.sonar.api.Plugin;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarQubeVersion;
import org.sonar.api.internal.ApiVersion;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.UriReader;
import org.sonar.api.utils.Version;
@@ -48,8 +51,8 @@ import org.sonar.scanner.repository.MetricsRepositoryLoader;
import org.sonar.scanner.repository.MetricsRepositoryProvider;
import org.sonar.scanner.repository.settings.DefaultSettingsLoader;
import org.sonar.scanner.repository.settings.SettingsLoader;
import org.sonar.scanner.scan.ProjectScanContainer;
import org.sonar.scanner.storage.StoragesManager;
import org.sonar.scanner.task.TaskContainer;

public class GlobalContainer extends ComponentContainer {
private static final Logger LOG = Loggers.get(GlobalContainer.class);
@@ -111,6 +114,18 @@ public class GlobalContainer extends ComponentContainer {
protected void doAfterStart() {
installPlugins();
loadCoreExtensions();

long startTime = System.currentTimeMillis();
String taskKey = StringUtils.defaultIfEmpty(scannerProperties.get(CoreProperties.TASK), CoreProperties.SCAN_TASK);
if (taskKey.equals("views")) {
throw MessageException.of("The task 'views' was removed with SonarQube 7.1. " +
"You can safely remove this call since portfolios and applications are automatically re-calculated.");
} else if (!taskKey.equals(CoreProperties.SCAN_TASK)) {
throw MessageException.of("Tasks support was removed in SonarQube 7.6.");
}
new ProjectScanContainer(this).execute();

LOG.info("Analysis total time: {}", formatTime(System.currentTimeMillis() - startTime));
}

private void installPlugins() {
@@ -126,13 +141,6 @@ public class GlobalContainer extends ComponentContainer {
loader.load();
}

public void executeTask(Map<String, String> taskProperties, Object... components) {
long startTime = System.currentTimeMillis();
new TaskContainer(this, taskProperties, components).execute();

LOG.info("Task total time: {}", formatTime(System.currentTimeMillis() - startTime));
}

static String formatTime(long time) {
long h = time / (60 * 60 * 1000);
long m = (time - h * 60 * 60 * 1000) / (60 * 1000);

+ 0
- 56
sonar-scanner-engine/src/main/java/org/sonar/scanner/task/ListTask.java View File

@@ -1,56 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 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.scanner.task;

import org.sonar.api.task.Task;
import org.sonar.api.task.TaskDefinition;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

public class ListTask implements Task {

private static final Logger LOG = Loggers.get(ListTask.class);

public static final String KEY = "list";

public static final TaskDefinition DEFINITION = TaskDefinition.builder()
.key(KEY)
.description("List available tasks")
.taskClass(ListTask.class)
.build();

private final Tasks tasks;

public ListTask(Tasks tasks) {
this.tasks = tasks;
}

@Override
public void execute() {
StringBuilder sb = new StringBuilder();
sb.append("\nAvailable tasks:\n");
for (TaskDefinition def : tasks.definitions()) {
sb.append(" - " + def.key() + ": " + def.description() + "\n");
}
sb.append("\n");
LOG.info(sb.toString());
}

}

+ 0
- 45
sonar-scanner-engine/src/main/java/org/sonar/scanner/task/ScanTask.java View File

@@ -1,45 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 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.scanner.task;

import org.sonar.api.CoreProperties;
import org.sonar.api.task.Task;
import org.sonar.api.task.TaskDefinition;
import org.sonar.core.platform.ComponentContainer;
import org.sonar.scanner.scan.ProjectScanContainer;

public class ScanTask implements Task {
public static final TaskDefinition DEFINITION = TaskDefinition.builder()
.description("Scan project")
.key(CoreProperties.SCAN_TASK)
.taskClass(ScanTask.class)
.build();

private final ComponentContainer taskContainer;

public ScanTask(TaskContainer taskContainer) {
this.taskContainer = taskContainer;
}

@Override
public void execute() {
new ProjectScanContainer(taskContainer).execute();
}
}

+ 0
- 88
sonar-scanner-engine/src/main/java/org/sonar/scanner/task/TaskContainer.java View File

@@ -1,88 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 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.scanner.task;

import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties;
import org.sonar.api.task.Task;
import org.sonar.api.task.TaskDefinition;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.core.extension.CoreExtensionsInstaller;
import org.sonar.core.platform.ComponentContainer;
import org.sonar.scanner.bootstrap.ExtensionInstaller;

import static org.sonar.api.batch.InstantiationStrategy.PER_TASK;
import static org.sonar.core.extension.CoreExtensionsInstaller.noExtensionFilter;
import static org.sonar.scanner.bootstrap.ExtensionUtils.isDeprecatedScannerSide;
import static org.sonar.scanner.bootstrap.ExtensionUtils.isInstantiationStrategy;

public class TaskContainer extends ComponentContainer {

private static final Logger LOG = Loggers.get(TaskContainer.class);

private final Map<String, String> taskProperties;
private final Object[] components;

public TaskContainer(ComponentContainer parent, Map<String, String> taskProperties, Object... components) {
super(parent);
this.taskProperties = taskProperties;
this.components = components;
}

@Override
protected void doBeforeStart() {
addTaskExtensions();
for (Object component : components) {
add(component);
}
}

private void addTaskExtensions() {
getComponentByType(CoreExtensionsInstaller.class)
.install(this, noExtensionFilter(), t -> isInstantiationStrategy(t, PER_TASK));
getComponentByType(ExtensionInstaller.class)
.install(this, extension -> isDeprecatedScannerSide(extension) && isInstantiationStrategy(extension, PER_TASK));
}

@Override
public void doAfterStart() {
// default value is declared in CorePlugin
String taskKey = StringUtils.defaultIfEmpty(taskProperties.get(CoreProperties.TASK), CoreProperties.SCAN_TASK);
if (!taskKey.equals(CoreProperties.SCAN_TASK)) {
LOG.warn("Scanner tasks are deprecated");
}
// Release memory
taskProperties.clear();

TaskDefinition def = getComponentByType(Tasks.class).definition(taskKey);
if (def == null) {
throw MessageException.of("Task '" + taskKey + "' does not exist. Please use '" + ListTask.KEY + "' task to see all available tasks.");
}
Task task = getComponentByType(def.taskClass());
if (task != null) {
task.execute();
} else {
throw new IllegalStateException("Task " + taskKey + " is badly defined");
}
}
}

+ 0
- 75
sonar-scanner-engine/src/main/java/org/sonar/scanner/task/Tasks.java View File

@@ -1,75 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 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.scanner.task;

import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Maps;
import java.util.Collection;
import java.util.Map;
import java.util.SortedMap;
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.task.Task;
import org.sonar.api.task.TaskDefinition;

@ScannerSide
@InstantiationStrategy(InstantiationStrategy.PER_TASK)
public class Tasks {

private final SortedMap<String, TaskDefinition> byKey;

public Tasks(TaskDefinition[] definitions) {
SortedMap<String, TaskDefinition> map = Maps.newTreeMap();
for (TaskDefinition definition : definitions) {
if (map.containsKey(definition.key())) {
throw new IllegalStateException("Task '" + definition.key() + "' is declared twice");
}
map.put(definition.key(), definition);
}
this.byKey = ImmutableSortedMap.copyOf(map);
}

public TaskDefinition definition(String taskKey) {
return byKey.get(taskKey);
}

public Collection<TaskDefinition> definitions() {
return byKey.values();
}

/**
* Perform validation of task definitions
*/
public void start() {
checkDuplicatedClasses();
}

private void checkDuplicatedClasses() {
Map<Class<? extends Task>, TaskDefinition> byClass = Maps.newHashMap();
for (TaskDefinition def : definitions()) {
TaskDefinition other = byClass.get(def.taskClass());
if (other == null) {
byClass.put(def.taskClass(), def);
} else {
throw new IllegalStateException("Task '" + def.taskClass().getName() + "' is defined twice: first by '" + other.key() + "' and then by '" + def.key() + "'");
}
}
}
}

+ 0
- 47
sonar-scanner-engine/src/main/java/org/sonar/scanner/task/ViewsTask.java View File

@@ -1,47 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 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.scanner.task;

import org.sonar.api.task.Task;
import org.sonar.api.task.TaskDefinition;
import org.sonar.api.utils.MessageException;

/**
* This task is deprecated since the refresh of portfolios and application is now automatic
* This task does nothing
*
* @deprecated since 7.1
*/
@Deprecated
public class ViewsTask implements Task {

private static final String KEY = "views";

public static final TaskDefinition DEFINITION = TaskDefinition.builder()
.key(KEY)
.description("Removed - was used to trigger portfolios refresh")
.taskClass(ViewsTask.class)
.build();

@Override
public void execute() {
throw MessageException.of("The task 'views' was removed with SonarQube 7.1. You can safely remove this call since portfolios and applications are automatically re-calculated.");
}
}

+ 0
- 23
sonar-scanner-engine/src/main/java/org/sonar/scanner/task/package-info.java View File

@@ -1,23 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 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.
*/
@ParametersAreNonnullByDefault
package org.sonar.scanner.task;

import javax.annotation.ParametersAreNonnullByDefault;

+ 1
- 1
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java View File

@@ -289,7 +289,7 @@ public class ScannerMediumTester extends ExternalResource {
props.putAll(taskProperties);

Batch.builder()
.setScannerProperties(props)
.setGlobalProperties(props)
.setEnableLoggingConfiguration(true)
.addComponents(new EnvironmentInformation("mediumTest", "1.0"),
tester.pluginInstaller,

+ 24
- 47
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/tasks/TasksMediumTest.java View File

@@ -20,7 +20,6 @@
package org.sonar.scanner.mediumtest.tasks;

import com.google.common.collect.ImmutableMap;
import org.assertj.core.api.Condition;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -29,10 +28,10 @@ import org.sonar.api.task.Task;
import org.sonar.api.task.TaskDefinition;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.scanner.mediumtest.ScannerMediumTester;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

public class TasksMediumTest {

@@ -47,47 +46,36 @@ public class TasksMediumTest {
.registerPlugin("faketask", new FakeTaskPlugin());

@Test
public void listTasksIncludingBroken() throws Exception {
tester.newAnalysis()
.properties(ImmutableMap.<String, String>builder()
.put("sonar.task", "list").build())
.execute();

assertThat(logTester.logs()).haveExactly(1, new Condition<String>() {

@Override
public boolean matches(String value) {
return value.contains("Available tasks:") && value.contains("fake: Fake description") && value.contains("broken: Broken description");
}
});

assertThat(logTester.logs(LoggerLevel.WARN)).contains("Scanner tasks are deprecated");
public void failWhenCallingTask() throws Exception {
try {
tester.newAnalysis()
.properties(ImmutableMap.<String, String>builder()
.put("sonar.task", "fake").build())
.execute();
fail("Expected exception");
} catch (Exception e) {
assertThat(e).isInstanceOf(MessageException.class).hasMessage("Tasks support was removed in SonarQube 7.6.");
}
}

@Test
public void runBroken() throws Exception {
thrown.expect(IllegalStateException.class);
thrown.expectMessage(
"Unable to load component class org.sonar.scanner.mediumtest.tasks.TasksMediumTest$BrokenTask");

tester.newAnalysis()
.properties(ImmutableMap.<String, String>builder()
.put("sonar.task", "broken").build())
.execute();
}

@Test(expected = MessageException.class)
public void unsupportedTask() throws Exception {
tester.newAnalysis()
.properties(ImmutableMap.<String, String>builder()
.put("sonar.task", "foo").build())
.execute();
public void failWhenCallingViews() throws Exception {
try {
tester.newAnalysis()
.properties(ImmutableMap.<String, String>builder()
.put("sonar.task", "views").build())
.execute();
fail("Expected exception");
} catch (Exception e) {
assertThat(e).isInstanceOf(MessageException.class).hasMessage("The task 'views' was removed with SonarQube 7.1. You can safely remove this call since portfolios and applications are automatically re-calculated.");
}
}

private static class FakeTaskPlugin implements Plugin {

@Override public void define(Context context) {
context.addExtensions(FakeTask.DEF, FakeTask.class, BrokenTask.DEF, BrokenTask.class);
@Override
public void define(Context context) {
context.addExtensions(FakeTask.DEF, FakeTask.class);
}
}

@@ -103,15 +91,4 @@ public class TasksMediumTest {

}

private static class BrokenTask implements Task {

public static final TaskDefinition DEF = TaskDefinition.builder().key("broken").description("Broken description").taskClass(BrokenTask.class).build();

@Override
public void execute() {
// do nothing
}

}

}

+ 0
- 63
sonar-scanner-engine/src/test/java/org/sonar/scanner/task/ListTaskTest.java View File

@@ -1,63 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 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.scanner.task;

import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.task.Task;
import org.sonar.api.task.TaskDefinition;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ListTaskTest {

@Rule
public LogTester logTester = new LogTester();

@Test
public void should_list_available_tasks() {
Tasks tasks = mock(Tasks.class);
when(tasks.definitions()).thenReturn(Arrays.asList(
TaskDefinition.builder().key("foo").description("Foo").taskClass(FooTask.class).build(),
TaskDefinition.builder().key("purge").description("Purge database").taskClass(FakePurgeTask.class).build()));

ListTask task = new ListTask(tasks);

task.execute();

assertThat(logTester.logs(LoggerLevel.INFO)).hasSize(1);
assertThat(logTester.logs(LoggerLevel.INFO).get(0)).contains("Available tasks:", " - foo: Foo", " - purge: Purge database");
}

private static class FakePurgeTask implements Task {
public void execute() {
}
}

private static class FooTask implements Task {
public void execute() {
}
}
}

+ 0
- 90
sonar-scanner-engine/src/test/java/org/sonar/scanner/task/TasksTest.java View File

@@ -1,90 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 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.scanner.task;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.task.Task;
import org.sonar.api.task.TaskDefinition;

import static org.assertj.core.api.Assertions.assertThat;

public class TasksTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void should_get_definitions() {
Tasks tasks = new Tasks(new TaskDefinition[] {ScanTask.DEFINITION, ListTask.DEFINITION});
assertThat(tasks.definitions()).hasSize(2);
}

@Test
public void should_get_definition_by_key() {
Tasks tasks = new Tasks(new TaskDefinition[] {ScanTask.DEFINITION, ListTask.DEFINITION});
tasks.start();
assertThat(tasks.definition(ListTask.DEFINITION.key())).isEqualTo(ListTask.DEFINITION);
}

@Test
public void should_return_null_if_task_not_found() {
Tasks tasks = new Tasks(new TaskDefinition[] {ScanTask.DEFINITION, ListTask.DEFINITION});

assertThat(tasks.definition("not-exists")).isNull();
}

@Test
public void should_fail_on_duplicated_keys() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Task 'foo' is declared twice");

new Tasks(new TaskDefinition[] {
TaskDefinition.builder().key("foo").taskClass(FakeTask1.class).description("foo1").build(),
TaskDefinition.builder().key("foo").taskClass(FakeTask2.class).description("foo2").build()
});
}

@Test
public void should_fail_on_duplicated_class() {
Tasks tasks = new Tasks(new TaskDefinition[] {
TaskDefinition.builder().key("foo1").taskClass(FakeTask1.class).description("foo1").build(),
TaskDefinition.builder().key("foo2").taskClass(FakeTask1.class).description("foo1").build()
});

thrown.expect(IllegalStateException.class);
thrown.expectMessage("Task 'org.sonar.scanner.task.TasksTest$FakeTask1' is defined twice: first by 'foo1' and then by 'foo2'");

tasks.start();
}

private static class FakeTask1 implements Task {
public void execute() {
}
}

private static class FakeTask2 implements Task {
public void execute() {
}

}

}

+ 0
- 52
sonar-scanner-engine/src/test/java/org/sonar/scanner/task/ViewsTaskTest.java View File

@@ -1,52 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 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.scanner.task;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.log.LogTester;

public class ViewsTaskTest {

@Rule
public LogTester logTester = new LogTester();
@Rule
public ExpectedException expectedException = ExpectedException.none();

private ViewsTask underTest = new ViewsTask();

@Test
public void triggerShowError() {
expectedException.expect(MessageException.class);
expectedException.expectMessage(
"The task 'views' was removed with SonarQube 7.1. You can safely remove this call since portfolios and applications are automatically re-calculated.");

underTest.execute();
}
}

Loading…
Cancel
Save