Skip to content

Commit

Permalink
[JENKINS-41196] added alternate bootstrap logic
Browse files Browse the repository at this point in the history
With more servlets and filters initialized from within WebAppMain, test
harness needs to adjust as well.
  • Loading branch information
kohsuke committed Feb 18, 2017
1 parent 32a41a9 commit 75b0b1e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Expand Up @@ -23,30 +23,46 @@
*/
package org.jvnet.hudson.test;

import hudson.WebAppMain;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.webapp.AbstractConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* Kills off {@link ServletContextListener}s loaded from web.xml.
* Initializes Jenkins web app in a differently, instead of the usual {@link ServletContextListener}s.
*
* <p>
* This is so that the harness can create the {@link jenkins.model.Jenkins} object.
* with the home directory of our choice.
*
* @author Kohsuke Kawaguchi
*/
final class NoListenerConfiguration extends AbstractLifeCycle {
final class AltWebXmlConfiguration extends AbstractLifeCycle {
private final WebAppContext context;

NoListenerConfiguration(WebAppContext context) {
AltWebXmlConfiguration(WebAppContext context) {
this.context = context;
}

@Override
protected void doStart() throws Exception {
// All the ServletContextListeners from web.xml are added to context as event listeners by the WebXmlConfiguration class.
// By resetting this, we can prevent ServletContextListeners from getting invoked.
context.setEventListeners(null);

try {
// Now that we don't run the regular bootstrap code (WebAppMain), we need to compensate for that differently
// see JENKINS-41196
WebAppMain wam = new WebAppMain();
Method m = wam.getClass().getMethod("initForTest", ServletContext.class);
m.invoke(wam,context.getServletContext());
} catch (NoSuchMethodException e) {
// probably running with older Jenkins core, so this is OK
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jvnet/hudson/test/HudsonTestCase.java
Expand Up @@ -525,7 +525,7 @@ public Thread newThread(Runnable r) {
context.setResourceBase(explodedWarDir.getPath());
context.setClassLoader(getClass().getClassLoader());
context.setConfigurations(new Configuration[]{new WebXmlConfiguration()});
context.addBean(new NoListenerConfiguration(context));
context.addBean(new AltWebXmlConfiguration(context));
server.setHandler(context);
context.setMimeTypes(MIME_TYPES);
context.getSecurityHandler().setLoginService(configureUserRealm());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jvnet/hudson/test/JenkinsRule.java
Expand Up @@ -632,7 +632,7 @@ public Thread newThread(Runnable r) {
WebAppContext context = new WebAppContext(WarExploder.getExplodedDir().getPath(), contextPath);
context.setClassLoader(getClass().getClassLoader());
context.setConfigurations(new Configuration[]{new WebXmlConfiguration()});
context.addBean(new NoListenerConfiguration(context));
context.addBean(new AltWebXmlConfiguration(context));
server.setHandler(context);
context.setMimeTypes(MIME_TYPES);
context.getSecurityHandler().setLoginService(configureUserRealm());
Expand Down

0 comments on commit 75b0b1e

Please sign in to comment.