KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > encoding > TestString2


1 package test.encoding;
2
3 import junit.framework.TestCase;
4 import org.apache.axis.MessageContext;
5 import org.apache.axis.encoding.DeserializationContext;
6 import org.apache.axis.message.RPCElement;
7 import org.apache.axis.message.RPCParam;
8 import org.apache.axis.server.AxisServer;
9 import org.xml.sax.InputSource JavaDoc;
10
11 import javax.xml.soap.SOAPMessage JavaDoc;
12 import javax.xml.soap.MessageFactory JavaDoc;
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.ByteArrayInputStream JavaDoc;
15
16 /** Little serialization test with a struct.
17  */

18 public class TestString2 extends TestCase {
19
20     public static final String JavaDoc myNS = "urn:myNS";
21     
22     public TestString2(String JavaDoc name) {
23         super(name);
24     }
25
26     private void runtest(String JavaDoc value, String JavaDoc expected) throws Exception JavaDoc {
27         MessageContext msgContext = new MessageContext(new AxisServer());
28         MessageFactory JavaDoc factory = MessageFactory.newInstance();
29         org.apache.axis.Message message = (org.apache.axis.Message) factory.createMessage();
30         message.setMessageContext(msgContext);
31         String JavaDoc requestEncoding = "UTF-16";
32         msgContext.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, requestEncoding);
33         message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, requestEncoding);
34         RPCParam input = new RPCParam("urn:myNamespace", "testParam", value);
35         
36         RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object JavaDoc[]{ input });
37         message.getSOAPBody().addChildElement(body);
38         
39         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
40         message.writeTo(baos);
41         
42         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
43         DeserializationContext dser = new DeserializationContext(
44             new InputSource JavaDoc(bais), msgContext, org.apache.axis.Message.REQUEST);
45         dser.parse();
46         
47         org.apache.axis.message.SOAPEnvelope env = dser.getEnvelope();
48         RPCElement rpcElem = (RPCElement)env.getFirstBody();
49         RPCParam output = rpcElem.getParam("testParam");
50         assertNotNull("No <testParam> param", output);
51         
52         String JavaDoc result = (String JavaDoc)output.getObjectValue();
53         assertNotNull("No value for testParam param", result);
54         assertEquals("Expected result not received.", expected, result);
55
56         String JavaDoc nodeValue = output.getValue();
57         assertNotNull("No node value for testParam param", nodeValue);
58         assertEquals(expected, nodeValue);
59     }
60
61     private void runtest(String JavaDoc value) throws Exception JavaDoc {
62         runtest(value, value);
63     }
64
65     public void testSimpleString() throws Exception JavaDoc {
66         runtest("a simple string");
67     }
68
69     public void testStringWithApostrophes() throws Exception JavaDoc {
70         runtest("this isn't a simple string");
71     }
72
73     public void testStringWithEntities() throws Exception JavaDoc {
74         runtest("&amp;&lt;&gt;&apos;&quot;", "&amp;&lt;&gt;&apos;&quot;");
75     }
76     
77     public void testStringWithRawEntities() throws Exception JavaDoc {
78         runtest("&<>'\"", "&<>'\"");
79     }
80     
81     public void testStringWithLeadingAndTrailingSpaces() throws Exception JavaDoc {
82         runtest(" centered ");
83     }
84     
85     public void testWhitespace() throws Exception JavaDoc {
86         runtest(" \n \t "); // note: \r fails
87
}
88
89     public void testFrenchAccents() throws Exception JavaDoc {
90         runtest("\u00e0\u00e2\u00e4\u00e7\u00e8\u00e9\u00ea\u00eb\u00ee\u00ef\u00f4\u00f6\u00f9\u00fb\u00fc");
91     }
92     
93     public void testFrenchAccents2() throws Exception JavaDoc {
94         runtest("Une chaîne avec des caractères accentués");
95     }
96     
97     public void testGermanUmlauts() throws Exception JavaDoc {
98         runtest(" Some text \u00df with \u00fc special \u00f6 chars \u00e4.");
99     }
100     
101     public void testWelcomeUnicode() throws Exception JavaDoc {
102         // welcome in several languages
103
runtest(
104           "Chinese (trad.) : \u6b61\u8fce ");
105     }
106
107     public void testWelcomeUnicode2() throws Exception JavaDoc {
108         // welcome in several languages
109
runtest(
110           "Greek : \u03ba\u03b1\u03bb\u03ce\u03c2 \u03bf\u03c1\u03af\u03c3\u03b1\u03c4\u03b5");
111     }
112
113     public void testWelcomeUnicode3() throws Exception JavaDoc {
114         // welcome in several languages
115
runtest(
116           "Japanese : \u3088\u3046\u3053\u305d");
117     }
118 }
119
Popular Tags