KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestMessageProperty


1 package test.saaj;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.InputStream JavaDoc;
6
7 import javax.xml.soap.MessageFactory JavaDoc;
8 import javax.xml.soap.MimeHeaders JavaDoc;
9 import javax.xml.soap.SOAPBody JavaDoc;
10 import javax.xml.soap.SOAPElement JavaDoc;
11 import javax.xml.soap.SOAPMessage JavaDoc;
12 import javax.xml.soap.Text JavaDoc;
13
14 public class TestMessageProperty extends junit.framework.TestCase {
15     public TestMessageProperty(String JavaDoc name) {
16         super(name);
17     }
18
19     private static final String JavaDoc textValue = "\uc548\ub155\ud558\uc138\uc694";
20     
21     private SOAPMessage JavaDoc createTestMessage() throws Exception JavaDoc {
22         MessageFactory JavaDoc mf = MessageFactory.newInstance();
23         SOAPMessage JavaDoc msg = mf.createMessage();
24         SOAPBody JavaDoc sb = msg.getSOAPBody();
25         SOAPElement JavaDoc se1 = sb.addChildElement("echoString", "ns1", "http://tempuri.org");
26         SOAPElement JavaDoc se2 = se1.addChildElement("string");
27         se2.addTextNode(textValue);
28         
29         return msg;
30     }
31     
32     
33     public void testWriteXmlDeclPropertyTrue() throws Exception JavaDoc {
34         testXmlDecl("true", "<?xml");
35     }
36     
37     public void testWriteXmlDeclPropertyFalse() throws Exception JavaDoc {
38         testXmlDecl("false", "<soapenv:Envelope");
39     }
40     
41     public void testEncodingPropertyUTF16() throws Exception JavaDoc {
42         testEncoding("UTF-16");
43     }
44         
45     public void testEncodingPropertyUTF8() throws Exception JavaDoc {
46         testEncoding("UTF-8");
47     }
48     
49     private void testXmlDecl(String JavaDoc xmlDecl, String JavaDoc expected) throws Exception JavaDoc {
50         SOAPMessage JavaDoc msg = createTestMessage();
51         
52         msg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, xmlDecl);
53         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
54         msg.writeTo(baos);
55         String JavaDoc msgString = new String JavaDoc(baos.toByteArray(), "UTF-8");
56         System.out.println("msgString =" + msgString);
57         assertTrue(msgString.startsWith(expected));
58     }
59     
60     private void testEncoding(String JavaDoc encoding) throws Exception JavaDoc {
61         SOAPMessage JavaDoc msg = createTestMessage();
62         
63         msg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
64         msg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, encoding);
65         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
66         msg.writeTo(baos);
67         
68         String JavaDoc msgString = new String JavaDoc(baos.toByteArray(), encoding);
69         System.out.println("msgString (" + encoding + ")=" + msgString);
70         assertTrue(msgString.startsWith("<?xml version=\"1.0\" encoding=\"" + encoding + "\""));
71
72         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
73         SOAPMessage JavaDoc msg1 = createMessageFromInputStream(bais);
74         SOAPElement JavaDoc se1 = (SOAPElement JavaDoc) msg1.getSOAPBody().getChildElements().next();
75         SOAPElement JavaDoc se2 = (SOAPElement JavaDoc) se1.getChildElements().next();
76         Text JavaDoc text = (Text JavaDoc)se2.getChildElements().next();
77         
78     assertEquals(textValue, text.getValue());
79     }
80
81     private SOAPMessage JavaDoc createMessageFromInputStream(InputStream JavaDoc is) throws Exception JavaDoc {
82         MessageFactory JavaDoc mf = MessageFactory.newInstance();
83         return mf.createMessage(new MimeHeaders JavaDoc(), is);
84     }
85 }
86
Popular Tags