@@ -130,7 +130,8 @@ final class IOUtils { | |||
} | |||
File[] files = directory.listFiles(); | |||
if (files == null) { // null if security restricted | |||
// null if security restricted | |||
if (files == null) { | |||
throw new IOException("Failed to list contents of " + directory); | |||
} | |||
@@ -49,10 +49,14 @@ public final class Main { | |||
// TODO Remove this after everything is updated to support tasks | |||
private static final String TASK_COMMAND = "sonar.task"; | |||
private boolean debugMode = false; | |||
private boolean displayVersionOnly = false; | |||
private boolean displayStackTrace = false; | |||
private String command; | |||
@VisibleForTesting | |||
boolean debugMode = false; | |||
@VisibleForTesting | |||
boolean displayVersionOnly = false; | |||
@VisibleForTesting | |||
boolean displayStackTrace = false; | |||
@VisibleForTesting | |||
String command; | |||
@VisibleForTesting | |||
Properties globalProperties; | |||
@VisibleForTesting |
@@ -19,6 +19,8 @@ | |||
*/ | |||
package org.sonar.runner; | |||
import com.google.common.annotations.VisibleForTesting; | |||
import java.io.File; | |||
import java.lang.reflect.Constructor; | |||
import java.lang.reflect.InvocationTargetException; | |||
@@ -244,7 +246,8 @@ public final class Runner { | |||
/** | |||
* @return global properties, project properties and command-line properties | |||
*/ | |||
protected Properties getProperties() { | |||
@VisibleForTesting | |||
public Properties getProperties() { | |||
Properties props = new Properties(); | |||
props.putAll(globalProperties); | |||
props.putAll(projectProperties); | |||
@@ -266,7 +269,8 @@ public final class Runner { | |||
private BootstrapClassLoader createClassLoader(Bootstrapper bootstrapper) { | |||
URL url = getJarPath(); | |||
return bootstrapper.createClassLoader( | |||
new URL[] {url}, // Add JAR with Sonar Runner - it's a Jar which contains this class | |||
// Add JAR with Sonar Runner - it's a Jar which contains this class | |||
new URL[] {url}, | |||
getClass().getClassLoader(), | |||
unmaskedPackages); | |||
} | |||
@@ -293,7 +297,7 @@ public final class Runner { | |||
if (uri != null) { | |||
try { | |||
return new URL(uri); | |||
} catch (MalformedURLException e) { // NOSONAR | |||
} catch (MalformedURLException e) { | |||
} | |||
} | |||
} |
@@ -50,6 +50,9 @@ public class Launcher { | |||
private Properties projectProperties; | |||
private List<Object> containerExtensions; | |||
/** | |||
* @deprecated Use {@link Launcher#Launcher(String, Properties, Properties, List)} instead | |||
*/ | |||
@Deprecated | |||
public Launcher(Properties properties, List<Object> containerExtensions) { | |||
this("project-analysis", new Properties(), properties, containerExtensions); |
@@ -128,7 +128,7 @@ public final class SonarProjectBuilder { | |||
} | |||
public ProjectDefinition generateProjectDefinition() { | |||
if (StringUtils.isBlank(command) || command.equals("inspect")) { | |||
if (StringUtils.isBlank(command) || "inspect".equals(command)) { | |||
ProjectDefinition rootProject = defineProject(properties, null); | |||
rootProjectWorkDir = rootProject.getWorkDir(); | |||
defineChildren(rootProject); |
@@ -0,0 +1,77 @@ | |||
/* | |||
* Sonar 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; | |||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import org.sonar.api.utils.SonarException; | |||
import java.io.ByteArrayOutputStream; | |||
import java.io.PrintStream; | |||
import static org.fest.assertions.Assertions.assertThat; | |||
public class LogsTest { | |||
private PrintStream oldSysout; | |||
private PrintStream oldSyserr; | |||
private ByteArrayOutputStream baosOut; | |||
private ByteArrayOutputStream baosErr; | |||
@Before | |||
public void prepare() { | |||
oldSysout = System.out; | |||
oldSyserr = System.err; | |||
baosOut = new ByteArrayOutputStream(); | |||
System.setOut(new PrintStream(baosOut)); | |||
baosErr = new ByteArrayOutputStream(); | |||
System.setErr(new PrintStream(baosErr)); | |||
} | |||
@After | |||
public void restore() { | |||
System.setOut(oldSysout); | |||
System.setErr(oldSyserr); | |||
} | |||
@Test | |||
public void shouldLogInfo() { | |||
Logs.info("info"); | |||
assertThat(baosOut.toString()).contains("INFO: info"); | |||
assertThat(baosErr.toString()).isEmpty(); | |||
} | |||
@Test | |||
public void shouldLogError() { | |||
Logs.error("error"); | |||
assertThat(baosOut.toString()).isEmpty(); | |||
assertThat(baosErr.toString()).contains("ERROR: error"); | |||
} | |||
@Test | |||
public void shouldLogErrorWithThrowable() { | |||
Logs.error("error", new SonarException()); | |||
assertThat(baosOut.toString()).isEmpty(); | |||
assertThat(baosErr.toString()).contains("ERROR: error").contains("SonarException"); | |||
} | |||
} |
@@ -19,6 +19,7 @@ | |||
*/ | |||
package org.sonar.runner; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import java.io.File; | |||
@@ -28,30 +29,57 @@ import static org.fest.assertions.Assertions.assertThat; | |||
public class MainTest { | |||
private Main main; | |||
@Before | |||
public void prepare() { | |||
main = new Main(); | |||
} | |||
@Test | |||
public void shouldParseEmptyArguments() { | |||
Properties props = new Main().parseArguments(new String[] {}); | |||
Properties props = main.parseArguments(new String[] {}); | |||
assertThat(props).isEmpty(); | |||
} | |||
@Test | |||
public void shouldParseArguments() { | |||
Properties props = new Main().parseArguments(new String[] {"-D", "foo=bar", "--define", "hello=world", "-Dboolean"}); | |||
Properties props = main.parseArguments(new String[] {"-D", "foo=bar", "--define", "hello=world", "-Dboolean"}); | |||
assertThat(props).hasSize(3); | |||
assertThat(props.getProperty("foo")).isEqualTo("bar"); | |||
assertThat(props.getProperty("hello")).isEqualTo("world"); | |||
assertThat(props.getProperty("boolean")).isEqualTo("true"); | |||
} | |||
@Test | |||
public void shouldParseCommand() { | |||
Properties props = main.parseArguments(new String[] {"cmd", "-D", "foo=bar", "--define", "hello=world", "-Dboolean"}); | |||
assertThat(main.command).isEqualTo("cmd"); | |||
assertThat(props).hasSize(3); | |||
} | |||
@Test | |||
public void shouldEnableDebugMode() { | |||
Properties props = new Main().parseArguments(new String[] {"-X"}); | |||
Properties props = main.parseArguments(new String[] {"-X"}); | |||
assertThat(main.debugMode).isTrue(); | |||
assertThat(main.displayStackTrace).isTrue(); | |||
assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isEqualTo("true"); | |||
} | |||
@Test | |||
public void shouldDisableDebugModeByDefault() { | |||
Properties props = new Main().parseArguments(new String[] {}); | |||
public void shouldEnableError() { | |||
Properties props = main.parseArguments(new String[] {"-e"}); | |||
assertThat(main.debugMode).isFalse(); | |||
assertThat(main.displayStackTrace).isTrue(); | |||
assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isNull(); | |||
} | |||
@Test | |||
public void shouldDisableDebugModeAndStackByDefault() { | |||
Properties props = main.parseArguments(new String[] {}); | |||
assertThat(main.debugMode).isFalse(); | |||
assertThat(main.displayStackTrace).isFalse(); | |||
assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isNull(); | |||
} | |||