KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestMessageProperty2


1 package test.saaj;
2
3 import org.apache.axis.Message;
4
5 import java.io.ByteArrayInputStream JavaDoc;
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import javax.xml.soap.MessageFactory JavaDoc;
9 import javax.xml.soap.MimeHeaders JavaDoc;
10 import javax.xml.soap.SOAPBody JavaDoc;
11 import javax.xml.soap.SOAPElement JavaDoc;
12 import javax.xml.soap.SOAPMessage JavaDoc;
13 import javax.xml.soap.Text JavaDoc;
14 import javax.xml.soap.SOAPHeader JavaDoc;
15 import javax.xml.soap.SOAPEnvelope JavaDoc;
16 import javax.xml.soap.SOAPPart JavaDoc;
17 import javax.xml.transform.stream.StreamSource JavaDoc;
18
19 import junit.framework.Assert;
20
21 public class TestMessageProperty2 extends junit.framework.TestCase {
22     public TestMessageProperty2(String JavaDoc name) {
23         super(name);
24     }
25
26     private static String JavaDoc GoodSoapMessage = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:tns=\"http://helloservice.org/wsdl\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><tns:hello><String_1 xsi:type=\"xsd:string\">&lt;Bozo&gt;</String_1></tns:hello></soap:Body></soap:Envelope>";
27
28     private SOAPMessage JavaDoc createTestMessage(String JavaDoc encoding, boolean xmlDecl) throws Exception JavaDoc {
29         MessageFactory JavaDoc factory = MessageFactory.newInstance();
30         SOAPMessage JavaDoc message = factory.createMessage();
31         SOAPPart JavaDoc sp = message.getSOAPPart();
32         SOAPEnvelope JavaDoc envelope = message.getSOAPPart().getEnvelope();
33         SOAPHeader JavaDoc header = envelope.getHeader();
34
35         ByteArrayInputStream JavaDoc bais =
36                 new ByteArrayInputStream JavaDoc(GoodSoapMessage.getBytes(encoding));
37         StreamSource JavaDoc ssrc = new StreamSource JavaDoc(bais);
38         sp.setContent(ssrc);
39
40         message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, encoding);
41         message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, xmlDecl ? "true" : "false");
42
43         return message;
44     }
45
46
47     private SOAPMessage JavaDoc createMessageFromInputStream(InputStream JavaDoc is) throws Exception JavaDoc {
48         MessageFactory JavaDoc mf = MessageFactory.newInstance();
49         return mf.createMessage(new MimeHeaders JavaDoc(), is);
50     }
51
52     public void testUTF8withXMLDecl() throws Exception JavaDoc {
53         SOAPMessage JavaDoc msg = createTestMessage("UTF-8", true);
54         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
55         msg.writeTo(baos);
56         String JavaDoc xml = new String JavaDoc(baos.toByteArray(),"UTF-8");
57         assertTrue(xml.indexOf("UTF-8") != -1);
58         assertTrue(xml.indexOf("&lt;Bozo&gt;") != -1);
59     }
60
61     public void testUTF16withXMLDecl() throws Exception JavaDoc {
62         SOAPMessage JavaDoc msg = createTestMessage("UTF-16", true);
63         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
64         msg.writeTo(baos);
65         String JavaDoc xml = new String JavaDoc(baos.toByteArray(),"UTF-16");
66         assertTrue(xml.indexOf("UTF-16") != -1);
67         assertTrue(xml.indexOf("&lt;Bozo&gt;") != -1);
68     }
69 }
70
Popular Tags