1   /*
2    * Copyright 2005-2010 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  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  }