Skip to content

Commit c9ad356

Browse files
committed
Merge branch '2.2.x'
Closes gh-19562
2 parents 6944e52 + 82dc7bc commit c9ad356

File tree

2 files changed

+41
-52
lines changed

2 files changed

+41
-52
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hateoas/HypermediaAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
3737
import org.springframework.plugin.core.Plugin;
3838
import org.springframework.web.bind.annotation.RequestMapping;
39+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
3940

4041
/**
4142
* {@link EnableAutoConfiguration Auto-configuration} for Spring HATEOAS's
@@ -47,7 +48,7 @@
4748
* @since 1.1.0
4849
*/
4950
@Configuration(proxyBeanMethods = false)
50-
@ConditionalOnClass({ EntityModel.class, RequestMapping.class, Plugin.class })
51+
@ConditionalOnClass({ EntityModel.class, RequestMapping.class, RequestMappingHandlerAdapter.class, Plugin.class })
5152
@ConditionalOnWebApplication
5253
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, JacksonAutoConfiguration.class,
5354
HttpMessageConvertersAutoConfiguration.class, RepositoryRestMvcAutoConfiguration.class })

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hateoas/HypermediaAutoConfigurationTests.java

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818

1919
import java.util.Optional;
2020

21-
import org.junit.jupiter.api.AfterEach;
2221
import org.junit.jupiter.api.Test;
2322

2423
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2524
import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration.HypermediaConfiguration;
2625
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
2726
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
2827
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
29-
import org.springframework.boot.test.util.TestPropertyValues;
30-
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
28+
import org.springframework.boot.test.context.FilteredClassLoader;
29+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
3130
import org.springframework.context.annotation.Configuration;
3231
import org.springframework.hateoas.MediaTypes;
3332
import org.springframework.hateoas.client.LinkDiscoverer;
@@ -39,7 +38,6 @@
3938
import org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter;
4039
import org.springframework.http.MediaType;
4140
import org.springframework.http.converter.HttpMessageConverter;
42-
import org.springframework.mock.web.MockServletContext;
4341
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
4442

