Skip to content

Commit 4817f56

Browse files
committed
Make WebMvc auto-config conditional and add tests
Refactor WebMvcAutoConfiguration to register components conditionally and group logging imports. Beans for ContentCachingFilter, content-negotiation configurer, and ExclusiveViewResolverApplicationListener are now created with @ConditionalOnProperty checks, and logging-related interceptors/advice are imported via a nested LoggingConfiguration class guarded by a logging property. Add FILTER_PROPERTY_NAME_PREFIX and LOGGING_PROPERTY_NAME_PREFIX constants to PropertyConstants. Add tests: WebMvcAutoConfigurationAllEnabledTest (new) to validate all features when enabled and update WebMvcAutoConfigurationTest to assert the presence/absence of beans; also add a test PropertyConstants file. These changes allow toggling individual WebMVC extensions via properties.
1 parent 3a4dbe9 commit 4817f56

5 files changed

Lines changed: 256 additions & 7 deletions

File tree

microsphere-spring-boot-webmvc/src/main/java/io/microsphere/spring/boot/webmvc/autoconfigure/WebMvcAutoConfiguration.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@
2626
import io.microsphere.spring.webmvc.interceptor.LoggingPageRenderContextHandlerInterceptor;
2727
import io.microsphere.spring.webmvc.method.support.LoggingHandlerMethodArgumentResolverAdvice;
2828
import org.springframework.boot.autoconfigure.AutoConfiguration;
29+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
30+
import org.springframework.context.annotation.Bean;
2931
import org.springframework.context.annotation.Import;
3032

