Introduce support for webjars-locator-lite
This commit introduces support for org.webjars:webjars-locator-lite via a new LiteWebJarsResourceResolver in Spring MVC and WebFlux, and deprecates WebJarsResourceResolver which is performing a classpath scanning that slows down application startup. Closes gh-27619
This commit is contained in:
@@ -439,6 +439,7 @@ public class MvcNamespaceTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("removal")
|
||||
void testResourcesWithResolversTransformers() {
|
||||
loadBeanDefinitions("mvc-config-resources-chain.xml");
|
||||
|
||||
|
||||
@@ -31,12 +31,12 @@ import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.resource.CachingResourceResolver;
|
||||
import org.springframework.web.servlet.resource.CachingResourceTransformer;
|
||||
import org.springframework.web.servlet.resource.CssLinkResourceTransformer;
|
||||
import org.springframework.web.servlet.resource.LiteWebJarsResourceResolver;
|
||||
import org.springframework.web.servlet.resource.PathResourceResolver;
|
||||
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
|
||||
import org.springframework.web.servlet.resource.ResourceResolver;
|
||||
import org.springframework.web.servlet.resource.ResourceTransformer;
|
||||
import org.springframework.web.servlet.resource.VersionResourceResolver;
|
||||
import org.springframework.web.servlet.resource.WebJarsResourceResolver;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
||||
import org.springframework.web.testfixture.servlet.MockServletContext;
|
||||
@@ -49,6 +49,7 @@ import static org.mockito.Mockito.mock;
|
||||
* Tests for {@link ResourceHandlerRegistry}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
class ResourceHandlerRegistryTests {
|
||||
|
||||
@@ -128,6 +129,7 @@ class ResourceHandlerRegistryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("removal")
|
||||
void resourceChain() {
|
||||
ResourceResolver mockResolver = mock();
|
||||
ResourceTransformer mockTransformer = mock();
|
||||
@@ -138,7 +140,7 @@ class ResourceHandlerRegistryTests {
|
||||
zero -> assertThat(zero).isInstanceOfSatisfying(CachingResourceResolver.class,
|
||||
cachingResolver -> assertThat(cachingResolver.getCache()).isInstanceOf(ConcurrentMapCache.class)),
|
||||
one -> assertThat(one).isEqualTo(mockResolver),
|
||||
two -> assertThat(two).isInstanceOf(WebJarsResourceResolver.class),
|
||||
two -> assertThat(two).isInstanceOf(LiteWebJarsResourceResolver.class),
|
||||
three -> assertThat(three).isInstanceOf(PathResourceResolver.class));
|
||||
assertThat(handler.getResourceTransformers()).satisfiesExactly(
|
||||
zero -> assertThat(zero).isInstanceOf(CachingResourceTransformer.class),
|
||||
@@ -151,7 +153,7 @@ class ResourceHandlerRegistryTests {
|
||||
|
||||
ResourceHttpRequestHandler handler = getHandler("/resources/**");
|
||||
assertThat(handler.getResourceResolvers()).hasExactlyElementsOfTypes(
|
||||
WebJarsResourceResolver.class, PathResourceResolver.class);
|
||||
LiteWebJarsResourceResolver.class, PathResourceResolver.class);
|
||||
assertThat(handler.getResourceTransformers()).isEmpty();
|
||||
}
|
||||
|
||||
@@ -167,7 +169,7 @@ class ResourceHandlerRegistryTests {
|
||||
assertThat(handler.getResourceResolvers()).satisfiesExactly(
|
||||
zero -> assertThat(zero).isInstanceOf(CachingResourceResolver.class),
|
||||
one -> assertThat(one).isSameAs(versionResolver),
|
||||
two -> assertThat(two).isInstanceOf(WebJarsResourceResolver.class),
|
||||
two -> assertThat(two).isInstanceOf(LiteWebJarsResourceResolver.class),
|
||||
three -> assertThat(three).isInstanceOf(PathResourceResolver.class));
|
||||
assertThat(handler.getResourceTransformers()).hasExactlyElementsOfTypes(
|
||||
CachingResourceTransformer.class, CssLinkResourceTransformer.class);
|
||||
@@ -177,7 +179,7 @@ class ResourceHandlerRegistryTests {
|
||||
void resourceChainWithOverrides() {
|
||||
CachingResourceResolver cachingResolver = mock();
|
||||
VersionResourceResolver versionResolver = mock();
|
||||
WebJarsResourceResolver webjarsResolver = mock();
|
||||
LiteWebJarsResourceResolver webjarsResolver = mock();
|
||||
PathResourceResolver pathResourceResolver = new PathResourceResolver();
|
||||
CachingResourceTransformer cachingTransformer = mock();
|
||||
CssLinkResourceTransformer cssLinkTransformer = new CssLinkResourceTransformer();
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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
|
||||
*
|
||||
* https://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.List;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link LiteWebJarsResourceResolver}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
class LiteWebJarsResourceResolverTests {
|
||||
|
||||
private List<Resource> locations = List.of(new ClassPathResource("/META-INF/resources/webjars"));
|
||||
|
||||
// for this to work, an actual WebJar must be on the test classpath
|
||||
private LiteWebJarsResourceResolver resolver = new LiteWebJarsResourceResolver();
|
||||
|
||||
private ResourceResolverChain chain = mock();
|
||||
|
||||
private HttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
|
||||
@Test
|
||||
void resolveUrlExisting() {
|
||||
String file = "/foo/2.3/foo.txt";
|
||||
given(this.chain.resolveUrlPath(file, this.locations)).willReturn(file);
|
||||
|
||||
String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain);
|
||||
|
||||
assertThat(actual).isEqualTo(file);
|
||||
verify(this.chain, times(1)).resolveUrlPath(file, this.locations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveUrlExistingNotInJarFile() {
|
||||
String file = "foo/foo.txt";
|
||||
given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null);
|
||||
|
||||
String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain);
|
||||
|
||||
assertThat(actual).isNull();
|
||||
verify(this.chain, times(1)).resolveUrlPath(file, this.locations);
|
||||
verify(this.chain, never()).resolveUrlPath("foo/2.3/foo.txt", this.locations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveUrlWebJarResource() {
|
||||
String file = "underscorejs/underscore.js";
|
||||
String expected = "underscorejs/1.8.3/underscore.js";
|
||||
given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null);
|
||||
given(this.chain.resolveUrlPath(expected, this.locations)).willReturn(expected);
|
||||
|
||||
String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain);
|
||||
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
verify(this.chain, times(1)).resolveUrlPath(file, this.locations);
|
||||
verify(this.chain, times(1)).resolveUrlPath(expected, this.locations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveUrlWebJarResourceNotFound() {
|
||||
String file = "something/something.js";
|
||||
given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null);
|
||||
|
||||
String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain);
|
||||
|
||||
assertThat(actual).isNull();
|
||||
verify(this.chain, times(1)).resolveUrlPath(file, this.locations);
|
||||
verify(this.chain, never()).resolveUrlPath(null, this.locations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveResourceExisting() {
|
||||
Resource expected = mock();
|
||||
String file = "foo/2.3/foo.txt";
|
||||
given(this.chain.resolveResource(this.request, file, this.locations)).willReturn(expected);
|
||||
|
||||
Resource actual = this.resolver.resolveResource(this.request, file, this.locations, this.chain);
|
||||
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
verify(this.chain, times(1)).resolveResource(this.request, file, this.locations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveResourceNotFound() {
|
||||
String file = "something/something.js";
|
||||
given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null);
|
||||
|
||||
Resource actual = this.resolver.resolveResource(this.request, file, this.locations, this.chain);
|
||||
|
||||
assertThat(actual).isNull();
|
||||
verify(this.chain, times(1)).resolveResource(this.request, file, this.locations);
|
||||
verify(this.chain, never()).resolveResource(this.request, null, this.locations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveResourceWebJar() {
|
||||
Resource expected = mock();
|
||||
String file = "underscorejs/underscore.js";
|
||||
String expectedPath = "underscorejs/1.8.3/underscore.js";
|
||||
given(this.chain.resolveResource(this.request, expectedPath, this.locations)).willReturn(expected);
|
||||
|
||||
Resource actual = this.resolver.resolveResource(this.request, file, this.locations, this.chain);
|
||||
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
verify(this.chain, times(1)).resolveResource(this.request, file, this.locations);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -38,6 +38,7 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Brian Clozel
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
class WebJarsResourceResolverTests {
|
||||
|
||||
private List<Resource> locations = List.of(new ClassPathResource("/META-INF/resources/webjars"));
|
||||
|
||||
Reference in New Issue
Block a user