001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2006, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs.ba.generic;
021:
022: import java.util.Arrays;
023: import java.util.List;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.bcel.generic.ReferenceType;
028: import org.apache.bcel.generic.Type;
029:
030: import edu.umd.cs.findbugs.ba.generic.GenericUtilities.TypeCategory;
031:
032: /**
033: * @author Nat Ayewah
034: */
035: public class TestGenericObjectType extends TestCase {
036:
037: GenericObjectType obj;
038: String javaSignature;
039: String underlyingClass;
040: GenericUtilities.TypeCategory typeCategory;
041:
042: String variable;
043: Type extension;
044: List<ReferenceType> parameters;
045:
046: public void initTest(String bytecodeSignature,
047: String javaSignature, String underlyingClass,
048: GenericUtilities.TypeCategory typeCategory,
049: String variable, Type extension,
050: List<ReferenceType> parameters) {
051: this .obj = (GenericObjectType) GenericUtilities
052: .getType(bytecodeSignature);
053: this .javaSignature = javaSignature;
054: this .underlyingClass = underlyingClass;
055: this .typeCategory = typeCategory;
056: this .variable = variable;
057: this .extension = extension;
058: this .parameters = parameters;
059: }
060:
061: public void processTest() {
062: assertEquals(obj.toString(true), javaSignature);
063: assertEquals(obj.getClassName(), underlyingClass);
064: assertEquals(obj.getTypeCategory(), typeCategory);
065:
066: if (typeCategory == TypeCategory.PARAMETERIZED) {
067: assertTrue(obj.hasParameters());
068: assertTrue(obj.getNumParameters() == parameters.size());
069: for (int i = 0; i < obj.getNumParameters(); i++)
070: compareTypes(obj.getParameterAt(i), parameters.get(i));
071: assertNull(obj.getVariable());
072: assertNull(obj.getExtension());
073: } else if (typeCategory == TypeCategory.TYPE_VARIABLE) {
074: assertFalse(obj.hasParameters());
075: assertNull(obj.getExtension());
076: assertNotNull(obj.getVariable());
077: assertEquals(obj.getVariable(), variable);
078: } else if (typeCategory == TypeCategory.WILDCARD) {
079: assertFalse(obj.hasParameters());
080: assertNull(obj.getExtension());
081: assertNotNull(obj.getVariable());
082: assertEquals(obj.getVariable(), "*");
083: } else if (typeCategory == TypeCategory.WILDCARD_EXTENDS
084: || typeCategory == TypeCategory.WILDCARD_SUPER) {
085: assertFalse(obj.hasParameters());
086: assertNotNull(obj.getExtension());
087: assertNotNull(obj.getVariable());
088: assertEquals(obj.getVariable(), variable);
089: compareTypes(obj.getExtension(), extension);
090: }
091: }
092:
093: private void compareTypes(Type a, Type b) {
094: assertEquals(a, b);
095: if (a instanceof GenericObjectType
096: || b instanceof GenericObjectType) {
097: assertTrue(a instanceof GenericObjectType
098: && b instanceof GenericObjectType);
099: assertEquals(((GenericObjectType) a).toString(true),
100: ((GenericObjectType) b).toString(true));
101: }
102: }
103:
104: public void testParameterizedList() {
105: initTest("Ljava/util/List<Ljava/lang/Comparable;>;",
106: "java.util.List<java.lang.Comparable>",
107: "java.util.List",
108: GenericUtilities.TypeCategory.PARAMETERIZED, null,
109: null, GenericUtilities
110: .getTypes("Ljava/lang/Comparable;"));
111: processTest();
112: }
113:
114: public void testCreateTypes() {
115: initTest("LDummyClass<Ljava/lang/Comparable;TE;>;",
116: "DummyClass<java.lang.Comparable,E>", "DummyClass",
117: GenericUtilities.TypeCategory.PARAMETERIZED, null,
118: null, Arrays
119: .asList((ReferenceType) GenericUtilities
120: .getType("Ljava/lang/Comparable;"),
121: (ReferenceType) GenericUtilities
122: .getType("TE;")));
123: processTest();
124: }
125:
126: public void testTypeVariables() {
127: initTest("TE;", "E", "java.lang.Object",
128: GenericUtilities.TypeCategory.TYPE_VARIABLE, "E", null,
129: null);
130: processTest();
131:
132: initTest("*", "?", "java.lang.Object",
133: GenericUtilities.TypeCategory.WILDCARD, "*", null, null);
134: processTest();
135:
136: initTest("+TE;", "? extends E", "java.lang.Object",
137: GenericUtilities.TypeCategory.WILDCARD_EXTENDS, "+",
138: GenericUtilities.getType("TE;"), null);
139: processTest();
140:
141: initTest("-TE;", "? super E", "java.lang.Object",
142: GenericUtilities.TypeCategory.WILDCARD_SUPER, "-",
143: GenericUtilities.getType("TE;"), null);
144: processTest();
145:
146: initTest("-[TE;", "? super E[]", "java.lang.Object",
147: GenericUtilities.TypeCategory.WILDCARD_SUPER, "-",
148: GenericUtilities.getType("[TE;"), null);
149: processTest();
150:
151: }
152: }
|