4543
import static org.assertj.core.api.Assertions.assertThat;
@@ -50,78 +48,68 @@
5048
* @author Roy Clarkson
5149
* @author Oliver Gierke
5250
* @author Andy Wilkinson
51+
* @author Madhura Bhave
5352
*/
5453
class HypermediaAutoConfigurationTests {
5554

56-
private AnnotationConfigServletWebApplicationContext context;
55+
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
56+
.withUserConfiguration(BaseConfig.class);
5757

58-
@AfterEach
59-
void close() {
60-
if (this.context != null) {
61-
this.context.close();
62-
}
58+
@Test
59+
void autoConfigurationWhenSpringMvcNotOnClasspathShouldBackOff() {
60+
this.contextRunner.withClassLoader(new FilteredClassLoader(RequestMappingHandlerAdapter.class))
61+
.run((context) -> assertThat(context.getBeansOfType(HypermediaConfiguration.class)).isEmpty());
6362
}
6463

6564
@Test
6665
void linkDiscoverersCreated() {
67-
this.context = new AnnotationConfigServletWebApplicationContext();
68-
this.context.setServletContext(new MockServletContext());
69-
this.context.register(BaseConfig.class);
70-
this.context.refresh();
71-
LinkDiscoverers discoverers = this.context.getBean(LinkDiscoverers.class);
72-
assertThat(discoverers).isNotNull();
73-
Optional<LinkDiscoverer> discoverer = discoverers.getLinkDiscovererFor(MediaTypes.HAL_JSON);
74-
assertThat(discoverer).containsInstanceOf(HalLinkDiscoverer.class);
66+
this.contextRunner.run((context) -> {
67+
LinkDiscoverers discoverers = context.getBean(LinkDiscoverers.class);
68+
assertThat(discoverers).isNotNull();
69+
Optional<LinkDiscoverer> discoverer = discoverers.getLinkDiscovererFor(MediaTypes.HAL_JSON);
70+
assertThat(discoverer).containsInstanceOf(HalLinkDiscoverer.class);
71+
});
7572
}
7673

7774
@Test
7875
void entityLinksCreated() {
79-
this.context = new AnnotationConfigServletWebApplicationContext();
80-
this.context.setServletContext(new MockServletContext());
81-
this.context.register(BaseConfig.class);
82-
this.context.refresh();
83-
EntityLinks discoverers = this.context.getBean(EntityLinks.class);
84-
assertThat(discoverers).isNotNull();
76+
this.contextRunner.run((context) -> {
77+
EntityLinks discoverers = context.getBean(EntityLinks.class);
78+
assertThat(discoverers).isNotNull();
79+
});
8580
}
8681

8782
@Test
8883
void doesBackOffIfEnableHypermediaSupportIsDeclaredManually() {
89-
this.context = new AnnotationConfigServletWebApplicationContext();
90-
this.context.setServletContext(new MockServletContext());
91-
this.context.register(EnableHypermediaSupportConfig.class, BaseConfig.class);
92-
TestPropertyValues.of("spring.jackson.serialization.INDENT_OUTPUT:true").applyTo(this.context);
93-
this.context.refresh();
94-
assertThat(this.context.getBeansOfType(HypermediaConfiguration.class)).isEmpty();
84+
this.contextRunner.withUserConfiguration(EnableHypermediaSupportConfig.class)
85+
.withPropertyValues("spring.jackson.serialization.INDENT_OUTPUT:true")
86+
.run((context) -> assertThat(context.getBeansOfType(HypermediaConfiguration.class)).isEmpty());
9587
}
9688

9789
@Test
9890
void supportedMediaTypesOfTypeConstrainedConvertersIsCustomized() {
99-
this.context = new AnnotationConfigServletWebApplicationContext();
100-
this.context.setServletContext(new MockServletContext());
101-
this.context.register(BaseConfig.class);
102-
this.context.refresh();
103-
RequestMappingHandlerAdapter handlerAdapter = this.context.getBean(RequestMappingHandlerAdapter.class);
104-
for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) {
105-
if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) {
106-
assertThat(converter.getSupportedMediaTypes()).contains(MediaType.APPLICATION_JSON,
107-
MediaTypes.HAL_JSON);
91+
this.contextRunner.run((context) -> {
92+
RequestMappingHandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
93+
for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) {
94+
if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) {
95+
assertThat(converter.getSupportedMediaTypes()).contains(MediaType.APPLICATION_JSON,
96+
MediaTypes.HAL_JSON);
97+
}
10898
}
109-
}
99+
});
110100
}
111101

112102
@Test
113103
void customizationOfSupportedMediaTypesCanBeDisabled() {
114-
this.context = new AnnotationConfigServletWebApplicationContext();
115-
this.context.setServletContext(new MockServletContext());
116-
this.context.register(BaseConfig.class);
117-
TestPropertyValues.of("spring.hateoas.use-hal-as-default-json-media-type:false").applyTo(this.context);
118-
this.context.refresh();
119-
RequestMappingHandlerAdapter handlerAdapter = this.context.getBean(RequestMappingHandlerAdapter.class);
120-
for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) {
121-
if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) {
122-
assertThat(converter.getSupportedMediaTypes()).containsExactly(MediaTypes.HAL_JSON);
123-
}
124-
}
104+
this.contextRunner.withPropertyValues("spring.hateoas.use-hal-as-default-json-media-type:false")
105+
.run((context) -> {
106+
RequestMappingHandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
107+
for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) {
108+
if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) {
109+
assertThat(converter.getSupportedMediaTypes()).containsExactly(MediaTypes.HAL_JSON);
110+
}
111+
}
112+
});
125113
}
126114

127115
@ImportAutoConfiguration({ HttpMessageConvertersAutoConfiguration.class, WebMvcAutoConfiguration.class,

0 commit comments

Comments
 (0)