Browse Source

SONARPLUGINS-1372 Allow definitions of properties without white space

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

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

@@ -133,16 +133,10 @@ public final class Main {
printError("Missing argument for option --define");
}
arg = args[i];
final String key, value;
int j = arg.indexOf('=');
if (j == -1) {
key = arg;
value = "true";
} else {
key = arg.substring(0, j);
value = arg.substring(j + 1);
}
props.setProperty(key, value);
parseProperty(arg, props);
} else if (arg.startsWith("-D")) {
arg = arg.substring(2);
parseProperty(arg, props);
} else {
printError("Unrecognized option: " + arg);
}
@@ -150,6 +144,19 @@ public final class Main {
return props;
}

private static void parseProperty(String arg, Properties props) {
final String key, value;
int j = arg.indexOf('=');
if (j == -1) {
key = arg;
value = "true";
} else {
key = arg.substring(0, j);
value = arg.substring(j + 1);
}
props.setProperty(key, value);
}

private static void printUsage() {
log("");
log("usage: sonar-runner [options]");

+ 9
- 8
src/test/java/org/sonar/runner/MainTest.java View File

@@ -33,27 +33,28 @@ public class MainTest {

@Test
public void shouldParseEmptyArguments() {
Properties props = Main.parseArguments(new String[]{});
Properties props = Main.parseArguments(new String[] {});
assertThat(props.isEmpty(), is(true));
}

@Test
public void shouldParseArguments() {
Properties props = Main.parseArguments(new String[]{"-D", "foo=bar", "--define", "hello=world"});
assertThat(props.size(), is(2));
Properties props = Main.parseArguments(new String[] { "-D", "foo=bar", "--define", "hello=world", "-Dboolean" });
assertThat(props.size(), is(3));
assertThat(props.getProperty("foo"), is("bar"));
assertThat(props.getProperty("hello"), is("world"));
assertThat(props.getProperty("boolean"), is("true"));
}

@Test
public void shouldEnableDebugMode() {
Properties props = Main.parseArguments(new String[]{"-X"});
Properties props = Main.parseArguments(new String[] { "-X" });
assertThat(props.getProperty(Runner.VERBOSE), is("true"));
}

@Test
public void shouldDisableDebugModeByDefault() {
Properties props = Main.parseArguments(new String[]{});
Properties props = Main.parseArguments(new String[] {});
assertThat(props.getProperty(Runner.VERBOSE), nullValue());
}

@@ -90,9 +91,9 @@ public class MainTest {
public void shouldLoadCompleteConfiguration() throws Exception {
File runnerHome = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner").toURI());
File projectHome = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project").toURI());
Properties props = Main.loadProperties(new String[]{
"-D", "runner.home=" + runnerHome.getCanonicalPath(),
"-D", "project.home=" + projectHome.getCanonicalPath()
Properties props = Main.loadProperties(new String[] {
"-D", "runner.home=" + runnerHome.getCanonicalPath(),
"-D", "project.home=" + projectHome.getCanonicalPath()
});

assertThat(props.getProperty("project.prop"), is("foo"));

Loading…
Cancel
Save