Asset pipeline proposal.

This commit is contained in:
Jeremy Grelle
2013-09-22 15:17:15 -04:00
committed by Rossen Stoyanchev
parent 8abe949734
commit 61e61bd5fd
27 changed files with 1417 additions and 28 deletions

View File

@@ -1 +0,0 @@
function foo() { console.log("hello bar"); }

View File

@@ -1 +0,0 @@
function foo() { console.log("hello world"); }

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* 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.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
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());
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());
Resource resolved = resolver.resolveAndTransform(null, resourceId, locations);
assertEquals(resource, resolved);
}
@Test
public void resolveLessUrl() {
String resourceId = "zoo.css";
String url = "zoo.css";
assertEquals(url, resolver.resolveUrl(resourceId, locations));
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.resource;
import java.lang.reflect.Method;
import java.util.ArrayList;
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 org.springframework.util.DigestUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ReflectionUtils;
import static org.junit.Assert.*;
/**
*
* @author Jeremy Grelle
*/
public class FingerprintingResourceResolverTests {
private ResourceResolverChain chain;
private FingerprintingResourceResolver resolver = new FingerprintingResourceResolver();
private List<Resource> locations;
@Before
public void setUp() {
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
resolvers.add(resolver);
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 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);
}
@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);
}
@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);
}
@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);
}
@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);
assertEquals(hash, result);
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPOutputStream;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.util.FileCopyUtils;
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);
Resource jsFile = new FileSystemResource(location.createRelative("/js/foo.js").getFile());
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());
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();
request.addHeader("Accept-Encoding", "gzip");
String file = "js/foo.js";
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);
}
@Test
public void resolveFingerprintedGzippedFile() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Accept-Encoding", "gzip");
String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css";
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);
}
}

View File

@@ -43,7 +43,7 @@ public class ResourceHttpRequestHandlerTests {
private ResourceHttpRequestHandler handler;
@Before
public void setUp() {
public void setUp() throws Exception {
List<Resource> resourcePaths = new ArrayList<Resource>();
resourcePaths.add(new ClassPathResource("test/", getClass()));
resourcePaths.add(new ClassPathResource("testalternatepath/", getClass()));
@@ -51,6 +51,7 @@ public class ResourceHttpRequestHandlerTests {
handler.setLocations(resourcePaths);
handler.setCacheSeconds(3600);
handler.setServletContext(new TestServletContext());
handler.afterPropertiesSet();
}
@Test

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* 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.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
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());
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());
handler.setResourceResolvers(resolvers);
resetMapper();
String url = mapper.getUrlForResource("/resources/zoo.css");
assertEquals("/resources/zoo.css", url);
}
}