soapenvelopetest.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 568 行 · 第 1/2 页
JAVA
568 行
SOAPHeader header = envelope.getHeader();
SOAPHeaderElement headerEle = header.addHeaderElement(envelope.createName("foo1",
"f1",
"foo1-URI"));
headerEle.setActor("actor-URI");
headerEle.setMustUnderstand(true);
Iterator iterator = header.extractHeaderElements("actor-URI");
int cnt = 0;
while (iterator.hasNext()) {
cnt++;
SOAPHeaderElement resultHeaderEle = (SOAPHeaderElement)iterator.next();
assertEquals(headerEle.getActor(), resultHeaderEle.getActor());
assertEquals(resultHeaderEle.getMustUnderstand(), headerEle.getMustUnderstand());
}
assertTrue(cnt == 1);
iterator = header.extractHeaderElements("actor-URI");
assertTrue(!iterator.hasNext());
}
public void testText() throws Exception {
SOAPEnvelope envelope = getSOAPEnvelope();
SOAPBody body = envelope.getBody();
Iterator iStart = body.getChildElements();
int countStart = getIteratorCount(iStart);
final String bodyText = "This is the body text";
SOAPElement se = body.addChildElement("Child");
assertTrue(se != null);
SOAPElement soapElement = se.addTextNode(bodyText);
assertEquals(bodyText, soapElement.getValue());
Iterator i = body.getChildElements();
int count = getIteratorCount(i);
assertTrue(count == countStart + 1);
}
public void testNonCommentText() throws Exception {
SOAPEnvelope envelope = getSOAPEnvelope();
SOAPBody body = envelope.getBody();
SOAPElement se = body.addChildElement("Child");
se.addTextNode("This is text");
Iterator iterator = se.getChildElements();
Object o = null;
while (iterator.hasNext()) {
o = iterator.next();
if (o instanceof Text) {
break;
}
}
assertTrue(o instanceof Text);
Text t = (Text)o;
assertTrue(!t.isComment());
}
public void testCommentText() throws Exception {
SOAPEnvelope envelope = getSOAPEnvelope();
SOAPBody body = envelope.getBody();
SOAPElement se = body.addChildElement("Child");
se.addTextNode("<!-- This is a comment -->");
Iterator iterator = se.getChildElements();
Node n = null;
while (iterator.hasNext()) {
n = (Node)iterator.next();
if (n instanceof Text) {
break;
}
}
assertTrue(n instanceof Text);
Text t = (Text)n;
assertTrue(t.isComment());
}
public void testAttributes() throws Exception {
SOAPEnvelope envelope = getSOAPEnvelope();
SOAPBody body = envelope.getBody();
Name name1 = envelope.createName("MyAttr1");
String value1 = "MyValue1";
Name name2 = envelope.createName("MyAttr2");
String value2 = "MyValue2";
Name name3 = envelope.createName("MyAttr3");
String value3 = "MyValue3";
body.addAttribute(name1, value1);
body.addAttribute(name2, value2);
body.addAttribute(name3, value3);
Iterator iterator = body.getAllAttributes();
assertTrue(getIteratorCount(iterator) == 3);
iterator = body.getAllAttributes();
boolean foundName1 = false;
boolean foundName2 = false;
boolean foundName3 = false;
while (iterator.hasNext()) {
Name name = (Name)iterator.next();
if (name.equals(name1)) {
foundName1 = true;
assertEquals(value1, body.getAttributeValue(name));
} else if (name.equals(name2)) {
foundName2 = true;
assertEquals(value2, body.getAttributeValue(name));
} else if (name.equals(name3)) {
foundName3 = true;
assertEquals(value3, body.getAttributeValue(name));
}
}
assertTrue(foundName1 && foundName2 && foundName3);
}
public void testAttributes2() throws Exception {
SOAPEnvelope envelope = getSOAPEnvelope();
SOAPBody body = envelope.getBody();
Name name1 = envelope.createName("MyAttr1", "att", "http://test.com/Attr");
String value1 = "MyValue1";
Name name2 = envelope.createName("MyAttr2");
String value2 = "MyValue2";
Name name3 = envelope.createName("MyAttr3");
String value3 = "MyValue3";
body.addAttribute(name1, value1);
body.addAttribute(name2, value2);
body.addAttribute(name3, value3);
Iterator iterator = body.getAllAttributes();
assertTrue(getIteratorCount(iterator) == 3);
iterator = body.getAllAttributes();
boolean foundName1 = false;
boolean foundName2 = false;
boolean foundName3 = false;
while (iterator.hasNext()) {
Name name = (Name)iterator.next();
if (name.equals(name1)) {
foundName1 = true;
assertEquals(value1, body.getAttributeValue(name));
} else if (name.equals(name2)) {
foundName2 = true;
assertEquals(value2, body.getAttributeValue(name));
} else if (name.equals(name3)) {
foundName3 = true;
assertEquals(value3, body.getAttributeValue(name));
}
}
assertTrue(foundName1 && foundName2 && foundName3);
}
public void testAttributes3() throws Exception {
SOAPEnvelope envelope = getSOAPEnvelope();
SOAPBody body = envelope.getBody();
Name name1 = envelope.createName("MyAttr1", "att", "http://test.com/Attr");
String value1 = "MyValue1";
Name name2 = envelope.createName("MyAttr2", "att", "http://test.com/Attr");
String value2 = "MyValue2";
Name name3 = envelope.createName("MyAttr3", "att", "http://test.com/Attr");
String value3 = "MyValue3";
body.addAttribute(name1, value1);
body.addAttribute(name2, value2);
body.addAttribute(name3, value3);
Iterator iterator = body.getAllAttributes();
assertTrue(getIteratorCount(iterator) == 3);
iterator = body.getAllAttributes();
boolean foundName1 = false;
boolean foundName2 = false;
boolean foundName3 = false;
while (iterator.hasNext()) {
Name name = (Name)iterator.next();
if (name.equals(name1)) {
foundName1 = true;
assertEquals(value1, body.getAttributeValue(name));
} else if (name.equals(name2)) {
foundName2 = true;
assertEquals(value2, body.getAttributeValue(name));
} else if (name.equals(name3)) {
foundName3 = true;
assertEquals(value3, body.getAttributeValue(name));
}
}
assertTrue(foundName1 && foundName2 && foundName3);
}
public void testAddHeader() {
try {
SOAPEnvelope envelope = getSOAPEnvelope();
try {
envelope.addHeader();
fail("Did not get expected SOAPException");
} catch (SOAPException e) {
assertTrue("Got expected SOAPException", true);
}
envelope.getHeader().detachNode();
assertNull(envelope.getHeader());
SOAPHeader myhdr;
try {
myhdr = envelope.addHeader();
assertNotNull("SOAPHeader return value is null", myhdr);
} catch (SOAPException e) {
fail("Unexpected SOAPException : " + e);
}
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected Exception : " + e);
}
}
private SOAPEnvelope getSOAPEnvelope() throws Exception {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
return message.getSOAPPart().getEnvelope();
}
private int getIteratorCount(java.util.Iterator i) {
int count = 0;
while (i.hasNext()) {
count++;
i.next();
}
return count;
}
private void validateBody(Iterator iter) {
while (iter.hasNext()) {
final Object obj = iter.next();
if (obj instanceof Text) {
final String data = ((Text)obj).getData();
assertTrue("\n".equals(data) || "GENT".equals(data));
} else {
final SOAPElement soapElement = (SOAPElement)obj;
final Iterator attIter = soapElement.getAllAttributes();
while (attIter.hasNext()) {
final Object o = attIter.next();
assertEquals("test", soapElement.getAttributeValue((Name)o));
}
final Iterator childElementIter = soapElement.getChildElements();
if (childElementIter == null) {
return;
}
validateBody(childElementIter);
}
}
}
public void testSetEncodingStyle() throws Exception {
SOAPEnvelope envelope = getSOAPEnvelope();
envelope.setEncodingStyle("http://example.com/MyEncodings");
assertNotNull(envelope.getEncodingStyle());
assertEquals("http://example.com/MyEncodings",envelope.getEncodingStyle());
}
public void testElementAfterBody() throws Exception {
MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage message = factory.createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
try {
//SOAP1.2 does not allow trailing blocks after the Body
//Call SOAPEnvelope.addChildElement() and (expect SOAPException)
Name elementAfterBody = envelope.createName("AfterBody", "e", "some-uri");
envelope.addChildElement(elementAfterBody);
fail("Did not throw expected SOAPException");
} catch (SOAPException e) {
//Did throw expected SOAPException"
} catch (Exception e) {
fail("Unexpected Exception: " + e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?