KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > systest > ws > addressing > MAPTest


1 package org.objectweb.celtix.systest.ws.addressing;
2
3
4 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.xml.namespace.QName JavaDoc;
11 import javax.xml.ws.BindingProvider;
12 import javax.xml.ws.ProtocolException;
13 import javax.xml.ws.handler.Handler;
14
15 import junit.framework.Test;
16 import junit.framework.TestSuite;
17
18 import org.objectweb.celtix.bus.ws.addressing.AddressingPropertiesImpl;
19 import org.objectweb.celtix.bus.ws.addressing.ContextUtils;
20 import org.objectweb.celtix.bus.ws.addressing.Names;
21 import org.objectweb.celtix.bus.ws.addressing.soap.VersionTransformer;
22 import org.objectweb.celtix.systest.common.ClientServerSetupBase;
23 import org.objectweb.celtix.systest.common.ClientServerTestBase;
24 import org.objectweb.celtix.ws.addressing.AddressingProperties;
25 import org.objectweb.celtix.ws.addressing.AttributedURIType;
26
27 import org.objectweb.hello_world_soap_http.BadRecordLitFault;
28 import org.objectweb.hello_world_soap_http.Greeter;
29 import org.objectweb.hello_world_soap_http.NoSuchCodeLitFault;
30 import org.objectweb.hello_world_soap_http.SOAPService;
31 import org.objectweb.hello_world_soap_http.types.BareDocumentResponse;
32
33 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES;
34
35
36 /**
37  * Tests the addition of WS-Addressing Message Addressing Properties.
38  */

