KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > generic > TestGenericObjectType


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.ba.generic;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.bcel.generic.Type;
26
27 import edu.umd.cs.findbugs.ba.generic.GenericUtilities.TypeCategory;
28
29 import junit.framework.TestCase;
30
31 /**
32  * @author Nat Ayewah
33  */

34 public class TestGenericObjectType extends TestCase {
35
36     GenericObjectType obj;
37     String JavaDoc javaSignature;
38     String JavaDoc underlyingClass;
39     GenericUtilities.TypeCategory typeCategory;
40     
41     String JavaDoc variable;
42     Type extension;
43     List JavaDoc<Type> parameters;
44     
45     
46     public void initTest(
47             String JavaDoc bytecodeSignature,
48             String JavaDoc javaSignature,
49             String JavaDoc underlyingClass,
50             GenericUtilities.TypeCategory typeCategory,
51             String JavaDoc variable,
52             Type extension,
53             List JavaDoc<Type> parameters) {
54         this.obj = (GenericObjectType) GenericUtilities.getType(bytecodeSignature);
55         this.javaSignature = javaSignature;
56         this.underlyingClass = underlyingClass;
57         this.typeCategory = typeCategory;
58         this.variable = variable;
59         this.extension = extension;
60         this.parameters = parameters;
61     }
62
63     public void processTest() {
64         assertEquals(obj.toString(true), javaSignature);
65         assertEquals(obj.getClassName(), underlyingClass);
66         assertEquals(obj.getTypeCategory(), typeCategory);
67         
68         if (typeCategory == TypeCategory.PARAMETERS) {
69             assertTrue(obj.hasParameters());
70             assertTrue(obj.getNumParameters() == parameters.size());
71             for (int i=0; i<obj.getNumParameters(); i++)
72                 compareTypes(obj.getParameterAt(i), parameters.get(i));
73             assertNull(obj.getVariable());
74             assertNull(obj.getExtension());
75         }else if (typeCategory == TypeCategory.TYPE_VARIABLE) {
76             assertFalse(obj.hasParameters());
77             assertNull(obj.getExtension());
78             assertNotNull(obj.getVariable());
79             assertEquals(obj.getVariable(), variable);
80         }else if (typeCategory == TypeCategory.WILDCARD) {
81             assertFalse(obj.hasParameters());
82             assertNull(obj.getExtension());
83             assertNotNull(obj.getVariable());
84             assertEquals(obj.getVariable(), "*");
85         } else if (typeCategory == TypeCategory.WILDCARD_EXTENDS || typeCategory == TypeCategory.WILDCARD_SUPER) {
86             assertFalse(obj.hasParameters());
87             assertNotNull(obj.getExtension());
88             assertNotNull(obj.getVariable());
89             assertEquals(obj.getVariable(), variable);
90             compareTypes(obj.getExtension(), extension);
91         }
92     }
93
94     
95     private void compareTypes(Type a, Type b) {
96         assertEquals(a, b);
97         if (a instanceof GenericObjectType || b instanceof GenericObjectType) {
98             assertTrue(a instanceof GenericObjectType && b instanceof GenericObjectType);
99             assertEquals(
100                     ((GenericObjectType)a).toString(true),
101                     ((GenericObjectType)b).toString(true)
102                     );
103         }
104     }
105     
106     public void testParameterizedList() {
107         initTest(
108                 "Ljava/util/List<Ljava/lang/Comparable;>;",
109                 "java.util.List<java.lang.Comparable>",
110                 "java.util.List",
111                 GenericUtilities.TypeCategory.PARAMETERS,
112                 null,
113                 null,
114                 GenericUtilities.getTypes("Ljava/lang/Comparable;")
115             );
116         processTest();
117     }
118     
119     public void testCreateTypes() {
120         initTest(
121                 "LDummyClass<Ljava/lang/Comparable;TE;>;",
122                 "DummyClass<java.lang.Comparable,E>",
123                 "DummyClass",
124                 GenericUtilities.TypeCategory.PARAMETERS,
125                 null,
126                 null,
127                 Arrays.asList(
128                         GenericUtilities.getType("Ljava/lang/Comparable;"),
129                         GenericUtilities.getType("TE;")
130                 )
131             );
132         processTest();
133     }
134     
135     public void testTypeVariables() {
136         initTest(
137                 "TE;",
138                 "E",
139                 "java.lang.Object",
140                 GenericUtilities.TypeCategory.TYPE_VARIABLE,
141                 "E",
142                 null,
143                 null
144             );
145         processTest();
146         
147         initTest(
148                 "*",
149                 "?",
150                 "java.lang.Object",
151                 GenericUtilities.TypeCategory.WILDCARD,
152                 "*",
153                 null,
154                 null
155             );
156         processTest();
157
158         initTest(
159                 "+TE;",
160                 "? extends E",
161                 "java.lang.Object",
162                 GenericUtilities.TypeCategory.WILDCARD_EXTENDS,
163                 "+",
164                 GenericUtilities.getType("TE;"),
165                 null
166             );
167         processTest();
168
169         initTest(
170                 "-TE;",
171                 "? super E",
172                 "java.lang.Object",
173                 GenericUtilities.TypeCategory.WILDCARD_SUPER,
174                 "-",
175                 GenericUtilities.getType("TE;"),
176                 null
177             );
178         processTest();
179
180         initTest(
181                 "-[TE;",
182                 "? super E[]",
183                 "java.lang.Object",
184                 GenericUtilities.TypeCategory.WILDCARD_SUPER,
185                 "-",
186                 GenericUtilities.getType("[TE;"),
187                 null
188             );
189         processTest();
190
191     }
192 }
193
Popular Tags