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.EnumsTestService.Basic;
021: import com.google.gwt.user.client.rpc.EnumsTestService.Complex;
022: import com.google.gwt.user.client.rpc.EnumsTestService.Subclassing;
023:
024: /**
025: * Tests enums over RPC.
026: */
027: public class EnumsTest extends GWTTestCase {
028: private static final int TEST_DELAY = 5000;
029:
030: private static EnumsTestServiceAsync getService() {
031: EnumsTestServiceAsync service = GWT
032: .create(EnumsTestService.class);
033: ServiceDefTarget target = (ServiceDefTarget) service;
034: target.setServiceEntryPoint(GWT.getModuleBaseURL() + "enums");
035: return service;
036: }
037:
038: private static void rethrowException(Throwable caught) {
039: if (caught instanceof RuntimeException) {
040: throw (RuntimeException) caught;
041: } else {
042: throw new RuntimeException(caught);
043: }
044: }
045:
046: @Override
047: public String getModuleName() {
048: return "com.google.gwt.user.RPCSuite";
049: }
050:
051: /**
052: * Test that basic enums can be used over RPC.
053: */
054: public void testBasicEnums() {
055: delayTestFinish(TEST_DELAY);
056: getService().echo(Basic.A, new AsyncCallback<Basic>() {
057: public void onFailure(Throwable caught) {
058: rethrowException(caught);
059: }
060:
061: public void onSuccess(Basic result) {
062: assertEquals(Basic.A, result);
063: finishTest();
064: }
065: });
066: }
067:
068: /**
069: * Test that complex enums with state and non-default constructors can be used
070: * over RPC and that the client state does not change.
071: */
072: public void testComplexEnums() {
073: delayTestFinish(TEST_DELAY);
074:
075: Complex a = Complex.A;
076: a.value = "client";
077:
078: getService().echo(Complex.A, new AsyncCallback<Complex>() {
079: public void onFailure(Throwable caught) {
080: rethrowException(caught);
081: }
082:
083: public void onSuccess(Complex result) {
084: assertEquals(Complex.A, result);
085:
086: // Ensure that the server's changes did not impact us.
087: assertEquals("client", result.value);
088:
089: finishTest();
090: }
091: });
092: }
093:
094: /**
095: * Test that null can be used as an enumeration.
096: */
097: public void testNull() {
098: delayTestFinish(TEST_DELAY);
099:
100: getService().echo((Basic) null, new AsyncCallback<Basic>() {
101: public void onFailure(Throwable caught) {
102: rethrowException(caught);
103: }
104:
105: public void onSuccess(Basic result) {
106: assertNull(result);
107: finishTest();
108: }
109: });
110: }
111:
112: /**
113: * Test that enums with subclasses can be passed over RPC.
114: */
115: public void testSubclassingEnums() {
116: delayTestFinish(TEST_DELAY);
117:
118: getService().echo(Subclassing.A,
119: new AsyncCallback<Subclassing>() {
120: public void onFailure(Throwable caught) {
121: rethrowException(caught);
122: }
123:
124: public void onSuccess(Subclassing result) {
125: assertEquals(Subclassing.A, result);
126: finishTest();
127: }
128: });
129: }
130: }
|