KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > saaj > SaajSoapActionTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.saaj;
18
19 import javax.jbi.messaging.ExchangeStatus;
20 import javax.jbi.messaging.InOut;
21 import javax.jbi.messaging.MessageExchange;
22 import javax.jbi.messaging.MessagingException;
23 import javax.jbi.messaging.NormalizedMessage;
24 import javax.xml.messaging.URLEndpoint;
25 import javax.xml.namespace.QName JavaDoc;
26 import javax.xml.soap.MessageFactory JavaDoc;
27
28 import junit.framework.TestCase;
29
30 import org.apache.servicemix.MessageExchangeListener;
31 import org.apache.servicemix.client.DefaultServiceMixClient;
32 import org.apache.servicemix.components.http.HttpConnector;
33 import org.apache.servicemix.components.util.ComponentSupport;
34 import org.apache.servicemix.jbi.container.ActivationSpec;
35 import org.apache.servicemix.jbi.container.JBIContainer;
36 import org.apache.servicemix.jbi.jaxp.StringSource;
37
38 public class SaajSoapActionTest extends TestCase {
39
40     private JBIContainer jbi;
41     private SaajBinding saaj;
42     private HttpConnector http;
43     
44     protected void setUp() throws Exception JavaDoc {
45         jbi = new JBIContainer();
46         jbi.setEmbedded(true);
47         jbi.setUseMBeanServer(false);
48         jbi.setCreateMBeanServer(false);
49         jbi.init();
50     }
51     
52     protected void tearDown() throws Exception JavaDoc {
53         jbi.shutDown();
54     }
55     
56     protected String JavaDoc testSoapAction(String JavaDoc soapAction, MessageFactory JavaDoc messageFactory) throws Exception JavaDoc {
57         saaj = new SaajBinding();
58         saaj.setService(new QName JavaDoc("saaj"));
59         saaj.setEndpoint("endpoint");
60         saaj.setSoapEndpoint(new URLEndpoint("http://localhost:8192/"));
61         saaj.setSoapAction(soapAction);
62         SaajMarshaler marshaler = new SaajMarshaler();
63         marshaler.setMessageFactory(messageFactory);
64         saaj.setMarshaler(marshaler);
65         jbi.activateComponent(saaj, "saaj");
66         
67         http = new HttpConnector(new org.mortbay.jetty.nio.SelectChannelConnector());
68         http.setHost("localhost");
69         http.setPort(8192);
70         //http.setDefaultInOut(false);
71
ActivationSpec httpAs = new ActivationSpec();
72         httpAs.setComponent(http);
73         httpAs.setComponentName("http");
74         httpAs.setDestinationService(new QName JavaDoc("receiver"));
75         jbi.activateComponent(httpAs);
76         
77         SoapActionReceiver receiver = new SoapActionReceiver();
78         jbi.activateComponent(receiver, "receiver");
79
80         jbi.start();
81         
82         DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
83         InOut me = client.createInOutExchange();
84         me.setService(new QName JavaDoc("saaj"));
85         me.getInMessage().setContent(new StringSource("<hello>world</hello>"));
86         client.sendSync(me);
87         if (me.getStatus() == ExchangeStatus.ERROR) {
88             if (me.getError() != null) {
89                 throw me.getError();
90             }
91         }
92         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
93         client.done(me);
94         
95         Thread.sleep(50);
96         
97         return receiver.sentSoapAction;
98     }
99     
100     public void testNullSoapActionAxis() throws Exception JavaDoc {
101         String JavaDoc received = testSoapAction(null, new org.apache.axis.soap.MessageFactoryImpl());
102         assertEquals("\"\"", received);
103     }
104     
105     /*
106     public void testNullSoapActionSun() throws Exception {
107         String received = testSoapAction(null, new com.sun.xml.messaging.saaj.soap.MessageFactoryImpl());
108         assertEquals("\"\"", received);
109     }
110     */

111     
112     public void testEmptySoapActionAxis() throws Exception JavaDoc {
113         String JavaDoc received = testSoapAction("", new org.apache.axis.soap.MessageFactoryImpl());
114         assertEquals("\"\"", received);
115     }
116     
117     /*
118     public void testEmptySoapActionSun() throws Exception {
119         String received = testSoapAction("", new com.sun.xml.messaging.saaj.soap.MessageFactoryImpl());
120         assertEquals("", received);
121     }
122     */

123     
124     public void testQuotesSoapActionAxis() throws Exception JavaDoc {
125         String JavaDoc received = testSoapAction("\"\"", new org.apache.axis.soap.MessageFactoryImpl());
126         assertEquals("\"\"", received);
127     }
128     
129     /*
130     public void testQuotesSoapActionSun() throws Exception {
131         String received = testSoapAction("\"\"", new com.sun.xml.messaging.saaj.soap.MessageFactoryImpl());
132         assertEquals("\"\"", received);
133     }
134     */

135     
136     public void testWithSoapActionAxis() throws Exception JavaDoc {
137         String JavaDoc received = testSoapAction("action", new org.apache.axis.soap.MessageFactoryImpl());
138         assertEquals("action", received);
139     }
140     
141     /*
142     public void testWithSoapActionSun() throws Exception {
143         String received = testSoapAction("action", new com.sun.xml.messaging.saaj.soap.MessageFactoryImpl());
144         assertEquals("action", received);
145     }
146     */

147     
148     protected static class SoapActionReceiver extends ComponentSupport implements MessageExchangeListener {
149         public String JavaDoc sentSoapAction;
150         public SoapActionReceiver() {
151             setService(new QName JavaDoc("receiver"));
152             setEndpoint("endpoint");
153         }
154         public void onMessageExchange(MessageExchange exchange) throws MessagingException {
155             if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
156                 NormalizedMessage msg = exchange.getMessage("in");
157                 sentSoapAction = (String JavaDoc) msg.getProperty("SOAPAction");
158                 NormalizedMessage out = exchange.createMessage();
159                 out.setContent(msg.getContent());
160                 answer(exchange, out);
161             }
162         }
163         
164     }
165     
166 }
167
Popular Tags