KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.celtix.systest.ws.addressing.jms;
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.hello_world_jms.HelloWorldPortType;
23 import org.objectweb.celtix.hello_world_jms.JMSSOAPServiceAddressing;
24 import org.objectweb.celtix.systest.common.ClientServerSetupBase;
25 import org.objectweb.celtix.systest.common.ClientServerTestBase;
26 import org.objectweb.celtix.systest.jms.EmbeddedJMSBrokerLauncher;
27 import org.objectweb.celtix.systest.ws.addressing.HeaderVerifier;
28 import org.objectweb.celtix.systest.ws.addressing.MAPVerifier;
29 import org.objectweb.celtix.systest.ws.addressing.VerificationCache;
30 import org.objectweb.celtix.ws.addressing.AddressingProperties;
31 import org.objectweb.celtix.ws.addressing.AttributedURIType;
32
33
34 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES;
35
36
37 /**
38  * Tests the addition of WS-Addressing Message Addressing Properties.
39  */

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

114     public void testImplicitMAPs() throws Exception JavaDoc {
115         try {
116             String JavaDoc greeting = greeter.greetMe("implicit1");
117             assertEquals("unexpected response received from service",
118                          "Hello implicit1",
119                          greeting);
120             checkVerification();
121             greeting = greeter.greetMe("implicit2");
122             assertEquals("unexpected response received from service",
123                          "Hello implicit2",
124                          greeting);
125             checkVerification();
126         } catch (UndeclaredThrowableException JavaDoc ex) {
127             throw (Exception JavaDoc)ex.getCause();
128         }
129     }
130
131     public void testExplicitMAPs() throws Exception JavaDoc {
132         try {
133             Map JavaDoc<String JavaDoc, Object JavaDoc> requestContext =
134                 ((BindingProvider)greeter).getRequestContext();
135             AddressingProperties maps = new AddressingPropertiesImpl();
136             AttributedURIType id =
137                 ContextUtils.getAttributedURI("urn:uuid:12345");
138             maps.setMessageID(id);
139             requestContext.put(CLIENT_ADDRESSING_PROPERTIES, maps);
140             String JavaDoc greeting = greeter.greetMe("explicit1");
141             assertEquals("unexpected response received from service",
142                          "Hello explicit1",
143                          greeting);
144             checkVerification();
145
146             // the previous addition to the request context impacts
147
// on all subsequent invocations on this proxy => a duplicate
148
// message ID fault is expected
149
try {
150                 greeter.greetMe("explicit2");
151                 fail("expected ProtocolException on duplicate message ID");
152             } catch (ProtocolException pe) {
153                 assertTrue("expected duplicate message ID failure",
154                            "Duplicate Message ID urn:uuid:12345".equals(pe.getMessage()));
155                 checkVerification();
156             }
157
158             // clearing the message ID ensure a duplicate is not sent
159
maps.setMessageID(null);
160             maps.setRelatesTo(ContextUtils.getRelatesTo(id.getValue()));
161             greeting = greeter.greetMe("explicit3");
162             assertEquals("unexpected response received from service",
163                          "Hello explicit3",
164                          greeting);
165         } catch (UndeclaredThrowableException JavaDoc ex) {
166             throw (Exception JavaDoc)ex.getCause();
167         }
168     }
169     
170     public void testOneway() throws Exception JavaDoc {
171         try {
172             greeter.greetMeOneWay("implicit_oneway1");
173             checkVerification();
174         } catch (UndeclaredThrowableException JavaDoc ex) {
175             throw (Exception JavaDoc)ex.getCause();
176         }
177     }
178     
179
180     public void testVersioning() throws Exception JavaDoc {
181         try {
182             // expect two MAPs instances versioned with 200408, i.e. for both
183
// the partial and full responses
184
mapVerifier.addToExpectedExposedAs(VersionTransformer.Names200408.WSA_NAMESPACE_NAME);
185             mapVerifier.addToExpectedExposedAs(VersionTransformer.Names200408.WSA_NAMESPACE_NAME);
186             String JavaDoc greeting = greeter.greetMe("versioning1");
187             assertEquals("unexpected response received from service",
188                          "Hello versioning1",
189                          greeting);
190             checkVerification();
191             greeting = greeter.greetMe("versioning2");
192             assertEquals("unexpected response received from service",
193                          "Hello versioning2",
194                          greeting);
195             checkVerification();
196         } catch (UndeclaredThrowableException JavaDoc ex) {
197             throw (Exception JavaDoc)ex.getCause();
198         }
199     }
200
201     //--VerificationCache implementation
202

