1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
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
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
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 }