KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > functional > TestEncoding


1 package test.functional;
2
3 import junit.framework.TestCase;
4 import org.apache.axis.AxisEngine;
5 import org.apache.axis.client.Call;
6 import org.apache.axis.client.Service;
7
8 import javax.xml.messaging.URLEndpoint;
9 import javax.xml.soap.MessageFactory JavaDoc;
10 import javax.xml.soap.Name JavaDoc;
11 import javax.xml.soap.SOAPBody JavaDoc;
12 import javax.xml.soap.SOAPBodyElement JavaDoc;
13 import javax.xml.soap.SOAPConnection JavaDoc;
14 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
15 import javax.xml.soap.SOAPElement JavaDoc;
16 import javax.xml.soap.SOAPEnvelope JavaDoc;
17 import javax.xml.soap.SOAPMessage JavaDoc;
18
19 import java.net.URL JavaDoc;
20
21 /**
22  * Test string encoding roundtrip.
23  */

24 public class TestEncoding extends TestCase {
25     Call call = null;
26
27     public TestEncoding(String JavaDoc s) {
28         super(s);
29     }
30
31     protected void setUp() throws Exception JavaDoc {
32         if (call == null) {
33             Service service = new Service();
34             service.getEngine().setOption(AxisEngine.PROP_XML_ENCODING, "UTF-8");
35             call = (Call) service.createCall();
36             call.setTargetEndpointAddress(new URL JavaDoc("http://localhost:8080/jws/EchoHeaders.jws"));
37         }
38     }
39
40     private void runtest(String JavaDoc send, String JavaDoc get) throws Exception JavaDoc {
41         String JavaDoc ret = (String JavaDoc) call.invoke("echo", new Object JavaDoc[]{send});
42         assertEquals(ret, get);
43     }
44
45     private void runtest(String JavaDoc value) throws Exception JavaDoc {
46         runtest(value, value);
47     }
48
49     public void testSimpleString() throws Exception JavaDoc {
50         runtest("a simple string");
51     }
52
53     public void testStringWithApostrophes() throws Exception JavaDoc {
54         runtest("this isn't a simple string");
55     }
56
57     public void testStringWithEntities() throws Exception JavaDoc {
58         runtest("&<>'"", "&<>'"");
59     }
60
61     public void testStringWithRawEntities() throws Exception JavaDoc {
62         runtest("&<>'\"", "&<>'\"");
63     }
64
65     public void testStringWithLeadingAndTrailingSpaces() throws Exception JavaDoc {
66         runtest(" centered ");
67     }
68
69     public void testWhitespace() throws Exception JavaDoc {
70         runtest(" \n \t "); // note: \r fails
71
}
72
73     public void testFrenchAccents() throws Exception JavaDoc {
74         runtest("\u00e0\u00e2\u00e4\u00e7\u00e8\u00e9\u00ea\u00eb\u00ee\u00ef\u00f4\u00f6\u00f9\u00fb\u00fc");
75     }
76
77     public void testFrenchAccents2() throws Exception JavaDoc {
78         runtest("Une chaîne avec des caractères accentués");
79     }
80
81     public void testGermanUmlauts() throws Exception JavaDoc {
82         runtest(" Some text \u00df with \u00fc special \u00f6 chars \u00e4.");
83     }
84
85     public void testWelcomeUnicode() throws Exception JavaDoc {
86         // welcome in several languages
87
runtest(
88                 "Chinese (trad.) : \u6b61\u8fce \n" +
89                 "Greek : \u03ba\u03b1\u03bb\u03ce\u03c2 \u03bf\u03c1\u03af\u03c3\u03b1\u03c4\u03b5 \n" +
90                 "Japanese : \u3088\u3046\u3053\u305d");
91     }
92
93     public void testSynchronization() throws Exception JavaDoc {
94         SOAPConnectionFactory JavaDoc scFactory = SOAPConnectionFactory.newInstance();
95         SOAPConnection JavaDoc con = scFactory.createConnection();
96
97         MessageFactory JavaDoc factory = MessageFactory.newInstance();
98         SOAPMessage JavaDoc message = factory.createMessage();
99         String JavaDoc requestEncoding = "UTF-16";
100         message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, requestEncoding);
101
102         SOAPEnvelope JavaDoc envelope = message.getSOAPPart().getEnvelope();
103         SOAPBody JavaDoc body = envelope.getBody();
104
105         Name JavaDoc bodyName = envelope.createName("echo");
106         SOAPBodyElement JavaDoc bodyElement = body.addBodyElement(bodyName);
107
108         Name JavaDoc name = envelope.createName("arg0");
109         SOAPElement JavaDoc symbol = bodyElement.addChildElement(name);
110         symbol.addTextNode("Hello");
111
112         URLEndpoint endpoint = new URLEndpoint("http://localhost:8080/jws/EchoHeaders.jws");
113         SOAPMessage JavaDoc response = con.call(message, endpoint);
114         String JavaDoc responseEncoding = (String JavaDoc) response.getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
115         assertEquals(requestEncoding.toLowerCase(), responseEncoding.toLowerCase());
116     }
117 }
118
Popular Tags