Search in sources :

Example 21 with ClassPathResource

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

the class Jaxb2CollectionHttpMessageConverterTests method readXmlRootElementExternalEntityEnabled.

@Test
@SuppressWarnings("unchecked")
public void readXmlRootElementExternalEntityEnabled() throws Exception {
    Resource external = new ClassPathResource("external.txt", getClass());
    String content = "<!DOCTYPE root [" + "  <!ELEMENT external ANY >\n" + "  <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" + "  <list><rootElement><type s=\"1\"/><external>&ext;</external></rootElement></list>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
    Jaxb2CollectionHttpMessageConverter<?> c = new Jaxb2CollectionHttpMessageConverter<Collection<Object>>() {

        @Override
        protected XMLInputFactory createXmlInputFactory() {
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
            return inputFactory;
        }
    };
    Collection<RootElement> result = c.read(rootElementListType, null, inputMessage);
    assertEquals(1, result.size());
    assertEquals("Foo Bar", result.iterator().next().external);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 22 with ClassPathResource

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

the class ZeroCopyIntegrationTests method zeroCopy.

@Test
public void zeroCopy() throws Exception {
    // Zero-copy only does not support servlet
    assumeTrue(server instanceof ReactorHttpServer || server instanceof UndertowHttpServer);
    RestTemplate restTemplate = new RestTemplate();
    RequestEntity<?> request = RequestEntity.get(new URI("http://localhost:" + port)).build();
    ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class);
    Resource logo = new ClassPathResource("spring.png", ZeroCopyIntegrationTests.class);
    assertTrue(response.hasBody());
    assertEquals(logo.contentLength(), response.getHeaders().getContentLength());
    assertEquals(logo.contentLength(), response.getBody().length);
    assertEquals(MediaType.IMAGE_PNG, response.getHeaders().getContentType());
}
Also used : ReactorHttpServer(org.springframework.http.server.reactive.bootstrap.ReactorHttpServer) RestTemplate(org.springframework.web.client.RestTemplate) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) UndertowHttpServer(org.springframework.http.server.reactive.bootstrap.UndertowHttpServer) URI(java.net.URI) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 23 with ClassPathResource

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

the class AsyncRestTemplateIntegrationTests method multipart.

@Test
public void multipart() throws Exception {
    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
    parts.add("name 1", "value 1");
    parts.add("name 2", "value 2+1");
    parts.add("name 2", "value 2+2");
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    parts.add("logo", logo);
    HttpEntity<MultiValueMap<String, Object>> requestBody = new HttpEntity<>(parts);
    Future<URI> future = template.postForLocation(baseUrl + "/multipart", requestBody);
    future.get();
}
Also used : HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) URI(java.net.URI) ClassPathResource(org.springframework.core.io.ClassPathResource) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 24 with ClassPathResource

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

the class RestTemplateIntegrationTests method multipart.

@Test
public void multipart() throws UnsupportedEncodingException {
    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
    parts.add("name 1", "value 1");
    parts.add("name 2", "value 2+1");
    parts.add("name 2", "value 2+2");
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    parts.add("logo", logo);
    template.postForLocation(baseUrl + "/multipart", parts);
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 25 with ClassPathResource

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

the class CssLinkResourceTransformerTests method transformWithNonCssResource.

@Test
public void transformWithNonCssResource() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("/static/images/image.png").toExchange();
    Resource expected = new ClassPathResource("test/images/image.png", getClass());
    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)

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