1 package test.saaj; 2 3 import javax.xml.soap.MessageFactory ; 4 import javax.xml.soap.MimeHeaders ; 5 import javax.xml.soap.Name ; 6 import javax.xml.soap.SOAPEnvelope ; 7 import javax.xml.soap.SOAPHeader ; 8 import javax.xml.soap.SOAPHeaderElement ; 9 import javax.xml.soap.SOAPMessage ; 10 import java.io.ByteArrayInputStream ; 11 import java.io.ByteArrayOutputStream ; 12 import java.util.Iterator ; 13 14 public class TestHeaders extends junit.framework.TestCase { 15 16 public TestHeaders(String name) { 17 super(name); 18 } 19 20 public void testAddingHeaderElements() throws Exception { 21 SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); 22 SOAPEnvelope soapEnv = soapMessage.getSOAPPart().getEnvelope(); 23 SOAPHeader header = soapEnv.getHeader(); 24 header.addChildElement("ebxmlms"); 25 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 26 soapMessage.writeTo(baos); 27 String xml = new String (baos.toByteArray()); 28 assertTrue(xml.indexOf("ebxmlms") != -1); 29 } 30 31 32 private final String actor = "ACTOR#1"; 33 private final String localName = "Local1"; 34 private final String namespace = "http://ws.apache.org"; 35 private final String prefix = "P1"; 36 37 String xmlString = 38 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 39 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + 40 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" + 41 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + 42 " <soapenv:Body>\n" + 43 " <shw:Address xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n" + 44 " <shw:City>GENT</shw:City>\n" + 45 " </shw:Address>\n" + 46 " </soapenv:Body>\n" + 47 "</soapenv:Envelope>"; 48 49 public void testAddingHeaderElements2() throws Exception { 50 MessageFactory mf = MessageFactory.newInstance(); 51 SOAPMessage soapMessage = mf.createMessage(new MimeHeaders (), new ByteArrayInputStream (xmlString.getBytes())); 52 SOAPEnvelope soapEnv = soapMessage.getSOAPPart().getEnvelope(); 53 SOAPHeader header = soapEnv.getHeader(); 54 55 Name headerName = soapEnv.createName(localName, prefix, prefix); 56 SOAPHeaderElement he = header.addHeaderElement(headerName); 57 he.setActor(actor); 58 59 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 60 soapMessage.writeTo(baos); 61 62 String xml = new String (baos.toByteArray()); 63 assertTrue(xml.indexOf(localName) != -1); 64 } 65 66 public void testExtractAllHeaders() throws Exception { 67 MessageFactory mf = MessageFactory.newInstance(); 68 SOAPMessage soapMessage = mf.createMessage(); 69 SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope(); 70 SOAPHeader hdr = envelope.getHeader(); 71 SOAPHeaderElement she1 = hdr.addHeaderElement(envelope.createName("foo1", "f1", "foo1-URI")); 72 she1.setActor("actor-URI"); 73 74 Iterator iterator = hdr.extractAllHeaderElements(); 75 SOAPHeaderElement she = null; 76 int cnt = 0; 77 while (iterator.hasNext()) { 78 cnt++; 79 she = (SOAPHeaderElement ) iterator.next(); 80 assertEquals(she, she1); 81 } 82 assertEquals(1, cnt); 83 iterator = hdr.extractAllHeaderElements(); 84 assertTrue(!iterator.hasNext()); 85 } 86 87 public void testExamineAllHeaders() throws Exception { 88 MessageFactory mf = MessageFactory.newInstance(); 89 SOAPMessage soapMessage = mf.createMessage(); 90 SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope(); 91 SOAPHeader hdr = envelope.getHeader(); 92 SOAPHeaderElement she1 = hdr.addHeaderElement(envelope.createName("foo1", "f1", "foo1-URI")); 93 she1.setActor("actor-URI"); 94 95 Iterator iterator = hdr.examineAllHeaderElements(); 96 SOAPHeaderElement she = null; 97 int cnt = 0; 98 while (iterator.hasNext()) { 99 cnt++; 100 she = (SOAPHeaderElement ) iterator.next(); 101 assertEquals(she, she1); 102 } 103 assertEquals(1, cnt); 104 iterator = hdr.examineAllHeaderElements(); 105 assertTrue(iterator.hasNext()); 106 } 107 } 108 | Popular Tags |