View Javadoc
1   /*
2    * Copyright (C) 2011 The Guava Authors
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  
17  package com.google.common.reflect;
18  
19  import com.google.common.testing.EqualsTester;
20  import com.google.common.testing.NullPointerTester;
21  
22  import junit.framework.TestCase;
23  
24  import java.lang.reflect.Method;
25  import java.lang.reflect.TypeVariable;
26  
27  /**
28   * Unit test for {@link TypeParameter}.
29   *
30   * @author Ben Yu
31   */
32  public class TypeParameterTest extends TestCase {
33  
34    public <T> void testCaptureTypeParameter() throws Exception {
35      TypeVariable<?> variable = new TypeParameter<T>() {}.typeVariable;
36      TypeVariable<?> expected = TypeParameterTest.class
37          .getDeclaredMethod("testCaptureTypeParameter")
38          .getTypeParameters()[0];
39      assertEquals(expected, variable);
40    }
41  
42    public void testConcreteTypeRejected() {
43      try {
44        new TypeParameter<String>() {};
45        fail();
46      } catch (IllegalArgumentException expected) {}
47    }
48  
49    public <A, B> void testEquals() throws Exception {
50      Method method = TypeParameterTest.class.getDeclaredMethod("testEquals");
51      new EqualsTester()
52          .addEqualityGroup(
53              new TypeParameter<A>() {}, new TypeParameter<A>() {})
54          .addEqualityGroup(new TypeParameter<B>() {})
55          .testEquals();
56    }
57  
58    public void testNullPointers() {
59      new NullPointerTester().testAllPublicStaticMethods(TypeParameter.class);
60    }
61  }