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.dev.jjs.test;
017:
018: import com.google.gwt.core.client.JavaScriptException;
019: import com.google.gwt.core.client.JavaScriptObject;
020: import com.google.gwt.junit.client.GWTTestCase;
021:
022: /**
023: * This should probably be refactored at some point.
024: */
025: public class MiscellaneousTest extends GWTTestCase {
026:
027: interface I {
028: }
029:
030: interface IBar extends I {
031: }
032:
033: interface IFoo extends I {
034: }
035:
036: static class PolyA implements IFoo {
037: public String toString() {
038: return "A";
039: }
040: }
041:
042: static class PolyB implements IBar {
043: public String toString() {
044: return "B";
045: }
046: }
047:
048: private static class Foo extends JavaScriptObject {
049: public String toString() {
050: return "Foo";
051: }
052: }
053:
054: private static class HasClinit {
055: public static int i = 1;
056:
057: private static HasClinit sInstance = new HasClinit();
058:
059: public static int sfoo() {
060: return sInstance.foo();
061: }
062:
063: private static native void clinitInNative() /*-{
064: }-*/;
065:
066: private int foo() {
067: this .toString();
068: return 3;
069: }
070: }
071:
072: public static native boolean noOptimizeFalse() /*-{
073: return false;
074: }-*/;
075:
076: private static void assertAllCanStore(Object[] dest, Object[] src) {
077: for (int i = 0; i < src.length; ++i) {
078: dest[0] = src[i];
079: }
080: }
081:
082: private static void assertNoneCanStore(Object[] dest, Object[] src) {
083: for (int i = 0; i < src.length; ++i) {
084: try {
085: dest[0] = src[i];
086: fail();
087: } catch (ArrayStoreException e) {
088: }
089: }
090: }
091:
092: private static native void clinitFromNative() /*-{
093: @com.google.gwt.dev.jjs.test.MiscellaneousTest$HasClinit::i = 5;
094: }-*/;
095:
096: private static native Foo getFoo() /*-{
097: return {};
098: }-*/;
099:
100: private static native JavaScriptObject getJso() /*-{
101: return {toString:function(){return 'jso';}};
102: }-*/;
103:
104: private static native void throwNativeException() /*-{
105: var a; a.asdf();
106: }-*/;
107:
108: public String getModuleName() {
109: return "com.google.gwt.dev.jjs.CompilerSuite";
110: }
111:
112: public void testArrayCasts() {
113: {
114: // thwart optimizer
115: Object f1 = noOptimizeFalse() ? (Object) new PolyA()
116: : (Object) new IFoo[1];
117: assertEquals(
118: "[Lcom.google.gwt.dev.jjs.test.MiscellaneousTest$IFoo;",
119: f1.getClass().getName());
120: assertFalse(f1 instanceof PolyA[][]);
121: assertFalse(f1 instanceof IFoo[][]);
122: assertFalse(f1 instanceof PolyA[]);
123: assertTrue(f1 instanceof IFoo[]);
124: assertFalse(f1 instanceof PolyA);
125: assertFalse(f1 instanceof IFoo);
126: assertTrue(f1 instanceof Object[]);
127: assertFalse(f1 instanceof Object[][]);
128:
129: assertAllCanStore((Object[]) f1, new Object[] {
130: new PolyA(), new IFoo() {
131: } });
132: assertNoneCanStore((Object[]) f1, new Object[] {
133: new PolyB(), new Object(), new Object[0] });
134: }
135:
136: {
137: // thwart optimizer
138: Object a1 = noOptimizeFalse() ? (Object) new PolyA()
139: : (Object) new PolyA[1];
140: assertEquals(
141: "[Lcom.google.gwt.dev.jjs.test.MiscellaneousTest$PolyA;",
142: a1.getClass().getName());
143: assertFalse(a1 instanceof PolyA[][]);
144: assertFalse(a1 instanceof IFoo[][]);
145: assertTrue(a1 instanceof PolyA[]);
146: assertTrue(a1 instanceof IFoo[]);
147: assertFalse(a1 instanceof PolyA);
148: assertFalse(a1 instanceof IFoo);
149: assertTrue(a1 instanceof Object[]);
150: assertFalse(a1 instanceof Object[][]);
151:
152: assertAllCanStore((Object[]) a1,
153: new Object[] { new PolyA() });
154: assertNoneCanStore((Object[]) a1, new Object[] {
155: new IFoo() {
156: }, new PolyB(), new Object(), new Object[0] });
157: }
158:
159: {
160: // thwart optimizer
161: Object f2 = noOptimizeFalse() ? (Object) new PolyA()
162: : (Object) new IFoo[1][];
163: assertEquals(
164: "[[Lcom.google.gwt.dev.jjs.test.MiscellaneousTest$IFoo;",
165: f2.getClass().getName());
166: assertFalse(f2 instanceof PolyA[][]);
167: assertTrue(f2 instanceof IFoo[][]);
168: assertFalse(f2 instanceof PolyA[]);
169: assertFalse(f2 instanceof IFoo[]);
170: assertFalse(f2 instanceof PolyA);
171: assertFalse(f2 instanceof IFoo);
172: assertTrue(f2 instanceof Object[]);
173: assertTrue(f2 instanceof Object[][]);
174:
175: assertAllCanStore((Object[]) f2, new Object[] {
176: new PolyA[0], new IFoo[0] });
177: assertNoneCanStore((Object[]) f2, new Object[] {
178: new IFoo() {
179: }, new PolyB(), new Object(), new Object[0] });
180: }
181:
182: {
183: // thwart optimizer
184: Object a2 = noOptimizeFalse() ? (Object) new PolyA()
185: : (Object) new PolyA[1][];
186: assertEquals(
187: "[[Lcom.google.gwt.dev.jjs.test.MiscellaneousTest$PolyA;",
188: a2.getClass().getName());
189: assertTrue(a2 instanceof PolyA[][]);
190: assertTrue(a2 instanceof IFoo[][]);
191: assertFalse(a2 instanceof PolyA[]);
192: assertFalse(a2 instanceof IFoo[]);
193: assertFalse(a2 instanceof PolyA);
194: assertFalse(a2 instanceof IFoo);
195: assertTrue(a2 instanceof Object[]);
196: assertTrue(a2 instanceof Object[][]);
197:
198: assertAllCanStore((Object[]) a2,
199: new Object[] { new PolyA[0] });
200: assertNoneCanStore((Object[]) a2, new Object[] {
201: new IFoo() {
202: }, new PolyB(), new Object(), new Object[0],
203: new IFoo[0] });
204: }
205: }
206:
207: public void testArrays() {
208: int[] c = new int[] { 1, 2 };
209: assertEquals("[I", c.getClass().getName());
210: int[][] d = new int[][] { { 1, 2 }, { 3, 4 } };
211: assertEquals("[[I", d.getClass().getName());
212: assertEquals("[I", d[1].getClass().getName());
213: int[][][] e = new int[][][] { { { 1, 2 }, { 3, 4 } },
214: { { 5, 6 }, { 7, 8 } } };
215: assertEquals("[[[I", e.getClass().getName());
216: assertEquals("[[I", e[1].getClass().getName());
217: assertEquals("[I", e[1][1].getClass().getName());
218: assertEquals(2, c[1]);
219: assertEquals(3, d[1][0]);
220: assertEquals(8, e[1][1][1]);
221:
222: int[][][] b = new int[3][2][1];
223: b[2][1][0] = 1;
224: b = new int[3][2][];
225: b[2][1] = null;
226: b = new int[3][][];
227: b[2] = null;
228: }
229:
230: public void testCasts() {
231: Object o = noOptimizeFalse() ? (Object) new PolyA()
232: : (Object) new PolyB();
233: assertTrue(o instanceof I);
234: assertFalse(o instanceof IFoo);
235: assertTrue(o instanceof IBar);
236: assertFalse(o instanceof PolyA);
237: assertTrue(o instanceof PolyB);
238: try {
239: o = (PolyA) o;
240: fail();
241: } catch (ClassCastException e) {
242: }
243: }
244:
245: public void testClinit() {
246: ++HasClinit.i;
247: HasClinit x = new HasClinit();
248: ++x.i;
249: new HasClinit().i++;
250: HasClinit.i /= HasClinit.i;
251: HasClinit.sfoo();
252: HasClinit.i /= HasClinit.sfoo();
253: HasClinit.clinitInNative();
254: clinitFromNative();
255: }
256:
257: public void testExceptions() {
258: int i;
259: for (i = 0; i < 5; ++i) {
260: boolean hitOuter = false;
261: boolean hitInner = false;
262: try {
263: try {
264: switch (i) {
265: case 0:
266: throw new RuntimeException();
267: case 1:
268: throw new IndexOutOfBoundsException();
269: case 2:
270: throw new Exception();
271: case 3:
272: throw new StringIndexOutOfBoundsException();
273: case 4:
274: throwNativeException();
275: }
276: } catch (StringIndexOutOfBoundsException e) {
277: assertEquals(3, i);
278: } finally {
279: hitInner = true;
280: }
281: } catch (IndexOutOfBoundsException f) {
282: assertEquals(1, i);
283: } catch (JavaScriptException js) {
284: assertEquals(4, i);
285: } catch (RuntimeException g) {
286: assertEquals(0, i);
287: } catch (Throwable e) {
288: assertEquals(2, i);
289: } finally {
290: assertTrue(hitInner);
291: hitOuter = true;
292: }
293: assertTrue(hitOuter);
294: }
295: assertEquals(5, i);
296: }
297:
298: public void testJso() {
299: Foo foo = getFoo();
300: assertEquals("Foo", foo.toString());
301: JavaScriptObject jso = foo;
302: assertEquals("Foo", jso.toString());
303: Object y = noOptimizeFalse() ? new Object() : foo;
304: assertEquals("Foo", y.toString());
305: jso = getJso();
306: assertEquals("jso", jso.toString());
307: }
308:
309: public void testJsoArrayInit() {
310: Object[] jsos = { getJso(), "" };
311: JavaScriptObject jso = (JavaScriptObject) jsos[0];
312: }
313:
314: public void testJsoArrayStore() {
315: // Verify that a JSO stored into an array was correctly wrapped
316: String[] strings = { "" };
317: JavaScriptObject[] jsos = { getJso() };
318: Object[] objArray = noOptimizeFalse() ? (Object[]) strings
319: : jsos;
320: JavaScriptObject jso = (JavaScriptObject) objArray[0];
321:
322: // Verify that ArrayStoreExceptions are generated in the correct cases
323: try {
324: JavaScriptObject[] typeTightenedFooArray = new Foo[3];
325: typeTightenedFooArray[0] = getJso();
326: fail();
327: } catch (ArrayStoreException e) {
328: }
329:
330: try {
331: JavaScriptObject[] fooArray = noOptimizeFalse() ? new JavaScriptObject[3]
332: : new Foo[3];
333: fooArray[0] = getJso();
334: fail();
335: } catch (ArrayStoreException e) {
336: }
337:
338: JavaScriptObject[] jsoArray = noOptimizeFalse() ? new Foo[3]
339: : new JavaScriptObject[3];
340: jsoArray[0] = getJso();
341: }
342:
343: public void testString() {
344: String x = "hi";
345: assertEquals("hi", x);
346: assertEquals("hi", x.toString());
347: x = new String();
348: assertEquals("", x);
349: x = new String(x);
350: assertEquals("", x);
351: x = new String("hi");
352: assertEquals("hi", x);
353: assertEquals('i', x.charAt(1));
354: assertEquals("hiyay", x.concat("yay"));
355: assertEquals("hihi", x + x);
356:
357: assertEquals(
358: "blahcom.google.gwt.dev.jjs.test.MiscellaneousTestabctruefalsenullc27",
359: ("blah" + this
360: + String.valueOf(new char[] { 'a', 'b', 'c' })
361: + true + false + null + 'c' + 27));
362: }
363:
364: /**
365: * Ensures that polymorphic dispatch to String works correctly.
366: */
367: @SuppressWarnings("unchecked")
368: public void testStringPrototype() {
369: Object s = noOptimizeFalse() ? new Object() : "Hello, World!";
370: assertEquals(String.class, s.getClass());
371: assertEquals("Hello, World!".hashCode(), s.hashCode());
372: assertTrue(s.equals("Hello, World!"));
373: assertTrue("Hello, World!".equals(s));
374: assertFalse(s.equals(""));
375: assertFalse("".equals(s));
376: assertEquals("Hello, World!", s.toString());
377: assertTrue(s instanceof String);
378:
379: Comparable b = noOptimizeFalse() ? new Integer(1)
380: : "Hello, World!";
381: assertTrue(((Comparable) "Hello, World!").compareTo(b) == 0);
382: assertTrue(b.compareTo("Hello, World!") == 0);
383: assertTrue(((Comparable) "A").compareTo(b) < 0);
384: assertTrue(b.compareTo("A") > 0);
385: assertTrue(((Comparable) "Z").compareTo(b) > 0);
386: assertTrue(b.compareTo("Z") < 0);
387: assertTrue(b instanceof String);
388:
389: CharSequence c = noOptimizeFalse() ? new StringBuffer()
390: : "Hello, World!";
391: assertEquals('e', c.charAt(1));
392: assertEquals(13, c.length());
393: assertEquals("ello", c.subSequence(1, 5));
394: assertTrue(c instanceof String);
395: }
396:
397: public String toString() {
398: return "com.google.gwt.dev.jjs.test.MiscellaneousTest";
399: }
400:
401: }
|