KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > encoding > TestOmittedValues


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.encoding;
18
19 import junit.framework.TestCase;
20 import org.apache.axis.Constants;
21 import org.apache.axis.Message;
22 import org.apache.axis.MessageContext;
23 import org.apache.axis.client.Call;
24 import org.apache.axis.client.Service;
25 import org.apache.axis.configuration.BasicServerConfig;
26 import org.apache.axis.description.OperationDesc;
27 import org.apache.axis.description.ParameterDesc;
28 import org.apache.axis.description.ServiceDesc;
29 import org.apache.axis.handlers.soap.SOAPService;
30 import org.apache.axis.message.RPCElement;
31 import org.apache.axis.message.RPCParam;
32 import org.apache.axis.message.SOAPEnvelope;
33 import org.apache.axis.providers.java.RPCProvider;
34 import org.apache.axis.server.AxisServer;
35 import org.apache.axis.transport.local.LocalTransport;
36
37 import javax.xml.namespace.QName JavaDoc;
38
39 /**
40  * A test which confirms that we can correctly call methods where null arguments
41  * are represented by omission from the SOAP envelope.
42  *
43  * @author Glen Daniels (gdaniels@apache.org)
44  */

45 public class TestOmittedValues extends TestCase {
46     String JavaDoc header =
47         "<?xml version=\"1.0\"?>\n" +
48         "<soap:Envelope " +
49           "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
50           "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" " +
51           "xmlns:me=\"urn:me\" " +
52           "xmlns:xsi=\"" + Constants.URI_2001_SCHEMA_XSI + "\" " +
53           "xmlns:xsd=\"" + Constants.URI_2001_SCHEMA_XSD + "\">\n" +
54           "<soap:Body>\n" +
55             "<method>\n";
56
57     String JavaDoc missingParam2 =
58           " <param1 xsi:type=\"xsd:string\">One</param1>\n" +
59           " <param3 xsi:type=\"xsd:string\">Three</param3>\n";
60
61     String JavaDoc footer =
62             "</method>\n" +
63           "</soap:Body>\n" +
64         "</soap:Envelope>\n";
65
66     public TestOmittedValues(String JavaDoc s) {
67         super(s);
68     }
69
70     public TestOmittedValues() {
71         super("service version");
72     }
73
74     public void testOmittedValue() throws Exception JavaDoc {
75         // Set up a server and deploy our service
76
BasicServerConfig config = new BasicServerConfig();
77         AxisServer server = new AxisServer(config);
78
79         SOAPService service = new SOAPService(new RPCProvider());
80         service.setOption("className", "test.encoding.TestOmittedValues");
81         service.setOption("allowedMethods", "*");
82
83         ServiceDesc desc = service.getServiceDescription();
84         // We need parameter descriptors to make sure we can match by name
85
// (the only way omitted==null can work).
86
ParameterDesc [] params = new ParameterDesc [] {
87             new ParameterDesc(new QName JavaDoc("", "param1"), ParameterDesc.IN, null),
88             new ParameterDesc(new QName JavaDoc("", "param2"), ParameterDesc.IN, null),
89             new ParameterDesc(new QName JavaDoc("", "param3"), ParameterDesc.IN, null),
90         };
91         OperationDesc oper = new OperationDesc("method", params, null);
92         desc.addOperationDesc(oper);
93         config.deployService("testOmittedValue", service);
94
95         String JavaDoc msg = header + missingParam2 + footer;
96         Message message = new Message(msg);
97         MessageContext context = new MessageContext(server);
98         context.setRequestMessage(message);
99
100         Call call = new Call(new Service());
101
102         LocalTransport transport = new LocalTransport(server);
103         transport.setRemoteService("testOmittedValue");
104
105         call.setTransport(transport);
106
107         SOAPEnvelope resEnv = call.invoke(message.getSOAPEnvelope());
108         RPCElement rpcElem = (RPCElement)resEnv.getFirstBody();
109         RPCParam param = (RPCParam)rpcElem.getParams().get(0);
110         assertEquals("OK!", param.getObjectValue());
111     }
112
113     // Server-side test method for omitting values
114
public String JavaDoc method(String JavaDoc p1, String JavaDoc p2, String JavaDoc p3) {
115         if (p1.equals("One") && p2 == null && p3.equals("Three")) {
116             return "OK!";
117         }
118         return "Bad arguments!";
119     }
120 }
121
Popular Tags