Search in sources :

Example 31 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 {
    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 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 : 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)

Example 32 with ClassPathResource

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

the class ResourceWebHandlerTests method testInvalidPath.

private void testInvalidPath(HttpMethod httpMethod) throws Exception {
    Resource location = new ClassPathResource("test/", getClass());
    this.handler.setLocations(Collections.singletonList(location));
    testInvalidPath(httpMethod, "../testsecret/secret.txt", location);
    testInvalidPath(httpMethod, "test/../../testsecret/secret.txt", location);
    testInvalidPath(httpMethod, ":/../../testsecret/secret.txt", location);
    location = new UrlResource(getClass().getResource("./test/"));
    this.handler.setLocations(Collections.singletonList(location));
    Resource secretResource = new UrlResource(getClass().getResource("testsecret/secret.txt"));
    String secretPath = secretResource.getURL().getPath();
    testInvalidPath(httpMethod, "file:" + secretPath, location);
    testInvalidPath(httpMethod, "/file:" + secretPath, location);
    testInvalidPath(httpMethod, "url:" + secretPath, location);
    testInvalidPath(httpMethod, "/url:" + secretPath, location);
    testInvalidPath(httpMethod, "////../.." + secretPath, location);
    testInvalidPath(httpMethod, "/%2E%2E/testsecret/secret.txt", location);
    testInvalidPath(httpMethod, "url:" + secretPath, location);
// The following tests fail with a MalformedURLException on Windows
// testInvalidPath(location, "/" + secretPath);
// testInvalidPath(location, "/  " + secretPath);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 33 with ClassPathResource

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

the class CastorUnmarshallerTests method setBothTargetClassesAndMapping.

@Test
public void setBothTargetClassesAndMapping() throws IOException {
    CastorMarshaller unmarshaller = new CastorMarshaller();
    unmarshaller.setMappingLocation(new ClassPathResource("order-mapping.xml", CastorMarshaller.class));
    unmarshaller.setTargetClasses(new Class[] { Order.class });
    unmarshaller.afterPropertiesSet();
    String xml = "<order>" + "<order-item id=\"1\" quantity=\"15\"/>" + "<order-item id=\"3\" quantity=\"20\"/>" + "</order>";
    Order order = (Order) unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
    assertEquals("Invalid amount of items", 2, order.getOrderItemCount());
    OrderItem item = order.getOrderItem(0);
    assertEquals("Invalid items", "1", item.getId());
    assertThat("Invalid items", item.getQuantity(), equalTo(15));
    item = order.getOrderItem(1);
    assertEquals("Invalid items", "3", item.getId());
    assertThat("Invalid items", item.getQuantity(), equalTo(20));
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 34 with ClassPathResource

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

the class HazelcastAutoConfigurationTests method explicitConfigFile.

@Test
public void explicitConfigFile() throws IOException {
    load("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/" + "hazelcast-specific.xml");
    HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class);
    assertThat(hazelcastInstance.getConfig().getConfigurationFile()).isEqualTo(new ClassPathResource("org/springframework/boot/autoconfigure/hazelcast" + "/hazelcast-specific.xml").getFile());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 35 with ClassPathResource

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

the class WebFluxAnnotationAutoConfigurationTests method shouldRegisterResourceHandlerMapping.

@Test
public void shouldRegisterResourceHandlerMapping() throws Exception {
    load();
    SimpleUrlHandlerMapping hm = this.context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
    assertThat(hm.getUrlMap().get("/**")).isInstanceOf(ResourceWebHandler.class);
    ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/**");
    assertThat(staticHandler.getLocations()).hasSize(5);
    assertThat(hm.getUrlMap().get("/webjars/**")).isInstanceOf(ResourceWebHandler.class);
    ResourceWebHandler webjarsHandler = (ResourceWebHandler) hm.getUrlMap().get("/webjars/**");
    assertThat(webjarsHandler.getLocations()).hasSize(1);
    assertThat(webjarsHandler.getLocations().get(0)).isEqualTo(new ClassPathResource("/META-INF/resources/webjars/"));
}
Also used : ResourceWebHandler(org.springframework.web.reactive.resource.ResourceWebHandler) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) 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