]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
Delete the generated sonar-runner-batch.jar in temp dir
authorSimonBrandhof <simon.brandhof@gmail.com>
Mon, 8 Apr 2013 08:46:01 +0000 (10:46 +0200)
committerSimonBrandhof <simon.brandhof@gmail.com>
Mon, 8 Apr 2013 08:46:01 +0000 (10:46 +0200)
sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java
sonar-runner-api/src/test/java/org/sonar/runner/api/OsTest.java
sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java
sonar-runner-batch/src/main/java/org/sonar/runner/batch/Utils.java
sonar-runner-dist/src/main/java/org/sonar/runner/Main.java
sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java
sonar-runner-impl/src/main/java/org/sonar/runner/impl/TempCleaning.java [new file with mode: 0644]
sonar-runner-impl/src/test/java/org/sonar/runner/impl/BatchLauncherTest.java
sonar-runner-impl/src/test/java/org/sonar/runner/impl/TempCleaningTest.java [new file with mode: 0644]

index b1ab426d6cf67dfd7ba65d6ac4cc711775a86f5a..db479cae6824829898c3fb60b7a5ae44da9b5985 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.runner.api;
 
 import org.apache.commons.io.FileUtils;
+import org.sonar.runner.impl.Logs;
 
 import java.io.File;
 
@@ -55,6 +56,7 @@ class Dirs {
     }
     FileUtils.deleteQuietly(workDir);
     runner.setProperty(RunnerProperties.WORK_DIR, workDir.getAbsolutePath());
+    Logs.info("Work directory: " + workDir.getAbsolutePath());
   }
 
   /**
index 7792d535f483c5070782bfa6da014ea826d7b2e5..d43ab29554c1a36b2e7650a03bd20648c1d3ee5d 100644 (file)
@@ -40,6 +40,7 @@ public class OsTest {
 
   @Test
   public void testUsedJavaExe() throws Exception {
+    System.out.println(System.getProperty("java.io.tmpdir"));
     File javaExe = new Os().thisJavaExe();
     assertThat(javaExe).isNotNull().isFile().exists();
     assertThat(javaExe.getName()).contains("java");
index a0f557b7982a9369cfb443a02374b5a5f9fd8f8c..227e5774bf254da76cce0e93c7e9962acea41691 100644 (file)
@@ -46,7 +46,7 @@ import java.util.Properties;
  *
  * @since 1.5
  */
