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));
}
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();
}
Aggregations