KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > soap12 > TestEncodingStyle


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 /**
18  * @author Andras Avar (andras.avar@nokia.com)
19  */

20
21 package test.soap12;
22
23 import junit.framework.TestCase;
24 import org.apache.axis.AxisFault;
25 import org.apache.axis.Constants;
26 import org.apache.axis.Message;
27 import org.apache.axis.MessageContext;
28 import org.apache.axis.encoding.TypeMapping;
29 import org.apache.axis.encoding.TypeMappingRegistry;
30 import org.apache.axis.message.SOAPEnvelope;
31 import org.apache.axis.server.AxisServer;
32 import org.apache.axis.soap.SOAPConstants;
33 import org.apache.axis.utils.Messages;
34
35 import javax.xml.namespace.QName JavaDoc;
36
37 /**
38  * Test encodingstyle attribute appearance
39  */

40 public class TestEncodingStyle extends TestCase {
41     private AxisServer server = null;
42
43     public TestEncodingStyle(String JavaDoc name) {
44         super(name);
45         server = new AxisServer();
46     }
47
48
49     private final String JavaDoc ENVELOPE =
50         "<?xml version=\"1.0\"?>\n" +
51         "<soap:Envelope " +
52           "xmlns:soap=\"" + Constants.URI_SOAP12_ENV + "\" " +
53           "xmlns:xsi=\"" + Constants.URI_DEFAULT_SCHEMA_XSI + "\" " +
54           "xmlns:xsd=\"" + Constants.URI_DEFAULT_SCHEMA_XSD + "\" ";
55
56     private final String JavaDoc HEADER =
57           ">\n" +
58           "<soap:Header ";
59
60     private final String JavaDoc BODY =
61           "/>\n" +
62           "<soap:Body ";
63
64     private final String JavaDoc FAULT_HEAD =
65             ">\n" +
66             "<soap:Fault ";
67
68     private final String JavaDoc FAULT_DETAIL =
69             ">\n" +
70             "<soap:Code>" +
71                 "<soap:Value>soap:Sender</soap:Value>" +
72               "</soap:Code>" +
73               "<soap:Detail ";
74
75     private final String JavaDoc FAULT_TAIL =
76               ">\n" +
77                 "<hello/>" +
78              "</soap:Detail>" +
79             "</soap:Fault>";
80
81     private final String JavaDoc TAIL =
82           "</soap:Body>\n" +
83         "</soap:Envelope>\n";
84
85     private final String JavaDoc ENCSTYLE_DEF =
86           "soap:encodingStyle=\"" + Constants.URI_SOAP12_ENC + "\"";
87
88
89     private final String JavaDoc MESSAGE_HEAD =
90             ">\n" +
91              "<methodResult xmlns=\"http://tempuri.org/\" ";
92
93     private final String JavaDoc MESSAGE =
94             ">\n";
95
96     private final String JavaDoc MESSAGE_TAIL =
97             "</methodResult>\n";
98
99     private final String JavaDoc ITEM =
100            "<item xsi:type=\"xsd:string\">abc</item>\n";
101
102     private final String JavaDoc INVALID_ENCSTYLE = "http://invalidencodingstyle.org";
103     private final String JavaDoc NO_ENCSTYLE = Constants.URI_SOAP12_NOENC;
104
105     private final String JavaDoc INVALID_ENCSTYLE_DEF =
106           "soap:encodingStyle=\"" + INVALID_ENCSTYLE + "\"";
107
108     private final String JavaDoc NO_ENCSTYLE_DEF =
109           "soap:encodingStyle=\"" + NO_ENCSTYLE + "\"";
110
111
112     public boolean deserialize(String JavaDoc req, QName JavaDoc expected_code, String JavaDoc expected_str) throws Exception JavaDoc {
113         Message message = new Message(req);
114         MessageContext context = new MessageContext(server);
115         context.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
116         context.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE);
117
118         message.setMessageContext(context);
119
120         boolean expectedFault = false;
121         try {
122             SOAPEnvelope envelope = message.getSOAPEnvelope();
123         } catch (AxisFault af) {
124             return af.getFaultString().indexOf(expected_str) != -1 &&
125                    expected_code.equals(af.getFaultCode());
126         }
127
128         return expectedFault;
129     }
130
131     public void testEncStyleInEnvelope() throws Exception JavaDoc {
132         String JavaDoc req = ENVELOPE + ENCSTYLE_DEF + HEADER + BODY + FAULT_HEAD + FAULT_DETAIL + FAULT_TAIL + TAIL;
133         assertTrue(deserialize(req, Constants.FAULT_SOAP12_SENDER,
134             Messages.getMessage("noEncodingStyleAttrAppear", "Envelope")));
135     }
136
137     public void testEncStyleInHeader() throws Exception JavaDoc {
138         String JavaDoc req = ENVELOPE + HEADER + ENCSTYLE_DEF + BODY + FAULT_HEAD + FAULT_DETAIL + FAULT_TAIL + TAIL;
139         assertTrue(deserialize(req, Constants.FAULT_SOAP12_SENDER,
140             Messages.getMessage("noEncodingStyleAttrAppear", "Header")));
141     }
142
143     public void testEncStyleInBody() throws Exception JavaDoc {
144         String JavaDoc req = ENVELOPE + HEADER + BODY + ENCSTYLE_DEF + FAULT_HEAD + FAULT_DETAIL + FAULT_TAIL + TAIL;
145         assertTrue(deserialize(req, Constants.FAULT_SOAP12_SENDER,
146             Messages.getMessage("noEncodingStyleAttrAppear", "Body")));
147     }
148
149     public void testEncStyleInFault() throws Exception JavaDoc {
150         String JavaDoc req = ENVELOPE + HEADER + BODY + FAULT_HEAD + ENCSTYLE_DEF + FAULT_DETAIL + FAULT_TAIL + TAIL;
151         assertTrue(deserialize(req, Constants.FAULT_SOAP12_SENDER,
152             Messages.getMessage("noEncodingStyleAttrAppear", "Fault")));
153     }
154
155     public void testEncStyleInDetail() throws Exception JavaDoc {
156         String JavaDoc req = ENVELOPE + HEADER + BODY + FAULT_HEAD + FAULT_DETAIL + ENCSTYLE_DEF + FAULT_TAIL + TAIL;
157         assertTrue(deserialize(req, Constants.FAULT_SOAP12_SENDER,
158
159           Messages.getMessage("noEncodingStyleAttrAppear", "Detail")));
160     }
161
162     public void testInvalidEncodingStyle() throws Exception JavaDoc {
163         String JavaDoc req = ENVELOPE + HEADER + BODY + MESSAGE_HEAD + INVALID_ENCSTYLE_DEF + MESSAGE + ITEM + MESSAGE_TAIL + TAIL;
164         assertTrue(deserialize(req, Constants.FAULT_SOAP12_DATAENCODINGUNKNOWN,
165             Messages.getMessage("invalidEncodingStyle")));
166     }
167
168     public void testAcceptUserEncodingStyle() throws Exception JavaDoc {
169         String JavaDoc req = ENVELOPE + HEADER + BODY + MESSAGE_HEAD + INVALID_ENCSTYLE_DEF + MESSAGE + ITEM + MESSAGE_TAIL + TAIL;
170
171         Message message = new Message(req);
172         MessageContext context = new MessageContext(server);
173         context.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE);
174
175         // Set the "invalid" encoding style
176
TypeMappingRegistry reg = context.getTypeMappingRegistry();
177         TypeMapping tm = (TypeMapping) reg.createTypeMapping();
178         tm.setSupportedEncodings(new String JavaDoc[] { INVALID_ENCSTYLE });
179         reg.register(INVALID_ENCSTYLE, tm);
180         context.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
181
182         message.setMessageContext(context);
183
184         SOAPEnvelope envelope = message.getSOAPEnvelope();
185         assertTrue(envelope != null);
186    }
187
188     public void testNoEncodingStyle() throws Exception JavaDoc {
189         String JavaDoc req = ENVELOPE + HEADER + BODY + MESSAGE_HEAD + NO_ENCSTYLE_DEF + MESSAGE + ITEM + MESSAGE_TAIL + TAIL;
190         assertTrue(deserialize(req, null, null) == false);
191     }
192
193 }
194
Popular Tags