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