1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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 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  }