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.GenericClass;
021: import com.google.gwt.core.ext.typeinfo.test.MyCustomList;
022: import com.google.gwt.dev.util.log.PrintWriterTreeLogger;
023:
024: import java.io.Serializable;
025: import java.util.ArrayList;
026: import java.util.Arrays;
027: import java.util.Date;
028: import java.util.HashSet;
029: import java.util.List;
030: import java.util.Set;
031:
032: /**
033: * Tests for {@link JTypeParameter}.
034: */
035: public class JTypeParameterTest extends JDelegatingClassTypeTestBase {
036: private final boolean logToConsole = false;
037: private final ModuleContext moduleContext = new ModuleContext(
038: logToConsole ? new PrintWriterTreeLogger()
039: : TreeLogger.NULL,
040: "com.google.gwt.core.ext.typeinfo.TypeOracleTest");
041:
042: public JTypeParameterTest() throws UnableToCompleteException {
043: }
044:
045: @Override
046: public void testFindConstructor() {
047: }
048:
049: @Override
050: public void testFindNestedType() {
051: }
052:
053: @Override
054: public void testGetConstructors() {
055: }
056:
057: @Override
058: public void testGetEnclosingType() throws NotFoundException {
059: JTypeParameter testType = getTestType();
060: assertEquals(moduleContext.getOracle().getType(
061: GenericClass.class.getName()), testType
062: .getDeclaringClass());
063: }
064:
065: @Override
066: public void testGetName() throws NotFoundException {
067: assertEquals("T", getTestType().getName());
068: }
069:
070: @Override
071: public void testGetNestedType() {
072: }
073:
074: @Override
075: public void testGetNestedTypes() throws NotFoundException {
076: JTypeParameter testType = getTestType();
077:
078: assertTrue(Arrays.deepEquals(testType.getNestedTypes(),
079: testType.getFirstBound().getNestedTypes()));
080: }
081:
082: @Override
083: public void testGetOverridableMethods() throws NotFoundException {
084: JTypeParameter testType = getTestType();
085:
086: assertTrue(Arrays.deepEquals(testType.getOverridableMethods(),
087: testType.getFirstBound().getOverridableMethods()));
088: }
089:
090: /*
091: * Checks that all non-local subtypes of the type parameter,
092: * T extends Serializable & Comparable<T> are actually assignable to
093: * Serializable and the properly parameterized version of Comparable<T>.
094: */
095: @Override
096: public void testGetSubtypes() throws NotFoundException {
097:
098: TypeOracle oracle = moduleContext.getOracle();
099: JClassType testType = oracle.getType(MyCustomList.class
100: .getName());
101: JGenericType genericType = testType.isGenericType();
102: JTypeParameter[] typeParameters = genericType
103: .getTypeParameters();
104: JTypeParameter typeParameter = typeParameters[0];
105:
106: JClassType serializableType = oracle.getType(Serializable.class
107: .getCanonicalName());
108: JGenericType comparableType = (JGenericType) oracle
109: .getType(Comparable.class.getCanonicalName());
110: JClassType[] computedSubtypes = typeParameter.getSubtypes();
111:
112: for (JClassType computedSubtype : computedSubtypes) {
113: JParameterizedType parameterizedComparableType = oracle
114: .getParameterizedType(comparableType,
115: new JClassType[] { computedSubtype });
116: if (computedSubtype.isLocalType()) {
117: // Ignore local types.
118: continue;
119: }
120: assertTrue(computedSubtype.isAssignableTo(serializableType));
121: assertTrue(computedSubtype
122: .isAssignableTo(parameterizedComparableType));
123: }
124: }
125:
126: @Override
127: public void testIsAssignableFrom() throws NotFoundException {
128: JTypeParameter testType = getTestType();
129: assertTrue(testType.isAssignableFrom(moduleContext.getOracle()
130: .getJavaLangObject()));
131: }
132:
133: @Override
134: public void testIsAssignableTo() throws NotFoundException {
135: JTypeParameter testType = getTestType();
136: assertTrue(testType.isAssignableTo(moduleContext.getOracle()
137: .getJavaLangObject()));
138: }
139:
140: @Override
141: protected Substitution getSubstitution() {
142: return new Substitution() {
143: public JType getSubstitution(JType type) {
144: return type;
145: }
146: };
147: }
148:
149: /*
150: * NOTE: This method returns the type parameter T from the GenericClass<T>
151: * type.
152: */
153: @Override
154: protected JTypeParameter getTestType() throws NotFoundException {
155: TypeOracle oracle = moduleContext.getOracle();
156: JClassType testType = oracle.getType(GenericClass.class
157: .getName());
158: JGenericType genericTestType = testType.isGenericType();
159: assertNotNull(genericTestType);
160: JTypeParameter[] typeParameters = genericTestType
161: .getTypeParameters();
162: assertTrue(typeParameters.length > 0);
163: return typeParameters[0];
164: }
165: }
|