Search in sources :

Example 26 with ClassPathResource

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

the class CssLinkResourceTransformerTests method setup.

@Before
public void setup() {
    ClassPathResource allowedLocation = new ClassPathResource("test/", getClass());
    ResourceWebHandler resourceHandler = new ResourceWebHandler();
    ResourceUrlProvider resourceUrlProvider = new ResourceUrlProvider();
    resourceUrlProvider.setHandlerMap(Collections.singletonMap("/static/**", resourceHandler));
    VersionResourceResolver versionResolver = new VersionResourceResolver();
    versionResolver.setStrategyMap(Collections.singletonMap("/**", new ContentVersionStrategy()));
    PathResourceResolver pathResolver = new PathResourceResolver();
    pathResolver.setAllowedLocations(allowedLocation);
    List<ResourceResolver> resolvers = Arrays.asList(versionResolver, pathResolver);
    CssLinkResourceTransformer cssLinkResourceTransformer = new CssLinkResourceTransformer();
    cssLinkResourceTransformer.setResourceUrlProvider(resourceUrlProvider);
    List<ResourceTransformer> transformers = Collections.singletonList(cssLinkResourceTransformer);
    resourceHandler.setResourceResolvers(resolvers);
    resourceHandler.setResourceTransformers(transformers);
    resourceHandler.setLocations(Collections.singletonList(allowedLocation));
    ResourceResolverChain resolverChain = new DefaultResourceResolverChain(resolvers);
    this.transformerChain = new DefaultResourceTransformerChain(resolverChain, transformers);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Before(org.junit.Before)

Example 27 with ClassPathResource

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

the class CssLinkResourceTransformerTests method transformWithGzippedResource.

@Test
public void transformWithGzippedResource() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("/static/main.css").toExchange();
    Resource original = new ClassPathResource("test/main.css", getClass());
    createTempCopy("main.css", "main.css.gz");
    GzipResourceResolver.GzippedResource expected = new GzipResourceResolver.GzippedResource(original);
    StepVerifier.create(this.transformerChain.transform(exchange, expected)).expectNext(expected).expectComplete().verify();
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 28 with ClassPathResource

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

the class GzipResourceResolverTests method resolveWithNullRequest.

// SPR-13149
@Test
public void resolveWithNullRequest() throws IOException {
    String file = "js/foo.js";
    Resource resolved = this.resolver.resolveResource(null, file, this.locations).block(TIMEOUT);
    String gzFile = file + ".gz";
    Resource gzResource = new ClassPathResource("test/" + gzFile, getClass());
    assertEquals(gzResource.getDescription(), resolved.getDescription());
    assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
    assertTrue("Expected " + resolved + " to be of type " + HttpResource.class, resolved instanceof HttpResource);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 29 with ClassPathResource

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

the class GzipResourceResolverTests method createGzFile.

private static void createGzFile(String filePath) throws IOException {
    Resource location = new ClassPathResource("test/", GzipResourceResolverTests.class);
    Resource fileResource = new FileSystemResource(location.createRelative(filePath).getFile());
    Path gzFilePath = Paths.get(fileResource.getFile().getAbsolutePath() + ".gz");
    Files.deleteIfExists(gzFilePath);
    File gzFile = Files.createFile(gzFilePath).toFile();
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
    FileCopyUtils.copy(fileResource.getInputStream(), out);
    gzFile.deleteOnExit();
}
Also used : Path(java.nio.file.Path) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 30 with ClassPathResource

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

the class GzipResourceResolverTests method resolveFromCacheWithEncodingVariants.

@Test
public void resolveFromCacheWithEncodingVariants() throws IOException {
    MockServerWebExchange exchange = MockServerHttpRequest.get("").header("Accept-Encoding", "gzip").toExchange();
    String file = "js/foo.js";
    Resource resolved = this.resolver.resolveResource(exchange, file, this.locations).block(TIMEOUT);
    String gzFile = file + ".gz";
    Resource gzResource = new ClassPathResource("test/" + gzFile, getClass());
    assertEquals(gzResource.getDescription(), resolved.getDescription());
    assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
    assertTrue("Expected " + resolved + " to be of type " + HttpResource.class, resolved instanceof HttpResource);
    // resolved resource is now cached in CachingResourceResolver
    exchange = MockServerHttpRequest.get("/js/foo.js").toExchange();
    resolved = this.resolver.resolveResource(exchange, file, this.locations).block(TIMEOUT);
    Resource resource = new ClassPathResource("test/" + file, getClass());
    assertEquals(resource.getDescription(), resolved.getDescription());
    assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
    assertFalse("Expected " + resolved + " to *not* be of type " + HttpResource.class, resolved instanceof HttpResource);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

ClassPathResource (org.springframework.core.io.ClassPathResource)1441 Test (org.junit.jupiter.api.Test)558 Resource (org.springframework.core.io.Resource)314 Test (org.junit.Test)274 lombok.val (lombok.val)159 List (java.util.List)137 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)129 IOException (java.io.IOException)118 InputStream (java.io.InputStream)105 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)104 File (java.io.File)91 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)87 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)78 ArrayList (java.util.ArrayList)77 Bean (org.springframework.context.annotation.Bean)61 FileSystemResource (org.springframework.core.io.FileSystemResource)61 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)60 Path (java.nio.file.Path)51 Map (java.util.Map)47 BeforeEach (org.junit.jupiter.api.BeforeEach)45