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 import org.junit.Test;
29
30 import static org.junit.Assert.*;
31
32 public abstract class AbstractMimeMessageTestCase extends AbstractWebServiceMessageTestCase {
33
34 protected MimeMessage mimeMessage;
35
36 private Resource picture;
37
38 private String contentId;
39
40 private String contentType;
41
42 @Override
43 protected final WebServiceMessage createWebServiceMessage() throws Exception {
44 mimeMessage = createMimeMessage();
45 picture = new ClassPathResource("spring-ws.png", AbstractMimeMessageTestCase.class);
46 contentId = "spring-ws";
47 contentType = "image/png";
48 return mimeMessage;
49 }
50
51 protected abstract MimeMessage createMimeMessage() throws Exception;
52
53 @Test
54 public void testEmptyMessage() throws Exception {
55 Iterator<Attachment> iterator = mimeMessage.getAttachments();
56 assertFalse("Empty MimeMessage has attachments", iterator.hasNext());
57 }
58
59 @Test
60 public void testAddAttachment() throws Exception {
61 Attachment attachment = mimeMessage.addAttachment(contentId, picture, contentType);
62 testAttachment(attachment);
63 }
64
65 @Test
66 public void testGetAttachment() throws Exception {
67 mimeMessage.addAttachment(contentId, picture, contentType);
68 Attachment attachment = mimeMessage.getAttachment(contentId);
69 assertNotNull("Not Attachment found", attachment);
70 testAttachment(attachment);
71 }
72
73 @Test
74 public void testGetAttachments() throws Exception {
75 mimeMessage.addAttachment(contentId, picture, contentType);
76 Iterator<Attachment> iterator = mimeMessage.getAttachments();
77 assertNotNull("Attachment iterator is null", iterator);
78 assertTrue("Attachment iterator has no elements", iterator.hasNext());
79 Attachment attachment = (Attachment) iterator.next();
80 testAttachment(attachment);
81 assertFalse("Attachment iterator has too many elements", iterator.hasNext());
82 }
83
84 private void testAttachment(Attachment attachment) throws IOException {
85 assertEquals("Invalid content id", contentId, attachment.getContentId());
86 assertEquals("Invalid content type", contentType, attachment.getContentType());
87 assertTrue("Invalid size", attachment.getSize() != 0);
88 byte[] contents = FileCopyUtils.copyToByteArray(attachment.getInputStream());
89 assertTrue("No contents", contents.length > 0);
90 }
91
92 }