-public final class ProjectReactorBuilder {
+class ProjectReactorBuilder {
 
   private static final Logger LOG = LoggerFactory.getLogger(ProjectReactorBuilder.class);
 
@@ -113,11 +113,11 @@ public final class ProjectReactorBuilder {
   private Properties properties;
   private File rootProjectWorkDir;
 
-  public ProjectReactorBuilder(Properties properties) {
+  ProjectReactorBuilder(Properties properties) {
     this.properties = properties;
   }
 
-  public ProjectReactor build() {
+  ProjectReactor build() {
     ProjectDefinition rootProject = defineProject(properties, null);
     rootProjectWorkDir = rootProject.getWorkDir();
     defineChildren(rootProject);
index 99d8f37a79d59757e07a4137ba0b63f42700230b..8bf39f9b3312a4dc20a205085c81195a4b42121e 100644 (file)
@@ -38,7 +38,7 @@ class Utils {
    * This works even if they are separated by whitespace characters (space char, EOL, ...)
    *
    */
-  public static String[] getListFromProperty(Properties properties, String key) {
+  static String[] getListFromProperty(Properties properties, String key) {
     return StringUtils.stripAll(StringUtils.split(properties.getProperty(key, ""), ','));
   }
 
index 57a16230bb73988e334b35d0a15a396a2bf7ba9e..d8fb4a5074a7e7d53fcb70ca74fe8783bdbfe0f5 100644 (file)
@@ -68,7 +68,6 @@ public class Main {
         Logs.info("Error stacktraces are turned on.");
       }
       runnerFactory.create(conf.properties()).execute();
-      // Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath());
 
     } catch (Exception e) {
       displayExecutionResult(stats, "FAILURE");
index 6a7a20743fc364a2abf02daebc306befdfd6ebb3..51b917a4c7d609610c2022f68877567229eba007 100644 (file)
@@ -29,16 +29,18 @@ import java.util.Properties;
 
 public class BatchLauncher {
   final String isolatedLauncherClass;
+  private final TempCleaning tempCleaning;
 
   /**
    * For unit tests
    */
-  BatchLauncher(String isolatedLauncherClass) {
+  BatchLauncher(String isolatedLauncherClass, TempCleaning tempCleaning) {
     this.isolatedLauncherClass = isolatedLauncherClass;
+    this.tempCleaning = tempCleaning;
   }
 
   public BatchLauncher() {
-    this.isolatedLauncherClass = "org.sonar.runner.batch.IsolatedLauncher";
+    this("org.sonar.runner.batch.IsolatedLauncher", new TempCleaning());
   }
 
   public void execute(Properties props, List<Object> extensions) {
@@ -49,7 +51,7 @@ public class BatchLauncher {
   }
 
   /**
-   * @return the {@link IsolatedLauncher} instance for unit tests
+   * @return the {@link org.sonar.runner.batch.IsolatedLauncher} instance for unit tests
    */
   Object doExecute(final JarDownloader jarDownloader, final Properties props, final List<Object> extensions) {
     Object launcher = AccessController.doPrivileged(new PrivilegedAction<Object>() {
@@ -58,7 +60,9 @@ public class BatchLauncher {
         String unmaskedPackages = props.getProperty(InternalProperties.RUNNER_UNMASKED_PACKAGES, "");
         IsolatedClassloader classloader = new IsolatedClassloader(getClass().getClassLoader(), unmaskedPackages.split(":"));
         classloader.addFiles(jarFiles);
-        return delegateExecution(classloader, props, extensions);
+        Object launcher = delegateExecution(classloader, props, extensions);
+        tempCleaning.clean();
+        return launcher;
       }
 
       private Object delegateExecution(IsolatedClassloader classloader, Properties properties, List<Object> extensions) {
diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/TempCleaning.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/TempCleaning.java
new file mode 100644 (file)
index 0000000..813f4bb
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Sonar Runner - Implementation
+ * 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.impl;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.filefilter.AgeFileFilter;
+import org.apache.commons.io.filefilter.AndFileFilter;
+import org.apache.commons.io.filefilter.PrefixFileFilter;
+
+import java.io.File;
+import java.util.Collection;
+
+/**
+ * The file sonar-runner-batch.jar is locked by the classloader on Windows and can't be dropped at the end of the execution.
+ * See {@link BatchLauncher}
+ */
+class TempCleaning {
+  static final int ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;
+
+  final File tempDir;
+
+  TempCleaning() {
+    this(new File(System.getProperty("java.io.tmpdir")));
+  }
+
+  /**
+   * For unit tests
+   */
+  TempCleaning(File tempDir) {
+    this.tempDir = tempDir;
+  }
+
+  void clean() {
+    long cutoff = System.currentTimeMillis() - ONE_DAY_IN_MILLISECONDS;
+    Collection<File> files = FileUtils.listFiles(tempDir, new AndFileFilter(
+        new PrefixFileFilter("sonar-runner-batch"),
+        new AgeFileFilter(cutoff)
+    ), null);
+
+    for (File file : files) {
+      FileUtils.deleteQuietly(file);
+    }
+  }
+}
index 2d8f2e784e5a506a4edb8083d41417e48a4caa13..b914a395c2810810f6bd4caeb10481c7f08e8319 100644 (file)
@@ -42,7 +42,8 @@ public class BatchLauncherTest {
 
   @Test
   public void should_download_jars_and_execute_batch() {
-    BatchLauncher launcher = new BatchLauncher(FakeIsolatedLauncher.class.getName());
+    TempCleaning tempCleaning = mock(TempCleaning.class);
+    BatchLauncher launcher = new BatchLauncher(FakeIsolatedLauncher.class.getName(), tempCleaning);
     Properties props = new Properties();
     props.put("foo", "bar");
 
@@ -54,11 +55,12 @@ public class BatchLauncherTest {
     assertThat(isolatedLauncher.props.get("foo")).isEqualTo("bar");
     assertThat(isolatedLauncher.extensions).isSameAs(extensions);
     verify(jarDownloader).download();
+    verify(tempCleaning).clean();
   }
 
   @Test
   public void should_use_isolated_classloader() {
-    BatchLauncher launcher = new BatchLauncher(FakeIsolatedLauncher.class.getName());
+    BatchLauncher launcher = new BatchLauncher(FakeIsolatedLauncher.class.getName(), mock(TempCleaning.class));
     Properties props = new Properties();
 
     // The current classloader in not available -> fail to load FakeIsolatedLauncher
diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/TempCleaningTest.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/TempCleaningTest.java
new file mode 100644 (file)
index 0000000..25d5b82
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Sonar Runner - Implementation
+ * 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.impl;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class TempCleaningTest {
+
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  @Test
+  public void should_clean_jvm_tmp_dir( ){
+    TempCleaning cleaning = new TempCleaning();
+    assertThat(cleaning.tempDir).isDirectory().exists();
+  }
+
+  @Test
+  public void should_clean() throws Exception {
+    File dir = temp.newFolder();
+    File oldBatch = new File(dir, "sonar-runner-batch656.jar");
+    FileUtils.write(oldBatch, "foo");
+    oldBatch.setLastModified(System.currentTimeMillis() - 3 * TempCleaning.ONE_DAY_IN_MILLISECONDS);
+
+    File youngBatch = new File(dir, "sonar-runner-batch123.jar");
+    FileUtils.write(youngBatch, "foo");
+
+    File doNotDelete = new File(dir, "jacoco.txt");
+    FileUtils.write(doNotDelete, "foo");
+
+    assertThat(oldBatch).exists();
+    assertThat(youngBatch).exists();
+    assertThat(doNotDelete).exists();
+    new TempCleaning(dir).clean();
+
+    assertThat(oldBatch).doesNotExist();
+    assertThat(youngBatch).exists();
+    assertThat(doNotDelete).exists();
+  }
+}