soapelementtest.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 770 行 · 第 1/3 页
JAVA
770 行
"ch",
"http://test.apache.org/");
childEle1.addChildElement(childEle2);
soapEle.addChildElement(childEle1);
QName qname = new QName("http://test.apache.org/", "Child1", "ch");
Iterator childElements = soapEle.getChildElements(qname);
int childCount = 0;
while (childElements.hasNext()) {
Node node = (Node)childElements.next();
childCount++;
}
assertEquals(childCount, 2);
} catch (SOAPException e) {
fail("Unexpected Exception " + e);
}
}
//TODO : check why this is failing
public void _testGetChildElements2() {
try {
MessageFactory fact = MessageFactory.newInstance();
SOAPMessage message = fact.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody soapBody = soapEnvelope.getBody();
Name name = soapEnvelope.createName("MyChild1");
SOAPElement se = soapBody.addChildElement(name);
Iterator childElementsCount = soapBody.getChildElements();
Iterator childElements = soapBody.getChildElements();
int childCount = 0;
while (childElementsCount.hasNext()) {
Node node = (Node)childElementsCount.next();
childCount++;
}
assertEquals(childCount, 1);
SOAPElement se2 = (SOAPElement)childElements.next();
if (!se.equals(se2)) {
fail();
} else {
System.out.println("SOAPElement se = se2 (expected)");
}
Name n = se.getElementName();
assertEquals(n, name);
} catch (SOAPException e) {
fail("Unexpected Exception " + e);
}
}
public void testGetChildElements3() {
try {
MessageFactory fact = MessageFactory.newInstance();
SOAPMessage message = fact.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody soapBody = soapEnvelope.getBody();
//Name name = soapEnvelope.createName("MyChild1");
QName name = new QName("MyChild1");
SOAPElement se = soapBody.addChildElement(name);
Iterator childElementsCount = soapBody.getChildElements();
Iterator childElements = soapBody.getChildElements();
int childCount = 0;
while (childElementsCount.hasNext()) {
Node node = (Node)childElementsCount.next();
childCount++;
}
assertEquals(childCount, 1);
SOAPElement se2 = (SOAPElement)childElements.next();
assertEquals(se, se2);
QName n = se.getElementQName();
assertEquals(n, name);
} catch (SOAPException e) {
fail("Unexpected Exception " + e);
}
}
public void _testRemoveAttribute2() {
try {
QName qname = new QName("http://child1.apache.org/", "Child1", "ch");
String value = "MyValue1";
soapEle.addAttribute(qname, value);
boolean b = soapEle.removeAttribute(qname);
assertTrue("removeAttribute() did not return true", b);
b = soapEle.removeAttribute(qname);
assertFalse("removeAttribute() did not return false", b);
assertNull(soapEle.getAttributeValue(qname));
} catch (Exception e) {
e.printStackTrace();
fail("Exception: " + e);
}
}
public void _testSetElementQName() {
try {
QName qname = new QName("http://child1.apache.org/", "newName", "ch");
soapEle.setElementQName(qname);
assertNull(soapEle.getElementName().getLocalName(), "newName");
} catch (Exception e) {
e.printStackTrace();
fail("Exception: " + e);
}
}
public void _testCreateQName() {
String prefix = "";
try {
//SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPMessage message =
MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
QName qname = envelope.createQName("qname", prefix);
String tprefix = qname.getPrefix();
String turi = qname.getNamespaceURI();
String tname = qname.getLocalPart();
if (!tprefix.equals(prefix) || !turi.equals(envelope.getElementName().getURI())) {
fail("createQName() did not create correct qname\n" +
"expected: <uri=" + envelope.getElementName().getURI() +
", prefix=" + prefix + ", localpart=qname>\n" +
"got: <uri=" + turi +
", prefix=" + tprefix + ", localpart=" + tname + ">");
}
qname = body.createQName("qname", body.getElementName().getPrefix());
tprefix = qname.getPrefix();
turi = qname.getNamespaceURI();
tname = qname.getLocalPart();
if (!tprefix.equals(body.getElementName().getPrefix()) ||
!turi.equals(body.getElementName().getURI())) {
fail("createQName() did not create correct qname\n" +
"expected: <uri=" + body.getElementName().getURI() +
", prefix=" + body.getElementName().getPrefix() + ", localpart=qname>\n" +
"got: <uri=" + turi +
", prefix=" + tprefix + ", localpart=" + tname + ">");
}
} catch (Exception e) {
fail("Failed " + e);
}
}
public void testRemoveContent() {
boolean pass = true;
try {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
Name name = envelope.createName("MyChild");
SOAPElement se = body.addChildElement(name);
assertNotNull(se);
Iterator childs = body.getChildElements(name);
int childElementCount = 0;
for (int a = 0; childs.hasNext(); a++) {
childs.next();
childElementCount++;
}
childs = body.getChildElements(name);
assertEquals(childElementCount, 1);
Name n = se.getElementName();
assertEquals(n, name);
//Child addition verified, now call removeContents to delete it
se.removeContents();
childs = se.getChildElements();
childElementCount = 0;
for (int a = 0; childs.hasNext(); a++) {
childs.next();
childElementCount++;
}
assertEquals(childElementCount, 0);
} catch (Exception e) {
fail();
}
}
public void testSetElementQName() {
try {
MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage message = factory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
QName qname1 = new QName("http://fooURI.com", "fooElement", "foo");
QName qname2 = new QName("http://foo2URI.com", "fooElement2", "foo2");
SOAPElement se = body.addChildElement(qname1);
QName qname = se.getElementQName();
se = se.setElementQName(qname2);
qname = se.getElementQName();
if (!qname.getNamespaceURI().equals(qname2.getNamespaceURI()) ||
!qname.getLocalPart().equals(qname2.getLocalPart()) ||
!qname.getPrefix().equals(qname2.getPrefix())) {
System.out.println("setElementQName() did not reset " +
"element qname\nexpected: <URI=" + qname2.getNamespaceURI() +
", prefix=" + qname2.getPrefix() + ", localpart=" + qname2.getLocalPart() +
">\ngot: <URI=" + qname.getNamespaceURI() + ", prefix=" +
qname.getPrefix() +
", localpart=" + qname.getLocalPart() + ">");
}
} catch (Exception e) {
fail("Error :" + e);
}
}
public void testSetElementQName2() {
try {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
SOAPHeader header = envelope.getHeader();
QName qname = new QName("qname");
//Try and change element name of SOAPEnvelope (expect SOAPException)
try {
envelope.setElementQName(qname);
fail("Did not throw expected SOAPException");
} catch (SOAPException e) {
//Caught expected SOAPException
}
//Try and change element name of SOAPHeader (expect SOAPException)
try {
header.setElementQName(qname);
fail("Did not throw expected SOAPException");
} catch (SOAPException e) {
//Caught expected SOAPException
}
//Try and change element name of SOAPBody (expect SOAPException)
try {
body.setElementQName(qname);
fail("Did not throw expected SOAPException");
} catch (SOAPException e) {
//Caught expected SOAPException
}
} catch (Exception e) {
fail("Error : " + e);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?