Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGerard Davison2015-07-22 05:24:47 -0400
committerGerard Davison2015-07-22 05:43:25 -0400
commitdffd95b3c5f7d6f7400bd9f8d3128f0b6533d5cf (patch)
treeaddc12c08a55ec8b5dabbf81bc9204e1fc151a61
parent214e70fb873435481e425ed0d41be3427a12c33d (diff)
downloadorg.eclipse.hudson.core-dffd95b3c5f7d6f7400bd9f8d3128f0b6533d5cf.tar.gz
org.eclipse.hudson.core-dffd95b3c5f7d6f7400bd9f8d3128f0b6533d5cf.tar.xz
org.eclipse.hudson.core-dffd95b3c5f7d6f7400bd9f8d3128f0b6533d5cf.zip
Fixes to HudsonTestCase to prevent multiple loading and multuple registration of plugin classes during unit tests
also provide basic REST/Jersey client for accessing services. Signed-off-by: Gerard Davison <gerard.davison@oracle.com>
-rw-r--r--hudson-core/src/main/java/hudson/init/InitStrategy.java4
-rw-r--r--hudson-core/src/main/java/hudson/util/PluginServletFilter.java10
-rw-r--r--hudson-test-framework/pom.xml8
-rw-r--r--hudson-test-framework/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java59
4 files changed, 78 insertions, 3 deletions
diff --git a/hudson-core/src/main/java/hudson/init/InitStrategy.java b/hudson-core/src/main/java/hudson/init/InitStrategy.java
index 6b22c36d..c12cc95e 100644
--- a/hudson-core/src/main/java/hudson/init/InitStrategy.java
+++ b/hudson-core/src/main/java/hudson/init/InitStrategy.java
@@ -121,8 +121,8 @@ public class InitStrategy {
}
public boolean accept(File dir, String name) {
- return name.endsWith(extension) // plugin jar file
- || name.endsWith(".hpl"); // linked plugin. for debugging.
+ return name.endsWith(extension); // plugin jar file
+ // result in duplicate plugin loading during testing || name.endsWith(".hpl"); // linked plugin. for debugging.
}
}
}
diff --git a/hudson-core/src/main/java/hudson/util/PluginServletFilter.java b/hudson-core/src/main/java/hudson/util/PluginServletFilter.java
index 228d54db..274da52e 100644
--- a/hudson-core/src/main/java/hudson/util/PluginServletFilter.java
+++ b/hudson-core/src/main/java/hudson/util/PluginServletFilter.java
@@ -74,6 +74,16 @@ public class PluginServletFilter implements Filter, ExtensionPoint {
}
}
+ /**
+ * All the clearing of filters between test, was causing a leakage
+ * breaking all subsequent tests.
+ */
+ public static void clearFilters() throws ServletException {
+ synchronized (LIST) {
+ LIST.clear();
+ }
+ }
+
public void doFilter(ServletRequest request, ServletResponse response, final FilterChain chain) throws IOException, ServletException {
new FilterChain() {
private int position = 0;
diff --git a/hudson-test-framework/pom.xml b/hudson-test-framework/pom.xml
index 229d4bc5..c1c571de 100644
--- a/hudson-test-framework/pom.xml
+++ b/hudson-test-framework/pom.xml
@@ -153,6 +153,14 @@
</exclusion>
</exclusions>
</dependency>
+
+
+ <dependency>
+ <groupId>com.sun.jersey</groupId>
+ <artifactId>jersey-client</artifactId>
+ <version>1.15</version>
+ </dependency>
+
</dependencies>
<build>
diff --git a/hudson-test-framework/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java b/hudson-test-framework/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java
index 541bea33..2fee1f7e 100644
--- a/hudson-test-framework/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java
+++ b/hudson-test-framework/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java
@@ -50,6 +50,7 @@ import hudson.tasks.Maven.MavenInstallation;
import hudson.util.PersistedList;
import hudson.util.ReflectionUtils;
import hudson.util.StreamTaskListener;
+import hudson.util.PluginServletFilter;
import hudson.model.Node.Mode;
@@ -133,11 +134,17 @@ import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.xml.XmlPage;
import com.gargoylesoftware.htmlunit.html.*;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
import hudson.slaves.ComputerListener;
import java.util.concurrent.CountDownLatch;
import hudson.maven.MavenEmbedder;
import hudson.maven.MavenRequest;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.nio.charset.Charset;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
@@ -316,6 +323,7 @@ public abstract class HudsonTestCase extends TestCase implements RootAction {
clients.clear();
} finally {
server.stop();
+
for (LenientRunnable r : tearDowns) {
r.run();
}
@@ -328,6 +336,15 @@ public abstract class HudsonTestCase extends TestCase implements RootAction {
// Force the container bits to reset
SmoothieUtil.reset();
+ // Clear hudson refrence
+
+ hudson = null;
+ server = null;
+
+ // Clear any filters stored in this singlton
+
+ PluginServletFilter.clearFilters();
+
// Hudson creates ClassLoaders for plugins that hold on to file descriptors of its jar files,
// but because there's no explicit dispose method on ClassLoader, they won't get GC-ed until
// at some later point, leading to possible file descriptor overflow. So encourage GC now.
@@ -1295,7 +1312,27 @@ public abstract class HudsonTestCase extends TestCase implements RootAction {
if (shortName == null) {
throw new Error(hpl + " doesn't have the Short-Name attribute");
}
- FileUtils.copyURLToFile(hpl, new File(home, "plugins/" + shortName + ".hpl"));
+ final File targetLocation = new File(home, "plugins/" + shortName + ".hpl");
+ FileUtils.copyURLToFile(hpl, targetLocation);
+
+ // Clear libraries and resource-path to prevent duplicate classes
+ //
+
+ {
+ Manifest copied = null;
+ try (FileInputStream fis = new FileInputStream(targetLocation))
+ {
+ copied = new Manifest(fis);
+ copied.getMainAttributes().putValue("Libraries", "");
+ copied.getMainAttributes().putValue("Resource-Path", "");
+
+ }
+
+ try (FileOutputStream fos = new FileOutputStream(targetLocation))
+ {
+ copied.write(fos);
+ }
+ }
// make dependency plugins available
// TODO: probably better to read POM, but where to read from?
@@ -1435,7 +1472,27 @@ public abstract class HudsonTestCase extends TestCase implements RootAction {
private Object writeReplace() {
throw new AssertionError("HudsonTestCase " + getName() + " is not supposed to be serialized");
}
+
+ /**
+ * @return A resource that allows testing of REST services, at the root
+ * of the application
+ */
+ public WebResource createJaxRSClient() {
+
+ Client client = Client.create(customizeJaxRSClient(new DefaultClientConfig()));
+ return client.resource("http://localhost:" + localPort + contextPath + "/");
+ }
+
+ /**
+ * All sub classes to customize the client configuration
+ * @param client
+ */
+ protected ClientConfig customizeJaxRSClient(ClientConfig client) {
+ // To be overridden
+ return client;
+ }
+
/**
* This is to assist Groovy test clients who are incapable of instantiating
* the inner classes properly.

Back to the top