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