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.Node ; 7 import javax.xml.soap.SOAPBody ; 8 import javax.xml.soap.SOAPConnection ; 9 import javax.xml.soap.SOAPConnectionFactory ; 10 import javax.xml.soap.SOAPElement ; 11 import javax.xml.soap.SOAPEnvelope ; 12 import javax.xml.soap.SOAPFault ; 13 import javax.xml.soap.SOAPHeader ; 14 import javax.xml.soap.SOAPHeaderElement ; 15 import javax.xml.soap.SOAPMessage ; 16 import javax.xml.soap.SOAPPart ; 17 import javax.xml.soap.Text ; 18 import javax.xml.soap.Detail ; 19 import javax.xml.soap.DetailEntry ; 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.util.Iterator ; 23 24 public class TestEnvelope extends junit.framework.TestCase { 25 26 public TestEnvelope(String name) { 27 super(name); 28 } 29 30 31 String xmlString = 32 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 33 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + 34 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" + 35 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + 36 " <soapenv:Header>\n" + 37 " <shw:Hello xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n" + 38 " <shw:Myname>Tony</shw:Myname>\n" + 39 " </shw:Hello>\n" + 40 " </soapenv:Header>\n" + 41 " <soapenv:Body>\n" + 42 " <shw:Address xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n" + 43 " <shw:City>GENT</shw:City>\n" + 44 " </shw:Address>\n" + 45 " </soapenv:Body>\n" + 46 "</soapenv:Envelope>"; 47 48 public void testEnvelope() throws Exception { 50 MessageFactory mf = MessageFactory.newInstance(); 51 SOAPMessage smsg = 52 mf.createMessage(new MimeHeaders (), new ByteArrayInputStream (xmlString.getBytes())); 53 SOAPPart sp = smsg.getSOAPPart(); 54 SOAPEnvelope se = (SOAPEnvelope )sp.getEnvelope(); 55 assertTrue(se != null); 57 } 58 59 public void testEnvelope2() throws Exception { 61 MessageFactory mf = MessageFactory.newInstance(); 62 SOAPMessage smsg = 63 mf.createMessage(new MimeHeaders (), new ByteArrayInputStream (xmlString.getBytes())); 64 SOAPPart sp = smsg.getSOAPPart(); 65 SOAPEnvelope se = (SOAPEnvelope )sp.getEnvelope(); 66 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 67 smsg.writeTo(baos); 68 SOAPBody body = smsg.getSOAPPart().getEnvelope().getBody(); 69 assertTrue(body != null); 70 } 71 72 public void testEnvelopeWithLeadingComment() throws Exception { 73 String soapMessageWithLeadingComment = 74 "<?xml version='1.0' encoding='UTF-8'?>" + 75 "<!-- Comment -->" + 76 "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" + 77 "<env:Body><echo><arg0>Hello</arg0></echo></env:Body>" + 78 "</env:Envelope>"; 79 80 SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance(); 81 SOAPConnection con = scFactory.createConnection(); 82 83 MessageFactory factory = MessageFactory.newInstance(); 84 SOAPMessage message = 85 factory.createMessage(new MimeHeaders (), 86 new ByteArrayInputStream (soapMessageWithLeadingComment.getBytes())); 87 SOAPPart part = message.getSOAPPart(); 88 SOAPEnvelope envelope = (SOAPEnvelope ) part.getEnvelope(); 89 assertTrue(envelope != null); 91 } 92 93 private SOAPEnvelope getSOAPEnvelope() throws Exception { 94 SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance(); 95 SOAPConnection con = scFactory.createConnection(); 96 97 MessageFactory factory = MessageFactory.newInstance(); 98 SOAPMessage message = factory.createMessage(); 99 SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); 100 return envelope; 101 } 102 103 public void testAttributes() throws Exception { 104 SOAPEnvelope envelope = getSOAPEnvelope(); 105 SOAPBody body = envelope.getBody(); 106 107 Name name1 = envelope.createName("MyAttr1"); 108 String value1 = "MyValue1"; 109 Name name2 = envelope.createName("MyAttr2"); 110 String value2 = "MyValue2"; 111 Name name3 = envelope.createName("MyAttr3"); 112 String value3 = "MyValue3"; 113 body.addAttribute(name1, value1); 114 body.addAttribute(name2, value2); 115 body.addAttribute(name3, value3); 116 java.util.Iterator iterator = body.getAllAttributes(); 117 assertTrue(getIteratorCount(iterator) == 3); 118 iterator = body.getAllAttributes(); 119 boolean foundName1 = false; 120 boolean foundName2 = false; 121 boolean foundName3 = false; 122 while (iterator.hasNext()) { 123 Name name = (Name ) iterator.next(); 124 if (name.equals(name1)) 125 foundName1 = true; 126 else if (name.equals(name2)) 127 foundName2 = true; 128 else if (name.equals(name3)) 129 foundName3 = true; 130 } 131 assertTrue(foundName1 && foundName2 && foundName3); 132 } 133 134 public void testFaults() throws Exception { 135 SOAPEnvelope envelope = getSOAPEnvelope(); 136 SOAPBody body = envelope.getBody(); 137 SOAPFault sf = body.addFault(); 138 sf.setFaultCode("myFault"); 139 String fc = sf.getFaultCode(); 140 assertTrue(fc.equals("myFault")); 141 } 142 143 public void testFaults2() throws Exception { 144 SOAPEnvelope envelope = getSOAPEnvelope(); 145 SOAPBody body = envelope.getBody(); 146 SOAPFault sf = body.addFault(); 147 148 assertTrue(body.getFault() != null); 149 150 Detail d1 = sf.addDetail(); 151 Name name = envelope.createName("GetLastTradePrice", "WOMBAT", 152 "http://www.wombat.org/trader"); 153 d1.addDetailEntry(name); 154 155 Detail d2 = sf.getDetail(); 156 assertTrue(d2 != null); 157 Iterator i = d2.getDetailEntries(); 158 assertTrue(getIteratorCount(i) == 1); 159 i = d2.getDetailEntries(); 160 while(i.hasNext()) { 161 DetailEntry de = (DetailEntry )i.next(); 162 assertEquals(de.getElementName(),name); 163 } 164 } 165 166 public void testHeaderElements() throws Exception { 167 SOAPEnvelope envelope = getSOAPEnvelope(); 168 SOAPBody body = envelope.getBody(); 169 SOAPHeader hdr = envelope.getHeader(); 170 171 SOAPHeaderElement she1 = hdr.addHeaderElement(envelope.createName("foo1", "f1", "foo1-URI")); 172 she1.setActor("actor-URI"); 173 java.util.Iterator iterator = hdr.extractHeaderElements("actor-URI"); 174 int cnt = 0; 175 while (iterator.hasNext()) { 176 cnt++; 177 SOAPHeaderElement she = (SOAPHeaderElement ) iterator.next(); 178 assertTrue(she.equals(she1)); 179 } 180 assertTrue(cnt == 1); 181 iterator = hdr.extractHeaderElements("actor-URI"); 182 assertTrue(!iterator.hasNext()); 183 } 184 185 public void testText1() throws Exception { 186 SOAPEnvelope envelope = getSOAPEnvelope(); 187 Iterator iStart = envelope.getChildElements(); 188 int countStart = getIteratorCount(iStart); 189 SOAPElement se = envelope.addTextNode("<txt>This is text</txt>"); 190 assertTrue(se != null); 191 assertTrue(envelope.getValue().equals("<txt>This is text</txt>")); 192 Iterator i = envelope.getChildElements(); 193 int count = getIteratorCount(i); 194 assertTrue(count == countStart + 1); 195 } 196 197 public void testText2() throws Exception { 198 SOAPEnvelope envelope = getSOAPEnvelope(); 199 SOAPElement se = envelope.addTextNode("This is text"); 200 Iterator iterator = se.getChildElements(); 201 Node n = null; 202 while (iterator.hasNext()) { 203 n = (Node )iterator.next(); 204 if (n instanceof Text ) 205 break; 206 } 207 assertTrue(n instanceof Text ); 208 Text t = (Text )n; 209 assertTrue(!t.isComment()); 210 } 211 212 public void testText3() throws Exception { 213 SOAPEnvelope envelope = getSOAPEnvelope(); 214 SOAPElement se = envelope.addTextNode("<!-- This is a comment -->"); 215 Iterator iterator = se.getChildElements(); 216 Node n = null; 217 while (iterator.hasNext()) { 218 n = (Node )iterator.next(); 219 if (n instanceof Text ) 220 break; 221 } 222 assertTrue(n instanceof Text ); 223 Text t = (Text )n; 224 assertTrue(t.isComment()); 225 } 226 227 private int getIteratorCount(java.util.Iterator i) { 228 int count = 0; 229 while (i.hasNext()) { 230 count++; 231 i.next(); 232 } 233 return count; 234 } 235 236 public static void main(String [] args) throws Exception { 237 test.saaj.TestEnvelope tester = new test.saaj.TestEnvelope("TestEnvelope"); 238 tester.testFaults2(); 239 tester.testEnvelope(); 240 tester.testText3(); 241 tester.testText2(); 242 tester.testText1(); 243 tester.testHeaderElements(); 244 tester.testFaults(); 245 tester.testAttributes(); 246 } 247 } 248 | Popular Tags |