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 test;
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.AsyncCallback;
021: import com.google.gwt.user.client.rpc.ServiceDefTarget;
022: import com.google.gwt.user.client.rpc.TestSetValidator;
023:
024: /**
025: * TODO: document me.
026: */
027: public class ServletMappingTest extends GWTTestCase {
028:
029: private static final int RPC_WAIT = 5000;
030:
031: public String getModuleName() {
032: return "test.ServletMappingTest";
033: }
034:
035: /**
036: * Should call the implementation that returns 1.
037: */
038: public void testServletMapping1() {
039: makeAsyncCall(GWT.getModuleBaseURL() + "test",
040: new AsyncCallback() {
041: public void onFailure(Throwable caught) {
042: TestSetValidator.rethrowException(caught);
043: }
044:
045: public void onSuccess(Object result) {
046: finishTest();
047: assertEquals(new Integer(1), result);
048: }
049: });
050: }
051:
052: /**
053: * Should call the implementation that returns 2.
054: */
055: public void testServletMapping2() {
056: makeAsyncCall(GWT.getModuleBaseURL() + "test/longer",
057: new AsyncCallback() {
058: public void onFailure(Throwable caught) {
059: TestSetValidator.rethrowException(caught);
060: }
061:
062: public void onSuccess(Object result) {
063: finishTest();
064: assertEquals(new Integer(2), result);
065: }
066: });
067: }
068:
069: /**
070: * Should call the implementation that returns 3.
071: */
072: public void testServletMapping3() {
073: makeAsyncCall(GWT.getModuleBaseURL() + "test/long",
074: new AsyncCallback() {
075: public void onFailure(Throwable caught) {
076: TestSetValidator.rethrowException(caught);
077: }
078:
079: public void onSuccess(Object result) {
080: finishTest();
081: assertEquals(new Integer(3), result);
082: }
083: });
084: }
085:
086: /**
087: * Should fail to find an entry point.
088: */
089: public void testBadRequestWithExtraPath() {
090: makeAsyncCall(GWT.getModuleBaseURL() + "test/bogus/extra/path",
091: new AsyncCallback() {
092: public void onFailure(Throwable caught) {
093: finishTest();
094: }
095:
096: public void onSuccess(Object result) {
097: finishTest();
098: assertEquals(new Integer(1), result);
099: }
100: });
101: }
102:
103: /**
104: * Should fail to find an entry point.
105: */
106: public void testBadRequestWithQueryString() {
107: makeAsyncCall(GWT.getModuleBaseURL() + "test/bogus?a=b&c=d",
108: new AsyncCallback() {
109: public void onFailure(Throwable caught) {
110: finishTest();
111: }
112:
113: public void onSuccess(Object result) {
114: finishTest();
115: assertEquals(new Integer(1), result);
116: }
117: });
118: }
119:
120: /**
121: * Should call the implementation that returns 3 with a query string.
122: */
123: public void testServletMapping3WithQueryString() {
124: makeAsyncCall(GWT.getModuleBaseURL() + "test/long?a=b&c=d",
125: new AsyncCallback() {
126: public void onFailure(Throwable caught) {
127: TestSetValidator.rethrowException(caught);
128: }
129:
130: public void onSuccess(Object result) {
131: finishTest();
132: assertEquals(new Integer(3), result);
133: }
134: });
135: }
136:
137: /**
138: * Should call the implementation that returns 3 with a query string.
139: */
140: public void testTotallyDifferentServletMapping3() {
141: makeAsyncCall(GWT.getModuleBaseURL()
142: + "totally/different/but/valid?a=b&c=d",
143: new AsyncCallback() {
144: public void onFailure(Throwable caught) {
145: TestSetValidator.rethrowException(caught);
146: }
147:
148: public void onSuccess(Object result) {
149: finishTest();
150: assertEquals(new Integer(3), result);
151: }
152: });
153: }
154:
155: private void makeAsyncCall(String url, AsyncCallback callback) {
156: ServletMappingTestServiceAsync async = (ServletMappingTestServiceAsync) GWT
157: .create(ServletMappingTestService.class);
158: ServiceDefTarget target = (ServiceDefTarget) async;
159: target.setServiceEntryPoint(url);
160: delayTestFinish(RPC_WAIT);
161: async.which(callback);
162: }
163:
164: }
|