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.core.ext.typeinfo;
017:
018: import com.google.gwt.core.ext.TreeLogger;
019: import com.google.gwt.core.ext.UnableToCompleteException;
020: import com.google.gwt.core.ext.typeinfo.test.MyEnum;
021: import com.google.gwt.dev.cfg.ModuleDef;
022: import com.google.gwt.dev.cfg.ModuleDefLoader;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * Tests for {@link JEnumType}.
028: */
029: public class JEnumTypeTest extends TestCase {
030: private final TreeLogger logger = TreeLogger.NULL;
031:
032: private ModuleDef moduleDef;
033:
034: private final TypeOracle typeOracle;
035:
036: public JEnumTypeTest() throws UnableToCompleteException,
037: NotFoundException {
038: moduleDef = ModuleDefLoader.loadFromClassPath(logger,
039: "com.google.gwt.core.ext.typeinfo.TypeOracleTest");
040: typeOracle = moduleDef.getTypeOracle(logger);
041: }
042:
043: /**
044: * Test method for
045: * {@link com.google.gwt.core.ext.typeinfo.JEnumType#getEnumConstants()}.
046: *
047: * @throws NotFoundException
048: */
049: public void testGetEnumConstants() throws NotFoundException {
050: JClassType type = typeOracle.getType(MyEnum.class.getName());
051: JEnumType enumType = type.isEnum();
052: assertNotNull(enumType);
053:
054: JEnumConstant[] enumConstants = enumType.getEnumConstants();
055: assertEquals(3, enumConstants.length);
056:
057: for (JEnumConstant enumConstant : enumConstants) {
058: assertEquals(Integer.parseInt(enumConstant.getName()
059: .substring(3)), enumConstant.getOrdinal());
060: }
061: }
062:
063: /**
064: * Test method for
065: * {@link com.google.gwt.core.ext.typeinfo.JClassType#getFields()}.
066: *
067: * @throws NotFoundException
068: */
069: public void testGetFields() throws NotFoundException {
070: JClassType type = typeOracle.getType(MyEnum.class.getName());
071: JEnumType enumType = validateTypeIsEnum(type);
072:
073: assertEquals(5, enumType.getFields().length);
074: }
075:
076: /**
077: * Test method for
078: * {@link com.google.gwt.core.ext.typeinfo.JClassType#getField(String)}.
079: *
080: * @throws NotFoundException
081: */
082: public void testGetFieldString() throws NotFoundException {
083: JClassType type = typeOracle.getType(MyEnum.class.getName());
084:
085: validateTypeIsEnum(type);
086:
087: type.getField("VAL0");
088: type.getField("VAL1");
089: type.getField("VAL2");
090: type.getField("instanceField");
091: type.getField("e");
092: }
093:
094: /**
095: * Test method for {@link JEnumType#getConstructors()}
096: *
097: * @throws NotFoundException
098: * @throws NotFoundException
099: */
100: public void testGetConstructors() throws NotFoundException {
101: JClassType type = typeOracle.getType(MyEnum.class.getName());
102: JEnumType enumType = validateTypeIsEnum(type);
103:
104: assertEquals(1, enumType.getConstructors().length);
105: }
106:
107: /**
108: * Test method for {@link JEnumType#getMethods()}
109: *
110: * @throws NotFoundException
111: */
112: public void testGetMethods() throws NotFoundException {
113: JClassType type = typeOracle.getType(MyEnum.class.getName());
114: JEnumType enumType = validateTypeIsEnum(type);
115:
116: assertEquals(1, enumType.getMethods().length);
117: }
118:
119: /**
120: * Test method for {@link com.google.gwt.core.ext.typeinfo.JEnumType#isEnum()}.
121: *
122: * @throws NotFoundException
123: */
124: public void testIsEnum() throws NotFoundException {
125: JClassType type = typeOracle.getType(MyEnum.class.getName());
126: validateTypeIsEnum(type);
127: }
128:
129: private static JEnumType validateTypeIsEnum(JClassType type)
130: throws NotFoundException {
131: JEnumType maybeEnum = type.isEnum();
132: assertNotNull(maybeEnum);
133:
134: JClassType enumType = type.getOracle().getType(
135: Enum.class.getName());
136:
137: assertTrue(enumType.isAssignableFrom(maybeEnum));
138:
139: return maybeEnum;
140: }
141: }
|