aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-api
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-05-28 16:20:52 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-06-05 12:09:03 +0200
commit93217fe761c5fa51a695133f9c0358ddb66f68e1 (patch)
treec0f08206f2fd56de2c0e50e1d97d9c0bccdb2c8c /sonar-runner-api
parentb6630b9dc4d4011448bf650a359e1063288099a0 (diff)
downloadsonar-scanner-cli-93217fe761c5fa51a695133f9c0358ddb66f68e1.tar.gz
sonar-scanner-cli-93217fe761c5fa51a695133f9c0358ddb66f68e1.zip
SONARUNNER-132 Add support for offline mode
Diffstat (limited to 'sonar-runner-api')
-rw-r--r--sonar-runner-api/pom.xml19
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java18
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java3
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java18
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java7
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java57
-rw-r--r--sonar-runner-api/src/test/java/org/sonar/runner/api/CommandExecutorTest.java4
-rw-r--r--sonar-runner-api/src/test/java/org/sonar/runner/api/DirsTest.java4
-rw-r--r--sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java54
9 files changed, 131 insertions, 53 deletions
diff --git a/sonar-runner-api/pom.xml b/sonar-runner-api/pom.xml
index 84ea642..0f491c7 100644
--- a/sonar-runner-api/pom.xml
+++ b/sonar-runner-api/pom.xml
@@ -15,19 +15,18 @@
<groupId>${project.groupId}</groupId>
<artifactId>sonar-runner-impl</artifactId>
<version>${project.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>*</groupId>
+ <artifactId>*</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.0.1</version>
- <!-- Provided by sonar-runner-impl but relocated -->
- <scope>provided</scope>
- </dependency>
<!-- Unit tests -->
<dependency>
@@ -113,12 +112,6 @@
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
- <relocations>
- <relocation>
- <pattern>org.apache.commons.io</pattern>
- <shadedPattern>org.sonar.runner.commonsio</shadedPattern>
- </relocation>
- </relocations>
</configuration>
</execution>
</executions>
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java
index f54dbb9..999150d 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java
@@ -19,8 +19,6 @@
*/
package org.sonar.runner.api;
-import org.apache.commons.io.IOUtils;
-
import javax.annotation.Nullable;
import java.io.BufferedReader;
@@ -136,10 +134,10 @@ class CommandExecutor {
private void closeStreams(Process process) {
if (process != null) {
- IOUtils.closeQuietly(process.getInputStream());
- IOUtils.closeQuietly(process.getInputStream());
- IOUtils.closeQuietly(process.getOutputStream());
- IOUtils.closeQuietly(process.getErrorStream());
+ Utils.closeQuietly(process.getInputStream());
+ Utils.closeQuietly(process.getInputStream());
+ Utils.closeQuietly(process.getOutputStream());
+ Utils.closeQuietly(process.getErrorStream());
}
}
@@ -167,9 +165,8 @@ class CommandExecutor {
@Override
public void run() {
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- try {
+ try (InputStreamReader isr = new InputStreamReader(is);
+ BufferedReader br = new BufferedReader(isr)) {
String line;
while ((line = br.readLine()) != null) {
consumeLine(line);
@@ -177,9 +174,6 @@ class CommandExecutor {
} catch (IOException ioe) {
exception = ioe;
- } finally {
- IOUtils.closeQuietly(br);
- IOUtils.closeQuietly(isr);
}
}
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java
index 1e9654e..369a745 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java
@@ -19,7 +19,6 @@
*/
package org.sonar.runner.api;
-import org.apache.commons.io.FileUtils;
import org.sonar.runner.impl.Logs;
import java.io.File;
@@ -54,7 +53,7 @@ class Dirs {
workDir = new File(projectDir, path);
}
}
- FileUtils.deleteQuietly(workDir);
+ Utils.deleteQuietly(workDir);
runner.setProperty(RunnerProperties.WORK_DIR, workDir.getAbsolutePath());
Logs.info("Work directory: " + workDir.getAbsolutePath());
}
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java
index 02ae333..fd7dc0b 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java
@@ -19,8 +19,6 @@
*/
package org.sonar.runner.api;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
import org.sonar.runner.impl.BatchLauncherMain;
import org.sonar.runner.impl.JarExtractor;
@@ -163,24 +161,20 @@ public class ForkedRunner extends Runner<ForkedRunner> {
}
private File writeProperties() {
- OutputStream output = null;
try {
File file = File.createTempFile("sonar-project", ".properties");
- output = new FileOutputStream(file);
- properties().store(output, "Generated by sonar-runner");
- return file;
-
+ try (OutputStream output = new FileOutputStream(file)) {
+ properties().store(output, "Generated by sonar-runner");
+ return file;
+ }
} catch (Exception e) {
throw new IllegalStateException("Fail to export sonar-runner properties", e);
-
- } finally {
- IOUtils.closeQuietly(output);
}
}
private void deleteTempFiles(ForkCommand forkCommand) {
- FileUtils.deleteQuietly(forkCommand.jarFile);
- FileUtils.deleteQuietly(forkCommand.propertiesFile);
+ Utils.deleteQuietly(forkCommand.jarFile);
+ Utils.deleteQuietly(forkCommand.propertiesFile);
}
private void fork(ForkCommand forkCommand) {
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java
index c4013f6..b4e06bc 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java
@@ -19,7 +19,6 @@
*/
package org.sonar.runner.api;
-import org.apache.commons.io.IOUtils;
import org.sonar.runner.impl.InternalProperties;
import javax.annotation.Nullable;
@@ -102,14 +101,10 @@ public abstract class Runner<T extends Runner> {
}
private void writeProperties(File outputFile) {
- OutputStream output = null;
- try {
- output = new FileOutputStream(outputFile);
+ try (OutputStream output = new FileOutputStream(outputFile)) {
properties().store(output, "Generated by sonar-runner");
} catch (Exception e) {
throw new IllegalStateException("Fail to export sonar-runner properties", e);
- } finally {
- IOUtils.closeQuietly(output);
}
}
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java
index d5e8d58..a4286c1 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java
@@ -19,9 +19,19 @@
*/
package org.sonar.runner.api;
+import javax.annotation.Nullable;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Properties;
+import java.nio.file.attribute.*;
class Utils {
private Utils() {
@@ -33,7 +43,7 @@ class Utils {
*/
static String join(String[] array, String delimiter) {
StringBuilder sb = new StringBuilder();
- Iterator it = Arrays.asList(array).iterator();
+ Iterator<String> it = Arrays.asList(array).iterator();
while (it.hasNext()) {
sb.append(it.next());
if (!it.hasNext()) {
@@ -48,4 +58,49 @@ class Utils {
Object task = props.get(RunnerProperties.TASK);
return task == null || ScanProperties.SCAN_TASK.equals(task);
}
+
+ public static void deleteQuietly(File f) {
+ try {
+ Files.walkFileTree(f.toPath(), new DeleteFileVisitor());
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+
+ private static class DeleteFileVisitor extends SimpleFileVisitor<Path> {
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+ Files.delete(file);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
+ Files.delete(file);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
+ if (exc == null) {
+ Files.delete(dir);
+ return FileVisitResult.CONTINUE;
+ } else {
+ // directory iteration failed; propagate exception
+ throw exc;
+ }
+ }
+ }
+
+ public static void closeQuietly(@Nullable Closeable c) {
+ if (c == null) {
+ return;
+ }
+
+ try {
+ c.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
}
diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/CommandExecutorTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/CommandExecutorTest.java
index 88353ce..c585acf 100644
--- a/sonar-runner-api/src/test/java/org/sonar/runner/api/CommandExecutorTest.java
+++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/CommandExecutorTest.java
@@ -19,7 +19,6 @@
*/
package org.sonar.runner.api;
-import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -29,6 +28,7 @@ import org.junit.rules.TestName;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.fail;
@@ -122,7 +122,7 @@ public class CommandExecutorTest {
assertThat(exitCode).isEqualTo(0);
File logFile = new File(workDir, "echo.log");
assertThat(logFile).exists();
- String log = FileUtils.readFileToString(logFile);
+ String log = new String(Files.readAllBytes(logFile.toPath()));
assertThat(log).contains(workDir.getAbsolutePath());
assertThat(log).contains("Parameter: 1");
assertThat(log).contains("Environment variable: 2");
diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/DirsTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/DirsTest.java
index a08d169..8313760 100644
--- a/sonar-runner-api/src/test/java/org/sonar/runner/api/DirsTest.java
+++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/DirsTest.java
@@ -19,7 +19,6 @@
*/
package org.sonar.runner.api;
-import org.apache.commons.io.FilenameUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -54,7 +53,8 @@ public class DirsTest {
File workDir = new File(runner.property("sonar.working.directory", null));
assertThat(workDir).isNotNull();
- assertThat(FilenameUtils.separatorsToUnix(workDir.getCanonicalPath())).contains("generated/reports");
+ //separators from windows to unix
+ assertThat(workDir.getCanonicalPath().replace("\\", "/")).contains("generated/reports");
}
@Test
diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java
index 451ad3d..a56030d 100644
--- a/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java
+++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java
@@ -21,16 +21,24 @@ package org.sonar.runner.api;
import org.junit.Test;
+import java.io.Closeable;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Properties;
+import static org.mockito.Mockito.verify;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.doThrow;
import static org.fest.assertions.Assertions.assertThat;
public class UtilsTest {
@Test
public void should_join_strings() {
- assertThat(Utils.join(new String[]{}, ",")).isEqualTo("");
- assertThat(Utils.join(new String[]{"foo"}, ",")).isEqualTo("foo");
- assertThat(Utils.join(new String[]{"foo", "bar"}, ",")).isEqualTo("foo,bar");
+ assertThat(Utils.join(new String[] {}, ",")).isEqualTo("");
+ assertThat(Utils.join(new String[] {"foo"}, ",")).isEqualTo("foo");
+ assertThat(Utils.join(new String[] {"foo", "bar"}, ",")).isEqualTo("foo,bar");
}
@Test
@@ -48,4 +56,44 @@ public class UtilsTest {
props.setProperty("sonar.task", "views");
assertThat(Utils.taskRequiresProject(props)).isFalse();
}
+
+ @Test
+ public void close_quietly() throws IOException {
+ Closeable c = mock(Closeable.class);
+ doThrow(IOException.class).when(c).close();
+ Utils.closeQuietly(c);
+ verify(c).close();
+ }
+
+ @Test
+ public void close_quietly_null() throws IOException {
+ Utils.closeQuietly(null);
+ }
+
+ @Test
+ public void delete_non_empty_directory() throws IOException {
+ /*-
+ * Create test structure:
+ * tmp
+ * |-folder1
+ * |- file1
+ * |- folder2
+ * |- file2
+ */
+ Path tmpDir = Files.createTempDirectory("junit");
+ Path folder1 = tmpDir.resolve("folder1");
+ Files.createDirectories(folder1);
+ Path file1 = folder1.resolve("file1");
+ Files.write(file1, "test1".getBytes());
+
+ Path folder2 = folder1.resolve("folder2");
+ Files.createDirectories(folder2);
+ Path file2 = folder1.resolve("file2");
+ Files.write(file2, "test2".getBytes());
+
+ // delete it
+ assertThat(tmpDir.toFile()).exists();
+ Utils.deleteQuietly(tmpDir.toFile());
+ assertThat(tmpDir.toFile()).doesNotExist();
+ }
}