1 22 package org.jboss.test.webservice.utf16; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStreamWriter ; 29 30 import javax.naming.InitialContext ; 31 import javax.xml.rpc.Service ; 32 import javax.xml.soap.MessageFactory ; 33 import javax.xml.soap.MimeHeaders ; 34 import javax.xml.soap.SOAPConnection ; 35 import javax.xml.soap.SOAPConnectionFactory ; 36 import javax.xml.soap.SOAPElement ; 37 import javax.xml.soap.SOAPException ; 38 import javax.xml.soap.SOAPMessage ; 39 40 import junit.framework.Test; 41 42 import org.jboss.test.webservice.WebserviceTestBase; 43 44 51 public class UTF16TestCase extends WebserviceTestBase 52 { 53 private SOAPConnection soapCon; 54 55 private static String reqEnv = 56 "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>" + 57 " <soapenv:Body>" + 58 " <ns1:hello xmlns:ns1='http://org.jboss.test.webservice/utf16'>" + 59 " <String_1>Kermit</String_1>" + 60 " </ns1:hello>" + 61 " </soapenv:Body>" + 62 "</soapenv:Envelope>"; 63 64 public UTF16TestCase(String name) 65 { 66 super(name); 67 } 68 69 70 public static Test suite() throws Exception 71 { 72 return getDeploySetup(UTF16TestCase.class, "ws4ee-utf16.war, ws4ee-utf16-client.jar"); 73 } 74 75 public void setUp() throws Exception 76 { 77 super.setUp(); 78 SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance(); 79 soapCon = conFactory.createConnection(); 80 } 81 82 public void testClientAccess() throws Exception 83 { 84 InitialContext iniCtx = getClientContext(); 85 Service service = (Service )iniCtx.lookup("java:comp/env/service/TestService"); 86 Hello endpoint = (Hello)service.getPort(Hello.class); 87 88 String retObj = endpoint.hello("Kermit"); 89 assertEquals("Kermit", retObj); 90 } 91 92 94 public void testDefaultAccess() throws Exception 95 { 96 SOAPMessage reqMsg = createSOAPMessage(reqEnv, null); 97 SOAPMessage resMsg = soapCon.call(reqMsg, getTargetAddress()); 98 verifyResponseMessage(resMsg); 99 } 100 101 103 public void testUTF8Access() throws Exception 104 { 105 SOAPMessage reqMsg = createSOAPMessage(reqEnv, "UTF-8"); 106 SOAPMessage resMsg = soapCon.call(reqMsg, getTargetAddress()); 107 verifyResponseMessage(resMsg); 108 } 109 110 112 public void testUTF16Access() throws Exception 113 { 114 SOAPMessage reqMsg = createSOAPMessage(reqEnv, "UTF-16"); 115 SOAPMessage resMsg = soapCon.call(reqMsg, getTargetAddress()); 116 verifyResponseMessage(resMsg); 117 } 118 119 private SOAPMessage createSOAPMessage(String envStr, String csName) throws IOException , SOAPException 120 { 121 MessageFactory msgFactory = MessageFactory.newInstance(); 122 123 MimeHeaders headers = null; 124 if (csName != null) 125 { 126 headers = new MimeHeaders (); 127 headers.addHeader("Content-Type", "text/xml; charset=\"" + csName + "\""); 128 envStr = "<?xml version='1.0' encoding='" + csName + "'?>" + envStr; 129 } 130 131 InputStream is = getInputStreamForString(envStr, csName); 132 SOAPMessage reqMsg = msgFactory.createMessage(headers, is); 133 return reqMsg; 134 } 135 136 private InputStream getInputStreamForString(String envStr, String csName) throws IOException 137 { 138 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 139 OutputStreamWriter osw; 140 if (csName != null) 141 { 142 osw = new OutputStreamWriter (bos, csName); 143 } 144 else 145 { 146 osw = new OutputStreamWriter (bos, "UTF-8"); 147 } 148 osw.write(envStr); 149 osw.flush(); 150 151 ByteArrayInputStream bis = new ByteArrayInputStream (bos.toByteArray()); 152 return bis; 153 } 154 155 private void verifyResponseMessage(SOAPMessage resMsg) throws SOAPException 156 { 157 SOAPElement soapElement = (SOAPElement )resMsg.getSOAPBody().getChildElements().next(); 158 soapElement = (SOAPElement )soapElement.getChildElements().next(); 159 String retObj = soapElement.getValue(); 160 assertEquals("Kermit", retObj); 161 } 162 163 private String getTargetAddress() 164 { 165 return "http://" + getServerHost() + ":8080/ws4ee-utf16"; 166 } 167 } 168 | Popular Tags |