01: package org.wings.plaf.css;
02:
03: import java.io.IOException;
04: import java.util.List;
05: import java.util.ArrayList;
06: import java.util.Map;
07: import java.util.TreeMap;
08:
09: import org.wings.io.StringBuilderDevice;
10:
11: import junit.framework.TestCase;
12:
13: public class UtilsTest extends TestCase {
14: private static String encodeJSToString(Object o) {
15: final StringBuilderDevice sb = new StringBuilderDevice(10);
16: try {
17: Utils.encodeJS(sb, o);
18: } catch (IOException e) {
19: }
20: return sb.toString();
21: }
22:
23: public void test_encodeJS_anytype() {
24: assertEquals("null", encodeJSToString(null));
25: assertEquals("42", encodeJSToString(new Integer(42)));
26: assertEquals("'foo'", encodeJSToString("foo"));
27: }
28:
29: public void test_encodeJS_Stringquoting() {
30: // Empty.
31: assertEquals("null", encodeJSToString(null));
32: assertEquals("''", encodeJSToString(""));
33:
34: // Generic escapes.
35: assertEquals("'\\\\'", encodeJSToString("\\"));
36: assertEquals("'\\b'", encodeJSToString("\b"));
37: assertEquals("'\\f'", encodeJSToString("\f"));
38: assertEquals("'\\n'", encodeJSToString("\n"));
39: assertEquals("'\\r'", encodeJSToString("\r"));
40: assertEquals("'\\t'", encodeJSToString("\t"));
41:
42: // Quoting quotes. We have more double quotes than single
43: // quotes in the output so its wise to quote strings with single
44: // quotes and escape only them.
45: assertEquals("'\\''", encodeJSToString("'")); // Single quote escaped.
46: assertEquals("'\"'", encodeJSToString("\"")); // Double not.
47:
48: // Special characters are encoded as utf-8 escape.
49: assertEquals("'\\u0000'", encodeJSToString("\u0000"));
50: assertEquals("'\\u001f'", encodeJSToString("\u001F"));
51: assertEquals("' '", encodeJSToString("\u0020")); // first non-special
52:
53: // Seems, that we need to encode every UTF-8 which is not ASCII.
54: // Did it work before ? - mmh, maybe by accident.
55: // If this is changed, back this decision with selenium browser tests.
56: assertEquals("'\\u00e4'", encodeJSToString("\u00E4"));
57:
58: // And now: all together ;-)
59: assertEquals("'foo\\\\\\'bar'", encodeJSToString("foo\\'bar"));
60: assertEquals("'\\nfoo\\\\\\'bar'",
61: encodeJSToString("\nfoo\\'bar"));
62: assertEquals("'\\nfoo\\\\\"b\\u00e4r\\t'",
63: encodeJSToString("\nfoo\\\"b\u00E4r\t"));
64: }
65:
66: public void test_JsonArray_rendering() {
67: List<Object> list = new ArrayList<Object>();
68: list.add("foo");
69: list.add(new Integer(42));
70: assertEquals("['foo',42]", encodeJSToString(Utils
71: .listToJsArray(list)));
72: }
73:
74: public void test_JsonObject_rendering() {
75: // Use TreeMap to have a predictable sequence.
76: final Map<String, Object> map = new TreeMap<String, Object>();
77: map.put("bar", new Integer(42));
78: map.put("baz", "s");
79:
80: final Map<String, Object> nestedMap = new TreeMap<String, Object>();
81: nestedMap.put("success", new Boolean(true));
82: map.put("foo", Utils.mapToJsObject(nestedMap));
83:
84: final Object json = Utils.mapToJsObject(map);
85: assertEquals("{'bar':42,'baz':'s','foo':{'success':true}}",
86: encodeJSToString(json));
87: }
88: }
|