soapelementtest.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 770 行 · 第 1/3 页

JAVA
770
字号
                    count++;
                }

                childs = body.getChildElements(name);
                assertTrue(count == 1);

                SOAPElement se2 = (SOAPElement)childs.next();
                assertEquals(se, se2);
                //se = se2 (expected)

                //Retrieve the SOAPElement Name
                Name n = se.getElementName();
                //System.out.println("localName="+n.getLocalName()+" prefix="
                //			+n.getPrefix()+" URI="+n.getURI()+" qualifiedName="
                //			+n.getQualifiedName());
                assertEquals(n, name);
                //if (!n.equals(name)) {
                //System.out.println("Name objects are not equal (unexpected)");
                //System.out.println("addChildElement() did not return " +
                //"correct Name object expected localName=" +
                //name.getLocalName() + ", got localName="
                //+ n.getLocalName());
                //}

                //Name objects are equal (expected)
            }

        } catch (Exception e) {
            fail("Exception: " + e);
        }
    }

    public void testAddTextNode2() {
        try {
            SOAPMessage msg = MessageFactory.newInstance().createMessage();
            SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
            SOAPBody body = envelope.getBody();
            Iterator iStart = envelope.getChildElements();
            int countStart = getIteratorCount(iStart);
            SOAPElement se = envelope.addTextNode("<txt>This is text</txt>");
            if (se == null) {
                fail("addTextNode() did not return SOAPElement");
            } else if (!envelope.getValue().equals("<txt>This is text</txt>")) {
                String s = body.getValue();
                fail("addTextNode() did not return expected text, Returned " + s +
                        ", Expected <txt>This is text</txt>");
            }
            Iterator i = envelope.getChildElements();
            int count = getIteratorCount(i);
            i = envelope.getChildElements();
            if (count != ++countStart) {
                fail("Wrong iterator count returned of " +
                        count + ", expected " + countStart);
            } else {
                Object obj = null;
                while (i.hasNext()) {
                    obj = i.next();
                    if (obj instanceof Text) {
                        break;
                    }
                }
                if (!(obj instanceof Text)) {
                    fail("obj is not instanceof Text");
                }
            }
        } catch (Exception e) {
            fail("Exception: " + e);
        }
    }

    public void testRemoveAttribute() {
        try {
            SOAPMessage msg = MessageFactory.newInstance().createMessage();
            SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
            SOAPBody body = envelope.getBody();
            Name name = envelope.createName("MyAttr1");
            String value = "MyValue1";
            body.addAttribute(name, value);
            boolean b = body.removeAttribute(name);
            assertTrue("removeAttribute() did not return true", b);
            b = body.removeAttribute(name);
            assertFalse("removeAttribute() did not return false", b);
            assertNull(body.getAttributeValue(name));
        } catch (Exception e) {
            fail("Exception: " + e);
        }
    }

    public void testRemoveAttribute2() {
        try {
            SOAPMessage msg = MessageFactory.newInstance().createMessage();
            SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
            SOAPBody body = envelope.getBody();

            QName name = new QName("MyAttr1");
            String value = "MyValue1";
            body.addAttribute(name, value);
            boolean b = body.removeAttribute(name);
            assertTrue(b);

            b = body.removeAttribute(name);
            if (b) {
                //removeAttribute() did not return false
                fail();
            }
            //getAttributeValue should return null
            assertNull(body.getAttributeValue(name));
        } catch (Exception e) {
            fail("Error : " + e);
        }
    }

    public void testRemoveAttributeName() {
        try {
            SOAPMessage msg = MessageFactory.newInstance().createMessage();
            SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
            SOAPBody body = envelope.getBody();

            Name name = envelope.createName("MyAttr1");
            String value = "MyValue1";
            body.addAttribute(name, value);
            boolean b = body.removeAttribute(name);
            assertTrue(b);

            b = body.removeAttribute(name);
            assertTrue(!b);

            String s = body.getAttributeValue(name);
            assertNull(s);
        } catch (Exception e) {
            fail("Failed : " + e);
        }
    }


    public void _testRemoveAttributeQName() {
        try {
            SOAPMessage msg =
                    MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
            SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
            SOAPBody body = envelope.getBody();

            QName name = new QName("MyAttr1");
            String value = "MyValue1";
            body.addAttribute(name, value);
            boolean b = body.removeAttribute(name);
            assertTrue(b);
            b = body.removeAttribute(name);
            assertTrue(!b);

            assertNull(body.getAttributeValue(name));
        } catch (Exception e) {
            fail();
        }

    }

    public void testRemoveNamespaceDeclaration() {
        try {
            String prefix = "myPrefix";
            String uri = "myURI";
            SOAPMessage msg = MessageFactory.newInstance().createMessage();
            SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
            SOAPBody body = envelope.getBody();
            body.addNamespaceDeclaration(prefix, uri);
            boolean b = body.removeNamespaceDeclaration(prefix);
            assertTrue("removeNamespaceDeclaration() did not return true", b);
            b = body.removeNamespaceDeclaration(prefix);
            assertFalse("removeNamespaceDeclaration() did not return false", b);
            assertNull(body.getNamespaceURI(prefix));
        } catch (Exception e) {
            fail("Exception: " + e);
        }
    }

    public void _testSetEncodingStyle() {
        try {
            SOAPMessage msg = MessageFactory.newInstance().createMessage();
            SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
            SOAPBody body = envelope.getBody();
            body.setEncodingStyle(SOAPConstants.URI_NS_SOAP_ENCODING);
            try {
                body.setEncodingStyle("BOGUS");
                fail("Expected Exception did not occur");
            } catch (IllegalArgumentException e) {
                assertTrue("Expected Exception occurred", true);
            }
        } catch (Exception e) {
            fail("Exception: " + e);
        }
    }

    private int getIteratorCount(Iterator iter) {
        int count = 0;
        while (iter.hasNext()) {
            iter.next();
            count ++;
        }
        return count;
    }

    private TextImplEx assertContainsText(SOAPElement soapElem) {
        assertTrue(soapElem.hasChildNodes());
        List childElems = toList(soapElem.getChildElements());
        assertTrue(childElems.size() == 1);
        NodeImpl node = (NodeImpl)childElems.get(0);
        assertTrue(node instanceof TextImplEx);
        return (TextImplEx)node;
    }

    private List toList(java.util.Iterator iter) {
        List list = new java.util.ArrayList();
        while (iter.hasNext()) {
            list.add(iter.next());
        }
        return list;
    }


    /*
    * test for addChildElement(QName qname)
    */
    public void testAddChildElement3() {
        try {
            QName qname = new QName("http://sample.apache.org/trader", "GetStockQuote", "w");
            soapEle.addChildElement(qname);
            assertNotNull(soapEle);

        } catch (Exception e) {
            fail("Exception: " + e);
        }
    }


    public void testGetAttributeValue() {
        assertNotNull(soapEle);
        String value = "234.50";
        try {
            QName qname = new QName("http://sample.apache.org/trader", "GetStockQuote", "w");
            soapEle.addAttribute(qname, value);
            String valueReturned = soapEle.getAttributeValue(qname);
            assertEquals(value, valueReturned);

        } catch (SOAPException e) {
            fail("Unexpected Exception " + e);
        }
    }


    public void _testGetChildElements() {
        try {
            SOAPElement childEle1 =
                    SOAPFactoryImpl.newInstance().createElement("Child1",
                                                                "ch",
                                                                "http://test.apache.org/");
            SOAPElement childEle2 =
                    SOAPFactoryImpl.newInstance().createElement("Child2",

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?