Improve Spring Mobile Auto-configuration
- Upgrade Spring Mobile dependency to 1.1.2 - Rename SitePreferenceAutoConfiguration "enabled" property - Add Auto-configuration for LiteDeviceDelegatingViewResolver - Update docs Fixes gh-1049pull/1050/merge
parent
e891aa3525
commit
8f32b87c81
@ -0,0 +1,162 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.mobile;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Mobile's
|
||||
* {@link LiteDeviceDelegatingViewResolver}. If {@link ThymeleafViewResolver} is available
|
||||
* it is configured as the delegate view resolver. Otherwise,
|
||||
* {@link InternalResourceViewResolver} is used as a fallback.
|
||||
*
|
||||
* @author Roy Clarkson
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnClass(LiteDeviceDelegatingViewResolver.class)
|
||||
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
|
||||
public class DevceDelegatingViewResolverAutoConfiguration {
|
||||
|
||||
private static Log logger = LogFactory.getLog(WebMvcConfigurerAdapter.class);
|
||||
|
||||
public static final String DEFAULT_NORMAL_PREFIX = "";
|
||||
|
||||
public static final String DEFAULT_MOBILE_PREFIX = "mobile/";
|
||||
|
||||
public static final String DEFAULT_TABLET_PREFIX = "tablet/";
|
||||
|
||||
public static final String DEFAULT_NORMAL_SUFFIX = "";
|
||||
|
||||
public static final String DEFAULT_MOBILE_SUFFIX = "";
|
||||
|
||||
public static final String DEFAULT_TABLET_SUFFIX = "";
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(name = "deviceDelegatingViewResolver")
|
||||
@ConditionalOnExpression("${spring.mobile.deviceDelegatingViewResolver.enabled:false}")
|
||||
protected static class DevceDelegatingViewResolverConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(ThymeleafViewResolver.class)
|
||||
@AutoConfigureAfter(ThymeleafAutoConfiguration.class)
|
||||
protected static class ThymeleafViewResolverViewResolverDelegateConfiguration
|
||||
extends AbstractDelegateConfiguration {
|
||||
|
||||
@Autowired
|
||||
private ThymeleafViewResolver thymeleafViewResolver;
|
||||
|
||||
@Bean
|
||||
public LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver() {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("LiteDeviceDelegatingViewResolver delegates to ThymeleafViewResolver");
|
||||
}
|
||||
return getConfiguredViewResolver(thymeleafViewResolver,
|
||||
thymeleafViewResolver.getOrder());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(ThymeleafViewResolver.class)
|
||||
@ConditionalOnBean(InternalResourceViewResolver.class)
|
||||
protected static class InternalResourceViewResolverDelegateConfiguration extends
|
||||
AbstractDelegateConfiguration {
|
||||
|
||||
@Autowired
|
||||
private InternalResourceViewResolver internalResourceViewResolver;
|
||||
|
||||
@Bean
|
||||
public LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver() {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("LiteDeviceDelegatingViewResolver delegates to InternalResourceViewResolver");
|
||||
}
|
||||
return getConfiguredViewResolver(internalResourceViewResolver,
|
||||
internalResourceViewResolver.getOrder());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static abstract class AbstractDelegateConfiguration implements
|
||||
EnvironmentAware {
|
||||
|
||||
private RelaxedPropertyResolver environment;
|
||||
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = new RelaxedPropertyResolver(environment,
|
||||
"spring.mobile.deviceDelegatingViewResolver.");
|
||||
}
|
||||
|
||||
protected LiteDeviceDelegatingViewResolver getConfiguredViewResolver(
|
||||
ViewResolver delegate, int delegateOrder) {
|
||||
LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(
|
||||
delegate);
|
||||
resolver.setNormalPrefix(this.environment.getProperty("normalPrefix",
|
||||
DEFAULT_NORMAL_PREFIX));
|
||||
resolver.setMobilePrefix(this.environment.getProperty("mobilePrefix",
|
||||
DEFAULT_MOBILE_PREFIX));
|
||||
resolver.setTabletPrefix(this.environment.getProperty("tabletPrefix",
|
||||
DEFAULT_TABLET_PREFIX));
|
||||
resolver.setNormalSuffix(this.environment.getProperty("normalSuffix",
|
||||
DEFAULT_NORMAL_SUFFIX));
|
||||
resolver.setMobileSuffix(this.environment.getProperty("mobileSuffix",
|
||||
DEFAULT_MOBILE_SUFFIX));
|
||||
resolver.setTabletSuffix(this.environment.getProperty("tabletSuffix",
|
||||
DEFAULT_TABLET_SUFFIX));
|
||||
resolver.setOrder(getAdjustedOrder(delegateOrder));
|
||||
return resolver;
|
||||
}
|
||||
|
||||
private int getAdjustedOrder(int delegateViewResolverOrder) {
|
||||
if (delegateViewResolverOrder == Ordered.HIGHEST_PRECEDENCE) {
|
||||
return Ordered.HIGHEST_PRECEDENCE;
|
||||
} else {
|
||||
// The view resolver must be ordered higher than the delegate view
|
||||
// resolver, otherwise the view names will not be adjusted
|
||||
return delegateViewResolverOrder - 1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,356 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.mobile;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mobile.DevceDelegatingViewResolverAutoConfiguration.DevceDelegatingViewResolverConfiguration;
|
||||
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mobile.device.view.AbstractDeviceDelegatingViewResolver;
|
||||
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||
|
||||
/**
|
||||
* Tests for {@link DevceDelegatingViewResolverAutoConfiguration}.
|
||||
*
|
||||
* @author Roy Clarkson
|
||||
*/
|
||||
public class DeviceDelegatingViewResolverAutoConfigurationTests {
|
||||
|
||||
private static final MockEmbeddedServletContainerFactory containerFactory = new MockEmbeddedServletContainerFactory();
|
||||
|
||||
private AnnotationConfigEmbeddedWebApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void deviceDelegatingViewResolverDefaultDisabled() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
this.context.register(Config.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
this.context.getBean("deviceDelegatingViewResolver",
|
||||
AbstractDeviceDelegatingViewResolver.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceDelegatingInternalResourceViewResolverEnabled() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context,
|
||||
"spring.mobile.deviceDelegatingViewResolver.enabled:true");
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
InternalResourceViewResolver internalResourceViewResolver = this.context.getBean(InternalResourceViewResolver.class);
|
||||
AbstractDeviceDelegatingViewResolver deviceDelegatingViewResolver = this.context.getBean(
|
||||
"deviceDelegatingViewResolver",
|
||||
AbstractDeviceDelegatingViewResolver.class);
|
||||
assertNotNull(internalResourceViewResolver);
|
||||
assertNotNull(deviceDelegatingViewResolver);
|
||||
assertTrue(deviceDelegatingViewResolver.getViewResolver() instanceof InternalResourceViewResolver);
|
||||
try {
|
||||
this.context.getBean(ThymeleafViewResolver.class);
|
||||
} catch (NoSuchBeanDefinitionException e) {
|
||||
// expected. ThymeleafViewResolver shouldn't be defined.
|
||||
}
|
||||
assertTrue(deviceDelegatingViewResolver.getOrder() == internalResourceViewResolver.getOrder() - 1);
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void deviceDelegatingInternalResourceViewResolverDisabled() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context,
|
||||
"spring.mobile.deviceDelegatingViewResolver.enabled:false");
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(InternalResourceViewResolver.class));
|
||||
try {
|
||||
this.context.getBean(ThymeleafViewResolver.class);
|
||||
} catch (NoSuchBeanDefinitionException e) {
|
||||
// expected. ThymeleafViewResolver shouldn't be defined.
|
||||
}
|
||||
this.context.getBean("deviceDelegatingViewResolver",
|
||||
AbstractDeviceDelegatingViewResolver.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceDelegatingThymeleafViewResolverEnabled() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context,
|
||||
"spring.mobile.deviceDelegatingViewResolver.enabled:true");
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
ThymeleafAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
ThymeleafViewResolver thymeleafViewResolver = this.context.getBean(ThymeleafViewResolver.class);
|
||||
AbstractDeviceDelegatingViewResolver deviceDelegatingViewResolver = this.context.getBean(
|
||||
"deviceDelegatingViewResolver",
|
||||
AbstractDeviceDelegatingViewResolver.class);
|
||||
assertNotNull(thymeleafViewResolver);
|
||||
assertNotNull(deviceDelegatingViewResolver);
|
||||
assertTrue(deviceDelegatingViewResolver.getViewResolver() instanceof ThymeleafViewResolver);
|
||||
assertNotNull(this.context.getBean(InternalResourceViewResolver.class));
|
||||
assertNotNull(this.context.getBean(ThymeleafViewResolver.class));
|
||||
assertTrue(deviceDelegatingViewResolver.getOrder() == thymeleafViewResolver.getOrder() - 1);
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void deviceDelegatingThymeleafViewResolverDisabled() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context,
|
||||
"spring.mobile.deviceDelegatingViewResolver.enabled:false");
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
ThymeleafAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(InternalResourceViewResolver.class));
|
||||
assertNotNull(this.context.getBean(ThymeleafViewResolver.class));
|
||||
this.context.getBean("deviceDelegatingViewResolver",
|
||||
AbstractDeviceDelegatingViewResolver.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultPropertyValues() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context,
|
||||
"spring.mobile.deviceDelegatingViewResolver.enabled:true");
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context.getBean(
|
||||
"deviceDelegatingViewResolver", LiteDeviceDelegatingViewResolver.class);
|
||||
|
||||
Field normalPrefixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "normalPrefix");
|
||||
normalPrefixField.setAccessible(true);
|
||||
String normalPrefix = (String) ReflectionUtils.getField(normalPrefixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals("", normalPrefix);
|
||||
|
||||
Field mobilePrefixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "mobilePrefix");
|
||||
mobilePrefixField.setAccessible(true);
|
||||
String mobilePrefix = (String) ReflectionUtils.getField(mobilePrefixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals("mobile/", mobilePrefix);
|
||||
|
||||
Field tabletPrefixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "tabletPrefix");
|
||||
tabletPrefixField.setAccessible(true);
|
||||
String tabletPrefix = (String) ReflectionUtils.getField(tabletPrefixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals("tablet/", tabletPrefix);
|
||||
|
||||
Field normalSuffixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "normalSuffix");
|
||||
normalSuffixField.setAccessible(true);
|
||||
String normalSuffix = (String) ReflectionUtils.getField(normalSuffixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals("", normalSuffix);
|
||||
|
||||
Field mobileSuffixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "mobileSuffix");
|
||||
mobileSuffixField.setAccessible(true);
|
||||
String mobileSuffix = (String) ReflectionUtils.getField(mobileSuffixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals("", mobileSuffix);
|
||||
|
||||
Field tabletSuffixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "tabletSuffix");
|
||||
tabletSuffixField.setAccessible(true);
|
||||
String tabletSuffix = (String) ReflectionUtils.getField(tabletSuffixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals("", tabletSuffix);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideNormalPrefix() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context,
|
||||
"spring.mobile.deviceDelegatingViewResolver.enabled:true",
|
||||
"spring.mobile.deviceDelegatingViewResolver.normalPrefix:normal/");
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context.getBean(
|
||||
"deviceDelegatingViewResolver", LiteDeviceDelegatingViewResolver.class);
|
||||
Field normalPrefixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "normalPrefix");
|
||||
normalPrefixField.setAccessible(true);
|
||||
String normalPrefix = (String) ReflectionUtils.getField(normalPrefixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals("normal/", normalPrefix);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideMobilePrefix() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context,
|
||||
"spring.mobile.deviceDelegatingViewResolver.enabled:true",
|
||||
"spring.mobile.deviceDelegatingViewResolver.mobilePrefix:mob/");
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context.getBean(
|
||||
"deviceDelegatingViewResolver", LiteDeviceDelegatingViewResolver.class);
|
||||
Field mobilePrefixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "mobilePrefix");
|
||||
mobilePrefixField.setAccessible(true);
|
||||
String mobilePrefix = (String) ReflectionUtils.getField(mobilePrefixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals("mob/", mobilePrefix);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideTabletPrefix() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context,
|
||||
"spring.mobile.deviceDelegatingViewResolver.enabled:true",
|
||||
"spring.mobile.deviceDelegatingViewResolver.tabletPrefix:tab/");
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context.getBean(
|
||||
"deviceDelegatingViewResolver", LiteDeviceDelegatingViewResolver.class);
|
||||
Field tabletPrefixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "tabletPrefix");
|
||||
tabletPrefixField.setAccessible(true);
|
||||
String tabletPrefix = (String) ReflectionUtils.getField(tabletPrefixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals("tab/", tabletPrefix);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideNormalSuffix() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context,
|
||||
"spring.mobile.deviceDelegatingViewResolver.enabled:true",
|
||||
"spring.mobile.deviceDelegatingViewResolver.normalSuffix:.nor");
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context.getBean(
|
||||
"deviceDelegatingViewResolver", LiteDeviceDelegatingViewResolver.class);
|
||||
Field normalSuffixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "normalSuffix");
|
||||
normalSuffixField.setAccessible(true);
|
||||
String normalSuffix = (String) ReflectionUtils.getField(normalSuffixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals(".nor", normalSuffix);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideMobileSuffix() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context,
|
||||
"spring.mobile.deviceDelegatingViewResolver.enabled:true",
|
||||
"spring.mobile.deviceDelegatingViewResolver.mobileSuffix:.mob");
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context.getBean(
|
||||
"deviceDelegatingViewResolver", LiteDeviceDelegatingViewResolver.class);
|
||||
Field mobileSuffixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "mobileSuffix");
|
||||
mobileSuffixField.setAccessible(true);
|
||||
String mobileSuffix = (String) ReflectionUtils.getField(mobileSuffixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals(".mob", mobileSuffix);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideTabletSuffix() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context,
|
||||
"spring.mobile.deviceDelegatingViewResolver.enabled:true",
|
||||
"spring.mobile.deviceDelegatingViewResolver.tabletSuffix:.tab");
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DevceDelegatingViewResolverConfiguration.class);
|
||||
this.context.refresh();
|
||||
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context.getBean(
|
||||
"deviceDelegatingViewResolver", LiteDeviceDelegatingViewResolver.class);
|
||||
Field tabletSuffixField = ReflectionUtils.findField(
|
||||
LiteDeviceDelegatingViewResolver.class, "tabletSuffix");
|
||||
tabletSuffixField.setAccessible(true);
|
||||
String tabletSuffix = (String) ReflectionUtils.getField(tabletSuffixField,
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertEquals(".tab", tabletSuffix);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class Config {
|
||||
|
||||
@Bean
|
||||
public EmbeddedServletContainerFactory containerFactory() {
|
||||
return containerFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() {
|
||||
return new EmbeddedServletContainerCustomizerBeanPostProcessor();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue