Polish static resource handling mechanism

- ResourceResolver and ResourceResolverChain now have a consistent API
  with regard to method names and terminology.

- ResourceResolver and ResourceResolverChain now accept
  List<? extends Resource> instead of List<Resource> for simplified
  programmatic use.

- Improved Javadoc across the package.

- Formatted code to align with standards.

- Removed all references to ResourceUrlPathTranslator.

Issue: SPR-10933
This commit is contained in:
Sam Brannen
2014-04-22 10:48:12 -04:00
parent cd13b4882c
commit 3d18cfeab6
12 changed files with 135 additions and 121 deletions

View File

@@ -13,58 +13,61 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Test fixture for {@link PrefixResourceResolver}
*
* @author Brian Clozel
* @author Sam Brannen
*/
public class PrefixResourceResolverTests {
private ResourceResolverChain resolver;
private List<Resource> locations;
private final List<? extends Resource> locations = Arrays.asList(new ClassPathResource("test/", getClass()));
private final String shaPrefix = "1df341f";
private ResourceResolverChain chain;
@Before
public void setUp() {
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
resolvers.add(new PrefixResourceResolver(this.shaPrefix));
resolvers.add(new PathResourceResolver());
this.resolver = new DefaultResourceResolverChain(resolvers);
this.locations = new ArrayList<Resource>();
this.locations.add(new ClassPathResource("test/", getClass()));
this.chain = new DefaultResourceResolverChain(resolvers);
}
@Test
public void testResolveResource() {
public void resolveResource() {
String resourceId = "foo.css";
Resource expected = new ClassPathResource("test/foo.css", getClass());
Resource actual = this.resolver.resolveResource(null, "/" + this.shaPrefix + "/" + resourceId, this.locations);
Resource actual = this.chain.resolveResource(null, "/" + this.shaPrefix + "/" + resourceId, this.locations);
assertEquals(expected, actual);
}
@Test
public void testResolveUrlPath() {
public void resolvePublicUrlPath() {
String resourceId = "/foo.css";
String url = "/" + this.shaPrefix + resourceId;
assertEquals(url, resolver.resolveUrlPath(resourceId, locations));
assertEquals(url, chain.resolvePublicUrlPath(resourceId, locations));
}
@Test(expected = IllegalArgumentException.class)
public void testFailPrefixResolverConstructor() {
PrefixResourceResolver resolver = new PrefixResourceResolver("");
public void constructWithEmptyPrefix() {
new PrefixResourceResolver(" ");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-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.
@@ -44,25 +44,25 @@ import static org.junit.Assert.*;
*/
public class PublicResourceUrlProviderJavaConfigTests {
private MockFilterChain filterChain;
private final TestServlet servlet = new TestServlet();
private TestServlet servlet;
private MockFilterChain filterChain;
private MockHttpServletRequest request;
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
this.servlet = new TestServlet();
this.filterChain = new MockFilterChain(this.servlet, new ResourceUrlEncodingFilter());
AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
cxt.setServletContext(new MockServletContext());
cxt.register(WebConfig.class);
cxt.refresh();
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.setServletContext(new MockServletContext());
ctx.register(WebConfig.class);
ctx.refresh();
PublicResourceUrlProvider urlProvider = cxt.getBean(PublicResourceUrlProvider.class);
PublicResourceUrlProvider urlProvider = ctx.getBean(PublicResourceUrlProvider.class);
this.request = new MockHttpServletRequest("GET", "/");
request.setAttribute(PublicResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);