KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > encoding > TestString


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.encoding.SerializationContext;
7 import org.apache.axis.encoding.SerializationContext;
8 import org.apache.axis.message.RPCElement;
9 import org.apache.axis.message.RPCParam;
10 import org.apache.axis.message.SOAPEnvelope;
11 import org.apache.axis.server.AxisServer;
12 import org.xml.sax.InputSource JavaDoc;
13
14 import javax.xml.soap.MessageFactory JavaDoc;
15 import javax.xml.soap.SOAPMessage JavaDoc;
16 import java.io.StringReader JavaDoc;
17 import java.io.StringWriter JavaDoc;
18 import java.io.Writer JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.OutputStreamWriter JavaDoc;
21 import java.io.ByteArrayInputStream JavaDoc;
22
23 /** Little serialization test with a struct.
24  */

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