How to use GenericMetadataSupportTest class of org.mockito.internal.util.reflection package

Best Mockito code snippet using org.mockito.internal.util.reflection.GenericMetadataSupportTest

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/**
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.util.reflection;
6
7
8import java.io.Serializable;
9import java.lang.reflect.Method;
10import java.lang.reflect.Type;
11import java.util.ArrayList;
12import java.util.Collection;
13import java.util.Iterator;
14import java.util.List;
15import java.util.Map;
16import java.util.Set;
17import org.junit.Test;
18
19
20public class GenericMetadataSupportTest {
21    interface GenericsSelfReference<T extends GenericMetadataSupportTest.GenericsSelfReference<T>> {
22        T self();
23    }
24
25    interface UpperBoundedTypeWithClass<E extends Number & Comparable<E>> {
26        E get();
27    }
28
29    interface UpperBoundedTypeWithInterfaces<E extends Comparable<E> & Cloneable> {
30        E get();
31    }
32
33    interface ListOfNumbers extends List<Number> {}
34
35    interface AnotherListOfNumbers extends GenericMetadataSupportTest.ListOfNumbers {}
36
37    abstract class ListOfNumbersImpl implements GenericMetadataSupportTest.ListOfNumbers {}
38
39    abstract class AnotherListOfNumbersImpl extends GenericMetadataSupportTest.ListOfNumbersImpl {}
40
41    interface ListOfAnyNumbers<N extends Number & Cloneable> extends List<N> {}
42
43    interface GenericsNest<K extends Comparable<K> & Cloneable> extends Map<K, Set<Number>> {
44        Set<Number> remove(Object key);// override with fixed ParameterizedType
45
46
47        List<? super Integer> returning_wildcard_with_class_lower_bound();
48
49        List<? super K> returning_wildcard_with_typeVar_lower_bound();
50
51        List<? extends K> returning_wildcard_with_typeVar_upper_bound();
52
53        K returningK();
54
55        <O extends K> List<O> paramType_with_type_params();
56
57        <S, T extends S> T two_type_params();
58
59        <O extends K> O typeVar_with_type_params();
60    }
61
62    static class StringList extends ArrayList<String> {}
63
64    public interface TopInterface<T> {
65        T generic();
66    }
67
68    public interface MiddleInterface<T> extends GenericMetadataSupportTest.TopInterface<T> {}
69
70    public class OwningClassWithDeclaredUpperBounds<T extends List<String> & Comparable<String> & Cloneable> {
71        public abstract class AbstractInner implements GenericMetadataSupportTest.MiddleInterface<T> {}
72    }
73
74    public class OwningClassWithNoDeclaredUpperBounds<T> {
75        public abstract class AbstractInner implements GenericMetadataSupportTest.MiddleInterface<T> {}
76    }
77
78    @Test
79    public void typeVariable_of_self_type() {
80        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsSelfReference.class).resolveGenericReturnType(firstNamedMethod("self", GenericMetadataSupportTest.GenericsSelfReference.class));
81        assertThat(genericMetadata.rawType()).isEqualTo(GenericMetadataSupportTest.GenericsSelfReference.class);
82    }
83
84    @Test
85    public void can_get_raw_type_from_Class() {
86        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.ListOfAnyNumbers.class).rawType()).isEqualTo(GenericMetadataSupportTest.ListOfAnyNumbers.class);
87        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.ListOfNumbers.class).rawType()).isEqualTo(GenericMetadataSupportTest.ListOfNumbers.class);
88        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class).rawType()).isEqualTo(GenericMetadataSupportTest.GenericsNest.class);
89        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.StringList.class).rawType()).isEqualTo(GenericMetadataSupportTest.StringList.class);
90    }
91
92    @Test
93    public void can_get_raw_type_from_ParameterizedType() {
94        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.ListOfAnyNumbers.class.getGenericInterfaces()[0]).rawType()).isEqualTo(List.class);
95        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.ListOfNumbers.class.getGenericInterfaces()[0]).rawType()).isEqualTo(List.class);
96        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class.getGenericInterfaces()[0]).rawType()).isEqualTo(Map.class);
97        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.StringList.class.getGenericSuperclass()).rawType()).isEqualTo(ArrayList.class);
98    }
99
100    @Test
101    public void can_get_type_variables_from_Class() {
102        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class).actualTypeArguments().keySet()).hasSize(1).extracting("name").contains("K");
103        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.ListOfNumbers.class).actualTypeArguments().keySet()).isEmpty();
104        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.ListOfAnyNumbers.class).actualTypeArguments().keySet()).hasSize(1).extracting("name").contains("N");
105        assertThat(GenericMetadataSupport.inferFrom(Map.class).actualTypeArguments().keySet()).hasSize(2).extracting("name").contains("K", "V");
106        assertThat(GenericMetadataSupport.inferFrom(Serializable.class).actualTypeArguments().keySet()).isEmpty();
107        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.StringList.class).actualTypeArguments().keySet()).isEmpty();
108    }
109
110    @Test
111    public void can_resolve_type_variables_from_ancestors() throws Exception {
112        Method listGet = List.class.getMethod("get", int.class);
113        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.AnotherListOfNumbers.class).resolveGenericReturnType(listGet).rawType()).isEqualTo(Number.class);
114        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.AnotherListOfNumbersImpl.class).resolveGenericReturnType(listGet).rawType()).isEqualTo(Number.class);
115    }
116
117    @Test
118    public void can_get_type_variables_from_ParameterizedType() {
119        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class.getGenericInterfaces()[0]).actualTypeArguments().keySet()).hasSize(2).extracting("name").contains("K", "V");
120        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.ListOfAnyNumbers.class.getGenericInterfaces()[0]).actualTypeArguments().keySet()).hasSize(1).extracting("name").contains("E");
121        assertThat(GenericMetadataSupport.inferFrom(Integer.class.getGenericInterfaces()[0]).actualTypeArguments().keySet()).hasSize(1).extracting("name").contains("T");
122        assertThat(GenericMetadataSupport.inferFrom(StringBuilder.class.getGenericInterfaces()[0]).actualTypeArguments().keySet()).isEmpty();
123        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.StringList.class).actualTypeArguments().keySet()).isEmpty();
124    }
125
126    @Test
127    public void typeVariable_return_type_of____iterator____resolved_to_Iterator_and_type_argument_to_String() {
128        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.StringList.class).resolveGenericReturnType(firstNamedMethod("iterator", GenericMetadataSupportTest.StringList.class));
129        assertThat(genericMetadata.rawType()).isEqualTo(Iterator.class);
130        assertThat(genericMetadata.actualTypeArguments().values()).contains(String.class);
131    }
132
133    @Test
134    public void typeVariable_return_type_of____get____resolved_to_Set_and_type_argument_to_Number() {
135        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class).resolveGenericReturnType(firstNamedMethod("get", GenericMetadataSupportTest.GenericsNest.class));
136        assertThat(genericMetadata.rawType()).isEqualTo(Set.class);
137        assertThat(genericMetadata.actualTypeArguments().values()).contains(Number.class);
138    }
139
140    @Test
141    public void bounded_typeVariable_return_type_of____returningK____resolved_to_Comparable_and_with_BoundedType() {
142        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class).resolveGenericReturnType(firstNamedMethod("returningK", GenericMetadataSupportTest.GenericsNest.class));
143        assertThat(genericMetadata.rawType()).isEqualTo(Comparable.class);
144        GenericMetadataSupport extraInterface_0 = GenericMetadataSupport.inferFrom(genericMetadata.extraInterfaces().get(0));
145        assertThat(extraInterface_0.rawType()).isEqualTo(Cloneable.class);
146    }
147
148    @Test
149    public void fixed_ParamType_return_type_of____remove____resolved_to_Set_and_type_argument_to_Number() {
150        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class).resolveGenericReturnType(firstNamedMethod("remove", GenericMetadataSupportTest.GenericsNest.class));
151        assertThat(genericMetadata.rawType()).isEqualTo(Set.class);
152        assertThat(genericMetadata.actualTypeArguments().values()).contains(Number.class);
153    }
154
155    @Test
156    public void paramType_return_type_of____values____resolved_to_Collection_and_type_argument_to_Parameterized_Set() {
157        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class).resolveGenericReturnType(firstNamedMethod("values", GenericMetadataSupportTest.GenericsNest.class));
158        assertThat(genericMetadata.rawType()).isEqualTo(Collection.class);
159        GenericMetadataSupport fromTypeVariableE = GenericMetadataSupport.inferFrom(typeVariableValue(genericMetadata.actualTypeArguments(), "E"));
160        assertThat(fromTypeVariableE.rawType()).isEqualTo(Set.class);
161        assertThat(fromTypeVariableE.actualTypeArguments().values()).contains(Number.class);
162    }
163
164    @Test
165    public void paramType_with_type_parameters_return_type_of____paramType_with_type_params____resolved_to_Collection_and_type_argument_to_Parameterized_Set() {
166        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class).resolveGenericReturnType(firstNamedMethod("paramType_with_type_params", GenericMetadataSupportTest.GenericsNest.class));
167        assertThat(genericMetadata.rawType()).isEqualTo(List.class);
168        Type firstBoundOfE = ((GenericMetadataSupport.TypeVarBoundedType) (typeVariableValue(genericMetadata.actualTypeArguments(), "E"))).firstBound();
169        assertThat(GenericMetadataSupport.inferFrom(firstBoundOfE).rawType()).isEqualTo(Comparable.class);
170    }
171
172    @Test
173    public void typeVariable_with_type_parameters_return_type_of____typeVar_with_type_params____resolved_K_hence_to_Comparable_and_with_BoundedType() {
174        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class).resolveGenericReturnType(firstNamedMethod("typeVar_with_type_params", GenericMetadataSupportTest.GenericsNest.class));
175        assertThat(genericMetadata.rawType()).isEqualTo(Comparable.class);
176        GenericMetadataSupport extraInterface_0 = GenericMetadataSupport.inferFrom(genericMetadata.extraInterfaces().get(0));
177        assertThat(extraInterface_0.rawType()).isEqualTo(Cloneable.class);
178    }
179
180    @Test
181    public void class_return_type_of____append____resolved_to_StringBuilder_and_type_arguments() {
182        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(StringBuilder.class).resolveGenericReturnType(firstNamedMethod("append", StringBuilder.class));
183        assertThat(genericMetadata.rawType()).isEqualTo(StringBuilder.class);
184        assertThat(genericMetadata.actualTypeArguments()).isEmpty();
185    }
186
187    @Test
188    public void paramType_with_wildcard_return_type_of____returning_wildcard_with_class_lower_bound____resolved_to_List_and_type_argument_to_Integer() {
189        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class).resolveGenericReturnType(firstNamedMethod("returning_wildcard_with_class_lower_bound", GenericMetadataSupportTest.GenericsNest.class));
190        assertThat(genericMetadata.rawType()).isEqualTo(List.class);
191        GenericMetadataSupport.BoundedType boundedType = ((GenericMetadataSupport.BoundedType) (typeVariableValue(genericMetadata.actualTypeArguments(), "E")));
192        assertThat(boundedType.firstBound()).isEqualTo(Integer.class);
193        assertThat(boundedType.interfaceBounds()).isEmpty();
194    }
195
196    @Test
197    public void paramType_with_wildcard_return_type_of____returning_wildcard_with_typeVar_lower_bound____resolved_to_List_and_type_argument_to_Integer() {
198        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class).resolveGenericReturnType(firstNamedMethod("returning_wildcard_with_typeVar_lower_bound", GenericMetadataSupportTest.GenericsNest.class));
199        assertThat(genericMetadata.rawType()).isEqualTo(List.class);
200        GenericMetadataSupport.BoundedType boundedType = ((GenericMetadataSupport.BoundedType) (typeVariableValue(genericMetadata.actualTypeArguments(), "E")));
201        assertThat(GenericMetadataSupport.inferFrom(boundedType.firstBound()).rawType()).isEqualTo(Comparable.class);
202        assertThat(boundedType.interfaceBounds()).contains(Cloneable.class);
203    }
204
205    @Test
206    public void paramType_with_wildcard_return_type_of____returning_wildcard_with_typeVar_upper_bound____resolved_to_List_and_type_argument_to_Integer() {
207        GenericMetadataSupport genericMetadata = GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.GenericsNest.class).resolveGenericReturnType(firstNamedMethod("returning_wildcard_with_typeVar_upper_bound", GenericMetadataSupportTest.GenericsNest.class));
208        assertThat(genericMetadata.rawType()).isEqualTo(List.class);
209        GenericMetadataSupport.BoundedType boundedType = ((GenericMetadataSupport.BoundedType) (typeVariableValue(genericMetadata.actualTypeArguments(), "E")));
210        assertThat(GenericMetadataSupport.inferFrom(boundedType.firstBound()).rawType()).isEqualTo(Comparable.class);
211        assertThat(boundedType.interfaceBounds()).contains(Cloneable.class);
212    }
213
214    @Test
215    public void can_extract_raw_type_from_bounds_on_terminal_typeVariable() {
216        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.OwningClassWithDeclaredUpperBounds.AbstractInner.class).resolveGenericReturnType(firstNamedMethod("generic", GenericMetadataSupportTest.OwningClassWithDeclaredUpperBounds.AbstractInner.class)).rawType()).isEqualTo(List.class);
217        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.OwningClassWithNoDeclaredUpperBounds.AbstractInner.class).resolveGenericReturnType(firstNamedMethod("generic", GenericMetadataSupportTest.OwningClassWithNoDeclaredUpperBounds.AbstractInner.class)).rawType()).isEqualTo(Object.class);
218    }
219
220    @Test
221    public void can_extract_interface_type_from_bounds_on_terminal_typeVariable() {
222        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.OwningClassWithDeclaredUpperBounds.AbstractInner.class).resolveGenericReturnType(firstNamedMethod("generic", GenericMetadataSupportTest.OwningClassWithDeclaredUpperBounds.AbstractInner.class)).rawExtraInterfaces()).containsExactly(Comparable.class, Cloneable.class);
223        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.OwningClassWithDeclaredUpperBounds.AbstractInner.class).resolveGenericReturnType(firstNamedMethod("generic", GenericMetadataSupportTest.OwningClassWithDeclaredUpperBounds.AbstractInner.class)).extraInterfaces()).containsExactly(parameterizedTypeOf(Comparable.class, null, String.class), Cloneable.class);
224        assertThat(GenericMetadataSupport.inferFrom(GenericMetadataSupportTest.OwningClassWithNoDeclaredUpperBounds.AbstractInner.class).resolveGenericReturnType(firstNamedMethod("generic", GenericMetadataSupportTest.OwningClassWithNoDeclaredUpperBounds.AbstractInner.class)).extraInterfaces()).isEmpty();
225    }
226}
227
228
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)