Prevent Jetty tests from polluting URL’s URLStreamHandlerFactory

The JVM only allows URL.setURLStreamHandlerFactory to be called once.
This is problematic as the JSP support in embedded Tomcat and embedded
Jetty both call this method.

This commit uses reflection to null out URL’s factory field before and
after the embedded Jetty tests have run. This ensures that they can
run successfully if Tomcat has already installed its factory and that
Tomcat-related tests can also run afterwards.

See gh-5290
This commit is contained in:
Andy Wilkinson 2016-06-15 16:05:55 +01:00
parent 75032c46dc
commit d341499c6b
1 changed files with 12 additions and 2 deletions

View File

@ -17,6 +17,7 @@
package org.springframework.boot.context.embedded.jetty;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@ -37,6 +38,8 @@ import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.InOrder;
@ -46,6 +49,7 @@ import org.springframework.boot.context.embedded.Compression;
import org.springframework.boot.context.embedded.Ssl;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.http.HttpHeaders;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.anyObject;
@ -64,6 +68,12 @@ import static org.mockito.Mockito.mock;
public class JettyEmbeddedServletContainerFactoryTests
extends AbstractEmbeddedServletContainerFactoryTests {
@BeforeClass
@AfterClass
public static void uninstallUrlStreamHandlerFactory() {
ReflectionTestUtils.setField(URL.class, "factory", null);
}
@Override
protected JettyEmbeddedServletContainerFactory getFactory() {
return new JettyEmbeddedServletContainerFactory(0);
@ -136,8 +146,8 @@ public class JettyEmbeddedServletContainerFactoryTests
.getConnectionFactory(SslConnectionFactory.class);
assertThat(connectionFactory.getSslContextFactory().getIncludeCipherSuites())
.containsExactly("ALPHA", "BRAVO", "CHARLIE");
assertThat(connectionFactory.getSslContextFactory()
.getExcludeCipherSuites()).isEmpty();
assertThat(connectionFactory.getSslContextFactory().getExcludeCipherSuites())
.isEmpty();
}
@Override