01: /*
02: * Copyright 2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
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
29: AbstractWebServiceMessageTestCase {
30:
31: protected MimeMessage mimeMessage;
32:
33: private Resource picture;
34:
35: private String contentId;
36:
37: private String contentType;
38:
39: protected final WebServiceMessage createWebServiceMessage()
40: throws Exception {
41: mimeMessage = createMimeMessage();
42: picture = new ClassPathResource("spring-ws.png",
43: AbstractMimeMessageTestCase.class);
44: contentId = "spring-ws";
45: contentType = "image/png";
46: return mimeMessage;
47: }
48:
49: protected abstract MimeMessage createMimeMessage() throws Exception;
50:
51: public void testEmptyMessage() throws Exception {
52: Iterator iterator = mimeMessage.getAttachments();
53: assertFalse("Empty MimeMessage has attachments", iterator
54: .hasNext());
55: }
56:
57: public void testAddAttachment() throws Exception {
58: Attachment attachment = mimeMessage.addAttachment(contentId,
59: picture, contentType);
60: testAttachment(attachment);
61: }
62:
63: public void testGetAttachment() throws Exception {
64: mimeMessage.addAttachment(contentId, picture, contentType);
65: Attachment attachment = mimeMessage.getAttachment(contentId);
66: assertNotNull("Not Attachment found", attachment);
67: testAttachment(attachment);
68: }
69:
70: public void testGetAttachments() throws Exception {
71: mimeMessage.addAttachment(contentId, picture, contentType);
72: Iterator iterator = mimeMessage.getAttachments();
73: assertNotNull("Attachment iterator is null", iterator);
74: assertTrue("Attachment iterator has no elements", iterator
75: .hasNext());
76: Attachment attachment = (Attachment) iterator.next();
77: testAttachment(attachment);
78: assertFalse("Attachment iterator has too many elements",
79: iterator.hasNext());
80: }
81:
82: private void testAttachment(Attachment attachment)
83: throws IOException {
84: assertEquals("Invalid content id", contentId, attachment
85: .getContentId());
86: assertEquals("Invalid content type", contentType, attachment
87: .getContentType());
88: assertTrue("Invalid size", attachment.getSize() != 0);
89: byte[] contents = FileCopyUtils.copyToByteArray(attachment
90: .getInputStream());
91: assertTrue("No contents", contents.length > 0);
92: }
93:
94: }
|