@ -16,28 +16,36 @@
@@ -16,28 +16,36 @@
package org.springframework.boot.autoconfigure.mobile ;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.List ;
import org.junit.After ;
import org.junit.Rule ;
import org.junit.Test ;
import org.junit.rules.ExpectedException ;
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.boot.autoconfigure.PropertyPlaceholderAutoConfiguration ;
import org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration.DeviceDelegatingViewResolverConfiguration ;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration ;
import org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration ;
import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration ;
import org.springframework.boot.autoconfigure.mustache.web.MustacheViewResolver ;
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.util.EnvironmentTestUtils ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.mobile.device.view.AbstractDeviceDelegatingViewResolver ;
import org.springframework.core.Ordered ;
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver ;
import org.springframework.mock.web.MockServletContext ;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext ;
import org.springframework.web.servlet.ViewResolver ;
import org.springframework.web.servlet.view.InternalResourceViewResolver ;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver ;
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver ;
import static org.assertj.core.api.Assertions.assertThat ;
@ -49,9 +57,10 @@ import static org.assertj.core.api.Assertions.assertThat;
@@ -49,9 +57,10 @@ import static org.assertj.core.api.Assertions.assertThat;
* /
public class DeviceDelegatingViewResolverAutoConfigurationTests {
private static final MockEmbeddedServletContainerFactory containerFactory = new MockEmbeddedServletContainerFactory ( ) ;
@Rule
public final ExpectedException thrown = ExpectedException . none ( ) ;
private AnnotationConfigEmbedded WebApplicationContext context ;
private AnnotationConfigWebApplicationContext context ;
@After
public void close ( ) {
@ -60,123 +69,82 @@ public class DeviceDelegatingViewResolverAutoConfigurationTests {
@@ -60,123 +69,82 @@ public class DeviceDelegatingViewResolverAutoConfigurationTests {
}
}
@Test ( expected = NoSuchBeanDefinitionException . class )
@Test
public void deviceDelegatingViewResolverDefaultDisabled ( ) throws Exception {
this . context = new AnnotationConfigEmbeddedWebApplicationContext ( ) ;
this . context . register ( Config . class ,
DeviceDelegatingViewResolverConfiguration . class ) ;
this . context . refresh ( ) ;
this . context . getBean ( "deviceDelegatingViewResolver" ,
AbstractDeviceDelegatingViewResolver . class ) ;
load ( ) ;
this . thrown . expect ( NoSuchBeanDefinitionException . class ) ;
this . context . getBean ( LiteDeviceDelegatingViewResolver . class ) ;
}
@Test
public void deviceDelegatingInternalResourceViewResolverEnabled ( ) throws Exception {
this . context = new AnnotationConfigEmbeddedWebApplicationContext ( ) ;
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.mobile.devicedelegatingviewresolver.enabled:true" ) ;
this . context . register ( Config . class , WebMvcAutoConfiguration . class ,
HttpMessageConvertersAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ,
DeviceDelegatingViewResolverConfiguration . class ) ;
this . context . refresh ( ) ;
public void deviceDelegatingJspResourceViewResolver ( ) throws Exception {
load ( "spring.mobile.devicedelegatingviewresolver.enabled:true" ) ;
assertThat ( this . context . getBeansOfType ( LiteDeviceDelegatingViewResolver . class ) ) . hasSize ( 1 ) ;
InternalResourceViewResolver internalResourceViewResolver = this . context
. getBean ( InternalResourceViewResolver . class ) ;
AbstractDeviceDelegatingViewResolver deviceDelegatingViewResolver = this . context
. getBean ( "deviceDelegatingViewResolver" ,
AbstractDeviceDelegatingViewResolver . class ) ;
assertThat ( internalResourceViewResolver ) . isNotNull ( ) ;
assertThat ( deviceDelegatingViewResolver ) . isNotNull ( ) ;
assertThat ( deviceDelegatingViewResolver . getViewResolver ( ) )
. isInstanceOf ( InternalResourceViewResolver . class ) ;
try {
this . context . getBean ( ThymeleafViewResolver . class ) ;
}
catch ( NoSuchBeanDefinitionException ex ) {
// expected. ThymeleafViewResolver shouldn't be defined.
}
assertThat ( deviceDelegatingViewResolver . getOrder ( ) )
. isEqualTo ( internalResourceViewResolver . getOrder ( ) - 1 ) ;
assertLiteDeviceDelegatingViewResolver ( internalResourceViewResolver ,
"deviceDelegatingJspViewResolver" ) ;
}
@Test ( expected = NoSuchBeanDefinitionException . class )
public void deviceDelegatingInternalResourceViewResolverDisabled ( ) throws Exception {
this . context = new AnnotationConfigEmbeddedWebApplicationContext ( ) ;
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.mobile.devicedelegatingviewresolver.enabled:false" ) ;
this . context . register ( Config . class , WebMvcAutoConfiguration . class ,
HttpMessageConvertersAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ,
DeviceDelegatingViewResolverConfiguration . class ) ;
this . context . refresh ( ) ;
assertThat ( this . context . getBean ( InternalResourceViewResolver . class ) ) . isNotNull ( ) ;
try {
this . context . getBean ( ThymeleafViewResolver . class ) ;
}
catch ( NoSuchBeanDefinitionException ex ) {
// expected. ThymeleafViewResolver shouldn't be defined.
}
this . context . getBean ( "deviceDelegatingViewResolver" ,
AbstractDeviceDelegatingViewResolver . class ) ;
@Test
public void deviceDelegatingFreemarkerViewResolver ( ) throws Exception {
load ( Collections . < Class < ? > > singletonList ( FreeMarkerAutoConfiguration . class ) ,
"spring.mobile.devicedelegatingviewresolver.enabled:true" ) ;
assertThat ( this . context . getBeansOfType ( LiteDeviceDelegatingViewResolver . class ) ) . hasSize ( 2 ) ;
assertLiteDeviceDelegatingViewResolver ( this . context . getBean ( FreeMarkerViewResolver . class ) ,
"deviceDelegatingFreemarkerViewResolver" ) ;
}
@Test
public void deviceDelegatingThymeleafViewResolverEnabled ( ) throws Exception {
this . context = new AnnotationConfigEmbeddedWebApplicationContext ( ) ;
EnvironmentTestUtils . addEnvironment ( this . context ,
public void deviceDelegatingGroovyMarkupViewResolver ( ) throws Exception {
load ( Collections . < Class < ? > > singletonList ( GroovyTemplateAutoConfiguration . class ) ,
"spring.mobile.devicedelegatingviewresolver.enabled:true" ) ;
this . context . register ( Config . class , WebMvcAutoConfiguration . class ,
ThymeleafAutoConfiguration . class ,
HttpMessageConvertersAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ,
DeviceDelegatingViewResolverConfiguration . class ) ;
this . context . refresh ( ) ;
ThymeleafViewResolver thymeleafViewResolver = this . context
. getBean ( ThymeleafViewResolver . class ) ;
AbstractDeviceDelegatingViewResolver deviceDelegatingViewResolver = this . context
. getBean ( "deviceDelegatingViewResolver" ,
AbstractDeviceDelegatingViewResolver . class ) ;
assertThat ( thymeleafViewResolver ) . isNotNull ( ) ;
assertThat ( deviceDelegatingViewResolver ) . isNotNull ( ) ;
assertThat ( this . context . getBeansOfType ( LiteDeviceDelegatingViewResolver . class ) ) . hasSize ( 2 ) ;
assertLiteDeviceDelegatingViewResolver ( this . context . getBean ( GroovyMarkupViewResolver . class ) ,
"deviceDelegatingGroovyMarkupViewResolver" ) ;
}
@Test
public void deviceDelegatingMustacheViewResolver ( ) throws Exception {
load ( Collections . < Class < ? > > singletonList ( MustacheAutoConfiguration . class ) ,
"spring.mobile.devicedelegatingviewresolver.enabled:true" ) ;
assertThat ( this . context . getBeansOfType ( LiteDeviceDelegatingViewResolver . class ) ) . hasSize ( 2 ) ;
assertLiteDeviceDelegatingViewResolver ( this . context . getBean ( MustacheViewResolver . class ) ,
"deviceDelegatingMustacheViewResolver" ) ;
}
@Test
public void deviceDelegatingThymeleafViewResolver ( ) throws Exception {
load ( Collections . < Class < ? > > singletonList ( ThymeleafAutoConfiguration . class ) ,
"spring.mobile.devicedelegatingviewresolver.enabled:true" ) ;
assertThat ( this . context . getBeansOfType ( LiteDeviceDelegatingViewResolver . class ) ) . hasSize ( 2 ) ;
assertLiteDeviceDelegatingViewResolver ( this . context . getBean ( ThymeleafViewResolver . class ) ,
"deviceDelegatingThymeleafViewResolver" ) ;
}
public void assertLiteDeviceDelegatingViewResolver ( ViewResolver delegate , String delegatingBeanName ) {
LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver = this . context
. getBean ( delegatingBeanName , LiteDeviceDelegatingViewResolver . class ) ;
assertThat ( deviceDelegatingViewResolver . getViewResolver ( ) )
. isInstanceOf ( ThymeleafViewResolver . class ) ;
assertThat ( this . context . getBean ( InternalResourceViewResolver . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( ThymeleafViewResolver . class ) ) . isNotNull ( ) ;
. isSameAs ( delegate ) ;
assertThat ( deviceDelegatingViewResolver . getOrder ( ) )
. isEqualTo ( thymeleafViewResolver . getOrder ( ) - 1 ) ;
. isEqualTo ( ( ( Ordered ) delegate ) . getOrder ( ) - 1 ) ;
}
@Test ( expected = NoSuchBeanDefinitionException . class )
public void deviceDelegatingThymeleafViewResolverDisabled ( ) throws Exception {
this . context = new AnnotationConfigEmbeddedWebApplicationContext ( ) ;
EnvironmentTestUtils . addEnvironment ( this . context ,
@Test
public void deviceDelegatingViewResolverDisabled ( ) throws Exception {
load ( Arrays . asList ( FreeMarkerAutoConfiguration . class , GroovyTemplateAutoConfiguration . class ,
MustacheAutoConfiguration . class , ThymeleafAutoConfiguration . class ) ,
"spring.mobile.devicedelegatingviewresolver.enabled:false" ) ;
this . context . register ( Config . class , WebMvcAutoConfiguration . class ,
ThymeleafAutoConfiguration . class ,
HttpMessageConvertersAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ,
DeviceDelegatingViewResolverConfiguration . class ) ;
this . context . refresh ( ) ;
assertThat ( this . context . getBean ( InternalResourceViewResolver . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( ThymeleafViewResolver . class ) ) . isNotNull ( ) ;
this . context . getBean ( "deviceDelegatingViewResolver" ,
AbstractDeviceDelegatingViewResolver . class ) ;
assertThat ( this . context . getBeansOfType ( LiteDeviceDelegatingViewResolver . class ) ) . hasSize ( 0 ) ;
}
@Test
public void defaultPropertyValues ( ) throws Exception {
this . context = new AnnotationConfigEmbeddedWebApplicationContext ( ) ;
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.mobile.devicedelegatingviewresolver.enabled:true" ) ;
this . context . register ( Config . class , WebMvcAutoConfiguration . class ,
HttpMessageConvertersAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ,
DeviceDelegatingViewResolverConfiguration . class ) ;
this . context . refresh ( ) ;
load ( "spring.mobile.devicedelegatingviewresolver.enabled:true" ) ;
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this . context
. getBean ( "deviceDelegatingViewResolver" ,
. getBean ( "deviceDelegatingJspViewResolver" ,
LiteDeviceDelegatingViewResolver . class ) ;
DirectFieldAccessor accessor = new DirectFieldAccessor (
liteDeviceDelegatingViewResolver ) ;
assertThat ( accessor . getPropertyValue ( "enableFallback" ) ) . isEqualTo ( Boolean . FALSE ) ;
@ -246,32 +214,29 @@ public class DeviceDelegatingViewResolverAutoConfigurationTests {
@@ -246,32 +214,29 @@ public class DeviceDelegatingViewResolverAutoConfigurationTests {
private PropertyAccessor getLiteDeviceDelegatingViewResolverAccessor (
String . . . configuration ) {
this . context = new AnnotationConfigEmbeddedWebApplicationContext ( ) ;
EnvironmentTestUtils . addEnvironment ( this . context , configuration ) ;
this . context . register ( Config . class , WebMvcAutoConfiguration . class ,
HttpMessageConvertersAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ,
DeviceDelegatingViewResolverConfiguration . class ) ;
this . context . refresh ( ) ;
load ( configuration ) ;
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this . context
. getBean ( "deviceDelegatingViewResolver" ,
. getBean ( "deviceDelegatingJspViewResolver" ,
LiteDeviceDelegatingViewResolver . class ) ;
return new DirectFieldAccessor ( liteDeviceDelegatingViewResolver ) ;
}
@Configuration
protected static class Config {
@Bean
public EmbeddedServletContainerFactory containerFactory ( ) {
return containerFactory ;
}
public void load ( String . . . environment ) {
load ( null , environment ) ;
}
@Bean
public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor ( ) {
return new EmbeddedServletContainerCustomizerBeanPostProcessor ( ) ;
public void load ( List < Class < ? > > config , String . . . environment ) {
this . context = new AnnotationConfigWebApplicationContext ( ) ;
this . context . setServletContext ( new MockServletContext ( ) ) ;
if ( config ! = null ) {
this . context . register ( config . toArray ( new Class [ config . size ( ) ] ) ) ;
}
this . context . register ( WebMvcAutoConfiguration . class ,
HttpMessageConvertersAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ,
DeviceDelegatingViewResolverAutoConfiguration . class ) ;
EnvironmentTestUtils . addEnvironment ( this . context , environment ) ;
this . context . refresh ( ) ;
}
}