|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.jersey; |
| 18 | + |
| 19 | +import javax.ws.rs.GET; |
| 20 | +import javax.ws.rs.Path; |
| 21 | + |
| 22 | +import org.apache.catalina.Context; |
| 23 | +import org.apache.catalina.Wrapper; |
| 24 | +import org.glassfish.jersey.server.ResourceConfig; |
| 25 | +import org.glassfish.jersey.servlet.ServletContainer; |
| 26 | +import org.junit.ClassRule; |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.runner.RunWith; |
| 29 | + |
| 30 | +import org.springframework.beans.factory.annotation.Value; |
| 31 | +import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; |
| 32 | +import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationServletContainerTests.Application; |
| 33 | +import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration; |
| 34 | +import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; |
| 35 | +import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; |
| 36 | +import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; |
| 37 | +import org.springframework.boot.test.IntegrationTest; |
| 38 | +import org.springframework.boot.test.OutputCapture; |
| 39 | +import org.springframework.boot.test.SpringApplicationConfiguration; |
| 40 | +import org.springframework.context.annotation.Bean; |
| 41 | +import org.springframework.context.annotation.Configuration; |
| 42 | +import org.springframework.context.annotation.Import; |
| 43 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 44 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 45 | + |
| 46 | +import static org.hamcrest.Matchers.containsString; |
| 47 | +import static org.junit.Assert.assertThat; |
| 48 | + |
| 49 | +/** |
| 50 | + * Tests that verify the behavior when deployed to a Servlet container where Jersey may |
| 51 | + * have already initialized itself. |
| 52 | + * |
| 53 | + * @author Andy Wilkinson |
| 54 | + */ |
| 55 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 56 | +@SpringApplicationConfiguration(Application.class) |
| 57 | +@IntegrationTest("server.port=0") |
| 58 | +@WebAppConfiguration |
| 59 | +public class JerseyAutoConfigurationServletContainerTests { |
| 60 | + |
| 61 | + @ClassRule |
| 62 | + public static OutputCapture output = new OutputCapture(); |
| 63 | + |
| 64 | + @Value("${local.server.port}") |
| 65 | + private int port; |
| 66 | + |
| 67 | + @Test |
| 68 | + public void existingJerseyServletIsAmended() { |
| 69 | + assertThat(output.toString(), |
| 70 | + containsString("Configuring existing registration for Jersey servlet")); |
| 71 | + assertThat(output.toString(), containsString( |
| 72 | + "Servlet " + Application.class.getName() + " was not registered")); |
| 73 | + } |
| 74 | + |
| 75 | + @ImportAutoConfiguration({ EmbeddedServletContainerAutoConfiguration.class, |
| 76 | + ServerPropertiesAutoConfiguration.class, JerseyAutoConfiguration.class, |
| 77 | + PropertyPlaceholderAutoConfiguration.class }) |
| 78 | + @Import(ContainerConfiguration.class) |
| 79 | + @Path("/hello") |
| 80 | + public static class Application extends ResourceConfig { |
| 81 | + |
| 82 | + @Value("${message:World}") |
| 83 | + private String msg; |
| 84 | + |
| 85 | + public Application() { |
| 86 | + register(Application.class); |
| 87 | + } |
| 88 | + |
| 89 | + @GET |
| 90 | + public String message() { |
| 91 | + return "Hello " + this.msg; |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + @Configuration |
| 97 | + public static class ContainerConfiguration { |
| 98 | + |
| 99 | + @Bean |
| 100 | + public TomcatEmbeddedServletContainerFactory tomcat() { |
| 101 | + return new TomcatEmbeddedServletContainerFactory() { |
| 102 | + |
| 103 | + @Override |
| 104 | + protected void postProcessContext(Context context) { |
| 105 | + Wrapper jerseyServlet = context.createWrapper(); |
| 106 | + String servletName = Application.class.getName(); |
| 107 | + jerseyServlet.setName(servletName); |
| 108 | + jerseyServlet.setServletClass(ServletContainer.class.getName()); |
| 109 | + jerseyServlet.setServlet(new ServletContainer()); |
| 110 | + jerseyServlet.setOverridable(false); |
| 111 | + context.addChild(jerseyServlet); |
| 112 | + context.addServletMapping("/*", servletName); |
| 113 | + } |
| 114 | + |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments