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.junit.client.GWTTestCase;
019:
020: /**
021: * Tests the web mode implementation of class literals.
022: */
023: public class ClassObjectTest extends GWTTestCase {
024:
025: private static enum Bar {
026: BAR, BAZ {
027: @Override
028: public String toString() {
029: return "BAZ!";
030: }
031: };
032: }
033:
034: private static class Foo implements IFoo {
035: }
036:
037: private static interface IFoo {
038: }
039:
040: private static void assertArrayEquals(Object[] expected,
041: Object[] actual) {
042: assertEquals(expected.length, actual.length);
043: for (int i = 0; i < expected.length; ++i) {
044: assertEquals(expected[i], actual[i]);
045: }
046: }
047:
048: public String getModuleName() {
049: return "com.google.gwt.dev.jjs.CompilerSuite";
050: }
051:
052: public void testArray() {
053: Object o = new Foo[3];
054: assertEquals(Foo[].class, o.getClass());
055: assertEquals(Object.class, o.getClass().getSuperclass());
056: assertEquals(
057: "[Lcom.google.gwt.dev.jjs.test.ClassObjectTest$Foo;", o
058: .getClass().getName());
059: assertEquals(
060: "class [Lcom.google.gwt.dev.jjs.test.ClassObjectTest$Foo;",
061: o.getClass().toString());
062: assertTrue(o.getClass().isArray());
063: assertFalse(o.getClass().isEnum());
064: assertFalse(o.getClass().isInterface());
065: assertFalse(o.getClass().isPrimitive());
066: assertNull(o.getClass().getEnumConstants());
067:
068: Foo[][] f = new Foo[3][3];
069: assertEquals(Foo[][].class, f.getClass());
070: assertEquals(Foo[].class, f[0].getClass());
071: }
072:
073: public void testClass() {
074: Object o = new Foo();
075: assertEquals(Foo.class, o.getClass());
076: assertEquals(Object.class, o.getClass().getSuperclass());
077: assertEquals("com.google.gwt.dev.jjs.test.ClassObjectTest$Foo",
078: Foo.class.getName());
079: assertEquals(
080: "class com.google.gwt.dev.jjs.test.ClassObjectTest$Foo",
081: Foo.class.toString());
082: assertFalse(Foo.class.isArray());
083: assertFalse(Foo.class.isEnum());
084: assertFalse(Foo.class.isInterface());
085: assertFalse(Foo.class.isPrimitive());
086: assertNull(o.getClass().getEnumConstants());
087: }
088:
089: public void testEnum() {
090: Object o = Bar.BAR;
091: assertEquals(Bar.class, o.getClass());
092: assertEquals(Enum.class, o.getClass().getSuperclass());
093: assertEquals("com.google.gwt.dev.jjs.test.ClassObjectTest$Bar",
094: o.getClass().getName());
095: assertEquals(
096: "class com.google.gwt.dev.jjs.test.ClassObjectTest$Bar",
097: o.getClass().toString());
098: assertFalse(o.getClass().isArray());
099: assertTrue(o.getClass().isEnum());
100: assertFalse(o.getClass().isInterface());
101: assertFalse(o.getClass().isPrimitive());
102: assertArrayEquals(Bar.values(), o.getClass().getEnumConstants());
103: }
104:
105: public void testEnumSubclass() {
106: Object o = Bar.BAZ;
107: assertNotSame(Bar.class, o.getClass());
108: assertEquals(Bar.class, o.getClass().getSuperclass());
109: /*
110: * TODO: implement
111: */
112: // assertEquals(Bar.class, o.getClass().getDeclaringClass());
113: assertTrue(o.getClass().getName().endsWith("$1"));
114: assertTrue(o.getClass().toString().endsWith("$1"));
115: assertFalse(o.getClass().isArray());
116: assertFalse(o.getClass().isEnum());
117: assertFalse(o.getClass().isInterface());
118: assertFalse(o.getClass().isPrimitive());
119: assertNull(o.getClass().getEnumConstants());
120: }
121:
122: public void testInterface() {
123: assertNull(IFoo.class.getSuperclass());
124: assertEquals(
125: "com.google.gwt.dev.jjs.test.ClassObjectTest$IFoo",
126: IFoo.class.getName());
127: assertEquals(
128: "interface com.google.gwt.dev.jjs.test.ClassObjectTest$IFoo",
129: IFoo.class.toString());
130: assertFalse(IFoo.class.isArray());
131: assertFalse(IFoo.class.isEnum());
132: assertTrue(IFoo.class.isInterface());
133: assertFalse(IFoo.class.isPrimitive());
134: assertNull(IFoo.class.getEnumConstants());
135: }
136:
137: public void testPrimitive() {
138: assertNull(int.class.getSuperclass());
139: assertEquals("int", int.class.getName());
140: assertEquals("int", int.class.toString());
141: assertFalse(int.class.isArray());
142: assertFalse(int.class.isEnum());
143: assertFalse(int.class.isInterface());
144: assertTrue(int.class.isPrimitive());
145: assertNull(int.class.getEnumConstants());
146: }
147:
148: }
|