1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ws.addressing.handler;
18
19 import junit.framework.TestCase;
20 import org.apache.axis.message.MessageElement;
21 import org.apache.axis.message.addressing.Action;
22 import org.apache.axis.message.addressing.Address;
23 import org.apache.axis.message.addressing.AddressingHeaders;
24 import org.apache.axis.message.addressing.AttributedURI;
25 import org.apache.axis.message.addressing.EndpointReference;
26 import org.apache.axis.message.addressing.FaultTo;
27 import org.apache.axis.message.addressing.From;
28 import org.apache.axis.message.addressing.MessageID;
29 import org.apache.axis.message.addressing.PortType;
30 import org.apache.axis.message.addressing.Recipient;
31 import org.apache.axis.message.addressing.ReferencePropertiesType;
32 import org.apache.axis.message.addressing.RelatesTo;
33 import org.apache.axis.message.addressing.ReplyTo;
34 import org.apache.axis.message.addressing.ServiceNameType;
35 import org.apache.axis.message.addressing.To;
36 import org.apache.axis.message.addressing.Constants;
37
38 import javax.xml.namespace.QName;
39 import javax.xml.rpc.handler.HandlerInfo;
40 import javax.xml.rpc.handler.soap.SOAPMessageContext;
41 import javax.xml.soap.MessageFactory;
42 import javax.xml.soap.MimeHeaders;
43 import javax.xml.soap.SOAPMessage;
44 import java.io.InputStream;
45 import java.util.ArrayList;
46 import java.util.Enumeration;
47 import java.util.Hashtable;
48 import java.util.Iterator;
49 import java.util.List;
50 import java.util.Map;
51 import java.util.Properties;
52 import java.util.StringTokenizer;
53 import java.util.Arrays;
54
55 /***
56 * @author Ian P. Springer <ian_springer@hp.com>
57 */
58 public abstract class AbstractAddressingHandlerTestCase extends TestCase {
59
60 protected static final String ACTOR_NEXT = "http://schemas.xmlsoap.org/soap/actor/next";
61
62 private static final QName[] EXPECTED_UNDERSTOOD_HEADERS = new QName[]{
63 new QName(Constants.NS_URI_ADDRESSING_2003_03, Constants.ACTION),
64 new QName(Constants.NS_URI_ADDRESSING_2003_03, Constants.FAULT_TO),
65 new QName(Constants.NS_URI_ADDRESSING_2003_03, Constants.FROM),
66 new QName(Constants.NS_URI_ADDRESSING_2003_03, Constants.MESSAGE_ID),
67 new QName(Constants.NS_URI_ADDRESSING_2003_03, Constants.RECIPIENT),
68 new QName(Constants.NS_URI_ADDRESSING_2003_03, Constants.RELATES_TO),
69 new QName(Constants.NS_URI_ADDRESSING_2003_03, Constants.REPLY_TO),
70 new QName(Constants.NS_URI_ADDRESSING_2003_03, Constants.TO),
71 new QName(Constants.NS_URI_ADDRESSING_2004_03, Constants.ACTION),
72 new QName(Constants.NS_URI_ADDRESSING_2004_03, Constants.FAULT_TO),
73 new QName(Constants.NS_URI_ADDRESSING_2004_03, Constants.FROM),
74 new QName(Constants.NS_URI_ADDRESSING_2004_03, Constants.MESSAGE_ID),
75 new QName(Constants.NS_URI_ADDRESSING_2004_03, Constants.RECIPIENT),
76 new QName(Constants.NS_URI_ADDRESSING_2004_03, Constants.RELATES_TO),
77 new QName(Constants.NS_URI_ADDRESSING_2004_03, Constants.REPLY_TO),
78 new QName(Constants.NS_URI_ADDRESSING_2004_03, Constants.TO),
79 new QName(Constants.NS_URI_ADDRESSING_2004_08, Constants.ACTION),
80 new QName(Constants.NS_URI_ADDRESSING_2004_08, Constants.FAULT_TO),
81 new QName(Constants.NS_URI_ADDRESSING_2004_08, Constants.FROM),
82 new QName(Constants.NS_URI_ADDRESSING_2004_08, Constants.MESSAGE_ID),
83 new QName(Constants.NS_URI_ADDRESSING_2004_08, Constants.RECIPIENT),
84 new QName(Constants.NS_URI_ADDRESSING_2004_08, Constants.RELATES_TO),
85 new QName(Constants.NS_URI_ADDRESSING_2004_08, Constants.REPLY_TO),
86 new QName(Constants.NS_URI_ADDRESSING_2004_08, Constants.TO)
87 };
88
89 /***
90 * Test for {@link AbstractAddressingHandler#init(javax.xml.rpc.handler.HandlerInfo)}.
91 *
92 * @throws Exception on error
93 */
94 public void testInit() throws Exception {
95 assertInitSuccessful(ACTOR_NEXT, Boolean.FALSE.toString(), "*");
96 assertInitSuccessful("urn:bob", Boolean.TRUE.toString(), "{urn:ns1}Prop1,{urn:ns2}Prop2");
97 assertInitSuccessful("", "", null);
98 }
99
100 /***
101 * Test for {@link AbstractAddressingHandler#getHeaders()}.
102 *
103 * @throws Exception on error
104 */
105 public void testGetHeaders() throws Exception {
106 QName[] headers = getHandler().getHeaders();
107 assertNotNull(headers);
108 List headerList = Arrays.asList(headers);
109 assertEquals(EXPECTED_UNDERSTOOD_HEADERS.length,headerList.size());
110 for (int i = 0; i < EXPECTED_UNDERSTOOD_HEADERS.length; i++) {
111 assertTrue(headerList.contains(EXPECTED_UNDERSTOOD_HEADERS[i]));
112 }
113 }
114
115 protected abstract AbstractAddressingHandler getHandler();
116
117 private void assertInitSuccessful(String actor, String removeHeaders, String refPropNames) {
118 Map handlerConfig = createHandlerConfig(actor, removeHeaders, refPropNames);
119 getHandler().init(createHandlerInfo(ClientSideAddressingHandler.class, handlerConfig));
120 assertActorEquals(actor);
121 assertRemoveHeadersEquals(removeHeaders);
122 assertRefPropQNamesEquals(refPropNames);
123 }
124
125 private void assertRefPropQNamesEquals(String expectedRefPropNames) {
126 Map handlerConfig = getHandler().getHandlerConfigProperties();
127 assertNotNull(handlerConfig);
128 assertEquals(expectedRefPropNames, handlerConfig.get(AbstractAddressingHandler.CONFIG_PROP__REFERENCE_PROPERTY_NAMES));
129 List refPropQNames = getHandler().getReferencePropertyQNames();
130 if (expectedRefPropNames == null) {
131 assertTrue(refPropQNames.isEmpty());
132 }
133 else if (expectedRefPropNames.equals("*")) {
134 assertNull(refPropQNames);
135 }
136 else {
137 List expectedRefPropQNames = new ArrayList();
138 StringTokenizer tokenizer = new StringTokenizer(expectedRefPropNames, ",");
139 while (tokenizer.hasMoreTokens()) {
140 expectedRefPropQNames.add(QName.valueOf(tokenizer.nextToken().trim()));
141 }
142 assertEquals(expectedRefPropQNames.size(), refPropQNames.size());
143 for (int i = 0; i < expectedRefPropQNames.size(); i++) {
144 assertTrue(refPropQNames.contains(expectedRefPropQNames.get(i)));
145 }
146 }
147 }
148
149 private void assertRemoveHeadersEquals(String expectedRemoveHeaders) {
150 Map handlerConfig = getHandler().getHandlerConfigProperties();
151 assertNotNull(handlerConfig);
152 assertEquals(expectedRemoveHeaders, handlerConfig.get(AbstractAddressingHandler.CONFIG_PROP__REMOVE_HEADERS));
153 assertEquals(new Boolean(expectedRemoveHeaders).booleanValue(), getHandler().isRemoveHeadersEnabled());
154 }
155
156 private void assertActorEquals(String expectedActor) {
157 Map handlerConfig = getHandler().getHandlerConfigProperties();
158 assertNotNull(handlerConfig);
159 assertEquals(expectedActor, handlerConfig.get(AbstractAddressingHandler.CONFIG_PROP__ACTOR));
160 assertEquals(expectedActor, getHandler().getActor());
161 }
162
163 protected SOAPMessageContext createSOAPMessageContext() throws Exception {
164 SOAPMessageContext msgContext = new SOAPMessageContext() {
165 Map props = new Hashtable(5);
166 SOAPMessage msg = createMessage(null);
167 String[] roles = new String[0];
168
169 public void setProperty(String name,
170 Object value) {
171 props.put(name, value);
172 }
173
174 public Object getProperty(String name) {
175 return props.get(name);
176 }
177
178 public void removeProperty(String name) {
179 props.remove(name);
180 }
181
182 public boolean containsProperty(String name) {
183 return props.containsKey(name);
184 }
185
186 public Iterator getPropertyNames() {
187 return props.keySet().iterator();
188 }
189
190 public SOAPMessage getMessage() {
191 return msg;
192 }
193
194 public void setMessage(SOAPMessage soapMsg) {
195 msg = soapMsg;
196 }
197
198 public String[] getRoles() {
199 return roles;
200 }
201 };
202 msgContext.setProperty(org.apache.axis.MessageContext.TRANS_URL, "http://xyz.com:8080/axis/services/myService");
203 return msgContext;
204 }
205
206 protected SOAPMessage createMessage(String msgResourcePath) throws Exception {
207 if (msgResourcePath == null) {
208 return MessageFactory.newInstance().createMessage();
209 }
210 ClassLoader classLoader = ServerSideAddressingHandler.class.getClassLoader();
211 InputStream msgInStream = classLoader.getResourceAsStream(msgResourcePath);
212 if (msgInStream == null) {
213 throw new Exception( "Resource " + msgResourcePath + " not found. Please make sure it is in your classpath before running this testcase." );
214 }
215 return MessageFactory.newInstance().createMessage(new MimeHeaders(), msgInStream);
216 }
217
218 protected Map createHandlerConfig(String actor, String removeHeaders, String refPropNames) {
219 Map config = new Hashtable();
220 if (actor != null) {
221 config.put(ServerSideAddressingHandler.CONFIG_PROP__ACTOR, actor);
222 }
223 if (removeHeaders != null) {
224 config.put(ServerSideAddressingHandler.CONFIG_PROP__REMOVE_HEADERS, removeHeaders);
225 }
226 if (refPropNames != null) {
227 config.put(ServerSideAddressingHandler.CONFIG_PROP__REFERENCE_PROPERTY_NAMES, refPropNames);
228 }
229 return config;
230 }
231
232 protected HandlerInfo createHandlerInfo(Class handlerClass, Map handlerConfig) {
233 return new HandlerInfo(handlerClass, handlerConfig, new QName[0]);
234 }
235
236 protected void checkToHeader(AddressingHeaders addrHeaders, To expectedTo) {
237 assertEquals(expectedTo, addrHeaders.getTo());
238 }
239
240 protected void checkFromHeader(AddressingHeaders addrHeaders, From expectedFrom) {
241 assertEquals(expectedFrom, addrHeaders.getFrom());
242 }
243
244 protected void checkRecipientHeader(AddressingHeaders addrHeaders, Recipient expectedRecipient) {
245
246
247
248
249 }
250
251 protected void checkActionHeader(AddressingHeaders addrHeaders, Action expectedAction) {
252 assertEquals(expectedAction, addrHeaders.getAction());
253 }
254
255 protected void checkReplyToHeader(AddressingHeaders addrHeaders, ReplyTo expectedReplyTo) {
256 assertEquals(expectedReplyTo, addrHeaders.getReplyTo());
257 }
258
259 protected void checkFaultToHeader(AddressingHeaders addrHeaders, FaultTo expectedFaultTo) {
260 assertEquals(expectedFaultTo, addrHeaders.getFaultTo());
261 }
262
263 protected void checkMessageIdHeader(AddressingHeaders addrHeaders, String uuid) {
264 MessageID msgId = addrHeaders.getMessageID();
265 assertNotNull(msgId);
266 assertTrue("uuid".equals(msgId.getScheme()));
267 if (uuid != null) {
268 assertEquals(uuid, msgId.getPath());
269 } else {
270 assertTrue(msgId.getPath().length() == 36);
271 }
272 }
273
274 protected void checkRelatesToHeader(AddressingHeaders addrHeaders, RelatesTo[] expectedRelatesTo) {
275 List relatesToList = addrHeaders.getRelatesTo();
276 assertNotNull(relatesToList);
277 assertEquals(new Integer(expectedRelatesTo.length), new Integer(relatesToList.size()));
278 for (int i = 0; i < relatesToList.size(); i++) {
279 RelatesTo relatesTo = (RelatesTo) relatesToList.get(i);
280 assertEquals(expectedRelatesTo[i].getType().toString(), relatesTo.getType().toString());
281 assertEquals(expectedRelatesTo[i].getURI(), relatesTo.getURI());
282 }
283 }
284
285 protected void checkReferenceProperties(AddressingHeaders addrHeaders, ReferencePropertiesType expectedRefProps) {
286 assertEquals(expectedRefProps, addrHeaders.getReferenceProperties());
287 }
288
289 private void assertEquals(EndpointReference expected, EndpointReference actual) {
290 if (expected != null && actual != null) {
291 assertEquals(String.valueOf(expected.getAddress()), String.valueOf(actual.getAddress()));
292 assertEquals(String.valueOf(expected.getPortType()), String.valueOf(actual.getPortType()));
293 assertEquals(String.valueOf(expected.getServiceName()), String.valueOf(actual.getServiceName()));
294 } else {
295 assertEquals(String.valueOf(expected), String.valueOf(actual));
296 }
297 }
298
299 private void assertEquals(AttributedURI expected, AttributedURI actual) {
300 assertEquals(String.valueOf(expected), String.valueOf(actual));
301 }
302
303 private void assertEquals(ReferencePropertiesType expected, ReferencePropertiesType actual) {
304 if (expected != null && actual != null) {
305 Properties actualProps = toProperties(actual);
306 Properties expectedProps = toProperties(expected);
307 Enumeration propNames = actualProps.propertyNames();
308 while (propNames.hasMoreElements()) {
309 String name = (String) propNames.nextElement();
310 assertEquals(expectedProps.getProperty(name), actualProps.getProperty(name));
311 }
312 } else {
313 assertEquals(String.valueOf(expected), String.valueOf(actual));
314 }
315 }
316
317 private Properties toProperties(ReferencePropertiesType refProps) {
318 if (refProps == null) {
319 return null;
320 }
321 MessageElement[] refPropElems = refProps.get_any();
322 Properties props = new Properties();
323 if (refPropElems != null) {
324 for (int i = 0; i < refPropElems.length; i++) {
325 MessageElement refPropElem = refPropElems[i];
326 props.setProperty(refPropElem.getQName().toString(), refPropElem.getValue());
327 }
328 }
329 return props;
330 }
331
332 protected ReplyTo createReplyTo(Address addr, PortType portType, ServiceNameType serviceName) {
333 ReplyTo replyTo = new ReplyTo(addr);
334 replyTo.setPortType(portType);
335 replyTo.setServiceName(serviceName);
336 return replyTo;
337 }
338
339 }