View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.commons.reflect;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.lang.reflect.*;
22  
23  import org.apache.juneau.*;
24  import org.junit.jupiter.api.*;
25  
26  class ReflectionUtils_Test extends TestBase {
27  
28  	public static class TestClass {
29  		public String field;
30  		public TestClass() {}
31  		public TestClass(String value) {}
32  		public void method() {}
33  		public void method(String param) {}
34  	}
35  
36  	//====================================================================================================
37  	// Constructor
38  	//====================================================================================================
39  	@Test
40  	void a000_constructor() {
41  		// Instantiate the class to cover the implicit constructor
42  		new ReflectionUtils();
43  	}
44  
45  	//====================================================================================================
46  	// info(Class<?>)
47  	//====================================================================================================
48  	@Test
49  	void a001_info_class() {
50  		// Non-null class
51  		ClassInfo ci = ReflectionUtils.info(TestClass.class);
52  		assertNotNull(ci);
53  		assertEquals(TestClass.class, ci.inner());
54  		
55  		// Null class - ClassInfo.of(null) throws IllegalArgumentException
56  		assertThrows(IllegalArgumentException.class, () -> ReflectionUtils.info((Class<?>)null));
57  	}
58  
59  	//====================================================================================================
60  	// info(Constructor<?>)
61  	//====================================================================================================
62  	@Test
63  	void a002_info_constructor() throws Exception {
64  		// Non-null constructor
65  		Constructor<?> ctor = TestClass.class.getConstructor();
66  		ConstructorInfo ci = ReflectionUtils.info(ctor);
67  		assertNotNull(ci);
68  		assertEquals(ctor, ci.inner());
69  		
70  		// Null constructor - ConstructorInfo.of(null) throws IllegalArgumentException
71  		assertThrows(IllegalArgumentException.class, () -> ReflectionUtils.info((Constructor<?>)null));
72  	}
73  
74  	//====================================================================================================
75  	// info(Field)
76  	//====================================================================================================
77  	@Test
78  	void a003_info_field() throws Exception {
79  		// Non-null field
80  		Field field = TestClass.class.getField("field");
81  		FieldInfo fi = ReflectionUtils.info(field);
82  		assertNotNull(fi);
83  		assertEquals(field, fi.inner());
84  		
85  		// Null field - FieldInfo.of(null) throws IllegalArgumentException
86  		assertThrows(IllegalArgumentException.class, () -> ReflectionUtils.info((Field)null));
87  	}
88  
89  	//====================================================================================================
90  	// info(Method)
91  	//====================================================================================================
92  	@Test
93  	void a004_info_method() throws Exception {
94  		// Non-null method
95  		Method method = TestClass.class.getMethod("method");
96  		MethodInfo mi = ReflectionUtils.info(method);
97  		assertNotNull(mi);
98  		assertEquals(method, mi.inner());
99  		
100 		// Null method - MethodInfo.of(null) throws IllegalArgumentException
101 		assertThrows(IllegalArgumentException.class, () -> ReflectionUtils.info((Method)null));
102 	}
103 
104 	//====================================================================================================
105 	// info(Object)
106 	//====================================================================================================
107 	@Test
108 	void a005_info_object() {
109 		// Non-null object
110 		TestClass obj = new TestClass();
111 		ClassInfo ci = ReflectionUtils.info(obj);
112 		assertNotNull(ci);
113 		assertEquals(TestClass.class, ci.inner());
114 		
115 		// Null object - ClassInfo.of(null) calls object.getClass() which throws NPE
116 		assertThrows(NullPointerException.class, () -> ReflectionUtils.info((Object)null));
117 		
118 		// Class object
119 		ClassInfo ciClass = ReflectionUtils.info(TestClass.class);
120 		assertNotNull(ciClass);
121 		assertEquals(TestClass.class, ciClass.inner());
122 	}
123 }
124