@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2014 the original author or authors .
* Copyright 2002 - 2015 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 .
@ -21,11 +21,17 @@ import java.util.HashMap;
import java.util.List ;
import java.util.Map ;
import org.hamcrest.Matchers ;
import org.junit.Before ;
import org.junit.Test ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.core.io.ClassPathResource ;
import org.springframework.core.io.Resource ;
import org.springframework.mock.web.test.MockServletContext ;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext ;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping ;
import static org.junit.Assert.* ;
@ -88,4 +94,34 @@ public class ResourceUrlProviderTests {
this . translator . setHandlerMap ( this . handlerMap ) ;
}
// SPR-12592
@Test
public void initializeOnce ( ) throws Exception {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext ( ) ;
context . setServletContext ( new MockServletContext ( ) ) ;
context . register ( HandlerMappingConfiguration . class ) ;
context . refresh ( ) ;
ResourceUrlProvider translator = context . getBean ( ResourceUrlProvider . class ) ;
assertThat ( translator . getHandlerMap ( ) , Matchers . hasKey ( "/resources/**" ) ) ;
assertFalse ( translator . isAutodetect ( ) ) ;
}
@Configuration
public static class HandlerMappingConfiguration {
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping ( ) {
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler ( ) ;
HashMap < String , ResourceHttpRequestHandler > handlerMap = new HashMap < String , ResourceHttpRequestHandler > ( ) ;
handlerMap . put ( "/resources/**" , handler ) ;
SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping ( ) ;
hm . setUrlMap ( handlerMap ) ;
return hm ;
}
@Bean
public ResourceUrlProvider resourceUrlProvider ( ) {
return new ResourceUrlProvider ( ) ;
}
}
}