|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 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.ApplicationPath; |
| 20 | +import javax.ws.rs.GET; |
| 21 | +import javax.ws.rs.Path; |
| 22 | +import javax.ws.rs.core.Application; |
| 23 | + |
| 24 | +import org.glassfish.jersey.server.ResourceConfig; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | + |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; |
| 30 | +import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; |
| 31 | +import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; |
| 32 | +import org.springframework.boot.test.context.SpringBootTest; |
| 33 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 34 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.Configuration; |
| 37 | +import org.springframework.context.annotation.Import; |
| 38 | +import org.springframework.http.HttpStatus; |
| 39 | +import org.springframework.http.ResponseEntity; |
| 40 | +import org.springframework.test.annotation.DirtiesContext; |
| 41 | +import org.springframework.test.context.junit4.SpringRunner; |
| 42 | + |
| 43 | +import static org.assertj.core.api.Assertions.assertThat; |
| 44 | + |
| 45 | +/** |
| 46 | + * Tests for {@link JerseyAutoConfiguration} when using a custom {@link Application}. |
| 47 | + * |
| 48 | + * @author Stephane Nicoll |
| 49 | + */ |
| 50 | +@RunWith(SpringRunner.class) |
| 51 | +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
| 52 | +@DirtiesContext |
| 53 | +public class JerseyAutoConfigurationCustomApplicationTests { |
| 54 | + |
| 55 | + |
| 56 | + @Autowired |
| 57 | + private TestRestTemplate restTemplate; |
| 58 | + |
| 59 | + @Test |
| 60 | + public void contextLoads() { |
| 61 | + ResponseEntity<String> entity = this.restTemplate.getForEntity("/test/hello", |
| 62 | + String.class); |
| 63 | + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 64 | + } |
| 65 | + |
| 66 | + @ApplicationPath("/test") |
| 67 | + public static class TestApplication extends Application { |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Path("/hello") |
| 72 | + public static class TestController { |
| 73 | + |
| 74 | + @GET |
| 75 | + public String message() { |
| 76 | + return "Hello World"; |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @Configuration |
| 82 | + @Import({ EmbeddedServletContainerAutoConfiguration.class, |
| 83 | + ServerPropertiesAutoConfiguration.class, JerseyAutoConfiguration.class, |
| 84 | + PropertyPlaceholderAutoConfiguration.class }) |
| 85 | + static class TestConfiguration { |
| 86 | + |
| 87 | + @Configuration |
| 88 | + public class JerseyConfiguration { |
| 89 | + |
| 90 | + @Bean |
| 91 | + public TestApplication testApplication() { |
| 92 | + return new TestApplication(); |
| 93 | + } |
| 94 | + |
| 95 | + @Bean |
| 96 | + public ResourceConfig conf(TestApplication app) { |
| 97 | + ResourceConfig config = ResourceConfig.forApplication(app); |
| 98 | + config.register(TestController.class); |
| 99 | + return config; |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments