KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > MSGDispatch > TestMessageService


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

16
17 package test.MSGDispatch;
18
19 import junit.framework.TestCase;
20 import org.apache.axis.client.Call;
21 import org.apache.axis.client.Service;
22 import org.apache.axis.configuration.SimpleProvider;
23 import org.apache.axis.configuration.BasicServerConfig;
24 import org.apache.axis.constants.Style;
25 import org.apache.axis.handlers.soap.SOAPService;
26 import org.apache.axis.message.SOAPBodyElement;
27 import org.apache.axis.message.SOAPEnvelope;
28 import org.apache.axis.message.SOAPHeaderElement;
29 import org.apache.axis.providers.java.MsgProvider;
30 import org.apache.axis.server.AxisServer;
31 import org.apache.axis.transport.local.LocalTransport;
32 import org.apache.axis.utils.XMLUtils;
33 import org.w3c.dom.Document JavaDoc;
34
35 import javax.xml.namespace.QName JavaDoc;
36 import java.io.ByteArrayInputStream JavaDoc;
37 import java.util.Vector JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 /**
41  * Test for message style service dispatch.
42  *
43  * @author Glen Daniels (gdaniels@apache.org)
44  */

45 public class TestMessageService extends TestCase {
46     LocalTransport transport;
47
48     public TestMessageService(String JavaDoc s) {
49         super(s);
50     }
51
52     protected void setUp() throws Exception JavaDoc {
53         SOAPService service = new SOAPService(new MsgProvider());
54
55         service.setName("MessageService");
56         service.setOption("className", "test.MSGDispatch.TestService");
57         service.setOption("allowedMethods", "*");
58         service.getServiceDescription().setDefaultNamespace("http://db.com");
59         service.getServiceDescription().setStyle(Style.MESSAGE);
60
61         SimpleProvider config = new BasicServerConfig();
62         config.deployService("MessageService", service);
63
64         AxisServer server = new AxisServer(config);
65
66         transport = new LocalTransport(server);
67         transport.setRemoteService("MessageService");
68     }
69
70     public void testBodyMethod() throws Exception JavaDoc {
71         Call call = new Call(new Service());
72         call.setTransport(transport);
73
74         String JavaDoc xml = "<m:testBody xmlns:m=\"http://db.com\"></m:testBody>";
75         Document JavaDoc doc = XMLUtils.newDocument(new ByteArrayInputStream JavaDoc(xml.getBytes()));
76         SOAPBodyElement[] input = new SOAPBodyElement[1];
77         input[0] = new SOAPBodyElement(doc.getDocumentElement());
78         Vector JavaDoc elems = (Vector JavaDoc) call.invoke( input );
79         assertNotNull("Return was null!", elems);
80         assertTrue("Return had " + elems.size() + " elements (needed 1)",
81                elems.size() == 1);
82         SOAPBodyElement firstBody = (SOAPBodyElement)elems.get(0);
83         assertEquals("http://db.com", firstBody.getNamespaceURI());
84         assertEquals("bodyResult", firstBody.getName());
85     }
86
87     public void testElementMethod() throws Exception JavaDoc {
88         Call call = new Call(new Service());
89         call.setTransport(transport);
90
91         String JavaDoc xml = "<m:testElement xmlns:m=\"http://db.com\"></m:testElement>";
92         Document JavaDoc doc = XMLUtils.newDocument(new ByteArrayInputStream JavaDoc(xml.getBytes()));
93         SOAPBodyElement[] input = new SOAPBodyElement[1];
94         input[0] = new SOAPBodyElement(doc.getDocumentElement());
95         Vector JavaDoc elems = (Vector JavaDoc) call.invoke( input );
96         assertNotNull("Return was null!", elems);
97         assertTrue("Return had " + elems.size() + " elements (needed 1)",
98                elems.size() == 1);
99         SOAPBodyElement firstBody = (SOAPBodyElement)elems.get(0);
100         assertEquals("http://db.com", firstBody.getNamespaceURI());
101         assertEquals("elementResult", firstBody.getName());
102     }
103
104     public void testEnvelopeMethod() throws Exception JavaDoc {
105         Call call = new Call(new Service());
106         call.setTransport(transport);
107
108         String JavaDoc xml = "<testEnvelope xmlns=\"http://db.com\"></testEnvelope>";
109         Document JavaDoc doc = XMLUtils.newDocument(new ByteArrayInputStream JavaDoc(xml.getBytes()));
110         SOAPBodyElement body = new SOAPBodyElement(doc.getDocumentElement());
111         SOAPEnvelope env = new SOAPEnvelope();
112         env.addBodyElement(body);
113         SOAPEnvelope result = call.invoke( env );
114         assertNotNull("Return was null!", result);
115         
116         SOAPBodyElement respBody = result.getFirstBody();
117         assertEquals(new QName JavaDoc("http://db.com", "testEnvelope"), respBody.getQName());
118         Iterator JavaDoc i = respBody.getNamespacePrefixes();
119         assertNotNull("No namespace mappings");
120         assertEquals("Non-default namespace found", "", i.next());
121         assertTrue("Multiple namespace mappings", !i.hasNext());
122         
123         Vector JavaDoc headers = result.getHeaders();
124         assertEquals("Had " + headers.size() + " headers, needed 1", 1, headers.size());
125         SOAPHeaderElement firstHeader = (SOAPHeaderElement)headers.get(0);
126         assertEquals("http://db.com", firstHeader.getNamespaceURI());
127         assertEquals("local", firstHeader.getName());
128         assertEquals(firstHeader.getValue(), "value");
129     }
130
131     /**
132      * Confirm that we get back EXACTLY what we put in when using the
133      * Element[]/Element[] signature for MESSAGE services.
134      *
135      * @throws Exception
136      */

137     public void testElementEcho() throws Exception JavaDoc {
138         Call call = new Call(new Service());
139         call.setTransport(transport);
140
141         // Create a DOM document using a default namespace, since bug
142
// http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16666 indicated
143
// that we might have had a problem here.
144
String JavaDoc xml = "<testElementEcho xmlns=\"http://db.com\" attr='foo'><Data></Data></testElementEcho>";
145         Document JavaDoc doc = XMLUtils.newDocument(new ByteArrayInputStream JavaDoc(xml.getBytes()));
146         SOAPBodyElement body = new SOAPBodyElement(doc.getDocumentElement());
147         SOAPEnvelope env = new SOAPEnvelope();
148         env.addBodyElement(body);
149         
150         // Send it along
151
SOAPEnvelope result = call.invoke( env );
152         assertNotNull("Return was null!", result);
153         
154         // Make sure we get back exactly what we expect, with no extraneous
155
// namespace mappings
156
SOAPBodyElement respBody = result.getFirstBody();
157         assertEquals(new QName JavaDoc("http://db.com", "testElementEcho"), respBody.getQName());
158         Iterator JavaDoc i = respBody.getNamespacePrefixes();
159         assertNotNull("No namespace mappings");
160         assertEquals("Non-default namespace found", "", i.next());
161         assertTrue("Multiple namespace mappings", !i.hasNext());
162     }
163 }
164
Popular Tags