| 1 |
package test.saaj; |
| 2 |
|
| 3 |
import junit.framework.TestCase; |
| 4 |
|
| 5 |
import javax.xml.soap.Node; |
| 6 |
import javax.xml.soap.SOAPElement; |
| 7 |
import javax.xml.soap.SOAPFactory; |
| 8 |
import javax.xml.soap.Text; |
| 9 |
import javax.xml.soap.SOAPConnectionFactory; |
| 10 |
import javax.xml.soap.SOAPConnection; |
| 11 |
import javax.xml.soap.MessageFactory; |
| 12 |
import javax.xml.soap.SOAPMessage; |
| 13 |
import javax.xml.soap.MimeHeaders; |
| 14 |
import javax.xml.soap.SOAPPart; |
| 15 |
import javax.xml.soap.SOAPEnvelope; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.Iterator; |
| 18 |
import java.util.List; |
| 19 |
import java.io.ByteArrayInputStream; |
| 20 |
|
| 21 |
import org.w3c.dom.NodeList; |
| 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 soapElem; |
| 31 |
|
| 32 |
protected void setUp() throws Exception |
| 33 |
{ |
| 34 |
soapElem = SOAPFactory.newInstance().createElement( "Test", "test", "http://test.apache.org/" ); |
| 35 |
} |
| 36 |
|
| 37 |
public void testGetElementsByTagName() throws Exception { |
| 38 |
String 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 factory = MessageFactory.newInstance(); |
| 62 |
SOAPMessage message = |
| 63 |
factory.createMessage(new MimeHeaders(), |
| 64 |
new ByteArrayInputStream(soapMessageWithLeadingComment.getBytes())); |
| 65 |
SOAPPart part = message.getSOAPPart(); |
| 66 |
SOAPEnvelope envelope = (SOAPEnvelope) part.getEnvelope(); |
| 67 |
NodeList nodes = envelope.getElementsByTagName("tag"); |
| 68 |
assertEquals(nodes.getLength(), 3); |
| 69 |
NodeList nodes2 = envelope.getElementsByTagName("Line"); |
| 70 |
assertEquals(nodes2.getLength(), 6); |
| 71 |
|
| 72 |
NodeList nodes3 = part.getElementsByTagName("tag"); |
| 73 |
assertEquals(nodes3.getLength(), 3); |
| 74 |
NodeList 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 |
| 84 |
{ |
| 85 |
assertNotNull( soapElem ); |
| 86 |
final String value = "foo"; |
| 87 |
soapElem.addTextNode( value ); |
| 88 |
assertEquals( value, soapElem.getValue() ); |
| 89 |
Text text = assertContainsText( soapElem ); |
| 90 |
assertEquals( value, text.getValue() ); |
| 91 |
} |
| 92 |
|
| 93 |
private Text assertContainsText( SOAPElement soapElem ) |
| 94 |
{ |
| 95 |
assertTrue( soapElem.hasChildNodes() ); |
| 96 |
List childElems = toList( soapElem.getChildElements() ); |
| 97 |
assertTrue( childElems.size() == 1 ); |
| 98 |
Node node = (Node) childElems.get( 0 ); |
| 99 |
assertTrue( node instanceof Text ); |
| 100 |
return (Text) node; |
| 101 |
} |
| 102 |
|
| 103 |
private List toList( Iterator iter ) |
| 104 |
{ |
| 105 |
List list = new ArrayList(); |
| 106 |
while ( iter.hasNext() ) |
| 107 |
{ |
| 108 |
list.add( iter.next() ); |
| 109 |
} |
| 110 |
return list; |
| 111 |
} |
| 112 |
|
| 113 |
} |