KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > HttpWsdlTest


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.http;
18
19 import javax.jbi.servicedesc.ServiceEndpoint;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import javax.wsdl.Definition;
22 import javax.wsdl.PortType;
23 import javax.wsdl.factory.WSDLFactory;
24 import javax.wsdl.xml.WSDLReader;
25 import javax.xml.namespace.QName JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import org.apache.commons.httpclient.HttpClient;
30 import org.apache.commons.httpclient.methods.GetMethod;
31 import org.apache.servicemix.components.util.EchoComponent;
32 import org.apache.servicemix.jbi.container.ActivationSpec;
33 import org.apache.servicemix.jbi.container.JBIContainer;
34 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
35 import org.apache.servicemix.jbi.jaxp.StringSource;
36 import org.springframework.core.io.UrlResource;
37 import org.w3c.dom.Document JavaDoc;
38
39 public class HttpWsdlTest extends TestCase {
40
41     protected JBIContainer container;
42     
43     protected void setUp() throws Exception JavaDoc {
44         container = new JBIContainer();
45         container.setUseMBeanServer(false);
46         container.setCreateMBeanServer(false);
47         container.setEmbedded(true);
48         container.init();
49     }
50     
51     protected void tearDown() throws Exception JavaDoc {
52         if (container != null) {
53             container.shutDown();
54         }
55     }
56
57     public void testWithNonStandaloneWsdl() throws Exception JavaDoc {
58         // Add a receiver component
59
ActivationSpec asEcho = new ActivationSpec("echo", new EchoComponent() {
60             public Document JavaDoc getServiceDescription(ServiceEndpoint endpoint) {
61                 try {
62                     Definition def = WSDLFactory.newInstance().newDefinition();
63                     PortType type = def.createPortType();
64                     type.setUndefined(false);
65                     type.setQName(new QName JavaDoc("http://porttype.test", "MyConsumerInterface"));
66                     def.setTargetNamespace("http://porttype.test");
67                     def.addNamespace("tns", "http://porttype.test");
68                     def.addPortType(type);
69                     Document JavaDoc doc = WSDLFactory.newInstance().newWSDLWriter().getDocument(def);
70                     return doc;
71                 } catch (Exception JavaDoc e) {
72                     throw new RuntimeException JavaDoc(e);
73                 }
74             }
75         });
76         asEcho.setEndpoint("myConsumer");
77         asEcho.setService(new QName JavaDoc("http://test", "MyConsumerService"));
78         container.activateComponent(asEcho);
79         
80         // HTTP Component
81
HttpEndpoint ep = new HttpEndpoint();
82         ep.setService(new QName JavaDoc("http://test", "MyConsumerService"));
83         ep.setEndpoint("myConsumer");
84         ep.setRoleAsString("consumer");
85         ep.setLocationURI("http://localhost:8195/Service");
86         HttpSpringComponent http = new HttpSpringComponent();
87         http.setEndpoints(new HttpEndpoint[] { ep });
88         container.activateComponent(http, "HttpWsdlTest");
89         
90         // Start container
91
container.start();
92
93         GetMethod get = new GetMethod("http://localhost:8195/Service/?wsdl");
94         int state = new HttpClient().executeMethod(get);
95         assertEquals(HttpServletResponse.SC_OK, state);
96         Document JavaDoc doc = (Document JavaDoc) new SourceTransformer().toDOMNode(new StringSource(get.getResponseBodyAsString()));
97         
98         // Test WSDL
99
WSDLFactory factory = WSDLFactory.newInstance();
100         WSDLReader reader = factory.newWSDLReader();
101         Definition def;
102         def = reader.readWSDL("http://localhost:8195/Service/?wsdl", doc);
103         assertNotNull(def);
104         assertNotNull(def.getImports());
105         assertEquals(1, def.getImports().size());
106     }
107     
108     public void testExternalNonStandaloneWsdl() throws Exception JavaDoc {
109         // HTTP Component
110
HttpEndpoint ep = new HttpEndpoint();
111         ep.setService(new QName JavaDoc("http://test", "MyConsumerService"));
112         ep.setEndpoint("myConsumer");
113         ep.setRoleAsString("consumer");
114         ep.setLocationURI("http://localhost:8196/Service/");
115         ep.setWsdlResource(new UrlResource("http://www.ws-i.org/SampleApplications/SupplyChainManagement/2002-08/Retailer.wsdl"));
116         HttpSpringComponent http = new HttpSpringComponent();
117         http.setEndpoints(new HttpEndpoint[] { ep });
118         container.activateComponent(http, "HttpWsdlTest");
119         
120         // Start container
121
container.start();
122
123         GetMethod get = new GetMethod("http://localhost:8196/Service/?wsdl");
124         int state = new HttpClient().executeMethod(get);
125         assertEquals(HttpServletResponse.SC_OK, state);
126         Document JavaDoc doc = (Document JavaDoc) new SourceTransformer().toDOMNode(new StringSource(get.getResponseBodyAsString()));
127         
128         // Test WSDL
129
WSDLFactory factory = WSDLFactory.newInstance();
130         WSDLReader reader = factory.newWSDLReader();
131         Definition def;
132         def = reader.readWSDL("http://localhost:8196/Service/?wsdl", doc);
133         assertNotNull(def);
134         assertNotNull(def.getImports());
135         assertEquals(1, def.getImports().size());
136     }
137     
138 }
139
Popular Tags