| 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 |
package test.message; |
| 17 |
|
| 18 |
import junit.framework.Test; |
| 19 |
import junit.framework.TestCase; |
| 20 |
import junit.framework.TestSuite; |
| 21 |
import org.apache.axis.Constants; |
| 22 |
import org.apache.axis.message.SOAPEnvelope; |
| 23 |
import org.apache.axis.message.SOAPHeader; |
| 24 |
import org.apache.axis.message.SOAPHeaderElement; |
| 25 |
|
| 26 |
import javax.xml.soap.MessageFactory; |
| 27 |
import javax.xml.soap.MimeHeaders; |
| 28 |
import javax.xml.soap.Name; |
| 29 |
import javax.xml.soap.SOAPBody; |
| 30 |
import javax.xml.soap.SOAPElement; |
| 31 |
import javax.xml.soap.SOAPMessage; |
| 32 |
import javax.xml.soap.SOAPPart; |
| 33 |
import java.io.ByteArrayInputStream; |
| 34 |
import java.util.Iterator; |
| 35 |
|
| 36 |
/** |
| 37 |
* @author [email protected] |
| 38 |
* @author $Author: gdaniels $ |
| 39 |
* @version $Revision: 257503 $ |
| 40 |
*/ |
| 41 |
public class TestSOAPHeader extends TestCase { |
| 42 |
|
| 43 |
/** |
| 44 |
* Method suite |
| 45 |
* |
| 46 |
* @return |
| 47 |
*/ |
| 48 |
public static Test suite() { |
| 49 |
return new TestSuite(test.message.TestSOAPHeader.class); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Method main |
| 54 |
* |
| 55 |
* @param argv |
| 56 |
*/ |
| 57 |
public static void main(String[] argv) throws Exception { |
| 58 |
TestSOAPHeader tester = new TestSOAPHeader("TestSOAPHeader"); |
| 59 |
tester.setUp(); |
| 60 |
tester.testExamineHeaderElements1(); |
| 61 |
tester.testSoapHeadersBUG(); |
| 62 |
} |
| 63 |
|
| 64 |
/** Field ACTOR */ |
| 65 |
public static final transient String ACTOR = "http://slashdot.org/"; |
| 66 |
|
| 67 |
/** Field HEADER_NAMESPACE */ |
| 68 |
public static final transient String HEADER_NAMESPACE = |
| 69 |
"http://xml.apache.org/"; |
| 70 |
|
| 71 |
/** Field env */ |
| 72 |
protected SOAPEnvelope env = null; |
| 73 |
|
| 74 |
/** Field headerElement1 */ |
| 75 |
protected SOAPHeaderElement headerElement1 = null; |
| 76 |
|
| 77 |
/** Field headerElement2 */ |
| 78 |
protected SOAPHeaderElement headerElement2 = null; |
| 79 |
|
| 80 |
/** |
| 81 |
* Constructor TestSOAPHeader |
| 82 |
* |
| 83 |
* @param name |
| 84 |
*/ |
| 85 |
public TestSOAPHeader(String name) { |
| 86 |
super(name); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Method setUp |
| 91 |
*/ |
| 92 |
protected void setUp() { |
| 93 |
env = new org.apache.axis.message.SOAPEnvelope(); |
| 94 |
headerElement1 = new SOAPHeaderElement(HEADER_NAMESPACE, "SomeHeader1", |
| 95 |
"SomeValue1"); |
| 96 |
headerElement1.setActor(ACTOR); |
| 97 |
env.addHeader(headerElement1); |
| 98 |
headerElement2 = new SOAPHeaderElement(HEADER_NAMESPACE, "SomeHeader2", |
| 99 |
"SomeValue2"); |
| 100 |
headerElement2.setActor(Constants.URI_SOAP11_NEXT_ACTOR); |
| 101 |
env.addHeader(headerElement2); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Method tearDown |
| 106 |
*/ |
| 107 |
protected void tearDown() { |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Tests the happy path. |
| 112 |
* |
| 113 |
* @throws Exception |
| 114 |
*/ |
| 115 |
public void testExamineHeaderElements1() throws Exception { |
| 116 |
SOAPHeader header = |
| 117 |
(org.apache.axis.message.SOAPHeader) env.getHeader(); |
| 118 |
Iterator iter = header.examineHeaderElements(ACTOR); |
| 119 |
|
| 120 |
// This would be a lot simpler if getHeadersByActor() were visible. |
| 121 |
SOAPHeaderElement headerElement = null; |
| 122 |
int expectedHeaders = 2; |
| 123 |
int foundHeaders = 0; |
| 124 |
|
| 125 |
while (iter.hasNext()) { |
| 126 |
headerElement = (SOAPHeaderElement) iter.next(); |
| 127 |
if (Constants.URI_SOAP11_NEXT_ACTOR.equals(headerElement.getActor()) |
| 128 |
|| ACTOR.equals(headerElement.getActor())) { |
| 129 |
foundHeaders++; |
| 130 |
} |
| 131 |
} |
| 132 |
assertEquals("Didn't find all the right actors.", expectedHeaders, |
| 133 |
foundHeaders); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Tests when the user submits a null actor. |
| 138 |
* |
| 139 |
* @throws Exception |
| 140 |
*/ |
| 141 |
public void testExamineHeaderElements2() throws Exception { |
| 142 |
SOAPHeader header = |
| 143 |
(org.apache.axis.message.SOAPHeader) env.getHeader(); |
| 144 |
Iterator iter = header.examineHeaderElements(null); |
| 145 |
|
| 146 |
// This would be a lot simpler if getHeadersByActor() were visible. |
| 147 |
SOAPHeaderElement headerElement = null; |
| 148 |
int expectedHeaders = 1; |
| 149 |
int foundHeaders = 0; |
| 150 |
|
| 151 |
while (iter.hasNext()) { |
| 152 |
headerElement = (SOAPHeaderElement) iter.next(); |
| 153 |
if (Constants.URI_SOAP11_NEXT_ACTOR.equals( |
| 154 |
headerElement.getActor())) { |
| 155 |
foundHeaders++; |
| 156 |
} |
| 157 |
} |
| 158 |
assertEquals("Didn't find all the right actors.", expectedHeaders, |
| 159 |
foundHeaders); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Tests the happy path. |
| 164 |
* |
| 165 |
* @throws Exception |
| 166 |
*/ |
| 167 |
public void testExtractHeaderElements1() throws Exception { |
| 168 |
SOAPHeader header = |
| 169 |
(org.apache.axis.message.SOAPHeader) env.getHeader(); |
| 170 |
Iterator iter = header.extractHeaderElements(ACTOR); |
| 171 |
|
| 172 |
// This would be a lot simpler if getHeadersByActor() were visible. |
| 173 |
SOAPHeaderElement headerElement = null; |
| 174 |
int expectedHeaders = 2; |
| 175 |
int foundHeaders = 0; |
| 176 |
|
| 177 |
while (iter.hasNext()) { |
| 178 |
headerElement = (SOAPHeaderElement) iter.next(); |
| 179 |
if (Constants.URI_SOAP11_NEXT_ACTOR.equals(headerElement.getActor()) |
| 180 |
|| ACTOR.equals(headerElement.getActor())) { |
| 181 |
foundHeaders++; |
| 182 |
} |
| 183 |
} |
| 184 |
assertEquals("Didn't find all the right actors.", expectedHeaders, |
| 185 |
foundHeaders); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Tests when the user submits a null actor. |
| 190 |
* |
| 191 |
* @throws Exception |
| 192 |
*/ |
| 193 |
public void testExtractHeaderElements2() throws Exception { |
| 194 |
SOAPHeader header = |
| 195 |
(org.apache.axis.message.SOAPHeader) env.getHeader(); |
| 196 |
Iterator iter = header.extractHeaderElements(null); |
| 197 |
|
| 198 |
// This would be a lot simpler if getHeadersByActor() were visible. |
| 199 |
SOAPHeaderElement headerElement = null; |
| 200 |
int expectedHeaders = 1; |
| 201 |
int foundHeaders = 0; |
| 202 |
|
| 203 |
while (iter.hasNext()) { |
| 204 |
headerElement = (SOAPHeaderElement) iter.next(); |
| 205 |
if (Constants.URI_SOAP11_NEXT_ACTOR.equals( |
| 206 |
headerElement.getActor())) { |
| 207 |
foundHeaders++; |
| 208 |
} |
| 209 |
} |
| 210 |
assertEquals("Didn't find all the right actors.", expectedHeaders, |
| 211 |
foundHeaders); |
| 212 |
} |
| 213 |
|
| 214 |
String xmlString = |
| 215 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ |
| 216 |
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"+ |
| 217 |
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"+ |
| 218 |
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"+ |
| 219 |
" <soapenv:Header>\n"+ |
| 220 |
" <shw:Hello xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n"+ |
| 221 |
" <shw:Myname>Tony</shw:Myname>\n"+ |
| 222 |
" </shw:Hello>\n"+ |
| 223 |
" </soapenv:Header>\n"+ |
| 224 |
" <soapenv:Body>\n"+ |
| 225 |
" <shw:Address xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n"+ |
| 226 |
" <shw:City>GENT</shw:City>\n"+ |
| 227 |
" </shw:Address>\n"+ |
| 228 |
" </soapenv:Body>\n"+ |
| 229 |
"</soapenv:Envelope>"; |
| 230 |
|
| 231 |
/** |
| 232 |
* Method testSoapHeadersBUG |
| 233 |
* |
| 234 |
* @param filename |
| 235 |
* |
| 236 |
* @throws Exception |
| 237 |
*/ |
| 238 |
public void testSoapHeadersBUG() throws Exception { |
| 239 |
MimeHeaders mimeheaders = new MimeHeaders(); |
| 240 |
|
| 241 |
mimeheaders.addHeader("Content-Type", "text/xml"); |
| 242 |
ByteArrayInputStream instream = new ByteArrayInputStream(xmlString.getBytes()); |
| 243 |
MessageFactory factory = |
| 244 |
MessageFactory.newInstance(); |
| 245 |
SOAPMessage msg = |
| 246 |
factory.createMessage(mimeheaders, instream); |
| 247 |
org.apache.axis.client.AxisClient axisengine = |
| 248 |
new org.apache.axis.client.AxisClient(); |
| 249 |
|
| 250 |
// need to set it not null , if not nullpointer in sp.getEnvelope() |
| 251 |
((org.apache.axis.Message) msg).setMessageContext( |
| 252 |
new org.apache.axis.MessageContext(axisengine)); |
| 253 |
SOAPPart sp = msg.getSOAPPart(); |
| 254 |
javax.xml.soap.SOAPEnvelope se = sp.getEnvelope(); |
| 255 |
javax.xml.soap.SOAPHeader sh = se.getHeader(); |
| 256 |
SOAPBody sb = se.getBody(); |
| 257 |
Iterator it = sh.getChildElements(); |
| 258 |
int count = 0; |
| 259 |
|
| 260 |
while (it.hasNext()) { |
| 261 |
SOAPElement el = (SOAPElement) it.next(); |
| 262 |
count++; |
| 263 |
Name name = el.getElementName(); |
| 264 |
System.out.println("Element:" + el); |
| 265 |
System.out.println("HEADER ELEMENT NAME:" + name.getPrefix() + ":" |
| 266 |
+ name.getLocalName() + " " + name.getURI()); |
| 267 |
} |
| 268 |
assertTrue(count==1); |
| 269 |
} |
| 270 |
} |