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.CustomFieldSerializerTestSetFactory.SerializableSubclass;
021:
022: /**
023: * Tests the following scenarios:
024: * <ul>
025: * <li>Manually serializable types use their custom field serializer</li>
026: * <li>Subtypes of manually serializable types that are not auto-serializable
027: * fail to be serialized</li>
028: * <li>Automatically serializable subtypes of manually serialized types can be
029: * serialized</li>
030: * </ul>
031: */
032: public class CustomFieldSerializerTest extends GWTTestCase {
033: private static final int TEST_DELAY = 5000;
034:
035: private CustomFieldSerializerTestServiceAsync customFieldSerializerTestService;
036:
037: public String getModuleName() {
038: return "com.google.gwt.user.RPCSuite";
039: }
040:
041: /**
042: * Test that custom field serializers do not make their subclasses
043: * serializable
044: */
045: public void testCustomFieldSerializabilityInheritance() {
046: delayTestFinish(TEST_DELAY);
047:
048: CustomFieldSerializerTestServiceAsync service = getServiceAsync();
049: service.echo(CustomFieldSerializerTestSetFactory
050: .createUnserializableSubclass(), new AsyncCallback() {
051: public void onFailure(Throwable caught) {
052: finishTest();
053: }
054:
055: public void onSuccess(Object result) {
056: fail("Class UnserializableSubclass should not be serializable");
057: }
058: });
059: }
060:
061: /**
062: * Tests that the custom field serializers are actually called
063: */
064: public void testCustomFieldSerialization() {
065: delayTestFinish(TEST_DELAY);
066:
067: CustomFieldSerializerTestServiceAsync service = getServiceAsync();
068: service.echo(CustomFieldSerializerTestSetFactory
069: .createUnserializableClass(), new AsyncCallback() {
070: public void onFailure(Throwable caught) {
071: fail("Class UnserializableClass should be serializable because it has a custom field serializer");
072: }
073:
074: public void onSuccess(Object result) {
075: assertNotNull(result);
076: assertTrue(CustomFieldSerializerTestSetValidator
077: .isValid((ManuallySerializedClass) result));
078: finishTest();
079: }
080: });
081: }
082:
083: /**
084: * Test that serializable subclasses of classes that have custom field
085: * serializers serialize and deserialize correctly
086: */
087: public void testSerializableSubclasses() {
088: delayTestFinish(TEST_DELAY);
089:
090: CustomFieldSerializerTestServiceAsync service = getServiceAsync();
091: service.echo(CustomFieldSerializerTestSetFactory
092: .createSerializableSubclass(), new AsyncCallback() {
093: public void onFailure(Throwable caught) {
094: fail("Class UnserializableClass should be serializable because it has a custom field serializer");
095: }
096:
097: public void onSuccess(Object result) {
098: assertNotNull(result);
099: assertTrue(CustomFieldSerializerTestSetValidator
100: .isValid((SerializableSubclass) result));
101: finishTest();
102: }
103: });
104: }
105:
106: private CustomFieldSerializerTestServiceAsync getServiceAsync() {
107: if (customFieldSerializerTestService == null) {
108: customFieldSerializerTestService = (CustomFieldSerializerTestServiceAsync) GWT
109: .create(CustomFieldSerializerTestService.class);
110: ((ServiceDefTarget) customFieldSerializerTestService)
111: .setServiceEntryPoint(GWT.getModuleBaseURL()
112: + "customfieldserializers");
113: }
114: return customFieldSerializerTestService;
115: }
116: }
|