Search in sources :

Example 66 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class ResourceTransformerSupportTests method resolveUrlPath.

@Test
public void resolveUrlPath() throws Exception {
    this.request.setRequestURI("/context/servlet/resources/main.css");
    this.request.setContextPath("/context");
    this.request.setServletPath("/servlet");
    String resourcePath = "/context/servlet/resources/bar.css";
    Resource css = new ClassPathResource("test/main.css", getClass());
    String actual = this.transformer.resolveUrlPath(resourcePath, this.request, css, this.transformerChain);
    assertEquals("/context/servlet/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 67 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class ResourceTransformerSupportTests method resolveUrlPathWithRelativePathInParentDirectory.

@Test
public void resolveUrlPathWithRelativePathInParentDirectory() throws Exception {
    Resource imagePng = new ClassPathResource("test/images/image.png", getClass());
    String actual = this.transformer.resolveUrlPath("../bar.css", this.request, imagePng, this.transformerChain);
    assertEquals("../bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 68 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class ResourceTransformerSupportTests method resolveUrlPathWithRelativePath.

@Test
public void resolveUrlPathWithRelativePath() throws Exception {
    Resource css = new ClassPathResource("test/main.css", getClass());
    String actual = this.transformer.resolveUrlPath("bar.css", this.request, css, this.transformerChain);
    assertEquals("bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 69 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class VersionResourceResolverTests method resolveResourceVersionDoesNotMatch.

@Test
public void resolveResourceVersionDoesNotMatch() throws Exception {
    String versionFile = "bar-version.css";
    String version = "version";
    String file = "bar.css";
    Resource expected = new ClassPathResource("test/" + file, getClass());
    given(this.chain.resolveResource(null, versionFile, this.locations)).willReturn(null);
    given(this.chain.resolveResource(null, file, this.locations)).willReturn(expected);
    given(this.versionStrategy.extractVersion(versionFile)).willReturn(version);
    given(this.versionStrategy.removeVersion(versionFile, version)).willReturn(file);
    given(this.versionStrategy.getResourceVersion(expected)).willReturn("newer-version");
    this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy));
    Resource actual = this.resolver.resolveResourceInternal(null, versionFile, this.locations, this.chain);
    assertNull(actual);
    verify(this.versionStrategy, times(1)).getResourceVersion(expected);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 70 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class VersionResourceResolverTests method resolveResourceSuccess.

@Test
public void resolveResourceSuccess() throws Exception {
    String versionFile = "bar-version.css";
    String version = "version";
    String file = "bar.css";
    Resource expected = new ClassPathResource("test/" + file, getClass());
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/resources/bar-version.css");
    given(this.chain.resolveResource(request, versionFile, this.locations)).willReturn(null);
    given(this.chain.resolveResource(request, file, this.locations)).willReturn(expected);
    given(this.versionStrategy.extractVersion(versionFile)).willReturn(version);
    given(this.versionStrategy.removeVersion(versionFile, version)).willReturn(file);
    given(this.versionStrategy.getResourceVersion(expected)).willReturn(version);
    this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy));
    Resource actual = this.resolver.resolveResourceInternal(request, versionFile, this.locations, this.chain);
    assertEquals(expected.getFilename(), actual.getFilename());
    verify(this.versionStrategy, times(1)).getResourceVersion(expected);
    assertThat(actual, instanceOf(HttpResource.class));
    assertEquals("\"" + version + "\"", ((HttpResource) actual).getResponseHeaders().getETag());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

Resource (org.springframework.core.io.Resource)610 Test (org.junit.Test)257 ClassPathResource (org.springframework.core.io.ClassPathResource)231 IOException (java.io.IOException)103 FileSystemResource (org.springframework.core.io.FileSystemResource)77 UrlResource (org.springframework.core.io.UrlResource)68 File (java.io.File)64 ArrayList (java.util.ArrayList)58 ByteArrayResource (org.springframework.core.io.ByteArrayResource)49 InputStream (java.io.InputStream)46 InputStreamResource (org.springframework.core.io.InputStreamResource)31 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)30 URL (java.net.URL)25 HashMap (java.util.HashMap)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)18 ServletContextResource (org.springframework.web.context.support.ServletContextResource)18 Map (java.util.Map)17 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)17 ResourceLoader (org.springframework.core.io.ResourceLoader)16 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)16