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.i18n.client;
017:
018: import com.google.gwt.core.client.GWT;
019: import com.google.gwt.i18n.client.gen.Colors;
020: import com.google.gwt.i18n.client.gen.Shapes;
021: import com.google.gwt.i18n.client.gen.TestMessages;
022: import com.google.gwt.i18n.client.impl.ConstantMap;
023: import com.google.gwt.i18n.client.resolutiontest.Inners;
024: import com.google.gwt.i18n.client.resolutiontest.Inners.ExtendsInnerInner;
025: import com.google.gwt.i18n.client.resolutiontest.Inners.HasInner;
026: import com.google.gwt.i18n.client.resolutiontest.Inners.InnerClass;
027: import com.google.gwt.i18n.client.resolutiontest.Inners.LocalizableSimpleInner;
028: import com.google.gwt.i18n.client.resolutiontest.Inners.OuterLoc;
029: import com.google.gwt.i18n.client.resolutiontest.Inners.SimpleInner;
030: import com.google.gwt.i18n.client.resolutiontest.Inners.HasInner.IsInner;
031: import com.google.gwt.i18n.client.resolutiontest.Inners.InnerClass.InnerInner;
032: import com.google.gwt.i18n.client.resolutiontest.Inners.InnerClass.InnerInnerMessages;
033: import com.google.gwt.i18n.client.resolutiontest.Inners.InnerClass.LocalizableInnerInner;
034: import com.google.gwt.junit.client.GWTTestCase;
035:
036: import java.util.ArrayList;
037: import java.util.Collection;
038: import java.util.Iterator;
039: import java.util.Map;
040: import java.util.MissingResourceException;
041: import java.util.Set;
042: import java.util.Map.Entry;
043:
044: /**
045: * Tests Internationalization. Assumes locale is set to piglatin_UK
046: */
047: public class I18NTest extends GWTTestCase {
048: public String getModuleName() {
049: return "com.google.gwt.i18n.I18NTest";
050: }
051:
052: public void testLocalizableInner() {
053: // Check simple inner
054: LocalizableSimpleInner s = (LocalizableSimpleInner) GWT
055: .create(Inners.LocalizableSimpleInner.class);
056: assertEquals("getLocalizableInner", s.getLocalizableInner());
057:
058: LocalizableInnerInner localizableInnerInner = (LocalizableInnerInner) GWT
059: .create(Inners.InnerClass.LocalizableInnerInner.class);
060: assertEquals("localizableInnerInner", localizableInnerInner
061: .string());
062:
063: // Check success of finding embedded
064: OuterLoc lock = (OuterLoc) GWT.create(OuterLoc.class);
065: assertEquals("piglatin", lock.string());
066:
067: assertEquals("InnerLoc", Inners.testInnerLoc());
068: }
069:
070: public void testLocalizableInterfaceInner() {
071: Inners inner = new Inners();
072:
073: // Simple Inner
074: SimpleInner simpleInner = (SimpleInner) GWT
075: .create(Inners.SimpleInner.class);
076: assertEquals(0, simpleInner.intZero());
077: assertEquals("Simple Inner", simpleInner.simpleInner());
078: assertTrue(inner.testProtectedInner());
079:
080: // Has Inner
081: HasInner hasInner = (HasInner) GWT
082: .create(Inners.HasInner.class);
083: assertEquals("Has Inner", hasInner.hasInner());
084: assertEquals(0, hasInner.floatZero(), .0001);
085:
086: // Is Inner
087: IsInner isInner = (IsInner) GWT.create(IsInner.class);
088: assertEquals(2, isInner.isInner());
089:
090: // Inner Inner
091: InnerInner innerInner = (InnerInner) GWT
092: .create(InnerInner.class);
093: assertEquals(4.321, innerInner.innerInner(), .0001);
094: assertEquals("outer", innerInner.outer());
095:
096: // Inner Inner Message
097: InnerInnerMessages innerInnerMessages = (InnerInnerMessages) GWT
098: .create(InnerInnerMessages.class);
099: assertEquals("I am a person", innerInnerMessages
100: .innerClassMessages("person"));
101:
102: // Extends Inner Inner
103: ExtendsInnerInner extendsInnerInner = (ExtendsInnerInner) GWT
104: .create(ExtendsInnerInner.class);
105: assertEquals("Extends Inner Inner", extendsInnerInner
106: .extendsInnerInner());
107:
108: // Protected InnerClass
109: InnerClass innerClass = new Inners.InnerClass();
110: String extendsAnotherInner = innerClass
111: .testExtendsAnotherInner();
112: assertEquals("{innerInner=4.321, outer=outer}",
113: extendsAnotherInner);
114:
115: // ExtendProtectedInner
116: String extendProtectedInner = innerClass
117: .testExtendsProtectedInner();
118: assertEquals("Extend Protected Inner", extendProtectedInner);
119: }
120:
121: public void testBindings() {
122: TestBinding b = (TestBinding) GWT.create(TestBinding.class);
123: assertEquals("default", b.a());
124: TestLeafBundle c = (TestLeafBundle) GWT
125: .create(TestLeafBundle.class);
126: assertEquals("TestLeafBundle_piglatin_UK_win", c.b());
127: com.google.gwt.i18n.client.Wrapper2.TestBindingImpl d = (com.google.gwt.i18n.client.Wrapper2.TestBindingImpl) GWT
128: .create(com.google.gwt.i18n.client.Wrapper2.TestBindingImpl.class);
129: assertEquals("default", d.a());
130: }
131:
132: public void testColors() {
133: Colors colors = (Colors) GWT.create(Colors.class);
134: assertNotNull(colors);
135: // No piglatin version exists for grey
136: assertEquals("Ä?réý", colors.grey());
137: assertEquals("ackblay", colors.black());
138: }
139:
140: public void testConstantBooleans() {
141: TestConstants types = (TestConstants) GWT
142: .create(TestConstants.class);
143: assertEquals(false, types.booleanFalse());
144: assertEquals(true, types.booleanTrue());
145: }
146:
147: public void testConstantDoubles() {
148: TestConstants types = (TestConstants) GWT
149: .create(TestConstants.class);
150: double delta = 0.0000001;
151: assertEquals(3.14159, types.doublePi(), delta);
152: assertEquals(0.0, types.doubleZero(), delta);
153: assertEquals(1.0, types.doubleOne(), delta);
154: assertEquals(-1.0, types.doubleNegOne(), delta);
155: assertEquals(Double.MAX_VALUE, types.doublePosMax(), delta);
156: assertEquals(Double.MIN_VALUE, types.doublePosMin(), delta);
157: assertEquals(-Double.MAX_VALUE, types.doubleNegMax(), delta);
158: assertEquals(-Double.MIN_VALUE, types.doubleNegMin(), delta);
159: }
160:
161: public void testConstantFloats() {
162: TestConstants types = (TestConstants) GWT
163: .create(TestConstants.class);
164: double delta = 0.0000001;
165: assertEquals(3.14159f, types.floatPi(), delta);
166: assertEquals(0.0f, types.floatZero(), delta);
167: assertEquals(1.0f, types.floatOne(), delta);
168: assertEquals(-1.0f, types.floatNegOne(), delta);
169: assertEquals(Float.MAX_VALUE, types.floatPosMax(), delta);
170: assertEquals(Float.MIN_VALUE, types.floatPosMin(), delta);
171: assertEquals(-Float.MAX_VALUE, types.floatNegMax(), delta);
172: assertEquals(-Float.MIN_VALUE, types.floatNegMin(), delta);
173: }
174:
175: /**
176: * Exercises ConstantMap more than the other map tests.
177: */
178: public void testConstantMapABCD() {
179: TestConstants types = (TestConstants) GWT
180: .create(TestConstants.class);
181:
182: Map map = types.mapABCD();
183: assertEquals(4, map.size());
184: assertEquals("valueA", map.get("keyA"));
185: assertEquals("valueB", map.get("keyB"));
186: assertEquals("valueC", map.get("keyC"));
187: assertEquals("valueD", map.get("keyD"));
188:
189: assertNull(map.get("bogus"));
190:
191: Set keys = map.keySet();
192: Iterator keyIter = keys.iterator();
193: assertEquals("keyA", keyIter.next());
194: assertEquals("keyB", keyIter.next());
195: assertEquals("keyC", keyIter.next());
196: assertEquals("keyD", keyIter.next());
197: assertFalse(keyIter.hasNext());
198:
199: Collection values = map.values();
200: Iterator valueIter = values.iterator();
201: assertEquals("valueA", valueIter.next());
202: assertEquals("valueB", valueIter.next());
203: assertEquals("valueC", valueIter.next());
204: assertEquals("valueD", valueIter.next());
205: assertFalse(keyIter.hasNext());
206:
207: try {
208: map.remove("keyA");
209: fail("Should have thrown UnsupportedOperationException");
210: } catch (UnsupportedOperationException e) {
211: // good if an exception was caught
212: }
213:
214: try {
215: keys.clear();
216: fail("Should have thrown UnsupportedOperationException");
217: } catch (UnsupportedOperationException e) {
218: // good if an exception was caught
219: }
220:
221: // TODO: fixme -- values are supposed to be backed by the map and should
222: // fail if modified
223: // try {
224: // Iterator nonmutableIter = keys.iterator();
225: // nonmutableIter.next();
226: // nonmutableIter.remove();
227: // fail("Should have thrown UnsupportedOperationException");
228: // } catch (UnsupportedOperationException e) {
229: // // good if an exception was caught
230: // }
231: }
232:
233: /**
234: * Tests focus on just the key order, since ABCD exercises the map.
235: */
236: public void testConstantMapBACD() {
237: TestConstants types = (TestConstants) GWT
238: .create(TestConstants.class);
239:
240: ConstantMap map = (ConstantMap) types.mapBACD();
241:
242: Set keys = map.keySet();
243: Iterator keyIter = keys.iterator();
244: assertEquals("keyB", keyIter.next());
245: assertEquals("keyA", keyIter.next());
246: assertEquals("keyC", keyIter.next());
247: assertEquals("keyD", keyIter.next());
248:
249: Collection values = map.values();
250: Iterator valueIter = values.iterator();
251: assertEquals("valueB", valueIter.next());
252: assertEquals("valueA", valueIter.next());
253: assertEquals("valueC", valueIter.next());
254: assertEquals("valueD", valueIter.next());
255: }
256:
257: /**
258: * Tests focus on correctness of entries, since ABCD exercises the map.
259: */
260: public void testConstantMapBBB() {
261: TestConstants types = (TestConstants) GWT
262: .create(TestConstants.class);
263:
264: ConstantMap map = (ConstantMap) types.mapBBB();
265:
266: assertEquals(1, map.size());
267:
268: Set keys = map.keySet();
269: assertEquals(1, keys.size());
270: Iterator keyIter = keys.iterator();
271: assertEquals("keyB", keyIter.next());
272:
273: Collection values = map.values();
274: assertEquals(1, values.size());
275: Iterator valueIter = values.iterator();
276: assertEquals("valueB", valueIter.next());
277: }
278:
279: /**
280: * Tests focus on just the key order, since ABCD exercises the map.
281: */
282: public void testConstantMapDBCA() {
283: TestConstants types = (TestConstants) GWT
284: .create(TestConstants.class);
285:
286: ConstantMap map = (ConstantMap) types.mapDCBA();
287:
288: Set keys = map.keySet();
289: Iterator keyIter = keys.iterator();
290: assertEquals("keyD", keyIter.next());
291: assertEquals("keyC", keyIter.next());
292: assertEquals("keyB", keyIter.next());
293: assertEquals("keyA", keyIter.next());
294:
295: Collection values = map.values();
296: Iterator valueIter = values.iterator();
297: assertEquals("valueD", valueIter.next());
298: assertEquals("valueC", valueIter.next());
299: assertEquals("valueB", valueIter.next());
300: assertEquals("valueA", valueIter.next());
301: }
302:
303: /**
304: * Tests focus on correctness of entries, since ABCD exercises the map.
305: */
306: public void testConstantMapXYZ() {
307: TestConstants types = (TestConstants) GWT
308: .create(TestConstants.class);
309:
310: ConstantMap map = (ConstantMap) types.mapXYZ();
311:
312: assertEquals(3, map.size());
313:
314: Set keys = map.keySet();
315: assertEquals(3, keys.size());
316: Iterator keyIter = keys.iterator();
317: assertEquals("keyX", keyIter.next());
318: assertEquals("keyY", keyIter.next());
319: assertEquals("keyZ", keyIter.next());
320:
321: Collection values = map.values();
322: assertEquals(3, values.size());
323: Iterator valueIter = values.iterator();
324: assertEquals("valueZ", valueIter.next());
325: assertEquals("valueZ", valueIter.next());
326: assertEquals("valueZ", valueIter.next());
327:
328: Set entries = map.entrySet();
329: assertEquals(3, entries.size());
330: Iterator entryIter = entries.iterator();
331: Map.Entry entry;
332:
333: entry = (Entry) entryIter.next();
334: assertEquals("keyX", entry.getKey());
335: assertEquals("valueZ", entry.getValue());
336: entry = (Entry) entryIter.next();
337: assertEquals("keyY", entry.getKey());
338: assertEquals("valueZ", entry.getValue());
339: entry = (Entry) entryIter.next();
340: assertEquals("keyZ", entry.getKey());
341: assertEquals("valueZ", entry.getValue());
342: }
343:
344: public void testConstantStringArrays() {
345: TestConstants types = (TestConstants) GWT
346: .create(TestConstants.class);
347: String[] s;
348:
349: s = types.stringArrayABCDEFG();
350: assertArrayEquals(new String[] { "A", "B", "C", "D", "E", "F",
351: "G" }, s);
352:
353: s = types.stringArraySizeOneEmptyString();
354: assertArrayEquals(new String[] { "" }, s);
355:
356: s = types.stringArraySizeOneX();
357: assertArrayEquals(new String[] { "X" }, s);
358:
359: s = types.stringArraySizeTwoBothEmpty();
360: assertArrayEquals(new String[] { "", "" }, s);
361:
362: s = types.stringArraySizeThreeAllEmpty();
363: assertArrayEquals(new String[] { "", "", "" }, s);
364:
365: s = types.stringArraySizeTwoWithEscapedComma();
366: assertArrayEquals(new String[] { "X", ", Y" }, s);
367:
368: s = types.stringArraySizeOneWithBackslashX();
369: assertArrayEquals(new String[] { "\\X" }, s);
370:
371: s = types.stringArraySizeThreeWithDoubleBackslash();
372: assertArrayEquals(new String[] { "X", "\\", "Y" }, s);
373: }
374:
375: public void testConstantStrings() {
376: TestConstants types = (TestConstants) GWT
377: .create(TestConstants.class);
378: assertEquals("string", types.getString());
379: assertEquals("stringTrimsLeadingWhitespace", types
380: .stringTrimsLeadingWhitespace());
381: assertEquals("stringDoesNotTrimTrailingThreeSpaces ", types
382: .stringDoesNotTrimTrailingThreeSpaces());
383: assertEquals("", types.stringEmpty());
384: String jaBlue = types.stringJapaneseBlue();
385: assertEquals("ã?‚ã?Š", jaBlue);
386: String jaGreen = types.stringJapaneseGreen();
387: assertEquals("ã?¿ã?©ã‚Š", jaGreen);
388: String jaRed = types.stringJapaneseRed();
389: assertEquals("ã?‚ã?‹", jaRed);
390: }
391:
392: public void testConstantsWithLookup() {
393: TestConstantsWithLookup l = (TestConstantsWithLookup) GWT
394: .create(TestConstantsWithLookup.class);
395: assertEquals(l.mapABCD(), l.getMap("mapABCD"));
396: assertEquals(l.mapDCBA(), l.getMap("mapDCBA"));
397: assertEquals(l.mapBACD(), l.getMap("mapBACD"));
398: assertEquals(l.getString(), l.getString("getString"));
399: assertSame(l.stringArrayABCDEFG(), l
400: .getStringArray("stringArrayABCDEFG"));
401: assertEquals(l.booleanFalse(), l.getBoolean("booleanFalse"));
402: assertEquals(l.floatPi(), l.getFloat("floatPi"), .001);
403: assertEquals(l.doublePi(), l.getDouble("doublePi"), .001);
404: try {
405: // even though getString has the gwt.key "string", it is not the lookup
406: // value
407: l.getMap("string");
408: fail("Should have thrown MissingResourceException");
409: } catch (MissingResourceException e) {
410: // success if the exception was caught
411: }
412: }
413:
414: // Uncomment for desk tests
415: // /**
416: // * Tests focus on correctness of entries, since ABCD exercises the map.
417: // */
418: // public void testConstantMapEmpty() {
419: // TestConstants types = (TestConstants) GWT.create(TestConstants.class);
420: //
421: // ConstantMap map = (ConstantMap) types.mapEmpty();
422: //
423: // assertEquals(0, map.size());
424: //
425: // Set keys = map.keySet();
426: // assertEquals(0, keys.size());
427: // Iterator keyIter = keys.iterator();
428: // assertFalse(keyIter.hasNext());
429: //
430: // Collection values = map.values();
431: // assertEquals(0, values.size());
432: // Iterator valueIter = values.iterator();
433: // assertFalse(valueIter.hasNext());
434: // }
435:
436: public void testDictionary() {
437: createDummyDictionaries();
438: Dictionary d = Dictionary.getDictionary("testDic");
439: assertEquals("3 {2},{2},{2}, one {0}, two {1} {1}", d
440: .get("formattedMessage"));
441: assertEquals("4", d.get("d"));
442: Set s = d.keySet();
443: assertTrue(s.contains("a"));
444: assertTrue(s.contains("b"));
445: assertFalse(s.contains("c"));
446: Collection s2 = d.values();
447: assertTrue(s2.contains("A"));
448: assertTrue(s2.contains("B"));
449: Iterator iter = s2.iterator();
450: assertEquals("3 {2},{2},{2}, one {0}, two {1} {1}", iter.next());
451: assertEquals(4, s2.size());
452: Dictionary empty = Dictionary.getDictionary("emptyDic");
453: assertEquals(0, empty.keySet().size());
454: boolean threwError = false;
455: try {
456: Dictionary.getDictionary("malformedDic");
457: } catch (MissingResourceException e) {
458: threwError = true;
459: }
460: assertTrue(threwError);
461: }
462:
463: public void testIntConstant() {
464: TestConstants types = (TestConstants) GWT
465: .create(TestConstants.class);
466: assertEquals(0, types.intZero());
467: assertEquals(1, types.intOne());
468: assertEquals(-1, types.intNegOne());
469: assertEquals(Integer.MAX_VALUE, types.intMax());
470: assertEquals(Integer.MIN_VALUE, types.intMin());
471: }
472:
473: public void testShapesFamily() {
474: Shapes shapes = (Shapes) GWT.create(Shapes.class);
475: // test overload
476: assertEquals("aya irclecay", shapes.circle());
477: ColorsAndShapesAndConcepts s = (ColorsAndShapesAndConcepts) GWT
478: .create(ColorsAndShapesAndConcepts.class);
479: assertEquals("aya irclecay", s.circle());
480: // test converge
481: assertEquals("any primary color", s.shapeColor());
482: assertEquals("trees", s.green());
483: }
484:
485: public void testTestMessages() {
486: TestMessages s = (TestMessages) GWT.create(TestMessages.class);
487: assertEquals("no args", s.args0());
488: assertEquals("a,b,c,d,e,f,g,h,i,j", s.args10("a", "b", "c",
489: "d", "e", "f", "g", "h", "i", "j"));
490: String shouldHave = "x,y, \"a\",\"b\", \"x\", \"y\", \'a\', b, {0}, \'y\'";
491: assertEquals(shouldHave, s.argsWithQuotes("x", "y"));
492: assertEquals("repeatedArgs: a, b, a, b, a, b, a, b", s
493: .testLotsOfUsageOfArgs("a", "b"));
494: assertEquals("\"~\" ~~ \"~~~~ \"\"", s.testWithXs());
495: assertEquals("ã?Šå¥½ä½ 好好", s.unicode("好", "好"));
496: assertEquals("", s.empty());
497: assertEquals("{quoted}", s.quotedBraces());
498: }
499:
500: public void testTypedMessages() {
501: TestTypedMessages typed = (TestTypedMessages) GWT
502: .create(TestTypedMessages.class);
503: String expected = "int(0) float(1.2), long(0), boolean(true), Object([], char(a), byte(127), short(-32768);";
504: assertEquals(expected, typed.testAllTypes(0, (float) 1.2, 0,
505: true, new ArrayList(), 'a', Byte.MAX_VALUE,
506: Short.MIN_VALUE));
507: String lotsOfInts = typed.testLotsOfInts(1, 2, 3, 4);
508: assertEquals("1, 2,3,4 ", lotsOfInts);
509: String oneFloat = typed.simpleMessageTest((float) 2.3);
510: assertEquals("2.3", oneFloat);
511: String singleQuotes = typed.testSingleQuotes("arg");
512: assertEquals("'A', 'arg', ','", singleQuotes);
513: String testSomeObjectTypes = typed.testSomeObjectTypes(
514: new I18NTest(), new StringBuffer("hello"), new Integer(
515: "34"), null);
516: assertEquals(
517: "this(null(com.google.gwt.i18n.client.I18NTest)), StringBuffer(hello), Integer(34), null(null);",
518: testSomeObjectTypes);
519: }
520:
521: private void assertArrayEquals(String[] shouldBe, String[] test) {
522: assertEquals(shouldBe.length, test.length);
523: for (int i = 0; i < test.length; i++) {
524: assertEquals(shouldBe[i], test[i]);
525: }
526: }
527:
528: private native void createDummyDictionaries() /*-{
529: $wnd.testDic = new Object();
530: $wnd.testDic.formattedMessage = "3 {2},{2},{2}, one {0}, two {1} {1}";
531: $wnd.testDic.a="A";
532: $wnd.testDic.b="B";
533: $wnd.testDic.d=4;
534: $wnd.emptyDic = new Object();
535: $wnd.malformedDic = 4;
536: }-*/;
537: }
|