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:
021: import java.util.ArrayList;
022: import java.util.Date;
023: import java.util.HashMap;
024: import java.util.HashSet;
025: import java.util.List;
026: import java.util.Vector;
027:
028: /**
029: * TODO: document me.
030: */
031: public class CollectionsTest extends GWTTestCase {
032: private static final int TEST_DELAY = 10000;
033:
034: private CollectionsTestServiceAsync collectionsTestService;
035:
036: public void _testDateArray() {
037: delayTestFinish(TEST_DELAY);
038:
039: CollectionsTestServiceAsync service = getServiceAsync();
040: final Date[] expected = TestSetFactory.createDateArray();
041: service.echo(expected, new AsyncCallback() {
042: public void onFailure(Throwable caught) {
043: TestSetValidator.rethrowException(caught);
044: }
045:
046: public void onSuccess(Object result) {
047: assertNotNull(result);
048: assertTrue(TestSetValidator.equals(expected,
049: (Date[]) result));
050: finishTest();
051: }
052: });
053: }
054:
055: public void disabledTestLongArray() {
056: delayTestFinish(TEST_DELAY);
057:
058: CollectionsTestServiceAsync service = getServiceAsync();
059: final Long[] expected = TestSetFactory.createLongArray();
060: service.echo(expected, new AsyncCallback() {
061: public void onFailure(Throwable caught) {
062: TestSetValidator.rethrowException(caught);
063: }
064:
065: public void onSuccess(Object result) {
066: assertNotNull(result);
067: assertTrue(TestSetValidator.equals(expected,
068: (Long[]) result));
069: finishTest();
070: }
071: });
072: }
073:
074: public void disabledTestPrimitiveLongArray() {
075: delayTestFinish(TEST_DELAY);
076:
077: CollectionsTestServiceAsync service = getServiceAsync();
078: final long[] expected = TestSetFactory
079: .createPrimitiveLongArray();
080: service.echo(expected, new AsyncCallback() {
081: public void onFailure(Throwable caught) {
082: TestSetValidator.rethrowException(caught);
083: }
084:
085: public void onSuccess(Object result) {
086: assertNotNull(result);
087: assertTrue(TestSetValidator.equals(expected,
088: (long[]) result));
089: finishTest();
090: }
091: });
092: }
093:
094: public String getModuleName() {
095: return "com.google.gwt.user.RPCSuite";
096: }
097:
098: public void testArrayList() {
099: delayTestFinish(TEST_DELAY);
100:
101: CollectionsTestServiceAsync service = getServiceAsync();
102: service.echo(TestSetFactory.createArrayList(),
103: new AsyncCallback() {
104: public void onFailure(Throwable caught) {
105: TestSetValidator.rethrowException(caught);
106: }
107:
108: public void onSuccess(Object result) {
109: assertNotNull(result);
110: assertTrue(TestSetValidator
111: .isValid((ArrayList) result));
112: finishTest();
113: }
114: });
115: }
116:
117: public void testBooleanArray() {
118: delayTestFinish(TEST_DELAY);
119:
120: CollectionsTestServiceAsync service = getServiceAsync();
121: final Boolean[] expected = TestSetFactory.createBooleanArray();
122: service.echo(expected, new AsyncCallback() {
123: public void onFailure(Throwable caught) {
124: TestSetValidator.rethrowException(caught);
125: }
126:
127: public void onSuccess(Object result) {
128: assertNotNull(result);
129: assertTrue(TestSetValidator.equals(expected,
130: (Boolean[]) result));
131: finishTest();
132: }
133: });
134: }
135:
136: public void testByteArray() {
137: delayTestFinish(TEST_DELAY);
138:
139: CollectionsTestServiceAsync service = getServiceAsync();
140: final Byte[] expected = TestSetFactory.createByteArray();
141: service.echo(expected, new AsyncCallback() {
142: public void onFailure(Throwable caught) {
143: TestSetValidator.rethrowException(caught);
144: }
145:
146: public void onSuccess(Object result) {
147: assertNotNull(result);
148: assertTrue(TestSetValidator.equals(expected,
149: (Byte[]) result));
150: finishTest();
151: }
152: });
153: }
154:
155: public void testCharArray() {
156: delayTestFinish(TEST_DELAY);
157:
158: CollectionsTestServiceAsync service = getServiceAsync();
159: final Character[] expected = TestSetFactory.createCharArray();
160: service.echo(expected, new AsyncCallback() {
161: public void onFailure(Throwable caught) {
162: TestSetValidator.rethrowException(caught);
163: }
164:
165: public void onSuccess(Object result) {
166: assertNotNull(result);
167: assertTrue(TestSetValidator.equals(expected,
168: (Character[]) result));
169: finishTest();
170: }
171: });
172: }
173:
174: public void testDoubleArray() {
175: delayTestFinish(TEST_DELAY);
176:
177: CollectionsTestServiceAsync service = getServiceAsync();
178: final Double[] expected = TestSetFactory.createDoubleArray();
179: service.echo(expected, new AsyncCallback() {
180: public void onFailure(Throwable caught) {
181: TestSetValidator.rethrowException(caught);
182: }
183:
184: public void onSuccess(Object result) {
185: assertNotNull(result);
186: assertTrue(TestSetValidator.equals(expected,
187: (Double[]) result));
188: finishTest();
189: }
190: });
191: }
192:
193: /**
194: * This method checks that attempting to return
195: * {@link java.util.Arrays#asList(Object[])} from the server will result in an
196: * InvocationException on the client.
197: */
198: public void testFailureWhenReturningArraysAsList() {
199: delayTestFinish(TEST_DELAY);
200:
201: CollectionsTestServiceAsync service = getServiceAsync();
202: final List expected = new ArrayList();
203: for (byte i = 0; i < 10; ++i) {
204: expected.add(new Byte(i));
205: }
206:
207: service.getArraysAsList(expected, new AsyncCallback() {
208: public void onFailure(Throwable caught) {
209: assertTrue(caught.getClass().getName()
210: + " should have been an InvocationException",
211: caught instanceof InvocationException);
212: finishTest();
213: }
214:
215: public void onSuccess(Object result) {
216: fail("Expected an InvocationException");
217: }
218: });
219: }
220:
221: public void testFloatArray() {
222: delayTestFinish(TEST_DELAY);
223:
224: CollectionsTestServiceAsync service = getServiceAsync();
225: final Float[] expected = TestSetFactory.createFloatArray();
226: service.echo(expected, new AsyncCallback() {
227: public void onFailure(Throwable caught) {
228: TestSetValidator.rethrowException(caught);
229: }
230:
231: public void onSuccess(Object result) {
232: assertNotNull(result);
233: assertTrue(TestSetValidator.equals(expected,
234: (Float[]) result));
235: finishTest();
236: }
237: });
238: }
239:
240: public void testHashMap() {
241: delayTestFinish(TEST_DELAY);
242:
243: CollectionsTestServiceAsync service = getServiceAsync();
244: final HashMap expected = TestSetFactory.createHashMap();
245: service.echo(expected, new AsyncCallback() {
246: public void onFailure(Throwable caught) {
247: TestSetValidator.rethrowException(caught);
248: }
249:
250: public void onSuccess(Object result) {
251: assertNotNull(result);
252: assertTrue(TestSetValidator.isValid(expected,
253: (HashMap) result));
254: finishTest();
255: }
256: });
257: }
258:
259: public void testHashSet() {
260: delayTestFinish(TEST_DELAY);
261:
262: CollectionsTestServiceAsync service = getServiceAsync();
263: final HashSet expected = TestSetFactory.createHashSet();
264: service.echo(expected, new AsyncCallback() {
265: public void onFailure(Throwable caught) {
266: TestSetValidator.rethrowException(caught);
267: }
268:
269: public void onSuccess(Object result) {
270: assertNotNull(result);
271: assertTrue(TestSetValidator.isValid(expected,
272: (HashSet) result));
273: finishTest();
274: }
275: });
276: }
277:
278: public void testIntegerArray() {
279: delayTestFinish(TEST_DELAY);
280:
281: CollectionsTestServiceAsync service = getServiceAsync();
282: final Integer[] expected = TestSetFactory.createIntegerArray();
283: service.echo(expected, new AsyncCallback() {
284: public void onFailure(Throwable caught) {
285: TestSetValidator.rethrowException(caught);
286: }
287:
288: public void onSuccess(Object result) {
289: assertNotNull(result);
290: assertTrue(TestSetValidator.equals(expected,
291: (Integer[]) result));
292: finishTest();
293: }
294: });
295: }
296:
297: public void testPrimitiveBooleanArray() {
298: delayTestFinish(TEST_DELAY);
299:
300: final boolean[] expected = TestSetFactory
301: .createPrimitiveBooleanArray();
302: CollectionsTestServiceAsync service = getServiceAsync();
303: service.echo(expected, new AsyncCallback() {
304: public void onFailure(Throwable caught) {
305: TestSetValidator.rethrowException(caught);
306: }
307:
308: public void onSuccess(Object result) {
309: assertTrue(TestSetValidator.equals(expected,
310: (boolean[]) result));
311: finishTest();
312: }
313: });
314: }
315:
316: public void testPrimitiveByteArray() {
317: delayTestFinish(TEST_DELAY);
318:
319: final byte[] expected = TestSetFactory
320: .createPrimitiveByteArray();
321: CollectionsTestServiceAsync service = getServiceAsync();
322: service.echo(expected, new AsyncCallback() {
323: public void onFailure(Throwable caught) {
324: TestSetValidator.rethrowException(caught);
325: }
326:
327: public void onSuccess(Object result) {
328: assertTrue(TestSetValidator.equals(expected,
329: (byte[]) result));
330: finishTest();
331: }
332: });
333: }
334:
335: public void testPrimitiveCharArray() {
336: delayTestFinish(TEST_DELAY);
337:
338: CollectionsTestServiceAsync service = getServiceAsync();
339: final char[] expected = TestSetFactory
340: .createPrimitiveCharArray();
341: service.echo(expected, new AsyncCallback() {
342: public void onFailure(Throwable caught) {
343: TestSetValidator.rethrowException(caught);
344: }
345:
346: public void onSuccess(Object result) {
347: assertNotNull(result);
348: assertTrue(TestSetValidator.equals(expected,
349: (char[]) result));
350: finishTest();
351: }
352: });
353: }
354:
355: public void testPrimitiveDoubleArray() {
356: delayTestFinish(TEST_DELAY);
357:
358: CollectionsTestServiceAsync service = getServiceAsync();
359: final double[] expected = TestSetFactory
360: .createPrimitiveDoubleArray();
361: service.echo(expected, new AsyncCallback() {
362: public void onFailure(Throwable caught) {
363: TestSetValidator.rethrowException(caught);
364: }
365:
366: public void onSuccess(Object result) {
367: assertNotNull(result);
368: assertTrue(TestSetValidator.equals(expected,
369: (double[]) result));
370: finishTest();
371: }
372: });
373: }
374:
375: public void testPrimitiveFloatArray() {
376: delayTestFinish(TEST_DELAY);
377:
378: CollectionsTestServiceAsync service = getServiceAsync();
379: final float[] expected = TestSetFactory
380: .createPrimitiveFloatArray();
381: service.echo(expected, new AsyncCallback() {
382: public void onFailure(Throwable caught) {
383: TestSetValidator.rethrowException(caught);
384: }
385:
386: public void onSuccess(Object result) {
387: assertNotNull(result);
388: assertTrue(TestSetValidator.equals(expected,
389: (float[]) result));
390: finishTest();
391: }
392: });
393: }
394:
395: public void testPrimitiveIntegerArray() {
396: delayTestFinish(TEST_DELAY);
397:
398: CollectionsTestServiceAsync service = getServiceAsync();
399: final int[] expected = TestSetFactory
400: .createPrimitiveIntegerArray();
401: service.echo(expected, new AsyncCallback() {
402: public void onFailure(Throwable caught) {
403: TestSetValidator.rethrowException(caught);
404: }
405:
406: public void onSuccess(Object result) {
407: assertNotNull(result);
408: assertTrue(TestSetValidator.equals(expected,
409: (int[]) result));
410: finishTest();
411: }
412: });
413: }
414:
415: public void testPrimitiveShortArray() {
416: delayTestFinish(TEST_DELAY);
417:
418: CollectionsTestServiceAsync service = getServiceAsync();
419: final short[] expected = TestSetFactory
420: .createPrimitiveShortArray();
421: service.echo(expected, new AsyncCallback() {
422: public void onFailure(Throwable caught) {
423: TestSetValidator.rethrowException(caught);
424: }
425:
426: public void onSuccess(Object result) {
427: assertNotNull(result);
428: assertTrue(TestSetValidator.equals(expected,
429: (short[]) result));
430: finishTest();
431: }
432: });
433: }
434:
435: public void testShortArray() {
436: delayTestFinish(TEST_DELAY);
437:
438: CollectionsTestServiceAsync service = getServiceAsync();
439: final Short[] expected = TestSetFactory.createShortArray();
440: service.echo(expected, new AsyncCallback() {
441: public void onFailure(Throwable caught) {
442: TestSetValidator.rethrowException(caught);
443: }
444:
445: public void onSuccess(Object result) {
446: assertNotNull(result);
447: assertTrue(TestSetValidator.equals(expected,
448: (Short[]) result));
449: finishTest();
450: }
451: });
452: }
453:
454: public void testStringArray() {
455: delayTestFinish(TEST_DELAY);
456:
457: CollectionsTestServiceAsync service = getServiceAsync();
458: final String[] expected = TestSetFactory.createStringArray();
459: service.echo(expected, new AsyncCallback() {
460: public void onFailure(Throwable caught) {
461: TestSetValidator.rethrowException(caught);
462: }
463:
464: public void onSuccess(Object result) {
465: assertNotNull(result);
466: assertTrue(TestSetValidator.equals(expected,
467: (String[]) result));
468: finishTest();
469: }
470: });
471: }
472:
473: public void testStringArrayArray() {
474: delayTestFinish(TEST_DELAY);
475:
476: CollectionsTestServiceAsync service = getServiceAsync();
477: final String[][] expected = new String[][] {
478: new String[] { "hello" }, new String[] { "bye" } };
479: service.echo(expected, new AsyncCallback() {
480: public void onFailure(Throwable caught) {
481: TestSetValidator.rethrowException(caught);
482: }
483:
484: public void onSuccess(Object result) {
485: assertNotNull(result);
486: finishTest();
487: }
488: });
489: }
490:
491: public void testVector() {
492: delayTestFinish(TEST_DELAY);
493:
494: CollectionsTestServiceAsync service = getServiceAsync();
495: final Vector expected = TestSetFactory.createVector();
496: service.echo(expected, new AsyncCallback() {
497: public void onFailure(Throwable caught) {
498: TestSetValidator.rethrowException(caught);
499: }
500:
501: public void onSuccess(Object result) {
502: assertNotNull(result);
503: assertTrue(TestSetValidator.isValid(expected,
504: (Vector) result));
505: finishTest();
506: }
507: });
508: }
509:
510: private CollectionsTestServiceAsync getServiceAsync() {
511: if (collectionsTestService == null) {
512: collectionsTestService = (CollectionsTestServiceAsync) GWT
513: .create(CollectionsTestService.class);
514: ((ServiceDefTarget) collectionsTestService)
515: .setServiceEntryPoint(GWT.getModuleBaseURL()
516: + "collections");
517: }
518: return collectionsTestService;
519: }
520: }
|