KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > http > HttpSoapAndSaajTest


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.http;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.HttpURLConnection JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25
26 import javax.xml.messaging.URLEndpoint;
27 import javax.xml.namespace.QName JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29
30 import junit.framework.TestCase;
31
32 import org.apache.activemq.broker.BrokerService;
33 import org.apache.activemq.xbean.BrokerFactoryBean;
34 import org.apache.servicemix.components.saaj.SaajBinding;
35 import org.apache.servicemix.components.util.MockServiceComponent;
36 import org.apache.servicemix.jbi.container.ActivationSpec;
37 import org.apache.servicemix.jbi.container.JBIContainer;
38 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
39 import org.apache.servicemix.jbi.jaxp.StringSource;
40 import org.apache.servicemix.jbi.util.DOMUtil;
41 import org.apache.servicemix.jbi.util.FileUtil;
42 import org.apache.xml.serialize.OutputFormat;
43 import org.apache.xml.serialize.XMLSerializer;
44 import org.apache.xpath.CachedXPathAPI;
45 import org.springframework.core.io.ClassPathResource;
46 import org.w3c.dom.Document JavaDoc;
47 import org.w3c.dom.Element JavaDoc;
48 import org.w3c.dom.Node JavaDoc;
49 import org.w3c.dom.traversal.NodeIterator;
50
51 public class HttpSoapAndSaajTest extends TestCase {
52     
53     private static final int PORT_1 = 7012;
54     private static final int PORT_2 = 7012;
55     private static final int PORT_WS_1 = 7014;
56     private static final int PORT_WS_2 = 7014;
57
58     protected JBIContainer container;
59     protected BrokerService broker;
60     
61     protected void setUp() throws Exception JavaDoc {
62         BrokerFactoryBean bfb = new BrokerFactoryBean(new ClassPathResource("broker.xml"));
63         bfb.afterPropertiesSet();
64         broker = bfb.getBroker();
65         broker.start();
66         container = new JBIContainer();
67         container.setMonitorInstallationDirectory(false);
68         container.setUseMBeanServer(false);
69         container.setCreateMBeanServer(false);
70         container.setEmbedded(true);
71         container.setFlowName("jms?jmsURL=tcp://localhost:61626");
72         container.init();
73         container.start();
74     }
75     
76     protected void tearDown() throws Exception JavaDoc {
77         if (container != null) {
78             container.shutDown();
79         }
80         if (broker != null) {
81             broker.stop();
82         }
83     }
84     
85     public void testInOut() throws Exception JavaDoc {
86         ActivationSpec as = new ActivationSpec();
87         as.setId("saaj");
88         SaajBinding saaj = new SaajBinding();
89         saaj.setSoapEndpoint(new URLEndpoint("http://localhost:" + PORT_WS_1));
90         as.setComponent(saaj);
91         as.setService(new QName JavaDoc("saaj"));
92         container.activateComponent(as);
93         
94         as = new ActivationSpec();
95         as.setId("xfireBinding");
96         as.setComponent(new HttpSoapConnector(null, PORT_2, true));
97         as.setDestinationService(new QName JavaDoc("saaj"));
98         container.activateComponent(as);
99         
100         as = new ActivationSpec();
101         as.setId("webservice");
102         as.setComponent(new HttpConnector(null, PORT_WS_2));
103         as.setDestinationService(new QName JavaDoc("mock"));
104         container.activateComponent(as);
105         
106         as = new ActivationSpec();
107         as.setId("mock");
108         MockServiceComponent mock = new MockServiceComponent();
109         mock.setResponseResource(new ClassPathResource("soap-response.xml", getClass()));
110         as.setComponent(mock);
111         as.setService(new QName JavaDoc("mock"));
112         container.activateComponent(as);
113
114         URLConnection JavaDoc connection = new URL JavaDoc("http://localhost:" + PORT_1).openConnection();
115         connection.setDoOutput(true);
116         connection.setDoInput(true);
117         ((HttpURLConnection JavaDoc) connection).setRequestProperty("Content-Type", "text/xml; charset=utf-8;");
118         OutputStream JavaDoc os = connection.getOutputStream();
119         // Post the request file.
120
InputStream JavaDoc fis = getClass().getResourceAsStream("soap-request.xml");
121         FileUtil.copyInputStream(fis, os);
122         // Read the response.
123
InputStream JavaDoc is = connection.getInputStream();
124         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
125         FileUtil.copyInputStream(is, baos);
126         
127         // Check xml validity
128
Node JavaDoc node = new SourceTransformer().toDOMNode(new StringSource(baos.toString()));
129
130         OutputFormat format = new OutputFormat((Document JavaDoc) node);
131         format.setLineWidth(65);
132         format.setIndenting(true);
133         format.setIndent(2);
134         XMLSerializer serializer = new XMLSerializer(System.err, format);
135         System.err.println();
136         serializer.serialize((Document JavaDoc) node);
137         
138         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
139         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "//*[local-name() = 'userId']");
140         Element JavaDoc root = (Element JavaDoc) iterator.nextNode();
141         QName JavaDoc qname = DOMUtil.createQName(root, root.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type"));
142         assertEquals("http://www.w3.org/2001/XMLSchema", qname.getNamespaceURI());
143         assertEquals("string", qname.getLocalPart());
144         
145     }
146
147     protected String JavaDoc textValueOfXPath(Node JavaDoc node, String JavaDoc xpath) throws TransformerException JavaDoc {
148         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
149         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, xpath);
150         Node JavaDoc root = iterator.nextNode();
151         if (root instanceof Element JavaDoc) {
152             Element JavaDoc element = (Element JavaDoc) root;
153             if (element == null) {
154                 return "";
155             }
156             String JavaDoc text = DOMUtil.getElementText(element);
157             return text;
158         }
159         else {
160             return root.getNodeValue();
161         }
162     }
163 }
164
Popular Tags