Polish
This commit is contained in:
@@ -28,33 +28,34 @@ import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class ExtensionMappingResourceResolverTests {
|
||||
|
||||
private ResourceResolverChain resolver;
|
||||
|
||||
|
||||
private List<Resource> locations;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
|
||||
resolvers.add(new ExtensionMappingResourceResolver());
|
||||
resolvers.add(new PathResourceResolver());
|
||||
resolver = new DefaultResourceResolverChain(resolvers, new ArrayList<ResourceTransformer>());
|
||||
locations = new ArrayList<Resource>();
|
||||
locations.add(new ClassPathResource("test/", getClass()));
|
||||
locations.add(new ClassPathResource("testalternatepath/", getClass()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveLessResource() throws Exception {
|
||||
String resourceId = "zoo.css";
|
||||
Resource resource = new ClassPathResource("test/"+resourceId+".less", getClass());
|
||||
String resourceId = "zoo.css";
|
||||
Resource resource = new ClassPathResource("test/" + resourceId + ".less", getClass());
|
||||
Resource resolved = resolver.resolveAndTransform(null, resourceId, locations);
|
||||
assertEquals(resource, resolved);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveLessUrl() {
|
||||
String resourceId = "zoo.css";
|
||||
|
||||
@@ -32,76 +32,92 @@ import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class FingerprintingResourceResolverTests {
|
||||
public class FingerprintResourceResolverTests {
|
||||
|
||||
private ResourceResolverChain chain;
|
||||
|
||||
private FingerprintingResourceResolver resolver = new FingerprintingResourceResolver();
|
||||
|
||||
|
||||
private FingerprintResourceResolver resolver = new FingerprintResourceResolver();
|
||||
|
||||
private List<Resource> locations;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
|
||||
resolvers.add(resolver);
|
||||
resolvers.add(new PathResourceResolver());
|
||||
chain = new DefaultResourceResolverChain(resolvers, new ArrayList<ResourceTransformer>());
|
||||
locations = new ArrayList<Resource>();
|
||||
locations.add(new ClassPathResource("test/", getClass()));
|
||||
locations.add(new ClassPathResource("testalternatepath/", getClass()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveWithoutHash() throws Exception {
|
||||
String file = "bar.css";
|
||||
Resource expected = new ClassPathResource("test/" + file, getClass());
|
||||
Resource actual = chain.resolveAndTransform(null, file, locations);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveWithHashNoMatch() throws Exception {
|
||||
String file = "bogus-e36d2e05253c6c7085a91522ce43a0b4.css";
|
||||
assertNull(chain.resolveAndTransform(null, file, locations));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveStaticFingerprintedResource() throws Exception {
|
||||
String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css";
|
||||
Resource resource = new ClassPathResource("test/"+file, getClass());
|
||||
Resource resolved = chain.resolveAndTransform(null, file, locations);
|
||||
assertEquals(resource, resolved);
|
||||
String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css";
|
||||
Resource expected = new ClassPathResource("test/"+file, getClass());
|
||||
Resource actual = chain.resolveAndTransform(null, file, locations);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveDynamicFingerprintedResource() throws Exception {
|
||||
Resource resource = new ClassPathResource("test/bar.css", getClass());
|
||||
String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(resource.getInputStream()));
|
||||
String path = "/bar-"+hash+".css";
|
||||
|
||||
Resource resolved = chain.resolveAndTransform(null, path, locations);
|
||||
|
||||
assertEquals(resource, resolved);
|
||||
Resource expected = new ClassPathResource("test/bar.css", getClass());
|
||||
String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(expected.getInputStream()));
|
||||
String path = "/bar-" + hash + ".css";
|
||||
Resource actual = chain.resolveAndTransform(null, path, locations);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveWithMultipleExtensions() throws Exception {
|
||||
Resource resource = new ClassPathResource("test/bar.min.css", getClass());
|
||||
String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(resource.getInputStream()));
|
||||
String path = "/bar.min-"+hash+".css";
|
||||
|
||||
Resource resolved = chain.resolveAndTransform(null, path, locations);
|
||||
|
||||
assertEquals(resource, resolved);
|
||||
Resource expected = new ClassPathResource("test/bar.min.css", getClass());
|
||||
String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(expected.getInputStream()));
|
||||
String path = "/bar.min-" + hash + ".css";
|
||||
Resource actual = chain.resolveAndTransform(null, path, locations);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveWithMultipleHyphens() throws Exception {
|
||||
Resource resource = new ClassPathResource("test/foo-bar/foo-bar.css", getClass());
|
||||
String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(resource.getInputStream()));
|
||||
String path = "/foo-bar/foo-bar-"+hash+".css";
|
||||
|
||||
Resource resolved = chain.resolveAndTransform(null, path, locations);
|
||||
|
||||
assertEquals(resource, resolved);
|
||||
Resource expected = new ClassPathResource("test/foo-bar/foo-bar.css", getClass());
|
||||
String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(expected.getInputStream()));
|
||||
String path = "/foo-bar/foo-bar-" + hash + ".css";
|
||||
Resource actual = chain.resolveAndTransform(null, path, locations);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void extractHash() throws Exception {
|
||||
String hash = "7fbe76cdac6093784895bb4989203e5a";
|
||||
String path = "font-awesome/css/font-awesome.min-"+hash+".css";
|
||||
|
||||
Method extractHash = ReflectionUtils.findMethod(FingerprintingResourceResolver.class, "extractHash", String.class);
|
||||
ReflectionUtils.makeAccessible(extractHash);
|
||||
String result = (String) ReflectionUtils.invokeMethod(extractHash, resolver, path);
|
||||
String path = "font-awesome/css/font-awesome.min-" + hash + ".css";
|
||||
|
||||
Method method = ReflectionUtils.findMethod(resolver.getClass(), "extractHash", String.class);
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
String result = (String) ReflectionUtils.invokeMethod(method, resolver, path);
|
||||
|
||||
assertEquals(hash, result);
|
||||
}
|
||||
}
|
||||
@@ -35,15 +35,15 @@ import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class GzipResourceResolverTests {
|
||||
|
||||
private ResourceResolverChain resolver;
|
||||
|
||||
|
||||
private List<Resource> locations;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void createGzippedResources() throws IOException {
|
||||
Resource location = new ClassPathResource("test/", GzipResourceResolverTests.class);
|
||||
@@ -51,32 +51,33 @@ public class GzipResourceResolverTests {
|
||||
Resource gzJsFile = jsFile.createRelative("foo.js.gz");
|
||||
Resource fingerPrintedFile = new FileSystemResource(location.createRelative("foo-e36d2e05253c6c7085a91522ce43a0b4.css").getFile());
|
||||
Resource gzFingerPrintedFile = fingerPrintedFile.createRelative("foo-e36d2e05253c6c7085a91522ce43a0b4.css.gz");
|
||||
|
||||
|
||||
if (gzJsFile.getFile().createNewFile()) {
|
||||
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzJsFile.getFile()));
|
||||
FileCopyUtils.copy(jsFile.getInputStream(), out);
|
||||
}
|
||||
|
||||
|
||||
if (gzFingerPrintedFile.getFile().createNewFile()) {
|
||||
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFingerPrintedFile.getFile()));
|
||||
FileCopyUtils.copy(fingerPrintedFile.getInputStream(), out);
|
||||
}
|
||||
|
||||
|
||||
assertTrue(gzJsFile.exists());
|
||||
assertTrue(gzFingerPrintedFile.exists());
|
||||
}
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
|
||||
resolvers.add(new GzipResourceResolver());
|
||||
resolvers.add(new FingerprintingResourceResolver());
|
||||
resolvers.add(new FingerprintResourceResolver());
|
||||
resolvers.add(new PathResourceResolver());
|
||||
resolver = new DefaultResourceResolverChain(resolvers, new ArrayList<ResourceTransformer>());
|
||||
locations = new ArrayList<Resource>();
|
||||
locations.add(new ClassPathResource("test/", getClass()));
|
||||
locations.add(new ClassPathResource("testalternatepath/", getClass()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveGzippedFile() throws IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -85,11 +86,13 @@ public class GzipResourceResolverTests {
|
||||
String gzFile = file+".gz";
|
||||
Resource resource = new ClassPathResource("test/"+gzFile, getClass());
|
||||
Resource resolved = resolver.resolveAndTransform(request, file, locations);
|
||||
|
||||
assertEquals(resource.getDescription(), resolved.getDescription());
|
||||
assertEquals(new ClassPathResource("test/"+file).getFilename(), resolved.getFilename());
|
||||
assertTrue("Expected " + resolved + " to be of type " + EncodedResource.class, resolved instanceof EncodedResource);
|
||||
assertTrue("Expected " + resolved + " to be of type " + EncodedResource.class,
|
||||
resolved instanceof EncodedResource);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveFingerprintedGzippedFile() throws IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -98,8 +101,10 @@ public class GzipResourceResolverTests {
|
||||
String gzFile = file+".gz";
|
||||
Resource resource = new ClassPathResource("test/"+gzFile, getClass());
|
||||
Resource resolved = resolver.resolveAndTransform(request, file, locations);
|
||||
|
||||
assertEquals(resource.getDescription(), resolved.getDescription());
|
||||
assertEquals(new ClassPathResource("test/"+file).getFilename(), resolved.getFilename());
|
||||
assertTrue("Expected " + resolved + " to be of type " + EncodedResource.class, resolved instanceof EncodedResource);
|
||||
assertTrue("Expected " + resolved + " to be of type " + EncodedResource.class,
|
||||
resolved instanceof EncodedResource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,66 +31,68 @@ import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class ResourceUrlMapperTests {
|
||||
|
||||
ResourceHttpRequestHandler handler;
|
||||
|
||||
|
||||
SimpleUrlHandlerMapping mapping;
|
||||
|
||||
|
||||
ResourceUrlMapper mapper;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
List<Resource> resourcePaths = new ArrayList<Resource>();
|
||||
resourcePaths.add(new ClassPathResource("test/", getClass()));
|
||||
resourcePaths.add(new ClassPathResource("testalternatepath/", getClass()));
|
||||
|
||||
|
||||
Map<String, ResourceHttpRequestHandler> urlMap = new HashMap<String, ResourceHttpRequestHandler>();
|
||||
handler = new ResourceHttpRequestHandler();
|
||||
handler.setLocations(resourcePaths);
|
||||
urlMap.put("/resources/**", handler);
|
||||
|
||||
|
||||
mapping = new SimpleUrlHandlerMapping();
|
||||
mapping.setUrlMap(urlMap);
|
||||
}
|
||||
|
||||
|
||||
private void resetMapper() {
|
||||
mapper = new ResourceUrlMapper();
|
||||
mapper.postProcessAfterInitialization(mapping, "resourceMapping");
|
||||
mapper.onApplicationEvent(null);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getStaticResourceUrl() {
|
||||
resetMapper();
|
||||
|
||||
|
||||
String url = mapper.getUrlForResource("/resources/foo.css");
|
||||
assertEquals("/resources/foo.css", url);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getFingerprintedResourceUrl() {
|
||||
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
|
||||
resolvers.add(new FingerprintingResourceResolver());
|
||||
resolvers.add(new FingerprintResourceResolver());
|
||||
resolvers.add(new PathResourceResolver());
|
||||
handler.setResourceResolvers(resolvers);
|
||||
resetMapper();
|
||||
|
||||
|
||||
String url = mapper.getUrlForResource("/resources/foo.css");
|
||||
assertEquals("/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css", url);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getExtensionMappedResourceUrl() {
|
||||
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
|
||||
resolvers.add(new ExtensionMappingResourceResolver());
|
||||
resolvers.add(new PathResourceResolver());
|
||||
handler.setResourceResolvers(resolvers);
|
||||
resetMapper();
|
||||
|
||||
|
||||
String url = mapper.getUrlForResource("/resources/zoo.css");
|
||||
assertEquals("/resources/zoo.css", url);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user