Browse Source

SONARPLUGINS-1466 Throw an exception with a meaningful message, when mandatory property was not defined

tags/2.5-rc1
Evgeny Mandrikov 12 years ago
parent
commit
0c4a588871

+ 1
- 1
src/main/java/org/sonar/runner/Main.java View File

@@ -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);
}

+ 19
- 2
src/main/java/org/sonar/runner/Runner.java View File

@@ -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)) {

+ 35
- 0
src/main/java/org/sonar/runner/RunnerException.java View File

@@ -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);
}

}

+ 28
- 4
src/test/java/org/sonar/runner/RunnerTest.java View File

@@ -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));

Loading…
Cancel
Save