Search in sources :

Example 76 with ClassPathResource

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

the class GzipResourceResolverTests method resolveGzippedFile.

@Test
public void resolveGzippedFile() throws IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Accept-Encoding", "gzip");
    String file = "js/foo.js";
    Resource resolved = this.resolver.resolveResource(request, file, this.locations);
    String gzFile = file + ".gz";
    Resource resource = new ClassPathResource("test/" + gzFile, getClass());
    assertEquals(resource.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 : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 77 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) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 78 with ClassPathResource

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

the class GzipResourceResolverTests method setUp.

@Before
public void setUp() {
    this.cache = new ConcurrentMapCache("resourceCache");
    Map<String, VersionStrategy> versionStrategyMap = new HashMap<>();
    versionStrategyMap.put("/**", new ContentVersionStrategy());
    VersionResourceResolver versionResolver = new VersionResourceResolver();
    versionResolver.setStrategyMap(versionStrategyMap);
    List<ResourceResolver> resolvers = new ArrayList<>();
    resolvers.add(new CachingResourceResolver(this.cache));
    resolvers.add(new GzipResourceResolver());
    resolvers.add(versionResolver);
    resolvers.add(new PathResourceResolver());
    this.resolver = new DefaultResourceResolverChain(resolvers);
    this.locations = new ArrayList<>();
    this.locations.add(new ClassPathResource("test/", getClass()));
    this.locations.add(new ClassPathResource("testalternatepath/", getClass()));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClassPathResource(org.springframework.core.io.ClassPathResource) ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) Before(org.junit.Before)

Example 79 with ClassPathResource

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

the class ResourceHttpRequestHandlerTests method getResourceWithMediaTypeResolvedThroughServletContext.

// SPR-14368
@Test
public void getResourceWithMediaTypeResolvedThroughServletContext() throws Exception {
    MockServletContext servletContext = new MockServletContext() {

        @Override
        public String getMimeType(String filePath) {
            return "foo/bar";
        }

        @Override
        public String getVirtualServerName() {
            return null;
        }
    };
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setServletContext(servletContext);
    handler.setLocations(paths);
    handler.afterPropertiesSet();
    this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
    handler.handleRequest(this.request, this.response);
    assertEquals("foo/bar", this.response.getContentType());
    assertEquals("h1 { color:red; }", this.response.getContentAsString());
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServletContext(org.springframework.mock.web.test.MockServletContext) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 80 with ClassPathResource

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

the class ResourceTransformerSupportTests method setUp.

@Before
public void setUp() {
    VersionResourceResolver versionResolver = new VersionResourceResolver();
    versionResolver.setStrategyMap(Collections.singletonMap("/**", new ContentVersionStrategy()));
    PathResourceResolver pathResolver = new PathResourceResolver();
    pathResolver.setAllowedLocations(new ClassPathResource("test/", getClass()));
    List<ResourceResolver> resolvers = Arrays.asList(versionResolver, pathResolver);
    this.transformerChain = new DefaultResourceTransformerChain(new DefaultResourceResolverChain(resolvers), null);
    this.transformer = new TestResourceTransformerSupport();
    this.transformer.setResourceUrlProvider(createResourceUrlProvider(resolvers));
    this.request = new MockHttpServletRequest("GET", "");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) Before(org.junit.Before)

Aggregations

ClassPathResource (org.springframework.core.io.ClassPathResource)628 Test (org.junit.Test)404 Resource (org.springframework.core.io.Resource)182 DhisSpringTest (org.hisp.dhis.DhisSpringTest)85 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)80 List (java.util.List)63 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)62 Before (org.junit.Before)55 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)54 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)39 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)36 ArrayList (java.util.ArrayList)35 DataElement (org.hisp.dhis.dataelement.DataElement)32 File (java.io.File)29 UrlResource (org.springframework.core.io.UrlResource)29 TestBean (org.springframework.tests.sample.beans.TestBean)27 IOException (java.io.IOException)26 InputStream (java.io.InputStream)23 FileSystemResource (org.springframework.core.io.FileSystemResource)23 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)19