diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java index 42fe5eb83d7a78651e47b1bde8d4c5dc0c6028a2..066ce52cea488a2dbc58ee8ed70bf54183759ffc 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -101,6 +101,7 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; +import java.net.URLConnection; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; @@ -276,6 +277,8 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { */ protected File explodedWarDir; + private boolean origDefaultUseCache = true; + protected HudsonTestCase(String name) { super(name); } @@ -298,6 +301,18 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { @Override protected void setUp() throws Exception { + if(Functions.isWindows()) { + // JENKINS-4409. + // URLConnection caches handles to jar files by default, + // and it prevents delete temporary directories on Windows. + // Disables caching here. + // Though defaultUseCache is a static field, + // its setter and getter are provided as instance methods. + URLConnection aConnection = new File(".").toURI().toURL().openConnection(); + origDefaultUseCache = aConnection.getDefaultUseCaches(); + aConnection.setDefaultUseCaches(false); + } + env.pin(); recipe(); for (Runner r : recipes) { @@ -425,6 +440,12 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { // at some later point, leading to possible file descriptor overflow. So encourage GC now. // see http://bugs.sun.com/view_bug.do?bug_id=4950148 System.gc(); + + // restore defaultUseCache + if(Functions.isWindows()) { + URLConnection aConnection = new File(".").toURI().toURL().openConnection(); + aConnection.setDefaultUseCaches(origDefaultUseCache); + } } } diff --git a/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java b/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java index 3c2eb131500f0dde0882c19a544a4304c749901e..b9707a2f63d7a145db3d2b70725efc239aa56e17 100644 --- a/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java +++ b/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java @@ -128,6 +128,7 @@ import java.net.SocketTimeoutException; import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; +import java.net.URLConnection; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; @@ -303,6 +304,8 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction { public JenkinsComputerConnectorTester computerConnectorTester = new JenkinsComputerConnectorTester(this); + private boolean origDefaultUseCache = true; + public Jenkins getInstance() { return jenkins; } @@ -312,6 +315,18 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction { * @throws Throwable if setup fails (which will disable {@code after} */ public void before() throws Throwable { + if(Functions.isWindows()) { + // JENKINS-4409. + // URLConnection caches handles to jar files by default, + // and it prevents delete temporary directories on Windows. + // Disables caching here. + // Though defaultUseCache is a static field, + // its setter and getter are provided as instance methods. + URLConnection aConnection = new File(".").toURI().toURL().openConnection(); + origDefaultUseCache = aConnection.getDefaultUseCaches(); + aConnection.setDefaultUseCaches(false); + } + // Not ideal (https://github.com/junit-team/junit/issues/116) but basically works. if (Boolean.getBoolean("ignore.random.failures")) { RandomlyFails rf = testDescription.getAnnotation(RandomlyFails.class); @@ -458,6 +473,12 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction { // see http://bugs.sun.com/view_bug.do?bug_id=4950148 // TODO use URLClassLoader.close() in Java 7 System.gc(); + + // restore defaultUseCache + if(Functions.isWindows()) { + URLConnection aConnection = new File(".").toURI().toURL().openConnection(); + aConnection.setDefaultUseCaches(origDefaultUseCache); + } } }