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.support;
18  
19  import java.net.URI;
20  
21  import junit.framework.TestCase;
22  import org.easymock.MockControl;
23  
24  import org.springframework.ws.MockWebServiceMessage;
25  import org.springframework.ws.MockWebServiceMessageFactory;
26  import org.springframework.ws.context.MessageContext;
27  import org.springframework.ws.transport.WebServiceConnection;
28  import org.springframework.ws.transport.WebServiceMessageReceiver;
29  
30  public class WebServiceMessageReceiverObjectSupportTest extends TestCase {
31  
32      private WebServiceMessageReceiverObjectSupport receiverSupport;
33  
34      private MockControl connectionControl;
35  
36      private WebServiceConnection connectionMock;
37  
38      private MockWebServiceMessageFactory messageFactory;
39  
40      private MockWebServiceMessage request;
41  
42      protected void setUp() throws Exception {
43          receiverSupport = new MyReceiverSupport();
44          messageFactory = new MockWebServiceMessageFactory();
45          receiverSupport.setMessageFactory(messageFactory);
46          connectionControl = MockControl.createStrictControl(WebServiceConnection.class);
47          connectionMock = (WebServiceConnection) connectionControl.getMock();
48          request = new MockWebServiceMessage();
49      }
50  
51      public void testHandleConnectionResponse() throws Exception {
52          connectionControl.expectAndReturn(connectionMock.getUri(), new URI("http://example.com"));
53          connectionControl.expectAndReturn(connectionMock.receive(messageFactory), request);
54          connectionMock.send(new MockWebServiceMessage());
55          connectionControl.setMatcher(MockControl.ALWAYS_MATCHER);
56          connectionMock.close();
57  
58          connectionControl.replay();
59  
60          WebServiceMessageReceiver receiver = new WebServiceMessageReceiver() {
61  
62              public void receive(MessageContext messageContext) throws Exception {
63                  assertNotNull("No message context", messageContext);
64                  messageContext.getResponse();
65              }
66          };
67  
68          receiverSupport.handleConnection(connectionMock, receiver);
69  
70          connectionControl.verify();
71      }
72  
73      public void testHandleConnectionNoResponse() throws Exception {
74          connectionControl.expectAndReturn(connectionMock.getUri(), new URI("http://example.com"));
75          connectionControl.expectAndReturn(connectionMock.receive(messageFactory), request);
76          connectionMock.close();
77  
78          connectionControl.replay();
79  
80          WebServiceMessageReceiver receiver = new WebServiceMessageReceiver() {
81  
82              public void receive(MessageContext messageContext) throws Exception {
83                  assertNotNull("No message context", messageContext);
84              }
85          };
86  
87          receiverSupport.handleConnection(connectionMock, receiver);
88  
89          connectionControl.verify();
90      }
91  
92      private static class MyReceiverSupport extends WebServiceMessageReceiverObjectSupport {
93  
94      }
95  }