File: TestSOAPElement.java

package info (click to toggle)
axis 1.4-29
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 52,100 kB
  • sloc: java: 129,124; xml: 10,602; jsp: 983; sh: 84; cs: 36; makefile: 18
file content (113 lines) | stat: -rw-r--r-- 3,699 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package test.saaj;

import junit.framework.TestCase;

import javax.xml.soap.Node;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.Text;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPPart;
import javax.xml.soap.SOAPEnvelope;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.io.ByteArrayInputStream;

import org.w3c.dom.NodeList;

/**
 * Test case for Axis impl of SAAJ {@link SOAPElement} interface ({@link org.apache.axis.message.MessageElement}).
 *
 * @author Ian P. Springer
 */
public class TestSOAPElement extends TestCase
{
    private SOAPElement soapElem;

    protected void setUp() throws Exception
    {
        soapElem = SOAPFactory.newInstance().createElement( "Test", "test", "http://test.apache.org/" );
    }

    public void testGetElementsByTagName() throws Exception {
    	String soapMessageWithLeadingComment =
    		"<?xml version='1.0' encoding='UTF-8'?>" + 
			"<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
			"<env:Body>" +
                "<echo>" +
                " <data>\n" +
                "   <tag name=\"First\" >\n" +
                "      <Line> One </Line>\n" +
                "      <Line> Two </Line>\n" +
                "   </tag>\n" +
                "   <tag name =\"Second\" >\n" +
                "      <Line> Three </Line>\n" +
                "      <Line> Four </Line>\n" +
                "   </tag>\n" +
                "   <tag name =\"Third\" >\n" +
                "      <Line> Five </Line>\n" +
                "      <Line> Six </Line>\n" +
                "   </tag>\n" +
                "</data>" +
                "</echo>" +
            "</env:Body>" +
			"</env:Envelope>";
    	
    	MessageFactory factory = MessageFactory.newInstance();
    	SOAPMessage message =
    		factory.createMessage(new MimeHeaders(), 
    				new ByteArrayInputStream(soapMessageWithLeadingComment.getBytes()));
        SOAPPart part = message.getSOAPPart();
        SOAPEnvelope envelope = (SOAPEnvelope) part.getEnvelope();
        NodeList nodes = envelope.getElementsByTagName("tag");
        assertEquals(nodes.getLength(), 3);
        NodeList nodes2 = envelope.getElementsByTagName("Line");
        assertEquals(nodes2.getLength(), 6);

        NodeList nodes3 = part.getElementsByTagName("tag");
        assertEquals(nodes3.getLength(), 3);
        NodeList nodes4 = part.getElementsByTagName("Line");
        assertEquals(nodes4.getLength(), 6);
    }

    /**
     * Test for Axis impl of {@link SOAPElement#addTextNode(String)}.
     *
     * @throws Exception on error
     */
    public void testAddTextNode() throws Exception
    {
        assertNotNull( soapElem );
        final String value = "foo";
        soapElem.addTextNode( value );
        assertEquals( value, soapElem.getValue() );
        Text text = assertContainsText( soapElem );
        assertEquals( value, text.getValue() );
    }

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

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

}