From 76a2db8ded403cb8c927a64efe005d689382ed7e Mon Sep 17 00:00:00 2001 From: kohsuke Date: Sat, 20 Dec 2008 01:37:21 +0000 Subject: [PATCH] [FIXED HUDSON-2753] I don't think it's wise to have one class that does two different things, so I adjusted how it's written. But this should work, too. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@13742 71c3de6d-444a-0410-be80-ed276b4c234a --- .../jvnet/hudson/test/HudsonHomeLoader.java | 38 ++++++++++++------- .../org/jvnet/hudson/test/HudsonTestCase.java | 2 +- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonHomeLoader.java b/test/src/main/java/org/jvnet/hudson/test/HudsonHomeLoader.java index 922d2261d7..6d3639e071 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonHomeLoader.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonHomeLoader.java @@ -1,11 +1,13 @@ package org.jvnet.hudson.test; import hudson.FilePath; +import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; -import java.net.URL; import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; /** * Controls how a {@link HudsonTestCase} initializes HUDSON_HOME. @@ -13,7 +15,7 @@ import java.lang.reflect.Method; * @author Kohsuke Kawaguchi */ public interface HudsonHomeLoader { - /** + /** * Returns a directory to be used as HUDSON_HOME * * @throws Exception @@ -34,14 +36,13 @@ public interface HudsonHomeLoader { * Allocates a new directory by copying from an existing directory, or unzipping from a zip file. */ public static final class CopyExisting implements HudsonHomeLoader { - private final File source; + private final URL source; /** * Either a zip file or a directory that contains the home image. */ - public CopyExisting(File source) { - if(source==null) throw new IllegalArgumentException(); - this.source = source; + public CopyExisting(File source) throws MalformedURLException { + this(source.toURL()); } /** @@ -52,18 +53,27 @@ public interface HudsonHomeLoader { * Only file URL is supported. */ public CopyExisting(URL source) { - if(!source.getProtocol().equals("file")) - throw new UnsupportedOperationException("Unsupported protocol: "+source); - this.source = new File(source.getPath()); + this.source = source; } public File allocate() throws Exception { File target = NEW.allocate(); - if(source.isDirectory()) - new FilePath(source).copyRecursiveTo("**/*",new FilePath(target)); - else - if(source.getName().endsWith(".zip")) - new FilePath(source).unzip(new FilePath(target)); + if(source.getProtocol().equals("file")) { + File src = new File(source.getPath()); + if(src.isDirectory()) + new FilePath(src).copyRecursiveTo("**/*",new FilePath(target)); + else + if(src.getName().endsWith(".zip")) + new FilePath(src).unzip(new FilePath(target)); + } else { + File tmp = File.createTempFile("hudson","zip"); + try { + FileUtils.copyURLToFile(source,tmp); + new FilePath(tmp).unzip(new FilePath(target)); + } finally { + tmp.delete(); + } + } return target; } } 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 563f15d979..005345432c 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -365,7 +365,7 @@ public abstract class HudsonTestCase extends TestCase { return with(HudsonHomeLoader.NEW); } - public HudsonTestCase withExistingHome(File source) { + public HudsonTestCase withExistingHome(File source) throws Exception { return with(new CopyExisting(source)); }