1 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 41 public class TestSOAPHeader extends TestCase { 42 43 48 public static Test suite() { 49 return new TestSuite(test.message.TestSOAPHeader.class); 50 } 51 52 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 65 public static final transient String ACTOR = "http://slashdot.org/"; 66 67 68 public static final transient String HEADER_NAMESPACE = 69 "http://xml.apache.org/"; 70 71 72 protected SOAPEnvelope env = null; 73 74 75 protected SOAPHeaderElement headerElement1 = null; 76 77 78 protected SOAPHeaderElement headerElement2 = null; 79 80 85 public TestSOAPHeader(String name) { 86 super(name); 87 } 88 89 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 107 protected void tearDown() { 108 } 109 110 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 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 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 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 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 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 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 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 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 ((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 } 271 | Popular Tags |