1 package test.saaj; 2 3 import org.apache.axis.Message; 4 5 import java.io.ByteArrayInputStream ; 6 import java.io.ByteArrayOutputStream ; 7 import java.io.InputStream ; 8 import javax.xml.soap.MessageFactory ; 9 import javax.xml.soap.MimeHeaders ; 10 import javax.xml.soap.SOAPBody ; 11 import javax.xml.soap.SOAPElement ; 12 import javax.xml.soap.SOAPMessage ; 13 import javax.xml.soap.Text ; 14 import javax.xml.soap.SOAPHeader ; 15 import javax.xml.soap.SOAPEnvelope ; 16 import javax.xml.soap.SOAPPart ; 17 import javax.xml.transform.stream.StreamSource ; 18 19 import junit.framework.Assert; 20 21 public class TestMessageProperty2 extends junit.framework.TestCase { 22 public TestMessageProperty2(String name) { 23 super(name); 24 } 25 26 private static String 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\"><Bozo></String_1></tns:hello></soap:Body></soap:Envelope>"; 27 28 private SOAPMessage createTestMessage(String encoding, boolean xmlDecl) throws Exception { 29 MessageFactory factory = MessageFactory.newInstance(); 30 SOAPMessage message = factory.createMessage(); 31 SOAPPart sp = message.getSOAPPart(); 32 SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); 33 SOAPHeader header = envelope.getHeader(); 34 35 ByteArrayInputStream bais = 36 new ByteArrayInputStream (GoodSoapMessage.getBytes(encoding)); 37 StreamSource ssrc = new StreamSource (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 createMessageFromInputStream(InputStream is) throws Exception { 48 MessageFactory mf = MessageFactory.newInstance(); 49 return mf.createMessage(new MimeHeaders (), is); 50 } 51 52 public void testUTF8withXMLDecl() throws Exception { 53 SOAPMessage msg = createTestMessage("UTF-8", true); 54 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 55 msg.writeTo(baos); 56 String xml = new String (baos.toByteArray(),"UTF-8"); 57 assertTrue(xml.indexOf("UTF-8") != -1); 58 assertTrue(xml.indexOf("<Bozo>") != -1); 59 } 60 61 public void testUTF16withXMLDecl() throws Exception { 62 SOAPMessage msg = createTestMessage("UTF-16", true); 63 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 64 msg.writeTo(baos); 65 String xml = new String (baos.toByteArray(),"UTF-16"); 66 assertTrue(xml.indexOf("UTF-16") != -1); 67 assertTrue(xml.indexOf("<Bozo>") != -1); 68 } 69 } 70 | Popular Tags |