001: package org.objectweb.celtix.systest.ws.addressing;
002:
003: import java.lang.reflect.UndeclaredThrowableException;
004: import java.net.URL;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008:
009: import javax.xml.namespace.QName;
010: import javax.xml.ws.BindingProvider;
011: import javax.xml.ws.ProtocolException;
012: import javax.xml.ws.handler.Handler;
013:
014: import junit.framework.Test;
015: import junit.framework.TestSuite;
016:
017: import org.objectweb.celtix.bus.ws.addressing.AddressingPropertiesImpl;
018: import org.objectweb.celtix.bus.ws.addressing.ContextUtils;
019: import org.objectweb.celtix.bus.ws.addressing.Names;
020: import org.objectweb.celtix.bus.ws.addressing.soap.VersionTransformer;
021: import org.objectweb.celtix.systest.common.ClientServerSetupBase;
022: import org.objectweb.celtix.systest.common.ClientServerTestBase;
023: import org.objectweb.celtix.ws.addressing.AddressingProperties;
024: import org.objectweb.celtix.ws.addressing.AttributedURIType;
025:
026: import org.objectweb.hello_world_soap_http.BadRecordLitFault;
027: import org.objectweb.hello_world_soap_http.Greeter;
028: import org.objectweb.hello_world_soap_http.NoSuchCodeLitFault;
029: import org.objectweb.hello_world_soap_http.SOAPService;
030: import org.objectweb.hello_world_soap_http.types.BareDocumentResponse;
031:
032: import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES;
033:
034: /**
035: * Tests the addition of WS-Addressing Message Addressing Properties.
036: */
037: public class MAPTest extends ClientServerTestBase implements
038: VerificationCache {
039:
040: static final String INBOUND_KEY = "inbound";
041: static final String OUTBOUND_KEY = "outbound";
042: private static final QName SERVICE_NAME = new QName(
043: "http://objectweb.org/hello_world_soap_http",
044: "SOAPServiceAddressing");
045: private static final QName PORT_NAME = new QName(
046: "http://objectweb.org/hello_world_soap_http", "SoapPort");
047: private static Map<Object, Map<String, String>> messageIDs = new HashMap<Object, Map<String, String>>();
048: private Greeter greeter;
049: private String verified;
050: private MAPVerifier mapVerifier;
051:
052: public static void main(String[] args) {
053: junit.textui.TestRunner.run(MAPTest.class);
054: }
055:
056: public static Test suite() throws Exception {
057: TestSuite suite = new TestSuite(MAPTest.class);
058: return new ClientServerSetupBase(suite) {
059: public void startServers() throws Exception {
060: // special case handling for WS-Addressing system test to avoid
061: // UUID related issue when server is run as separate process
062: // via maven on Win2k
063: boolean inProcess = "Windows 2000".equals(System
064: .getProperty("os.name"));
065: assertTrue("server did not launch correctly",
066: launchServer(Server.class, inProcess));
067: }
068:
069: public void setUp() throws Exception {
070: // set up configuration for decoupled response
071: // endpoint
072: URL url = getClass().getResource("client.xml");
073: assertNotNull("cannot find test resource", url);
074: configFileName = url.toString();
075: super .setUp();
076: }
077: };
078: }
079:
080: public void setUp() throws Exception {
081: super .setUp();
082: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
083: SOAPService service = new SOAPService(wsdl, SERVICE_NAME);
084: greeter = (Greeter) service.getPort(PORT_NAME, Greeter.class);
085: BindingProvider provider = (BindingProvider) greeter;
086: List<Handler> handlerChain = provider.getBinding()
087: .getHandlerChain();
088: for (Object h : handlerChain) {
089: if (h instanceof MAPVerifier) {
090: mapVerifier = (MAPVerifier) h;
091: mapVerifier.verificationCache = this ;
092: } else if (h instanceof HeaderVerifier) {
093: ((HeaderVerifier) h).verificationCache = this ;
094: }
095: }
096: }
097:
098: public void tearDown() {
099: verified = null;
100: }
101:
102: //--Tests
103:
104: public void testImplicitMAPs() throws Exception {
105: try {
106: String greeting = greeter.greetMe("implicit1");
107: assertEquals("unexpected response received from service",
108: "Hello implicit1", greeting);
109: checkVerification();
110: greeting = greeter.greetMe("implicit2");
111: assertEquals("unexpected response received from service",
112: "Hello implicit2", greeting);
113: checkVerification();
114: } catch (UndeclaredThrowableException ex) {
115: throw (Exception) ex.getCause();
116: }
117: }
118:
119: public void testExplicitMAPs() throws Exception {
120: try {
121: Map<String, Object> requestContext = ((BindingProvider) greeter)
122: .getRequestContext();
123: AddressingProperties maps = new AddressingPropertiesImpl();
124: AttributedURIType id = ContextUtils
125: .getAttributedURI("urn:uuid:12345");
126: maps.setMessageID(id);
127: requestContext.put(CLIENT_ADDRESSING_PROPERTIES, maps);
128: String greeting = greeter.greetMe("explicit1");
129: assertEquals("unexpected response received from service",
130: "Hello explicit1", greeting);
131: checkVerification();
132:
133: // the previous addition to the request context impacts
134: // on all subsequent invocations on this proxy => a duplicate
135: // message ID fault is expected
136: try {
137: greeter.greetMe("explicit2");
138: fail("expected ProtocolException on duplicate message ID");
139: } catch (ProtocolException pe) {
140: assertTrue("expected duplicate message ID failure",
141: "Duplicate Message ID urn:uuid:12345".equals(pe
142: .getMessage()));
143: checkVerification();
144: }
145:
146: // clearing the message ID ensure a duplicate is not sent
147: maps.setMessageID(null);
148: maps.setRelatesTo(ContextUtils.getRelatesTo(id.getValue()));
149: greeting = greeter.greetMe("explicit3");
150: assertEquals("unexpected response received from service",
151: "Hello explicit3", greeting);
152: } catch (UndeclaredThrowableException ex) {
153: throw (Exception) ex.getCause();
154: }
155: }
156:
157: public void testOneway() throws Exception {
158: try {
159: greeter.greetMeOneWay("implicit_oneway1");
160: checkVerification();
161: } catch (UndeclaredThrowableException ex) {
162: throw (Exception) ex.getCause();
163: }
164: }
165:
166: public void testApplicationFault() throws Exception {
167: try {
168: greeter.testDocLitFault("BadRecordLitFault");
169: fail("expected fault from service");
170: } catch (BadRecordLitFault brlf) {
171: checkVerification();
172: } catch (UndeclaredThrowableException ex) {
173: throw (Exception) ex.getCause();
174: }
175: String greeting = greeter.greetMe("intra-fault");
176: assertEquals("unexpected response received from service",
177: "Hello intra-fault", greeting);
178: try {
179: greeter.testDocLitFault("NoSuchCodeLitFault");
180: fail("expected NoSuchCodeLitFault");
181: } catch (NoSuchCodeLitFault nsclf) {
182: checkVerification();
183: } catch (UndeclaredThrowableException ex) {
184: throw (Exception) ex.getCause();
185: }
186: }
187:
188: public void testVersioning() throws Exception {
189: try {
190: // expect two MAPs instances versioned with 200408, i.e. for both
191: // the partial and full responses
192: mapVerifier.expectedExposedAs
193: .add(VersionTransformer.Names200408.WSA_NAMESPACE_NAME);
194: mapVerifier.expectedExposedAs
195: .add(VersionTransformer.Names200408.WSA_NAMESPACE_NAME);
196: String greeting = greeter.greetMe("versioning1");
197: assertEquals("unexpected response received from service",
198: "Hello versioning1", greeting);
199: checkVerification();
200: greeting = greeter.greetMe("versioning2");
201: assertEquals("unexpected response received from service",
202: "Hello versioning2", greeting);
203: checkVerification();
204: } catch (UndeclaredThrowableException ex) {
205: throw (Exception) ex.getCause();
206: }
207: }
208:
209: public void xtestAction() throws Exception {
210: try {
211: // testDocLitBare has an explicit soapAction attribute specified
212: // in the WSDL
213: BareDocumentResponse bareResponse = greeter
214: .testDocLitBare("MySimpleDocument");
215: assertNotNull("no response for testDocLitBare",
216: bareResponse);
217: assertEquals("Celtix", bareResponse.getCompany());
218: assertTrue(bareResponse.getId() == 1);
219: checkVerification();
220: } catch (UndeclaredThrowableException ex) {
221: throw (Exception) ex.getCause();
222: }
223: }
224:
225: //--VerificationCache implementation
226:
227: public void put(String verification) {
228: if (verification != null) {
229: verified = verified == null ? verification : verified
230: + "; " + verification;
231: }
232: }
233:
234: //--Verification methods called by handlers
235:
236: /**
237: * Verify presence of expected MAPs.
238: *
239: * @param maps the MAPs to verify
240: * @param checkPoint the check point
241: * @return null if all expected MAPs present, otherwise an error string.
242: */
243: protected static String verifyMAPs(AddressingProperties maps,
244: Object checkPoint) {
245: if (maps == null) {
246: return "expected MAPs";
247: }
248: //String rt = maps.getReplyTo() != null ? maps.getReplyTo().getAddress().getValue() : "null";
249: //System.out.println("verifying MAPs: " + maps
250: // + " id: " + maps.getMessageID().getValue()
251: // + " to: " + maps.getTo().getValue()
252: // + " reply to: " + rt);
253: // MessageID
254: String id = maps.getMessageID().getValue();
255: if (id == null) {
256: return "expected MessageID MAP";
257: }
258: if (!id.startsWith("urn:uuid")) {
259: return "bad URN format in MessageID MAP: " + id;
260: }
261: // ensure MessageID is unique for this check point
262: Map<String, String> checkPointMessageIDs = messageIDs
263: .get(checkPoint);
264: if (checkPointMessageIDs != null) {
265: if (checkPointMessageIDs.containsKey(id)) {
266: //return "MessageID MAP duplicate: " + id;
267: return null;
268: }
269: } else {
270: checkPointMessageIDs = new HashMap<String, String>();
271: messageIDs.put(checkPoint, checkPointMessageIDs);
272: }
273: checkPointMessageIDs.put(id, id);
274: // To
275: if (maps.getTo() == null) {
276: return "expected To MAP";
277: }
278: return null;
279: }
280:
281: /**
282: * Verify presence of expected MAP headers.
283: *
284: * @param wsaHeaders a list of the wsa:* headers present in the SOAP
285: * message
286: * @param parial true if partial response
287: * @return null if all expected headers present, otherwise an error string.
288: */
289: protected static String verifyHeaders(List<String> wsaHeaders,
290: boolean partial) {
291: //System.out.println("verifying headers: " + wsaHeaders);
292: String ret = null;
293: if (!wsaHeaders.contains(Names.WSA_MESSAGEID_NAME)) {
294: ret = "expected MessageID header";
295: }
296: if (!wsaHeaders.contains(Names.WSA_TO_NAME)) {
297: ret = "expected To header";
298: }
299:
300: if (!(wsaHeaders.contains(Names.WSA_REPLYTO_NAME) || wsaHeaders
301: .contains(Names.WSA_RELATESTO_NAME))) {
302: ret = "expected ReplyTo or RelatesTo header";
303: }
304: if (partial) {
305: if (!wsaHeaders.contains(Names.WSA_FROM_NAME)) {
306: ret = "expected From header";
307: }
308: } else {
309: if (!wsaHeaders.contains(Names.WSA_ACTION_NAME)) {
310: ret = "expected Action header";
311: }
312: }
313: return ret;
314: }
315:
316: private void checkVerification() {
317: assertNull("MAP/Header verification failed: " + verified,
318: verified);
319: }
320: }
|