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