improved the diagnosability when the explode() method fails

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11973 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke 2008-09-02 22:24:49 +00:00
parent 213a95c1a1
commit 2169dce3ca
2 changed files with 58 additions and 49 deletions

View File

@ -161,7 +161,7 @@ public abstract class HudsonTestCase extends TestCase {
protected ServletContext createWebServer() throws Exception {
server = new Server();
WebAppContext context = new WebAppContext(WarExploder.EXPLODE_DIR.getPath(), contextPath);
WebAppContext context = new WebAppContext(WarExploder.getExplodedDir().getPath(), contextPath);
context.setClassLoader(getClass().getClassLoader());
context.setConfigurations(new Configuration[]{new WebXmlConfiguration(),new NoListenerConfiguration()});
server.setHandler(context);

View File

@ -4,7 +4,6 @@ import hudson.remoting.Which;
import hudson.FilePath;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.net.URL;
@ -14,57 +13,67 @@ import java.net.URL;
* @author Kohsuke Kawaguchi
*/
final class WarExploder {
public static final File EXPLODE_DIR = explode();
public static File getExplodedDir() throws Exception {
// rethrow an exception every time someone tries to do this, so that when explode()
// fails, you can see the cause no matter which test case you look at.
// see http://www.nabble.com/Failing-tests-in-test-harness-module-on-hudson.ramfelt.se-td19258722.html
if(FAILURE !=null) throw new Exception("Failed to initialize exploded war", FAILURE);
return EXPLODE_DIR;
}
private static File EXPLODE_DIR;
private static Exception FAILURE;
static {
try {
EXPLODE_DIR = explode();
} catch (Exception e) {
FAILURE = e;
}
}
/**
* Explodes hudson.war, if necesasry, and returns its root dir.
*/
private static File explode() {
try {
// are we in the hudson main workspace? If so, pick up hudson/main/war/resources
// this saves the effort of packaging a war file and makes the debug cycle faster
File d = new File(".").getAbsoluteFile();
for( ; d!=null; d=d.getParentFile()) {
if(!d.getName().equals("main")) continue;
File p = d.getParentFile();
if(p!=null && p.getName().equals("hudson"))
break;
}
if(d!=null) {
File dir = new File(d,"war/target/hudson");
if(dir.exists()) {
System.out.println("Using hudson.war resources from "+dir);
return dir;
}
}
// locate hudson.war
URL winstone = WarExploder.class.getResource("/winstone.jar");
if(winstone==null)
// impossible, since the test harness pulls in hudson.war
throw new AssertionError("hudson.war is not in the classpath.");
File war = Which.jarFile(Class.forName("executable.Executable"));
File explodeDir = new File("./target/hudson-for-test");
File timestamp = new File(explodeDir,".timestamp");
if(!timestamp.exists() || (timestamp.lastModified()!=war.lastModified())) {
System.out.println("Exploding hudson.war at "+war);
new FilePath(explodeDir).deleteRecursive();
new FilePath(war).unzip(new FilePath(explodeDir));
new FileOutputStream(timestamp).close();
timestamp.setLastModified(war.lastModified());
} else {
System.out.println("Picking up existing exploded hudson.war at "+explodeDir.getAbsolutePath());
}
return explodeDir;
} catch (IOException e) {
throw new Error(e);
} catch (InterruptedException e) {
throw new Error(e);
} catch (ClassNotFoundException e) {
throw new Error(e);
private static File explode() throws Exception {
// are we in the hudson main workspace? If so, pick up hudson/main/war/resources
// this saves the effort of packaging a war file and makes the debug cycle faster
File d = new File(".").getAbsoluteFile();
for( ; d!=null; d=d.getParentFile()) {
if(!d.getName().equals("main")) continue;
File p = d.getParentFile();
if(p!=null && p.getName().equals("hudson"))
break;
}
if(d!=null) {
File dir = new File(d,"war/target/hudson");
if(dir.exists()) {
System.out.println("Using hudson.war resources from "+dir);
return dir;
}
}
// locate hudson.war
URL winstone = WarExploder.class.getResource("/winstone.jar");
if(winstone==null)
// impossible, since the test harness pulls in hudson.war
throw new AssertionError("hudson.war is not in the classpath.");
File war = Which.jarFile(Class.forName("executable.Executable"));
File explodeDir = new File("./target/hudson-for-test");
File timestamp = new File(explodeDir,".timestamp");
if(!timestamp.exists() || (timestamp.lastModified()!=war.lastModified())) {
System.out.println("Exploding hudson.war at "+war);
new FilePath(explodeDir).deleteRecursive();
new FilePath(war).unzip(new FilePath(explodeDir));
new FileOutputStream(timestamp).close();
timestamp.setLastModified(war.lastModified());
} else {
System.out.println("Picking up existing exploded hudson.war at "+explodeDir.getAbsolutePath());
}
return explodeDir;
}
}