KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestSOAPElement


1 package test.saaj;
2
3 import junit.framework.TestCase;
4
5 import javax.xml.soap.Node JavaDoc;
6 import javax.xml.soap.SOAPElement JavaDoc;
7 import javax.xml.soap.SOAPFactory JavaDoc;
8 import javax.xml.soap.Text JavaDoc;
9 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
10 import javax.xml.soap.SOAPConnection JavaDoc;
11 import javax.xml.soap.MessageFactory JavaDoc;
12 import javax.xml.soap.SOAPMessage JavaDoc;
13 import javax.xml.soap.MimeHeaders JavaDoc;
14 import javax.xml.soap.SOAPPart JavaDoc;
15 import javax.xml.soap.SOAPEnvelope JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.io.ByteArrayInputStream JavaDoc;
20
21 import org.w3c.dom.NodeList JavaDoc;
22
23 /**
24  * Test case for Axis impl of SAAJ {@link SOAPElement} interface ({@link org.apache.axis.message.MessageElement}).
25  *
26  * @author Ian P. Springer
27  */

28 public class TestSOAPElement extends TestCase
29 {
30     private SOAPElement JavaDoc soapElem;
31
32     protected void setUp() throws Exception JavaDoc
33     {
34         soapElem = SOAPFactory.newInstance().createElement( "Test", "test", "http://test.apache.org/" );
35     }
36
37     public void testGetElementsByTagName() throws Exception JavaDoc {
38         String JavaDoc soapMessageWithLeadingComment =
39             "<?xml version='1.0' encoding='UTF-8'?>" +
40             "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
41             "<env:Body>" +
42                 "<echo>" +
43                 " <data>\n" +
44                 " <tag name=\"First\" >\n" +
45                 " <Line> One </Line>\n" +
46                 " <Line> Two </Line>\n" +
47                 " </tag>\n" +
48                 " <tag name =\"Second\" >\n" +
49                 " <Line> Three </Line>\n" +
50                 " <Line> Four </Line>\n" +
51                 " </tag>\n" +
52                 " <tag name =\"Third\" >\n" +
53                 " <Line> Five </Line>\n" +
54                 " <Line> Six </Line>\n" +
55                 " </tag>\n" +
56                 "</data>" +
57                 "</echo>" +
58             "</env:Body>" +
59             "</env:Envelope>";
60         
61         MessageFactory JavaDoc factory = MessageFactory.newInstance();
62         SOAPMessage JavaDoc message =
63             factory.createMessage(new MimeHeaders JavaDoc(),
64                     new ByteArrayInputStream JavaDoc(soapMessageWithLeadingComment.getBytes()));
65         SOAPPart JavaDoc part = message.getSOAPPart();
66         SOAPEnvelope JavaDoc envelope = (SOAPEnvelope JavaDoc) part.getEnvelope();
67         NodeList JavaDoc nodes = envelope.getElementsByTagName("tag");
68         assertEquals(nodes.getLength(), 3);
69         NodeList JavaDoc nodes2 = envelope.getElementsByTagName("Line");
70         assertEquals(nodes2.getLength(), 6);
71
72         NodeList JavaDoc nodes3 = part.getElementsByTagName("tag");
73         assertEquals(nodes3.getLength(), 3);
74         NodeList JavaDoc nodes4 = part.getElementsByTagName("Line");
75         assertEquals(nodes4.getLength(), 6);
76     }
77
78     /**
79      * Test for Axis impl of {@link SOAPElement#addTextNode(String)}.
80      *
81      * @throws Exception on error
82      */

83     public void testAddTextNode() throws Exception JavaDoc
84     {
85         assertNotNull( soapElem );
86         final String JavaDoc value = "foo";
87         soapElem.addTextNode( value );
88         assertEquals( value, soapElem.getValue() );
89         Text JavaDoc text = assertContainsText( soapElem );
90         assertEquals( value, text.getValue() );
91     }
92
93     private Text JavaDoc assertContainsText( SOAPElement JavaDoc soapElem )
94     {
95         assertTrue( soapElem.hasChildNodes() );
96         List JavaDoc childElems = toList( soapElem.getChildElements() );
97         assertTrue( childElems.size() == 1 );
98         Node JavaDoc node = (Node JavaDoc) childElems.get( 0 );
99         assertTrue( node instanceof Text JavaDoc );
100         return (Text JavaDoc) node;
101     }
102
103     private List JavaDoc toList( Iterator JavaDoc iter )
104     {
105         List JavaDoc list = new ArrayList JavaDoc();
106         while ( iter.hasNext() )
107         {
108             list.add( iter.next() );
109         }
110         return list;
111     }
112
113 }
114
Popular Tags