Search in sources :

Example 1 with FileDataSource

use of jakarta.activation.FileDataSource in project spring-framework by spring-projects.

the class Jaxb2MarshallerTests method marshalAttachments.

@Test
void marshalAttachments() throws Exception {
    marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(BinaryObject.class);
    marshaller.setMtomEnabled(true);
    marshaller.afterPropertiesSet();
    MimeContainer mimeContainer = mock(MimeContainer.class);
    Resource logo = new ClassPathResource("spring-ws.png", getClass());
    DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
    given(mimeContainer.convertToXopPackage()).willReturn(true);
    byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream());
    BinaryObject object = new BinaryObject(bytes, dataHandler);
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, new StreamResult(writer), mimeContainer);
    assertThat(writer.toString().length() > 0).as("No XML written").isTrue();
    verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class));
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) FileDataSource(jakarta.activation.FileDataSource) DataHandler(jakarta.activation.DataHandler) ClassPathResource(org.springframework.core.io.ClassPathResource) MimeContainer(org.springframework.oxm.mime.MimeContainer) Test(org.junit.jupiter.api.Test)

Example 2 with FileDataSource

use of jakarta.activation.FileDataSource in project spring-framework by spring-projects.

the class Jaxb2UnmarshallerTests method marshalAttachments.

@Test
public void marshalAttachments() throws Exception {
    unmarshaller = new Jaxb2Marshaller();
    unmarshaller.setClassesToBeBound(BinaryObject.class);
    unmarshaller.setMtomEnabled(true);
    unmarshaller.afterPropertiesSet();
    MimeContainer mimeContainer = mock(MimeContainer.class);
    Resource logo = new ClassPathResource("spring-ws.png", getClass());
    DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
    given(mimeContainer.isXopPackage()).willReturn(true);
    given(mimeContainer.getAttachment("<6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws>")).willReturn(dataHandler);
    given(mimeContainer.getAttachment("<99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws>")).willReturn(dataHandler);
    given(mimeContainer.getAttachment("696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png")).willReturn(dataHandler);
    String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" + "<xop:Include href='cid:6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" + "</bytes>" + "<dataHandler>" + "<xop:Include href='cid:99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" + "</dataHandler>" + "<swaDataHandler>696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png</swaDataHandler>" + "</binaryObject>";
    StringReader reader = new StringReader(content);
    Object result = unmarshaller.unmarshal(new StreamSource(reader), mimeContainer);
    boolean condition = result instanceof BinaryObject;
    assertThat(condition).as("Result is not a BinaryObject").isTrue();
    BinaryObject object = (BinaryObject) result;
    assertThat(object.getBytes()).as("bytes property not set").isNotNull();
    assertThat(object.getBytes().length > 0).as("bytes property not set").isTrue();
    assertThat(object.getSwaDataHandler()).as("datahandler property not set").isNotNull();
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) ClassPathResource(org.springframework.core.io.ClassPathResource) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) FileDataSource(jakarta.activation.FileDataSource) StringReader(java.io.StringReader) DataHandler(jakarta.activation.DataHandler) ClassPathResource(org.springframework.core.io.ClassPathResource) MimeContainer(org.springframework.oxm.mime.MimeContainer) Test(org.junit.jupiter.api.Test)

Aggregations

DataHandler (jakarta.activation.DataHandler)2 FileDataSource (jakarta.activation.FileDataSource)2 Test (org.junit.jupiter.api.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Resource (org.springframework.core.io.Resource)2 MimeContainer (org.springframework.oxm.mime.MimeContainer)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1