}
@Validated @Test
public void testRemoveAttachements() {
Iterator iterator = null;
AttachmentPart ap1 = null;
AttachmentPart ap2 = null;
AttachmentPart ap3 = null;
try {
MessageFactory fac = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
//MessageFactory fac = MessageFactory.newInstance();
SOAPMessage msg = fac.createMessage();
SOAPPart soapPart = msg.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
SOAPFault sf = body.addFault();
InputStream in1 = TestUtils.getTestFile("attach.xml");
ap1 = msg.createAttachmentPart(in1, "text/xml");
msg.addAttachmentPart(ap1);
InputStream in2 = TestUtils.getTestFile("axis2.xml");
ap2 = msg.createAttachmentPart(in2, "text/xml");
msg.addAttachmentPart(ap2);
InputStream in3 = TestUtils.getTestFile("axis2.xml");
ap3 = msg.createAttachmentPart(in3, "text/plain");
msg.addAttachmentPart(ap3);
//get all attachments
iterator = msg.getAttachments();
int cnt = 0;
while (iterator.hasNext()) {
cnt++;
iterator.next();
}
assertEquals(cnt, 3);
//remove just the text/xml attachments
MimeHeaders mhs = new MimeHeaders();
mhs.addHeader("Content-Type", "text/xml");
msg.removeAttachments(mhs);
//get all attachments
iterator = msg.getAttachments();
cnt = 0;
iterator = msg.getAttachments();
while (iterator.hasNext()) {
cnt++;
iterator.next();
}
assertEquals(cnt, 1);
iterator = msg.getAttachments();
AttachmentPart ap = (AttachmentPart)iterator.next();
String ctype = ap.getContentType();
assertTrue(ctype.equals("text/plain"));
} catch (Exception e) {
fail("Exception: " + e);
}