01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.user.client.rpc;
17:
18: import com.google.gwt.core.client.GWT;
19: import com.google.gwt.junit.client.GWTTestCase;
20:
21: /**
22: * Test which verifies that we properly escape JSON strings sent back from the
23: * server.
24: */
25: public class UnicodeEscapingTest extends GWTTestCase {
26:
27: private static final int DEFAULT_TEST_FINISH_DELAY_MS = 5000;
28: private static final int CHARACTER_RANGE_SIZE = 1024;
29: private static final int LAST_CHARACTER = 0x10000;
30:
31: private int start = 0;
32:
33: private static UnicodeEscapingServiceAsync getService() {
34: UnicodeEscapingServiceAsync service = (UnicodeEscapingServiceAsync) GWT
35: .create(UnicodeEscapingService.class);
36: ServiceDefTarget target = (ServiceDefTarget) service;
37: target.setServiceEntryPoint(GWT.getModuleBaseURL()
38: + "unicodeEscape");
39: return service;
40: }
41:
42: public String getModuleName() {
43: return "com.google.gwt.user.RPCSuite";
44: }
45:
46: /**
47: * Requests strings of CHARACTER_RANGE_SIZE from the server and validates that
48: * the returned string length matches CHARACTER_RANGE_SIZE and that all of the
49: * characters remain intact.
50: */
51: public void testUnicodeEscaping() {
52: delayTestFinish(DEFAULT_TEST_FINISH_DELAY_MS);
53:
54: getService().getStringContainingCharacterRange(0,
55: CHARACTER_RANGE_SIZE, new AsyncCallback() {
56: public void onFailure(Throwable caught) {
57: TestSetValidator.rethrowException(caught);
58: }
59:
60: public void onSuccess(Object result) {
61: String str = (String) result;
62:
63: assertTrue("expected: "
64: + Integer
65: .toString(CHARACTER_RANGE_SIZE)
66: + " actual: "
67: + str.length()
68: + " for character range ["
69: + Integer.toString(start)
70: + ", "
71: + Integer.toString(start
72: + CHARACTER_RANGE_SIZE) + ")",
73: CHARACTER_RANGE_SIZE == str.length());
74:
75: char[] chars = str.toCharArray();
76: for (int i = 0; i < CHARACTER_RANGE_SIZE; ++i) {
77: assertEquals(i + start, chars[i]);
78: }
79:
80: start += CHARACTER_RANGE_SIZE;
81: if (start < LAST_CHARACTER) {
82: delayTestFinish(DEFAULT_TEST_FINISH_DELAY_MS);
83:
84: getService()
85: .getStringContainingCharacterRange(
86: start,
87: start
88: + CHARACTER_RANGE_SIZE,
89: this);
90: } else {
91: finishTest();
92: }
93: }
94: });
95: }
96: }
|