View Javadoc

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.test.client;
18  
19  import java.io.IOException;
20  import java.net.URI;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import org.springframework.util.Assert;
25  import org.springframework.ws.WebServiceMessage;
26  import org.springframework.ws.WebServiceMessageFactory;
27  import org.springframework.ws.transport.WebServiceConnection;
28  
29  /**
30   * Mock implementation of [email protected] WebServiceConnection}. Implements [email protected] ResponseActions} to form a fluent API.
31   *
32   * @author Arjen Poutsma
33   * @author Lukas Krecan
34   * @since 2.0
35   */
36  class MockSenderConnection implements WebServiceConnection, ResponseActions {
37  
38      private final List<RequestMatcher> requestMatchers = new LinkedList<RequestMatcher>();
39  
40      private URI uri;
41  
42      private WebServiceMessage request;
43  
44      private ResponseCreator responseCreator;
45  
46      void addRequestMatcher(RequestMatcher requestMatcher) {
47          Assert.notNull(requestMatcher, "'requestMatcher' must not be null");
48          requestMatchers.add(requestMatcher);
49      }
50  
51      void setUri(URI uri) {
52          Assert.notNull(uri, "'uri' must not be null");
53          this.uri = uri;
54      }
55  
56      // ResponseActions implementation
57  
58      public ResponseActions andExpect(RequestMatcher requestMatcher) {
59          addRequestMatcher(requestMatcher);
60          return this;
61      }
62  
63      public void andRespond(ResponseCreator responseCreator) {
64          Assert.notNull(responseCreator, "'responseCreator' must not be null");
65          this.responseCreator = responseCreator;
66      }
67  
68      // FaultAwareWebServiceConnection implementation
69  
70      @SuppressWarnings("unchecked")
71      public void send(WebServiceMessage message) throws IOException {
72          if (!requestMatchers.isEmpty()) {
73              for (RequestMatcher requestMatcher : requestMatchers) {
74                  requestMatcher.match(uri, message);
75              }
76          }
77          else {
78              throw new AssertionError("Unexpected send() for [" + message + "]");
79          }
80          this.request = message;
81      }
82  
83      @SuppressWarnings("unchecked")
84      public WebServiceMessage receive(WebServiceMessageFactory messageFactory) throws IOException {
85          if (responseCreator != null) {
86              return responseCreator.createResponse(uri, request, messageFactory);
87          }
88          else {
89              return null;
90          }
91      }
92  
93      public URI getUri() {
94          return uri;
95      }
96  
97      public boolean hasError() throws IOException {
98          return responseCreator instanceof ErrorResponseCreator;
99      }
100 
101     public String getErrorMessage() throws IOException {
102         if (responseCreator instanceof ErrorResponseCreator) {
103             return ((ErrorResponseCreator) responseCreator).getErrorMessage();
104         }
105         else {
106             return null;
107         }
108     }
109 
110     public void close() throws IOException {
111         requestMatchers.clear();
112         request = null;
113         responseCreator = null;
114         uri = null;
115     }
116 
117 }