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.transport.mail;
18  
19  import java.net.URI;
20  import javax.xml.namespace.QName;
21  import javax.xml.soap.MessageFactory;
22  import javax.xml.soap.SOAPConstants;
23  import javax.xml.soap.SOAPMessage;
24  
25  import org.springframework.ws.soap.SoapMessage;
26  import org.springframework.ws.soap.saaj.SaajSoapMessage;
27  import org.springframework.ws.transport.WebServiceConnection;
28  
29  import org.junit.After;
30  import org.junit.Assert;
31  import org.junit.Before;
32  import org.junit.Test;
33  import org.jvnet.mock_javamail.Mailbox;
34  
35  public class MailMessageSenderIntegrationTest {
36  
37      private MailMessageSender messageSender;
38  
39      private MessageFactory messageFactory;
40  
41      private static final String SOAP_ACTION = "http://springframework.org/DoIt";
42  
43      @Before
44      public void setUp() throws Exception {
45          messageSender = new MailMessageSender();
46          messageSender.setFrom("Spring-WS SOAP Client <[email protected]>");
47          messageSender.setTransportUri("smtp://smtp.example.com");
48          messageSender.setStoreUri("imap://imap.example.com/INBOX");
49          messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
50          messageSender.afterPropertiesSet();
51      }
52  
53      @After
54      public void tearDown() throws Exception {
55          Mailbox.clearAll();
56      }
57  
58      @Test
59      public void testSendAndReceiveQueueNoResponse() throws Exception {
60          WebServiceConnection connection = null;
61          try {
62              URI mailTo = new URI("mailto:[email protected]?subject=SOAP%20Test");
63              connection = messageSender.createConnection(mailTo);
64              SOAPMessage saajMessage = messageFactory.createMessage();
65              saajMessage.getSOAPBody().addBodyElement(new QName("http://springframework.org", "test"));
66              SoapMessage soapRequest = new SaajSoapMessage(saajMessage);
67              soapRequest.setSoapAction(SOAP_ACTION);
68              connection.send(soapRequest);
69              Assert.assertEquals("No mail message sent", 1, Mailbox.get("[email protected]").size());
70          }
71          finally {
72              if (connection != null) {
73                  connection.close();
74              }
75          }
76      }
77  
78  }