Browse Source

Fix some quality flaws.

tags/2.5-rc1
Julien HENRY 11 years ago
parent
commit
e727146d35

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

} }


File[] files = directory.listFiles(); 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); throw new IOException("Failed to list contents of " + directory);
} }



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

// TODO Remove this after everything is updated to support tasks // TODO Remove this after everything is updated to support tasks
private static final String TASK_COMMAND = "sonar.task"; 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 @VisibleForTesting
Properties globalProperties; Properties globalProperties;
@VisibleForTesting @VisibleForTesting

+ 7
- 3
src/main/java/org/sonar/runner/Runner.java View File

*/ */
package org.sonar.runner; package org.sonar.runner;


import com.google.common.annotations.VisibleForTesting;

import java.io.File; import java.io.File;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
/** /**
* @return global properties, project properties and command-line properties * @return global properties, project properties and command-line properties
*/ */
protected Properties getProperties() {
@VisibleForTesting
public Properties getProperties() {
Properties props = new Properties(); Properties props = new Properties();
props.putAll(globalProperties); props.putAll(globalProperties);
props.putAll(projectProperties); props.putAll(projectProperties);
private BootstrapClassLoader createClassLoader(Bootstrapper bootstrapper) { private BootstrapClassLoader createClassLoader(Bootstrapper bootstrapper) {
URL url = getJarPath(); URL url = getJarPath();
return bootstrapper.createClassLoader( 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(), getClass().getClassLoader(),
unmaskedPackages); unmaskedPackages);
} }
if (uri != null) { if (uri != null) {
try { try {
return new URL(uri); return new URL(uri);
} catch (MalformedURLException e) { // NOSONAR
} catch (MalformedURLException e) {
} }
} }
} }

+ 3
- 0
src/main/java/org/sonar/runner/internal/batch/Launcher.java View File

private Properties projectProperties; private Properties projectProperties;
private List<Object> containerExtensions; private List<Object> containerExtensions;


/**
* @deprecated Use {@link Launcher#Launcher(String, Properties, Properties, List)} instead
*/
@Deprecated @Deprecated
public Launcher(Properties properties, List<Object> containerExtensions) { public Launcher(Properties properties, List<Object> containerExtensions) {
this("project-analysis", new Properties(), properties, containerExtensions); this("project-analysis", new Properties(), properties, containerExtensions);

+ 1
- 1
src/main/java/org/sonar/runner/internal/batch/SonarProjectBuilder.java View File

} }


public ProjectDefinition generateProjectDefinition() { public ProjectDefinition generateProjectDefinition() {
if (StringUtils.isBlank(command) || command.equals("inspect")) {
if (StringUtils.isBlank(command) || "inspect".equals(command)) {
ProjectDefinition rootProject = defineProject(properties, null); ProjectDefinition rootProject = defineProject(properties, null);
rootProjectWorkDir = rootProject.getWorkDir(); rootProjectWorkDir = rootProject.getWorkDir();
defineChildren(rootProject); defineChildren(rootProject);

+ 77
- 0
src/test/java/org/sonar/runner/LogsTest.java View File

/*
* 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");
}

}

+ 33
- 5
src/test/java/org/sonar/runner/MainTest.java View File

*/ */
package org.sonar.runner; package org.sonar.runner;


import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import java.io.File; import java.io.File;


public class MainTest { public class MainTest {


private Main main;

@Before
public void prepare() {
main = new Main();
}

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


@Test @Test
public void shouldParseArguments() { 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).hasSize(3);
assertThat(props.getProperty("foo")).isEqualTo("bar"); assertThat(props.getProperty("foo")).isEqualTo("bar");
assertThat(props.getProperty("hello")).isEqualTo("world"); assertThat(props.getProperty("hello")).isEqualTo("world");
assertThat(props.getProperty("boolean")).isEqualTo("true"); 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 @Test
public void shouldEnableDebugMode() { 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"); assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isEqualTo("true");
} }


@Test @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(); assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isNull();
} }



Loading…
Cancel
Save