Commit bd6f7a58 authored by Stephane Nicoll's avatar Stephane Nicoll

Expose enableFallback property

Add an extra property to control the support of fallback resolution for
mobile views.

Fixes gh-3009, gh-3019
parent 597e1c3c
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -64,6 +64,7 @@ public class DeviceDelegatingViewResolverAutoConfiguration { ...@@ -64,6 +64,7 @@ public class DeviceDelegatingViewResolverAutoConfiguration {
ViewResolver delegate, int delegateOrder) { ViewResolver delegate, int delegateOrder) {
LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver( LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(
delegate); delegate);
resolver.setEnableFallback(this.viewResolverProperties.isEnableFallback());
resolver.setNormalPrefix(this.viewResolverProperties.getNormalPrefix()); resolver.setNormalPrefix(this.viewResolverProperties.getNormalPrefix());
resolver.setNormalSuffix(this.viewResolverProperties.getNormalSuffix()); resolver.setNormalSuffix(this.viewResolverProperties.getNormalSuffix());
resolver.setMobilePrefix(this.viewResolverProperties.getMobilePrefix()); resolver.setMobilePrefix(this.viewResolverProperties.getMobilePrefix());
......
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -27,6 +27,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -27,6 +27,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("spring.mobile.devicedelegatingviewresolver") @ConfigurationProperties("spring.mobile.devicedelegatingviewresolver")
public class DeviceDelegatingViewResolverProperties { public class DeviceDelegatingViewResolverProperties {
/**
* Enable support for fallback resolution.
*/
private boolean enableFallback;
/** /**
* Prefix that gets prepended to view names for normal devices. * Prefix that gets prepended to view names for normal devices.
*/ */
...@@ -57,6 +62,14 @@ public class DeviceDelegatingViewResolverProperties { ...@@ -57,6 +62,14 @@ public class DeviceDelegatingViewResolverProperties {
*/ */
private String tabletSuffix = ""; private String tabletSuffix = "";
public void setEnableFallback(boolean enableFallback) {
this.enableFallback = enableFallback;
}
public boolean isEnableFallback() {
return enableFallback;
}
public String getNormalPrefix() { public String getNormalPrefix() {
return this.normalPrefix; return this.normalPrefix;
} }
......
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,10 +16,12 @@ ...@@ -16,10 +16,12 @@
package org.springframework.boot.autoconfigure.mobile; package org.springframework.boot.autoconfigure.mobile;
import java.lang.reflect.Field;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.PropertyAccessor;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration.DeviceDelegatingViewResolverConfiguration; import org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration.DeviceDelegatingViewResolverConfiguration;
...@@ -35,9 +37,7 @@ import org.springframework.context.annotation.Bean; ...@@ -35,9 +37,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.mobile.device.view.AbstractDeviceDelegatingViewResolver; import org.springframework.mobile.device.view.AbstractDeviceDelegatingViewResolver;
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver; import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
...@@ -47,6 +47,7 @@ import static org.junit.Assert.assertTrue; ...@@ -47,6 +47,7 @@ import static org.junit.Assert.assertTrue;
* Tests for {@link DeviceDelegatingViewResolverAutoConfiguration}. * Tests for {@link DeviceDelegatingViewResolverAutoConfiguration}.
* *
* @author Roy Clarkson * @author Roy Clarkson
* @author Stephane Nicoll
*/ */
public class DeviceDelegatingViewResolverAutoConfigurationTests { public class DeviceDelegatingViewResolverAutoConfigurationTests {
...@@ -176,165 +177,75 @@ public class DeviceDelegatingViewResolverAutoConfigurationTests { ...@@ -176,165 +177,75 @@ public class DeviceDelegatingViewResolverAutoConfigurationTests {
.getBean("deviceDelegatingViewResolver", .getBean("deviceDelegatingViewResolver",
LiteDeviceDelegatingViewResolver.class); LiteDeviceDelegatingViewResolver.class);
Field normalPrefixField = ReflectionUtils.findField( DirectFieldAccessor accessor = new DirectFieldAccessor(liteDeviceDelegatingViewResolver);
LiteDeviceDelegatingViewResolver.class, "normalPrefix"); assertEquals(false, accessor.getPropertyValue("enableFallback"));
normalPrefixField.setAccessible(true); assertEquals("", accessor.getPropertyValue("normalPrefix"));
String normalPrefix = (String) ReflectionUtils.getField(normalPrefixField, assertEquals("mobile/", accessor.getPropertyValue("mobilePrefix"));
liteDeviceDelegatingViewResolver); assertEquals("tablet/", accessor.getPropertyValue("tabletPrefix"));
assertEquals("", normalPrefix); assertEquals("", accessor.getPropertyValue("normalSuffix"));
assertEquals("", accessor.getPropertyValue("mobileSuffix"));
Field mobilePrefixField = ReflectionUtils.findField( assertEquals("", accessor.getPropertyValue("tabletSuffix"));
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( @Test
LiteDeviceDelegatingViewResolver.class, "tabletSuffix"); public void overrideEnableFallback() throws Exception {
tabletSuffixField.setAccessible(true); PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
String tabletSuffix = (String) ReflectionUtils.getField(tabletSuffixField, "spring.mobile.devicedelegatingviewresolver.enabled:true",
liteDeviceDelegatingViewResolver); "spring.mobile.devicedelegatingviewresolver.enableFallback:true");
assertEquals("", tabletSuffix); assertEquals(true, accessor.getPropertyValue("enableFallback"));
} }
@Test @Test
public void overrideNormalPrefix() throws Exception { public void overrideNormalPrefix() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.enabled:true",
"spring.mobile.devicedelegatingviewresolver.normalPrefix:normal/"); "spring.mobile.devicedelegatingviewresolver.normalPrefix:normal/");
this.context.register(Config.class, WebMvcAutoConfiguration.class, assertEquals("normal/", accessor.getPropertyValue("normalPrefix"));
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
DeviceDelegatingViewResolverConfiguration.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 @Test
public void overrideMobilePrefix() throws Exception { public void overrideMobilePrefix() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.enabled:true",
"spring.mobile.devicedelegatingviewresolver.mobilePrefix:mob/"); "spring.mobile.devicedelegatingviewresolver.mobilePrefix:mob/");
this.context.register(Config.class, WebMvcAutoConfiguration.class, assertEquals("mob/", accessor.getPropertyValue("mobilePrefix"));
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
DeviceDelegatingViewResolverConfiguration.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 @Test
public void overrideTabletPrefix() throws Exception { public void overrideTabletPrefix() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.enabled:true",
"spring.mobile.devicedelegatingviewresolver.tabletPrefix:tab/"); "spring.mobile.devicedelegatingviewresolver.tabletPrefix:tab/");
this.context.register(Config.class, WebMvcAutoConfiguration.class, assertEquals("tab/", accessor.getPropertyValue("tabletPrefix"));
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
DeviceDelegatingViewResolverConfiguration.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 @Test
public void overrideNormalSuffix() throws Exception { public void overrideNormalSuffix() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.enabled:true",
"spring.mobile.devicedelegatingviewresolver.normalSuffix:.nor"); "spring.mobile.devicedelegatingviewresolver.normalSuffix:.nor");
this.context.register(Config.class, WebMvcAutoConfiguration.class, assertEquals(".nor", accessor.getPropertyValue("normalSuffix"));
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
DeviceDelegatingViewResolverConfiguration.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 @Test
public void overrideMobileSuffix() throws Exception { public void overrideMobileSuffix() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.enabled:true",
"spring.mobile.devicedelegatingviewresolver.mobileSuffix:.mob"); "spring.mobile.devicedelegatingviewresolver.mobileSuffix:.mob");
this.context.register(Config.class, WebMvcAutoConfiguration.class, assertEquals(".mob", accessor.getPropertyValue("mobileSuffix"));
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
DeviceDelegatingViewResolverConfiguration.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 @Test
public void overrideTabletSuffix() throws Exception { public void overrideTabletSuffix() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.enabled:true",
"spring.mobile.devicedelegatingviewresolver.tabletSuffix:.tab"); "spring.mobile.devicedelegatingviewresolver.tabletSuffix:.tab");
assertEquals(".tab", accessor.getPropertyValue("tabletSuffix"));
}
private PropertyAccessor getLiteDeviceDelegatingViewResolverAccessor(String... configuration) {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, configuration);
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
...@@ -343,12 +254,7 @@ public class DeviceDelegatingViewResolverAutoConfigurationTests { ...@@ -343,12 +254,7 @@ public class DeviceDelegatingViewResolverAutoConfigurationTests {
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context
.getBean("deviceDelegatingViewResolver", .getBean("deviceDelegatingViewResolver",
LiteDeviceDelegatingViewResolver.class); LiteDeviceDelegatingViewResolver.class);
Field tabletSuffixField = ReflectionUtils.findField( return new DirectFieldAccessor(liteDeviceDelegatingViewResolver);
LiteDeviceDelegatingViewResolver.class, "tabletSuffix");
tabletSuffixField.setAccessible(true);
String tabletSuffix = (String) ReflectionUtils.getField(tabletSuffixField,
liteDeviceDelegatingViewResolver);
assertEquals(".tab", tabletSuffix);
} }
@Configuration @Configuration
......
...@@ -497,6 +497,7 @@ content into your application; rather pick only the properties that you need. ...@@ -497,6 +497,7 @@ content into your application; rather pick only the properties that you need.
# SPRING MOBILE DEVICE VIEWS ({sc-spring-boot-autoconfigure}/mobile/DeviceDelegatingViewResolverAutoConfiguration.{sc-ext}[DeviceDelegatingViewResolverAutoConfiguration]) # SPRING MOBILE DEVICE VIEWS ({sc-spring-boot-autoconfigure}/mobile/DeviceDelegatingViewResolverAutoConfiguration.{sc-ext}[DeviceDelegatingViewResolverAutoConfiguration])
spring.mobile.devicedelegatingviewresolver.enabled=true # disabled by default spring.mobile.devicedelegatingviewresolver.enabled=true # disabled by default
spring.mobile.devicedelegatingviewresolver.enable-fallback= # enable support for fallback resolution, default to false.
spring.mobile.devicedelegatingviewresolver.normal-prefix= spring.mobile.devicedelegatingviewresolver.normal-prefix=
spring.mobile.devicedelegatingviewresolver.normal-suffix= spring.mobile.devicedelegatingviewresolver.normal-suffix=
spring.mobile.devicedelegatingviewresolver.mobile-prefix=mobile/ spring.mobile.devicedelegatingviewresolver.mobile-prefix=mobile/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment