@@ -22,6 +22,7 @@ package org.sonar.runner; | |||
import java.io.File; | |||
import java.io.FileFilter; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.util.Properties; | |||
@@ -96,7 +97,7 @@ public class Launcher { | |||
definition.addBinaryDir(dir); | |||
} | |||
for (String pattern : getList(properties, "libraries")) { | |||
for (File file : getLibraries(pattern)) { | |||
for (File file : getLibraries(baseDir, pattern)) { | |||
definition.addLibrary(file.getAbsolutePath()); | |||
} | |||
} | |||
@@ -107,18 +108,35 @@ public class Launcher { | |||
* Returns files matching specified pattern. | |||
* Visibility has been relaxed to make code testable. | |||
*/ | |||
static File[] getLibraries(String pattern) { | |||
static File[] getLibraries(File baseDir, String pattern) { | |||
final int i = Math.max(pattern.lastIndexOf('/'), pattern.lastIndexOf('\\')); | |||
final String dir, filePattern; | |||
final String dirPath, filePattern; | |||
if (i == -1) { | |||
dir = "."; | |||
dirPath = "."; | |||
filePattern = pattern; | |||
} else { | |||
dir = pattern.substring(0, i); | |||
dirPath = pattern.substring(0, i); | |||
filePattern = pattern.substring(i + 1); | |||
} | |||
FileFilter fileFilter = new AndFileFilter(FileFileFilter.FILE, new WildcardFileFilter(filePattern)); | |||
return new File(dir).listFiles(fileFilter); | |||
File dir = resolvePath(baseDir, dirPath); | |||
File[] files = dir.listFiles(fileFilter); | |||
if (files == null || files.length == 0) { | |||
throw new RunnerException("No files matching pattern \"" + filePattern + "\" in directory \"" + dir + "\""); | |||
} | |||
return files; | |||
} | |||
private static File resolvePath(File baseDir, String path) { | |||
File file = new File(path); | |||
if (!file.isAbsolute()) { | |||
try { | |||
file = new File(baseDir, path).getCanonicalFile(); | |||
} catch (IOException e) { | |||
throw new RunnerException("Unable to resolve path \"" + path + "\"", e); | |||
} | |||
} | |||
return file; | |||
} | |||
private String[] getList(Properties properties, String key) { |
@@ -53,10 +53,12 @@ public final class Main { | |||
+ ", vendor: " + System.getProperty("java.vendor", "<unknown vendor>")); | |||
log("OS name: \"" + System.getProperty("os.name") + "\", version: \"" + System.getProperty("os.version") + "\", arch: \"" + System.getProperty("os.arch") + "\""); | |||
log("Server: " + runner.getServerURL()); | |||
log("Work directory: " + runner.getWorkDir().getCanonicalPath()); | |||
try { | |||
log("Work directory: " + runner.getWorkDir().getCanonicalPath()); | |||
} catch (IOException e) { | |||
throw new RunnerException(e); | |||
} | |||
runner.execute(); | |||
} catch (IOException e) { | |||
throw new RunnerException(e); | |||
} finally { | |||
printStats(startTime); | |||
} |
@@ -19,6 +19,8 @@ | |||
*/ | |||
package org.sonar.runner; | |||
import java.io.IOException; | |||
/** | |||
* @since 1.2 | |||
*/ | |||
@@ -32,4 +34,8 @@ public class RunnerException extends RuntimeException { | |||
super(cause); | |||
} | |||
public RunnerException(String message, Throwable cause) { | |||
super(message, cause); | |||
} | |||
} |
@@ -20,7 +20,9 @@ | |||
package org.sonar.runner; | |||
import static org.hamcrest.Matchers.is; | |||
import static org.hamcrest.Matchers.startsWith; | |||
import static org.junit.Assert.assertThat; | |||
import static org.junit.Assert.fail; | |||
import java.io.File; | |||
import org.junit.Test; | |||
@@ -28,15 +30,30 @@ import org.junit.Test; | |||
public class LauncherTest { | |||
@Test | |||
public void shouldNotFailWhenPathNotSpecified() { | |||
Launcher.getLibraries("file.jar"); | |||
public void shouldFilterFiles() throws Exception { | |||
File baseDir = new File(getClass().getResource("/org/sonar/runner/LauncherTest/shouldFilterFiles/").toURI()); | |||
assertThat(Launcher.getLibraries(baseDir, "in*.txt").length, is(1)); | |||
assertThat(Launcher.getLibraries(baseDir, "*.txt").length, is(2)); | |||
assertThat(Launcher.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/in*.txt").length, is(1)); | |||
assertThat(Launcher.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/*.txt").length, is(2)); | |||
} | |||
@Test | |||
public void shouldFilterFiles() throws Exception { | |||
File dir = new File(getClass().getResource("/org/sonar/runner/LauncherTest/shouldFilterFiles/").toURI()); | |||
assertThat(Launcher.getLibraries(dir.getAbsolutePath() + "/in*.txt").length, is(1)); | |||
assertThat(Launcher.getLibraries(dir.getAbsolutePath() + "/*.txt").length, is(2)); | |||
public void shouldWorkWithAbsolutePath() throws Exception { | |||
File baseDir = new File("not-exists"); | |||
String absolutePattern = new File(getClass().getResource("/org/sonar/runner/LauncherTest/shouldFilterFiles/").toURI()).getAbsolutePath() + "/in*.txt"; | |||
assertThat(Launcher.getLibraries(baseDir.getParentFile(), absolutePattern).length, is(1)); | |||
} | |||
@Test | |||
public void shouldThrowExceptionWhenNoFilesMatchingPattern() throws Exception { | |||
File baseDir = new File(getClass().getResource("/org/sonar/runner/LauncherTest/shouldFilterFiles/").toURI()); | |||
try { | |||
Launcher.getLibraries(baseDir, "*/*.jar"); | |||
fail("Exception expected"); | |||
} catch (RunnerException e) { | |||
assertThat(e.getMessage(), startsWith("No files matching pattern \"*.jar\" in directory ")); | |||
} | |||
} | |||
} |
@@ -38,7 +38,7 @@ public class RunnerTest { | |||
public void shouldThrowExceptionIfMandatoryPropertyNotSpecified() { | |||
try { | |||
Runner.create(new Properties()).checkMandatoryProperties(); | |||
fail(); | |||
fail("Exception expected"); | |||
} catch (RunnerException e) { | |||
assertThat(e.getMessage(), is("You must define mandatory properties: sonar.projectKey, sonar.projectName, sonar.projectVersion, sources")); | |||
} |