Debian Codegraph @

test/soap12/TestEncodingStyle.java

raw
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 ([email protected])
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;
36
37 /**
38 * Test encodingstyle attribute appearance
39 */
40 public class TestEncodingStyle extends TestCase {
41 private AxisServer server = null;
42
43 public TestEncodingStyle(String name) {
44 super(name);
45 server = new AxisServer();
46 }
47
48
49 private final String 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 HEADER =
57 ">\n" +
58 "<soap:Header ";
59
60 private final String BODY =
61 "/>\n" +
62 "<soap:Body ";
63
64 private final String FAULT_HEAD =
65 ">\n" +
66 "<soap:Fault ";
67
68 private final String 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 FAULT_TAIL =
76 ">\n" +
77 "<hello/>" +
78 "</soap:Detail>" +
79 "</soap:Fault>";
80
81 private final String TAIL =
82 "</soap:Body>\n" +
83 "</soap:Envelope>\n";
84
85 private final String ENCSTYLE_DEF =
86 "soap:encodingStyle=\"" + Constants.URI_SOAP12_ENC + "\"";
87
88
89 private final String MESSAGE_HEAD =
90 ">\n" +
91 "<methodResult xmlns=\"http://tempuri.org/\" ";
92
93 private final String MESSAGE =
94 ">\n";
95
96 private final String MESSAGE_TAIL =
97 "</methodResult>\n";
98
99 private final String ITEM =
100 "<item xsi:type=\"xsd:string\">abc</item>\n";
101
102 private final String INVALID_ENCSTYLE = "http://invalidencodingstyle.org";
103 private final String NO_ENCSTYLE = Constants.URI_SOAP12_NOENC;
104
105 private final String INVALID_ENCSTYLE_DEF =
106 "soap:encodingStyle=\"" + INVALID_ENCSTYLE + "\"";
107
108 private final String NO_ENCSTYLE_DEF =
109 "soap:encodingStyle=\"" + NO_ENCSTYLE + "\"";
110
111
112 public boolean deserialize(String req, QName expected_code, String expected_str) throws Exception {
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 {
132 String 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 {
138 String 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 {
144 String 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 {
150 String 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 {
156 String 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 {
163 String 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 {
169 String 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[] { 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 {
189 String req = ENVELOPE + HEADER + BODY + MESSAGE_HEAD + NO_ENCSTYLE_DEF + MESSAGE + ITEM + MESSAGE_TAIL + TAIL;
190 assertTrue(deserialize(req, null, null) == false);
191 }
192
193 }