33+
import static io.microsphere.constants.PropertyConstants.ENABLED_PROPERTY_NAME;
34+
import static io.microsphere.spring.boot.webmvc.constants.PropertyConstants.FILTER_PROPERTY_NAME_PREFIX;
35+
import static io.microsphere.spring.boot.webmvc.constants.PropertyConstants.LOGGING_PROPERTY_NAME_PREFIX;
36+
import static io.microsphere.spring.webmvc.context.ExclusiveViewResolverApplicationListener.EXCLUSIVE_VIEW_RESOLVER_BEAN_NAME_PROPERTY_NAME;
37+
3138
/**
3239
* MicroSphere Spring Boot WebMVC Auto-Configuration
3340
*
@@ -39,12 +46,34 @@
3946
@AutoConfiguration(afterName = "org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration")
4047
@EnableWebMvcExtension(registerHandlerInterceptors = true)
4148
@Import(value = {
42-
ContentCachingFilter.class,
43-
LoggingMethodHandlerInterceptor.class,
44-
LoggingPageRenderContextHandlerInterceptor.class,
45-
LoggingHandlerMethodArgumentResolverAdvice.class,
46-
ConfigurableContentNegotiationManagerWebMvcConfigurer.class,
47-
ExclusiveViewResolverApplicationListener.class
49+
WebMvcAutoConfiguration.LoggingConfiguration.class
4850
})
4951
public class WebMvcAutoConfiguration {
52+
53+
@ConditionalOnProperty(prefix = FILTER_PROPERTY_NAME_PREFIX, name = ENABLED_PROPERTY_NAME, matchIfMissing = true)
54+
@Bean
55+
public ContentCachingFilter contentCachingFilter() {
56+
return new ContentCachingFilter();
57+
}
58+
59+
@ConditionalOnProperty(prefix = "microsphere.spring.webmvc.content-negotiation.", name = ENABLED_PROPERTY_NAME, matchIfMissing = true)
60+
@Bean
61+
public ConfigurableContentNegotiationManagerWebMvcConfigurer contentNegotiationManagerWebMvcConfigurer() {
62+
return new ConfigurableContentNegotiationManagerWebMvcConfigurer();
63+
}
64+
65+
@ConditionalOnProperty(name = EXCLUSIVE_VIEW_RESOLVER_BEAN_NAME_PROPERTY_NAME)
66+
@Bean
67+
public ExclusiveViewResolverApplicationListener exclusiveViewResolverApplicationListener() {
68+
return new ExclusiveViewResolverApplicationListener();
69+
}
70+
71+
@ConditionalOnProperty(prefix = LOGGING_PROPERTY_NAME_PREFIX, name = ENABLED_PROPERTY_NAME, matchIfMissing = true)
72+
@Import(value = {
73+
LoggingMethodHandlerInterceptor.class,
74+
LoggingPageRenderContextHandlerInterceptor.class,
75+
LoggingHandlerMethodArgumentResolverAdvice.class
76+
})
77+
static class LoggingConfiguration {
78+
}
5079
}

microsphere-spring-boot-webmvc/src/main/java/io/microsphere/spring/boot/webmvc/constants/PropertyConstants.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,14 @@ public interface PropertyConstants {
4949
source = APPLICATION_SOURCE
5050
)
5151
String MICROSPHERE_SPRING_BOOT_WEBMVC_ENALBED_PROPERTY_NAME = MICROSPHERE_SPRING_BOOT_WEBMVC_PROPERTY_NAME_PREFIX + ENABLED_PROPERTY_NAME;
52+
53+
/**
54+
* The property name prefix of Microsphere Spring Boot Web MVC Filter : "microsphere.spring.boot.webmvc.filter."
55+
*/
56+
String FILTER_PROPERTY_NAME_PREFIX = MICROSPHERE_SPRING_BOOT_WEBMVC_PROPERTY_NAME_PREFIX + "filter.";
57+
58+
/**
59+
* The property name prefix of Microsphere Spring Boot Web MVC Logging : "microsphere.spring.boot.webmvc.logging."
60+
*/
61+
String LOGGING_PROPERTY_NAME_PREFIX = MICROSPHERE_SPRING_BOOT_WEBMVC_PROPERTY_NAME_PREFIX + "logging.";
5262
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.spring.boot.webmvc.autoconfigure;
19+
20+
21+
import io.microsphere.spring.boot.webmvc.autoconfigure.WebMvcAutoConfiguration.LoggingConfiguration;
22+
import io.microsphere.spring.test.webmvc.AbstractWebMvcTest;
23+
import io.microsphere.spring.web.servlet.filter.ContentCachingFilter;
24+
import io.microsphere.spring.webmvc.annotation.WebMvcExtensionConfiguration;
25+
import io.microsphere.spring.webmvc.config.ConfigurableContentNegotiationManagerWebMvcConfigurer;
26+
import io.microsphere.spring.webmvc.context.ExclusiveViewResolverApplicationListener;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
31+
import org.springframework.boot.test.context.SpringBootTest;
32+
import org.springframework.http.MediaType;
33+
import org.springframework.test.context.TestPropertySource;
34+
import org.springframework.web.accept.ContentNegotiationManager;
35+
import org.springframework.web.accept.ContentNegotiationStrategy;
36+
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
37+
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
38+
39+
import java.util.List;
40+
import java.util.Map;
41+
42+
import static org.junit.jupiter.api.Assertions.assertEquals;
43+
import static org.junit.jupiter.api.Assertions.assertTrue;
44+
45+
/**
46+
* {@link WebMvcAutoConfiguration} Test with all enabled components
47+
*
48+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
49+
* @see WebMvcAutoConfiguration
50+
* @since 1.0.0
51+
*/
52+
@SpringBootTest(classes = {
53+
WebMvcAutoConfigurationAllEnabledTest.class
54+
})
55+
@TestPropertySource(
56+
properties = {
57+
"microsphere.spring.webmvc.view-resolver.exclusive-bean-name=mvcViewResolver",
58+
"microsphere.spring.webmvc.content-negotiation.enabled=true",
59+
"microsphere.spring.webmvc.content-negotiation.favorParameter=true",
60+
"microsphere.spring.webmvc.content-negotiation.parameterName=p",
61+
"microsphere.spring.webmvc.content-negotiation.favorPathExtension=true",
62+
"microsphere.spring.webmvc.content-negotiation.ignoreUnknownPathExtensions=false",
63+
"microsphere.spring.webmvc.content-negotiation.useRegisteredExtensionsOnly=true",
64+
"microsphere.spring.webmvc.content-negotiation.ignoreAcceptHeader=true",
65+
"microsphere.spring.webmvc.filter.enabled=true",
66+
"microsphere.spring.webmvc.logging.enabled=true"
67+
}
68+
)
69+
@EnableAutoConfiguration
70+
class WebMvcAutoConfigurationAllEnabledTest extends AbstractWebMvcTest {
71+
72+
@Autowired
73+
private WebMvcExtensionConfiguration webMvcExtensionConfiguration;
74+
75+
@Autowired
76+
private ContentCachingFilter contentCachingFilter;
77+
78+
@Autowired
79+
private ConfigurableContentNegotiationManagerWebMvcConfigurer webMvcConfigurer;
80+
81+
@Autowired
82+
private ContentNegotiationManager contentNegotiationManager;
83+
84+
@Autowired
85+
private LoggingConfiguration loggingConfiguration;
86+
87+
@Autowired
88+
private ExclusiveViewResolverApplicationListener listener;
89+
90+
@Override
91+
@BeforeEach
92+
public void setUp() {
93+
super.setUp();
94+
}
95+
96+
@Test
97+
void test() throws Exception {
98+
99+
super.testHelloWorld();
100+
super.testGreeting();
101+
super.testUser();
102+
super.testResponseEntity();
103+
104+
assertContentNegotiationManager(this.contentNegotiationManager);
105+
}
106+
107+
void assertContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) {
108+
List<ContentNegotiationStrategy> strategies = contentNegotiationManager.getStrategies();
109+
assertEquals(2, strategies.size());
110+
111+
ContentNegotiationStrategy strategy1 = strategies.get(0);
112+
ContentNegotiationStrategy strategy2 = strategies.get(1);
113+
114+
assertTrue(strategy1 instanceof PathExtensionContentNegotiationStrategy);
115+
assertTrue(strategy2 instanceof ParameterContentNegotiationStrategy);
116+
117+
PathExtensionContentNegotiationStrategy pathExtensionContentNegotiationStrategy = (PathExtensionContentNegotiationStrategy) strategy1;
118+
Map<String, MediaType> mediaTypes = pathExtensionContentNegotiationStrategy.getMediaTypes();
119+
assertEquals(2, mediaTypes.size());
120+
121+
ParameterContentNegotiationStrategy parameterContentNegotiationStrategy = (ParameterContentNegotiationStrategy) strategy2;
122+
123+
assertEquals("p", parameterContentNegotiationStrategy.getParameterName());
124+
assertEquals(false, pathExtensionContentNegotiationStrategy.isIgnoreUnknownExtensions());
125+
assertEquals(true, pathExtensionContentNegotiationStrategy.isUseRegisteredExtensionsOnly());
126+
}
127+
}

