1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.mime;
18
19 import java.io.IOException;
20 import java.util.Iterator;
21
22 import org.springframework.core.io.ClassPathResource;
23 import org.springframework.core.io.Resource;
24 import org.springframework.util.FileCopyUtils;
25 import org.springframework.ws.AbstractWebServiceMessageTestCase;
26 import org.springframework.ws.WebServiceMessage;
27
28 public abstract class AbstractMimeMessageTestCase extends AbstractWebServiceMessageTestCase {
29
30 protected MimeMessage mimeMessage;
31
32 private Resource picture;
33
34 private String contentId;
35
36 private String contentType;
37
38 protected final WebServiceMessage createWebServiceMessage() throws Exception {
39 mimeMessage = createMimeMessage();
40 picture = new ClassPathResource("spring-ws.png", AbstractMimeMessageTestCase.class);
41 contentId = "spring-ws";
42 contentType = "image/png";
43 return mimeMessage;
44 }
45
46 protected abstract MimeMessage createMimeMessage() throws Exception;
47
48 public void testEmptyMessage() throws Exception {
49 Iterator iterator = mimeMessage.getAttachments();
50 assertFalse("Empty MimeMessage has attachments", iterator.hasNext());
51 }
52
53 public void testAddAttachment() throws Exception {
54 Attachment attachment = mimeMessage.addAttachment(contentId, picture, contentType);
55 testAttachment(attachment);
56 }
57
58 public void testGetAttachment() throws Exception {
59 mimeMessage.addAttachment(contentId, picture, contentType);
60 Attachment attachment = mimeMessage.getAttachment(contentId);
61 assertNotNull("Not Attachment found", attachment);
62 testAttachment(attachment);
63 }
64
65 public void testGetAttachments() throws Exception {
66 mimeMessage.addAttachment(contentId, picture, contentType);
67 Iterator iterator = mimeMessage.getAttachments();
68 assertNotNull("Attachment iterator is null", iterator);
69 assertTrue("Attachment iterator has no elements", iterator.hasNext());
70 Attachment attachment = (Attachment) iterator.next();
71 testAttachment(attachment);
72 assertFalse("Attachment iterator has too many elements", iterator.hasNext());
73 }
74
75 private void testAttachment(Attachment attachment) throws IOException {
76 assertEquals("Invalid content id", contentId, attachment.getContentId());
77 assertEquals("Invalid content type", contentType, attachment.getContentType());
78 assertTrue("Invalid size", attachment.getSize() != 0);
79 byte[] contents = FileCopyUtils.copyToByteArray(attachment.getInputStream());
80 assertTrue("No contents", contents.length > 0);
81 }
82
83 }