View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package mockit.internal.util;
7   
8   import static org.junit.jupiter.api.Assertions.assertEquals;
9   import static org.junit.jupiter.api.Assertions.assertNotNull;
10  import static org.junit.jupiter.api.Assertions.assertNull;
11  import static org.junit.jupiter.api.Assertions.assertTrue;
12  
13  import java.lang.annotation.ElementType;
14  import java.lang.annotation.Retention;
15  import java.lang.annotation.RetentionPolicy;
16  import java.lang.annotation.Target;
17  import java.lang.reflect.GenericArrayType;
18  import java.lang.reflect.Method;
19  import java.lang.reflect.ParameterizedType;
20  import java.lang.reflect.Type;
21  import java.lang.reflect.TypeVariable;
22  import java.lang.reflect.WildcardType;
23  import java.security.CodeSource;
24  import java.util.Arrays;
25  import java.util.List;
26  
27  import org.junit.jupiter.api.Test;
28  
29  final class UtilitiesTest {
30  
31      @Retention(RetentionPolicy.RUNTIME)
32      @Target(ElementType.METHOD)
33      @interface TestAnnotation {
34      }
35  
36      static class AnnotatedClass {
37          @TestAnnotation
38          public void annotatedMethod() {
39          }
40  
41          public void unannotatedMethod() {
42          }
43  
44          @TestAnnotation
45          private void privateAnnotatedMethod() {
46          }
47      }
48  
49      @Test
50      void getAnnotatedMethodFindsPublicAnnotatedMethod() {
51          Method method = Utilities.getAnnotatedMethod(AnnotatedClass.class, TestAnnotation.class);
52          assertNotNull(method);
53          assertEquals("annotatedMethod", method.getName());
54      }
55  
56      @Test
57      void getAnnotatedMethodReturnsNullWhenNotFound() {
58          Method method = Utilities.getAnnotatedMethod(AnnotatedClass.class, Deprecated.class);
59          assertNull(method);
60      }
61  
62      @Test
63      void getAnnotatedDeclaredMethodFindsDeclaredAnnotatedMethod() {
64          Method method = Utilities.getAnnotatedDeclaredMethod(AnnotatedClass.class, TestAnnotation.class);
65          assertNotNull(method);
66          // Should find one of the annotated declared methods
67          assertNotNull(method.getAnnotation(TestAnnotation.class));
68      }
69  
70      @Test
71      void getAnnotatedDeclaredMethodReturnsNullWhenNotFound() {
72          Method method = Utilities.getAnnotatedDeclaredMethod(AnnotatedClass.class, Deprecated.class);
73          assertNull(method);
74      }
75  
76      @Test
77      void getClassTypeFromClass() {
78          Class<?> type = Utilities.getClassType(String.class);
79          assertEquals(String.class, type);
80      }
81  
82      @Test
83      void getClassTypeFromParameterizedType() throws NoSuchMethodException {
84          // Get a ParameterizedType from a method return type
85          Method method = AnnotatedWithGeneric.class.getMethod("getList");
86          Type genericReturnType = method.getGenericReturnType();
87          assertTrue(genericReturnType instanceof ParameterizedType);
88          Class<?> type = Utilities.getClassType(genericReturnType);
89          assertEquals(List.class, type);
90      }
91  
92      @Test
93      void getClassTypeFromGenericArrayType() throws NoSuchMethodException {
94          // Get a GenericArrayType
95          Method method = AnnotatedWithGeneric.class.getMethod("getArray");
96          Type genericReturnType = method.getGenericReturnType();
97          assertTrue(genericReturnType instanceof GenericArrayType);
98          Class<?> type = Utilities.getClassType(genericReturnType);
99          assertEquals(Object.class, type);
100     }
101 
102     @Test
103     void getClassTypeFromTypeVariable() throws NoSuchMethodException {
104         // Get a TypeVariable
105         Method method = AnnotatedWithGeneric.class.getMethod("getT");
106         Type genericReturnType = method.getGenericReturnType();
107         assertTrue(genericReturnType instanceof TypeVariable);
108         Class<?> type = Utilities.getClassType(genericReturnType);
109         assertNotNull(type);
110     }
111 
112     @Test
113     void getClassTypeFromWildcardType() throws NoSuchMethodException {
114         // Get a WildcardType from a bounded generic parameter
115         Method method = AnnotatedWithGeneric.class.getMethod("getWildcard");
116         Type genericReturnType = method.getGenericReturnType();
117         // The return type is List<? extends Number>
118         assertTrue(genericReturnType instanceof ParameterizedType);
119         Type wildcard = ((ParameterizedType) genericReturnType).getActualTypeArguments()[0];
120         assertTrue(wildcard instanceof WildcardType);
121         Class<?> type = Utilities.getClassType(wildcard);
122         assertEquals(Number.class, type);
123     }
124 
125     @Test
126     void containsReferenceReturnsTrueWhenFound() {
127         Object obj = new Object();
128         List<Object> list = Arrays.asList(new Object(), obj, new Object());
129         assertTrue(Utilities.containsReference(list, obj));
130     }
131 
132     @Test
133     void containsReferenceReturnsFalseWhenNotFound() {
134         Object obj = new Object();
135         List<Object> list = Arrays.asList(new Object(), new Object());
136         // containsReference uses == (identity), obj is not in list
137         boolean result = Utilities.containsReference(list, obj);
138         // We can't guarantee false for equal objects, but new instances should differ
139         assertEquals(false, result);
140     }
141 
142     @Test
143     void containsReferenceReturnsTrueForNullInList() {
144         List<Object> list = Arrays.asList(null, new Object());
145         assertTrue(Utilities.containsReference(list, null));
146     }
147 
148     @Test
149     void getClassFileLocationPathFromClass() {
150         // Use a class that has a code source (from a jar or classpath)
151         String path = Utilities.getClassFileLocationPath(UtilitiesTest.class);
152         assertNotNull(path);
153         assertFalse(path.isEmpty());
154     }
155 
156     @Test
157     void getClassFileLocationPathFromCodeSource() throws Exception {
158         CodeSource codeSource = UtilitiesTest.class.getProtectionDomain().getCodeSource();
159         if (codeSource != null) {
160             String path = Utilities.getClassFileLocationPath(codeSource);
161             assertNotNull(path);
162             assertFalse(path.isEmpty());
163         }
164     }
165 
166     static void assertFalse(boolean b) {
167         assertEquals(false, b);
168     }
169 
170     static boolean isEmpty(String s) {
171         return s == null || s.isEmpty();
172     }
173 
174     static boolean isEmpty(String[] arr) {
175         return arr == null || arr.length == 0;
176     }
177 }
178 
179 class AnnotatedWithGeneric<T> {
180     public List<String> getList() {
181         return null;
182     }
183 
184     public T[] getArray() {
185         return null;
186     }
187 
188     public T getT() {
189         return null;
190     }
191 
192     public List<? extends Number> getWildcard() {
193         return null;
194     }
195 }