ConvertWildcardTest.java revision 3da7534f2880068b90b88585c45be62751dbfd6b
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package signature.converter;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21
22import org.junit.Test;
23
24import signature.converter.util.AbstractConvertTest;
25import signature.converter.util.CompilationUnit;
26import signature.model.IApi;
27import signature.model.IClassDefinition;
28import signature.model.IClassReference;
29import signature.model.IField;
30import signature.model.IPackage;
31import signature.model.IParameterizedType;
32import signature.model.ITypeReference;
33import signature.model.IWildcardType;
34import signature.model.util.ModelUtil;
35
36import java.io.IOException;
37
38public abstract class ConvertWildcardTest extends AbstractConvertTest {
39
40    @Test
41    public void convertWildcardUpperBound() throws IOException {
42        String source =
43            "package a; " +
44            "public class A{" +
45            "  public java.util.Set<? extends Number> f; "+
46            "}";
47            IApi api = convert(new CompilationUnit("a.A", source));
48            IPackage sigPackage = ModelUtil.getPackage(api, "a");
49            IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
50            IField field = ModelUtil.getField(sigClass, "f");
51
52            ITypeReference type = field.getType();
53            assertTrue(type instanceof IParameterizedType);
54
55            IParameterizedType parametrizedType = (IParameterizedType)type;
56
57            IClassDefinition rawType = parametrizedType.getRawType().getClassDefinition();
58            assertEquals("Set", rawType.getName());
59
60            assertEquals(1, parametrizedType.getTypeArguments().size());
61            IWildcardType wildcardType = (IWildcardType) parametrizedType.getTypeArguments().get(0);
62            assertEquals(1, wildcardType.getUpperBounds().size());
63            ITypeReference upperBound = wildcardType.getUpperBounds().get(0);
64            assertTrue(upperBound instanceof IClassReference);
65
66            assertEquals("Number", ((IClassReference)upperBound).getClassDefinition().getName());
67    }
68
69    @Test
70    public void convertWildcardLowerBound() throws IOException {
71        String source =
72        "package a; " +
73        "public class A{" +
74        "  public java.util.Set<? super Number> f; "+
75        "}";
76        IApi api = convert(new CompilationUnit("a.A", source));
77        IPackage sigPackage = ModelUtil.getPackage(api, "a");
78        IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
79        IField field = ModelUtil.getField(sigClass, "f");
80
81        ITypeReference type = field.getType();
82        assertTrue(type instanceof IParameterizedType);
83
84        IParameterizedType parametrizedType = (IParameterizedType)type;
85
86        IClassDefinition rawType = parametrizedType.getRawType().getClassDefinition();
87        assertEquals("Set", rawType.getName());
88
89        assertEquals(1, parametrizedType.getTypeArguments().size());
90        IWildcardType wildcardType = (IWildcardType) parametrizedType.getTypeArguments().get(0);
91        assertEquals(1, wildcardType.getUpperBounds().size());
92        ITypeReference upperBound = wildcardType.getUpperBounds().get(0);
93        assertTrue(upperBound instanceof IClassReference);
94        assertEquals("Object", ((IClassReference)upperBound).getClassDefinition().getName());
95
96        ITypeReference lowerBound = wildcardType.getLowerBound();
97        assertTrue(lowerBound instanceof IClassReference);
98        assertEquals("Number", ((IClassReference)lowerBound).getClassDefinition().getName());
99    }
100}
101