microsphere-spring-boot-webmvc/src/test/java/io/microsphere/spring/boot/webmvc/autoconfigure/WebMvcAutoConfigurationTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919

2020

2121
import io.microsphere.spring.test.webmvc.AbstractWebMvcTest;
22+
import io.microsphere.spring.web.servlet.filter.ContentCachingFilter;
2223
import io.microsphere.spring.webmvc.annotation.WebMvcExtensionConfiguration;
24+
import io.microsphere.spring.webmvc.config.ConfigurableContentNegotiationManagerWebMvcConfigurer;
25+
import io.microsphere.spring.webmvc.context.ExclusiveViewResolverApplicationListener;
2326
import org.junit.jupiter.api.Test;
2427
import org.springframework.beans.factory.annotation.Autowired;
2528
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2629
import org.springframework.boot.test.context.SpringBootTest;
2730

2831
import static org.junit.jupiter.api.Assertions.assertNotNull;
32+
import static org.junit.jupiter.api.Assertions.assertNull;
2933

3034
/**
3135
* {@link WebMvcAutoConfiguration} Test
@@ -43,9 +47,26 @@ class WebMvcAutoConfigurationTest extends AbstractWebMvcTest {
4347
@Autowired
4448
private WebMvcExtensionConfiguration webMvcExtensionConfiguration;
4549

50+
@Autowired
51+
private ContentCachingFilter contentCachingFilter;
52+
53+
@Autowired
54+
private ConfigurableContentNegotiationManagerWebMvcConfigurer webMvcConfigurer;
55+
56+
@Autowired
57+
private WebMvcAutoConfiguration.LoggingConfiguration loggingConfiguration;
58+
59+
@Autowired(required = false)
60+
private ExclusiveViewResolverApplicationListener listener;
61+
4662
@Test
4763
void test() throws Exception {
48-
assertNotNull(webMvcExtensionConfiguration);
64+
assertNotNull(this.webMvcExtensionConfiguration);
65+
assertNotNull(this.contentCachingFilter);
66+
assertNotNull(this.webMvcConfigurer);
67+
assertNotNull(this.loggingConfiguration);
68+
assertNull(this.listener);
69+
4970
this.testHelloWorld();
5071
this.testGreeting();
5172
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.microsphere.spring.boot.webmvc.constants;
18+
19+
import io.microsphere.annotation.ConfigurationProperty;
20+
21+
import static io.microsphere.annotation.ConfigurationProperty.APPLICATION_SOURCE;
22+
import static io.microsphere.constants.PropertyConstants.ENABLED_PROPERTY_NAME;
23+
import static io.microsphere.spring.boot.constants.PropertyConstants.MICROSPHERE_SPRING_BOOT_PROPERTY_NAME_PREFIX;
24+
25+
/**
26+
* The Property constants for Microsphere Spring Boot Web MVC
27+
*
28+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/>
29+
* @since 1.0.0
30+
*/
31+
public interface PropertyConstants {
32+
33+
/**
34+
* The property name prefix of Microsphere Spring Boot Web MVC : "microsphere.spring.boot.webmvc."
35+
*/
36+
String MICROSPHERE_SPRING_BOOT_WEBMVC_PROPERTY_NAME_PREFIX = MICROSPHERE_SPRING_BOOT_PROPERTY_NAME_PREFIX + "webmvc.";
37+
38+
/**
39+
* The default value of 'enabled' property of Microsphere Spring Boot Web MVC : "true"
40+
*/
41+
String DEFAULT_MICROSPHERE_SPRING_BOOT_WEBMVC_ENABLED = "true";
42+
43+
/**
44+
* The 'enabled' property name of Microsphere Spring Boot Web MVC : "microsphere.spring.boot.webmvc.enabled"
45+
*/
46+
@ConfigurationProperty(
47+
type = boolean.class,
48+
defaultValue = DEFAULT_MICROSPHERE_SPRING_BOOT_WEBMVC_ENABLED,
49+
source = APPLICATION_SOURCE
50+
)
51+
String MICROSPHERE_SPRING_BOOT_WEBMVC_ENALBED_PROPERTY_NAME = MICROSPHERE_SPRING_BOOT_WEBMVC_PROPERTY_NAME_PREFIX + ENABLED_PROPERTY_NAME;
52+
53+
/**
54+
* The property name prefix of Microsphere Spring Boot Web MVC Filter : "microsphere.spring.boot.webmvc.filter."
55+
*/
56+
String FILTER_PROPERTY_NAME_PREFIX = MICROSPHERE_SPRING_BOOT_WEBMVC_PROPERTY_NAME_PREFIX + "filter.";
57+
58+
/**
59+
* The property name prefix of Microsphere Spring Boot Web MVC Logging : "microsphere.spring.boot.webmvc.logging."
60+
*/
61+
String LOGGING_PROPERTY_NAME_PREFIX = MICROSPHERE_SPRING_BOOT_WEBMVC_PROPERTY_NAME_PREFIX + "logging.";
62+
}

0 commit comments

Comments
 (0)