203     public void put(String JavaDoc verification) {
204         if (verification != null) {
205             verified = verified == null
206                        ? verification
207                 : verified + "; " + verification;
208         }
209     }
210
211     //--Verification methods called by handlers
212

213     /**
214      * Verify presence of expected MAPs.
215      *
216      * @param maps the MAPs to verify
217      * @param checkPoint the check point
218      * @return null if all expected MAPs present, otherwise an error string.
219      */

220     protected static String JavaDoc verifyMAPs(AddressingProperties maps,
221                                        Object JavaDoc checkPoint) {
222         if (maps == null) {
223             return "expected MAPs";
224         }
225         //String rt = maps.getReplyTo() != null ? maps.getReplyTo().getAddress().getValue() : "null";
226
//System.out.println("verifying MAPs: " + maps
227
// + " id: " + maps.getMessageID().getValue()
228
// + " to: " + maps.getTo().getValue()
229
// + " reply to: " + rt);
230
// MessageID
231
String JavaDoc id = maps.getMessageID().getValue();
232         if (id == null) {
233             return "expected MessageID MAP";
234         }
235         if (!id.startsWith("urn:uuid")) {
236             return "bad URN format in MessageID MAP: " + id;
237         }
238         // ensure MessageID is unique for this check point
239
Map JavaDoc<String JavaDoc, String JavaDoc> checkPointMessageIDs = messageIDs.get(checkPoint);
240         if (checkPointMessageIDs != null) {
241             if (checkPointMessageIDs.containsKey(id)) {
242                 //return "MessageID MAP duplicate: " + id;
243
return null;
244             }
245         } else {
246             checkPointMessageIDs = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
247             messageIDs.put(checkPoint, checkPointMessageIDs);
248         }
249         checkPointMessageIDs.put(id, id);
250         // To
251
if (maps.getTo() == null) {
252             return "expected To MAP";
253         }
254         return null;
255     }
256
257     /**
258      * Verify presence of expected MAP headers.
259      *
260      * @param wsaHeaders a list of the wsa:* headers present in the SOAP
261      * message
262      * @param parial true if partial response
263      * @return null if all expected headers present, otherwise an error string.
264      */

265     protected static String JavaDoc verifyHeaders(List JavaDoc<String JavaDoc> wsaHeaders, boolean partial) {
266         //System.out.println("verifying headers: " + wsaHeaders);
267
if (!wsaHeaders.contains(Names.WSA_MESSAGEID_NAME)) {
268             return "expected MessageID header";
269         }
270         if (!wsaHeaders.contains(Names.WSA_TO_NAME)) {
271             return "expected To header";
272         }
273         if (!(wsaHeaders.contains(Names.WSA_REPLYTO_NAME)
274               || wsaHeaders.contains(Names.WSA_RELATESTO_NAME))) {
275             return "expected ReplyTo or RelatesTo header";
276         }
277         if (partial) {
278             if (!wsaHeaders.contains(Names.WSA_FROM_NAME)) {
279                 return "expected From header";
280             }
281         } else {
282             if (!wsaHeaders.contains(Names.WSA_ACTION_NAME)) {
283                 return "expected Action header";
284             }
285         }
286         return null;
287     }
288
289     private void checkVerification() {
290         assertNull("MAP/Header verification failed: " + verified, verified);
291     }
292 }
293
294
Popular Tags