001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.rpc;
017:
018: import com.google.gwt.core.client.GWT;
019: import com.google.gwt.junit.client.GWTTestCase;
020: import com.google.gwt.user.client.rpc.InheritanceTestSetFactory.AnonymousClassInterface;
021: import com.google.gwt.user.client.rpc.InheritanceTestSetFactory.Circle;
022: import com.google.gwt.user.client.rpc.InheritanceTestSetFactory.SerializableClass;
023: import com.google.gwt.user.client.rpc.InheritanceTestSetFactory.SerializableClassWithTransientField;
024: import com.google.gwt.user.client.rpc.InheritanceTestSetFactory.SerializableSubclass;
025:
026: /**
027: * TODO: document me.
028: */
029: public class InheritanceTest extends GWTTestCase {
030: private static final int TEST_DELAY = 5000;
031:
032: private InheritanceTestServiceAsync inheritanceTestService;
033:
034: public String getModuleName() {
035: return "com.google.gwt.user.RPCSuite";
036: }
037:
038: /**
039: * Test that anonymous classes are not serializable.
040: */
041: public void testAnonymousClasses() {
042: delayTestFinish(TEST_DELAY);
043:
044: InheritanceTestServiceAsync service = getServiceAsync();
045: service.echo(new AnonymousClassInterface() {
046: public void foo() {
047: // purposely empty
048: }
049: }, new AsyncCallback() {
050: public void onFailure(Throwable caught) {
051: finishTest();
052: }
053:
054: public void onSuccess(Object result) {
055: fail("Anonymous inner classes should not be serializable");
056: }
057: });
058: }
059:
060: /**
061: * Tests that a shadowed field is properly serialized.
062: *
063: * Checks for <a href="bug
064: * http://code.google.com/p/google-web-toolkit/issues/detail?id=161">BUG 161</a>
065: */
066: public void testFieldShadowing() {
067: delayTestFinish(TEST_DELAY);
068:
069: InheritanceTestServiceAsync service = getServiceAsync();
070: service.echo(InheritanceTestSetFactory.createCircle(),
071: new AsyncCallback() {
072: public void onFailure(Throwable caught) {
073: TestSetValidator.rethrowException(caught);
074: }
075:
076: public void onSuccess(Object result) {
077: Circle circle = (Circle) result;
078: assertNotNull(circle.getName());
079: finishTest();
080: }
081: });
082: }
083:
084: /**
085: * Tests that transient fields do not prevent serializability.
086: */
087: public void testJavaSerializableClass() {
088: delayTestFinish(TEST_DELAY);
089:
090: InheritanceTestServiceAsync service = getServiceAsync();
091: service.echo(
092: new InheritanceTestSetFactory.JavaSerializableClass(3),
093: new AsyncCallback() {
094: public void onFailure(Throwable caught) {
095: TestSetValidator.rethrowException(caught);
096: }
097:
098: public void onSuccess(Object result) {
099: assertNotNull(result);
100: finishTest();
101: }
102: });
103: }
104:
105: /**
106: * Test that non-static inner classes are not serializable.
107: */
108: public void testNonStaticInnerClass() {
109: delayTestFinish(TEST_DELAY);
110:
111: InheritanceTestServiceAsync service = getServiceAsync();
112: service.echo(InheritanceTestSetFactory
113: .createNonStaticInnerClass(), new AsyncCallback() {
114: public void onFailure(Throwable caught) {
115: finishTest();
116: }
117:
118: public void onSuccess(Object result) {
119: fail("Non-static inner classes should not be serializable");
120: }
121: });
122: }
123:
124: public void testReturnOfUnserializableClassFromServer() {
125: delayTestFinish(TEST_DELAY);
126:
127: InheritanceTestServiceAsync service = getServiceAsync();
128: service.getUnserializableClass(new AsyncCallback() {
129: public void onFailure(Throwable caught) {
130: finishTest();
131: }
132:
133: public void onSuccess(Object result) {
134: fail("Returning an unserializable class from the server should fail");
135: }
136: });
137: }
138:
139: /**
140: * Test that a valid serializable class can be serialized.
141: */
142: public void testSerializableClass() {
143: delayTestFinish(TEST_DELAY);
144:
145: InheritanceTestServiceAsync service = getServiceAsync();
146: service.echo(InheritanceTestSetFactory
147: .createSerializableClass(), new AsyncCallback() {
148: public void onFailure(Throwable caught) {
149: TestSetValidator.rethrowException(caught);
150: }
151:
152: public void onSuccess(Object result) {
153: assertNotNull(result);
154: assertTrue(InheritanceTestSetValidator
155: .isValid((SerializableClass) result));
156: finishTest();
157: }
158: });
159: }
160:
161: /**
162: * Test that IsSerializable is inherited, also test static inner classes.
163: */
164: public void testSerializableSubclass() {
165: delayTestFinish(TEST_DELAY);
166:
167: InheritanceTestServiceAsync service = getServiceAsync();
168: service.echo(InheritanceTestSetFactory
169: .createSerializableSubclass(), new AsyncCallback() {
170: public void onFailure(Throwable caught) {
171: TestSetValidator.rethrowException(caught);
172: }
173:
174: public void onSuccess(Object result) {
175: assertNotNull(result);
176: assertTrue(InheritanceTestSetValidator
177: .isValid((SerializableSubclass) result));
178: finishTest();
179: }
180: });
181: }
182:
183: /**
184: * Tests that transient fields do not prevent serializability.
185: */
186: public void testTransientFieldExclusion() {
187: delayTestFinish(TEST_DELAY);
188:
189: InheritanceTestServiceAsync service = getServiceAsync();
190: service.echo(InheritanceTestSetFactory
191: .createSerializableClassWithTransientField(),
192: new AsyncCallback() {
193: public void onFailure(Throwable caught) {
194: TestSetValidator.rethrowException(caught);
195: }
196:
197: public void onSuccess(Object result) {
198: assertNotNull(result);
199: assertTrue(InheritanceTestSetValidator
200: .isValid((SerializableClassWithTransientField) result));
201: finishTest();
202: }
203: });
204: }
205:
206: private InheritanceTestServiceAsync getServiceAsync() {
207: if (inheritanceTestService == null) {
208: inheritanceTestService = (InheritanceTestServiceAsync) GWT
209: .create(InheritanceTestServiceSubtype.class);
210: ((ServiceDefTarget) inheritanceTestService)
211: .setServiceEntryPoint(GWT.getModuleBaseURL()
212: + "inheritance");
213: }
214: return inheritanceTestService;
215: }
216: }
|