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.Iterator;
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  import org.springframework.util.Assert;
26  import org.springframework.ws.transport.WebServiceMessageSender;
27  
28  /**
29   * Mock implementation of {@link WebServiceMessageSender}. Contains a list of expected {@link MockSenderConnection}s,
30   * and iterates over those.
31   *
32   * @author Arjen Poutsma
33   * @author Lukas Krecan
34   * @since 2.0
35   */
36  class MockWebServiceMessageSender implements WebServiceMessageSender {
37  
38      private final List<MockSenderConnection> expectedConnections = new LinkedList<MockSenderConnection>();
39  
40      private Iterator<MockSenderConnection> connectionIterator;
41  
42      public MockSenderConnection createConnection(URI uri) throws IOException {
43          Assert.notNull(uri, "'uri' must not be null");
44          if (connectionIterator == null) {
45              connectionIterator = expectedConnections.iterator();
46          }
47          if (!connectionIterator.hasNext()) {
48              throw new AssertionError("No further connections expected");
49          }
50  
51          MockSenderConnection currentConnection = connectionIterator.next();
52          currentConnection.setUri(uri);
53          return currentConnection;
54      }
55  
56      /**
57       * Always returns {@code true}.
58       */
59      public boolean supports(URI uri) {
60          return true;
61      }
62  
63      MockSenderConnection expectNewConnection() {
64          Assert.state(connectionIterator == null, "Can not expect another connection, the test is already underway");
65          MockSenderConnection connection = new MockSenderConnection();
66          expectedConnections.add(connection);
67          return connection;
68      }
69  
70      void verifyConnections() {
71          if (expectedConnections.isEmpty()) {
72              return;
73          }
74          if (connectionIterator == null || connectionIterator.hasNext()) {
75              throw new AssertionError("Further connection(s) expected");
76          }
77      }
78  
79  }