summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2011-12-15 20:39:31 +0000
committerEvgeny Mandrikov <mandrikov@gmail.com>2011-12-15 20:39:31 +0000
commit0c4a5888718bb4d8731cf328ca2071b3ffe1ca0b (patch)
tree3e80d67d0fecd7c131776738dc4a1a867eaa5b38 /src
parent20709639ec70e782a7cf92c87c112890adceb011 (diff)
downloadsonar-scanner-cli-0c4a5888718bb4d8731cf328ca2071b3ffe1ca0b.tar.gz
sonar-scanner-cli-0c4a5888718bb4d8731cf328ca2071b3ffe1ca0b.zip
SONARPLUGINS-1466 Throw an exception with a meaningful message, when mandatory property was not defined
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/sonar/runner/Main.java2
-rw-r--r--src/main/java/org/sonar/runner/Runner.java21
-rw-r--r--src/main/java/org/sonar/runner/RunnerException.java35
-rw-r--r--src/test/java/org/sonar/runner/RunnerTest.java32
4 files changed, 83 insertions, 7 deletions
diff --git a/src/main/java/org/sonar/runner/Main.java b/src/main/java/org/sonar/runner/Main.java
index 8ee7ccb..d596cc5 100644
--- a/src/main/java/org/sonar/runner/Main.java
+++ b/src/main/java/org/sonar/runner/Main.java
@@ -55,7 +55,7 @@ public final class Main {
log("Work directory: " + runner.getWorkDir().getCanonicalPath());
runner.execute();
} catch (IOException e) {
- throw new RuntimeException(e);
+ throw new RunnerException(e);
} finally {
printStats(startTime);
}
diff --git a/src/main/java/org/sonar/runner/Runner.java b/src/main/java/org/sonar/runner/Runner.java
index 5928c8f..1b7c9a9 100644
--- a/src/main/java/org/sonar/runner/Runner.java
+++ b/src/main/java/org/sonar/runner/Runner.java
@@ -48,6 +48,16 @@ public final class Runner {
*/
public static final String VERBOSE = "sonar.verbose";
+ /**
+ * Array of prefixes of versions of Sonar without support of this runner.
+ */
+ private static final String[] unsupportedVersions = { "1", "2.0", "2.1", "2.2", "2.3", "2.4", "2.5" };
+
+ /**
+ * Array of all mandatory properties required to execute runner.
+ */
+ private static final String[] MANDATORY_PROPERTIES = { "sonar.projectKey", "sonar.projectName", "sonar.projectVersion", "sources" };
+
private File projectDir;
private File workDir;
private Properties properties;
@@ -62,11 +72,20 @@ public final class Runner {
}
public void execute() {
+ checkMandatoryProperties();
Bootstrapper bootstrapper = new Bootstrapper("SonarRunner/" + getRunnerVersion(), getServerURl(), getWorkDir());
checkSonarVersion(bootstrapper);
delegateExecution(createClassLoader(bootstrapper));
}
+ void checkMandatoryProperties() {
+ for (String mandatoryProperty : MANDATORY_PROPERTIES) {
+ if (!properties.containsKey(mandatoryProperty)) {
+ throw new RunnerException("You must define mandatory property: " + mandatoryProperty);
+ }
+ }
+ }
+
public String getServerURl() {
return properties.getProperty("sonar.host.url", "http://localhost:9000");
}
@@ -131,8 +150,6 @@ public final class Runner {
getClass().getClassLoader());
}
- private static final String[] unsupportedVersions = { "1", "2.0", "2.1", "2.2", "2.3", "2.4", "2.5" };
-
static boolean isUnsupportedVersion(String version) {
for (String unsupportedVersion : unsupportedVersions) {
if (isVersion(version, unsupportedVersion)) {
diff --git a/src/main/java/org/sonar/runner/RunnerException.java b/src/main/java/org/sonar/runner/RunnerException.java
new file mode 100644
index 0000000..eb44490
--- /dev/null
+++ b/src/main/java/org/sonar/runner/RunnerException.java
@@ -0,0 +1,35 @@
+/*
+ * Sonar Standalone Runner
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * 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 02
+ */
+package org.sonar.runner;
+
+/**
+ * @since 1.2
+ */
+public class RunnerException extends RuntimeException {
+
+ public RunnerException(String message) {
+ super(message);
+ }
+
+ public RunnerException(Throwable cause) {
+ super(cause);
+ }
+
+}
diff --git a/src/test/java/org/sonar/runner/RunnerTest.java b/src/test/java/org/sonar/runner/RunnerTest.java
index e747313..a213bff 100644
--- a/src/test/java/org/sonar/runner/RunnerTest.java
+++ b/src/test/java/org/sonar/runner/RunnerTest.java
@@ -20,16 +20,40 @@
package org.sonar.runner;
-import org.junit.Test;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
import java.io.File;
-import java.net.URISyntaxException;
import java.util.Properties;
-import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.assertThat;
+import org.junit.Test;
public class RunnerTest {
+
+ @Test
+ public void shouldThrowExceptionIfMandatoryPropertyNotSpecified() {
+ try {
+ Runner.create(new Properties()).checkMandatoryProperties();
+ fail();
+ } catch (RunnerException e) {
+ assertThat(e.getMessage(), is("You must define mandatory property: sonar.projectKey"));
+ }
+ }
+
+ @Test
+ public void shouldNotThrowExceptionIfAllMandatoryPropertiesSpecified() {
+ Properties properties = new Properties();
+ properties.setProperty("sonar.projectKey", "foo");
+ properties.setProperty("sonar.projectName", "bar");
+ properties.setProperty("sonar.projectVersion", "1.0");
+ properties.setProperty("sources", "src");
+ Runner.create(properties).checkMandatoryProperties();
+ }
+
@Test
public void shouldCheckVersion() {
assertThat(Runner.isUnsupportedVersion("1.0"), is(true));