39 public class MAPTest extends ClientServerTestBase implements VerificationCache {
40     
41     static final String JavaDoc INBOUND_KEY = "inbound";
42     static final String JavaDoc OUTBOUND_KEY = "outbound";
43     private static final QName JavaDoc SERVICE_NAME =
44         new QName JavaDoc("http://objectweb.org/hello_world_soap_http", "SOAPServiceAddressing");
45     private static final QName JavaDoc PORT_NAME =
46         new QName JavaDoc("http://objectweb.org/hello_world_soap_http", "SoapPort");
47     private static Map JavaDoc<Object JavaDoc, Map JavaDoc<String JavaDoc, String JavaDoc>> messageIDs =
48         new HashMap JavaDoc<Object JavaDoc, Map JavaDoc<String JavaDoc, String JavaDoc>>();
49     private Greeter greeter;
50     private String JavaDoc verified;
51     private MAPVerifier mapVerifier;
52
53     public static void main(String JavaDoc[] args) {
54         junit.textui.TestRunner.run(MAPTest.class);
55     }
56     
57     public static Test suite() throws Exception JavaDoc {
58         TestSuite suite = new TestSuite(MAPTest.class);
59         return new ClientServerSetupBase(suite) {
60             public void startServers() throws Exception JavaDoc {
61                 // special case handling for WS-Addressing system test to avoid
62
// UUID related issue when server is run as separate process
63
// via maven on Win2k
64
boolean inProcess = "Windows 2000".equals(System.getProperty("os.name"));
65                 assertTrue("server did not launch correctly",
66                            launchServer(Server.class, inProcess));
67             }
68             
69             public void setUp() throws Exception JavaDoc {
70                 // set up configuration for decoupled response
71
// endpoint
72
URL JavaDoc url = getClass().getResource("client.xml");
73                 assertNotNull("cannot find test resource", url);
74                 configFileName = url.toString();
75                 super.setUp();
76             }
77         };
78     }
79
80     public void setUp() throws Exception JavaDoc {
81         super.setUp();
82         URL JavaDoc wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
83         SOAPService service = new SOAPService(wsdl, SERVICE_NAME);
84         greeter = (Greeter)service.getPort(PORT_NAME, Greeter.class);
85         BindingProvider provider = (BindingProvider)greeter;
86         List JavaDoc<Handler> handlerChain = provider.getBinding().getHandlerChain();
87         for (Object JavaDoc h : handlerChain) {
88             if (h instanceof MAPVerifier) {
89                 mapVerifier = (MAPVerifier)h;
90                 mapVerifier.verificationCache = this;
91             } else if (h instanceof HeaderVerifier) {
92                 ((HeaderVerifier)h).verificationCache = this;
93             }
94         }
95     }
96
97     public void tearDown() {
98         verified = null;
99     }
100
101     //--Tests
102

103     public void testImplicitMAPs() throws Exception JavaDoc {
104         try {
105             String JavaDoc greeting = greeter.greetMe("implicit1");
106             assertEquals("unexpected response received from service",
107                          "Hello implicit1",
108                          greeting);
109             checkVerification();
110             greeting = greeter.greetMe("implicit2");
111             assertEquals("unexpected response received from service",
112                          "Hello implicit2",
113                          greeting);
114             checkVerification();
115         } catch (UndeclaredThrowableException JavaDoc ex) {
116             throw (Exception JavaDoc)ex.getCause();
117         }
118     }
119
120     public void testExplicitMAPs() throws Exception JavaDoc {
121         try {
122             Map JavaDoc<String JavaDoc, Object JavaDoc> requestContext =
123                 ((BindingProvider)greeter).getRequestContext();
124             AddressingProperties maps = new AddressingPropertiesImpl();
125             AttributedURIType id =
126                 ContextUtils.getAttributedURI("urn:uuid:12345");
127             maps.setMessageID(id);
128             requestContext.put(CLIENT_ADDRESSING_PROPERTIES, maps);
129             String JavaDoc greeting = greeter.greetMe("explicit1");
130             assertEquals("unexpected response received from service",
131                          "Hello explicit1",
132                          greeting);
133             checkVerification();
134
135             // the previous addition to the request context impacts
136
// on all subsequent invocations on this proxy => a duplicate
137
// message ID fault is expected
138
try {
139                 greeter.greetMe("explicit2");
140                 fail("expected ProtocolException on duplicate message ID");
141             } catch (ProtocolException pe) {
142                 assertTrue("expected duplicate message ID failure",
143                            "Duplicate Message ID urn:uuid:12345".equals(pe.getMessage()));
144                 checkVerification();
145             }
146
147             // clearing the message ID ensure a duplicate is not sent
148
maps.setMessageID(null);
149             maps.setRelatesTo(ContextUtils.getRelatesTo(id.getValue()));
150             greeting = greeter.greetMe("explicit3");
151             assertEquals("unexpected response received from service",
152                          "Hello explicit3",
153                          greeting);
154         } catch (UndeclaredThrowableException JavaDoc ex) {
155             throw (Exception JavaDoc)ex.getCause();
156         }
157     }
158     
159     public void testOneway() throws Exception JavaDoc {
160         try {
161             greeter.greetMeOneWay("implicit_oneway1");
162             checkVerification();
163         } catch (UndeclaredThrowableException JavaDoc ex) {
164             throw (Exception JavaDoc)ex.getCause();
165         }
166     }
167     
168     public void testApplicationFault() throws Exception JavaDoc {
169         try {
170             greeter.testDocLitFault("BadRecordLitFault");
171             fail("expected fault from service");
172         } catch (BadRecordLitFault brlf) {
173             checkVerification();
174         } catch (UndeclaredThrowableException JavaDoc ex) {
175             throw (Exception JavaDoc)ex.getCause();
176         }
177         String JavaDoc greeting = greeter.greetMe("intra-fault");
178         assertEquals("unexpected response received from service",
179                      "Hello intra-fault",
180                      greeting);
181         try {
182             greeter.testDocLitFault("NoSuchCodeLitFault");
183             fail("expected NoSuchCodeLitFault");
184         } catch (NoSuchCodeLitFault nsclf) {
185             checkVerification();
186         } catch (UndeclaredThrowableException JavaDoc ex) {
187             throw (Exception JavaDoc)ex.getCause();
188         }
189     }
190
191     public void testVersioning() throws Exception JavaDoc {
192         try {
193             // expect two MAPs instances versioned with 200408, i.e. for both
194
// the partial and full responses
195
mapVerifier.expectedExposedAs.add(VersionTransformer.Names200408.WSA_NAMESPACE_NAME);
196             mapVerifier.expectedExposedAs.add(VersionTransformer.Names200408.WSA_NAMESPACE_NAME);
197             String JavaDoc greeting = greeter.greetMe("versioning1");
198             assertEquals("unexpected response received from service",
199                          "Hello versioning1",
200                          greeting);
201             checkVerification();
202             greeting = greeter.greetMe("versioning2");
203             assertEquals("unexpected response received from service",
204                          "Hello versioning2",
205                          greeting);
206             checkVerification();
207         } catch (UndeclaredThrowableException JavaDoc ex) {
208             throw (Exception JavaDoc)ex.getCause();
209         }
210     }
211
212     public void xtestAction() throws Exception JavaDoc {
213         try {
214             // testDocLitBare has an explicit soapAction attribute specified
215
// in the WSDL
216
BareDocumentResponse bareResponse =
217                 greeter.testDocLitBare("MySimpleDocument");
218             assertNotNull("no response for testDocLitBare", bareResponse);
219             assertEquals("Celtix", bareResponse.getCompany());
220             assertTrue(bareResponse.getId() == 1);
221             checkVerification();
222         } catch (UndeclaredThrowableException JavaDoc ex) {
223             throw (Exception JavaDoc)ex.getCause();
224         }
225     }
226
227     //--VerificationCache implementation
228

229     public void put(String JavaDoc verification) {
230         if (verification != null) {
231             verified = verified == null
232                        ? verification
233                 : verified + "; " + verification;
234         }
235     }
236
237     //--Verification methods called by handlers
238

239     /**
240      * Verify presence of expected MAPs.
241      *
242      * @param maps the MAPs to verify
243      * @param checkPoint the check point
244      * @return null if all expected MAPs present, otherwise an error string.
245      */

246     protected static String JavaDoc verifyMAPs(AddressingProperties maps,
247                                        Object JavaDoc checkPoint) {
248         if (maps == null) {
249             return "expected MAPs";
250         }
251         //String rt = maps.getReplyTo() != null ? maps.getReplyTo().getAddress().getValue() : "null";
252
//System.out.println("verifying MAPs: " + maps
253
// + " id: " + maps.getMessageID().getValue()
254
// + " to: " + maps.getTo().getValue()
255
// + " reply to: " + rt);
256
// MessageID
257
String JavaDoc id = maps.getMessageID().getValue();
258         if (id == null) {
259             return "expected MessageID MAP";
260         }
261         if (!id.startsWith("urn:uuid")) {
262             return "bad URN format in MessageID MAP: " + id;
263         }
264         // ensure MessageID is unique for this check point
265
Map JavaDoc<String JavaDoc, String JavaDoc> checkPointMessageIDs = messageIDs.get(checkPoint);
266         if (checkPointMessageIDs != null) {
267             if (checkPointMessageIDs.containsKey(id)) {
268                 //return "MessageID MAP duplicate: " + id;
269
return null;
270             }
271         } else {
272             checkPointMessageIDs = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
273             messageIDs.put(checkPoint, checkPointMessageIDs);
274         }
275         checkPointMessageIDs.put(id, id);
276         // To
277
if (maps.getTo() == null) {
278             return "expected To MAP";
279         }
280         return null;
281     }
282
283     /**
284      * Verify presence of expected MAP headers.
285      *
286      * @param wsaHeaders a list of the wsa:* headers present in the SOAP
287      * message
288      * @param parial true if partial response
289      * @return null if all expected headers present, otherwise an error string.
290      */

291     protected static String JavaDoc verifyHeaders(List JavaDoc<String JavaDoc> wsaHeaders, boolean partial) {
292         //System.out.println("verifying headers: " + wsaHeaders);
293
String JavaDoc ret = null;
294         if (!wsaHeaders.contains(Names.WSA_MESSAGEID_NAME)) {
295             ret = "expected MessageID header";
296         }
297         if (!wsaHeaders.contains(Names.WSA_TO_NAME)) {
298             ret = "expected To header";
299         }
300        
301         if (!(wsaHeaders.contains(Names.WSA_REPLYTO_NAME)
302               || wsaHeaders.contains(Names.WSA_RELATESTO_NAME))) {
303             ret = "expected ReplyTo or RelatesTo header";
304         }
305         if (partial) {
306             if (!wsaHeaders.contains(Names.WSA_FROM_NAME)) {
307                 ret = "expected From header";
308             }
309         } else {
310             if (!wsaHeaders.contains(Names.WSA_ACTION_NAME)) {
311                 ret = "expected Action header";
312             }
313         }
314         return ret;
315     }
316
317     private void checkVerification() {
318         assertNull("MAP/Header verification failed: " + verified, verified);
319     }
320 }
321
322
Popular Tags