View Javadoc
1   /*
2    * Copyright (C) 2013 the original author or 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 org.codehaus.gmavenplus.util;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  
24  /**
25   * Unit tests for the ReflectionUtils class.
26   *
27   * @author Keegan Witt
28   */
29  public class ReflectionUtilsTest {
30  
31      @Test
32      public void testHappyPaths() throws Exception {
33          String expectedString = "some string";
34          Object test1 = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(TestClass.class));
35          ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(TestClass.class, "setStringField", String.class), test1, expectedString);
36          assertEquals(expectedString, ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(TestClass.class, "getStringField"), test1));
37          assertEquals(TestClass.HELLO_WORLD, ReflectionUtils.invokeStaticMethod(ReflectionUtils.findMethod(TestClass.class, "helloWorld")));
38          assertEquals(TestClass.ENUM.VALUE, ReflectionUtils.getEnumValue(TestClass.ENUM.class, "VALUE"));
39          assertEquals(TestClass.HELLO_WORLD, ReflectionUtils.getStaticField(ReflectionUtils.findField(TestClass.class, "HELLO_WORLD", null)));
40          Object test2 = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(TestClass.class, String.class), expectedString);
41          assertEquals(expectedString, ReflectionUtils.getField(ReflectionUtils.findField(TestClass.class, "stringField", String.class), test2));
42      }
43  
44      @Test(expected = IllegalArgumentException.class)
45      public void testFindConstructorClassNull() {
46          ReflectionUtils.findConstructor(null);
47      }
48  
49      @Test(expected = IllegalArgumentException.class)
50      public void testFindConstructorNotFound() {
51          ReflectionUtils.findConstructor(TestClass.class, TestClass.class);
52      }
53  
54      @Test(expected = IllegalArgumentException.class)
55      public void testFindFieldClassNull() {
56          ReflectionUtils.findField(null, null, null);
57      }
58  
59      @Test(expected = IllegalArgumentException.class)
60      public void testFindFieldNameAndTypeNull() {
61          ReflectionUtils.findField(TestClass.class, null, null);
62      }
63  
64      @Test(expected = IllegalArgumentException.class)
65      public void testFindFieldNotFound() {
66          ReflectionUtils.findField(TestClass.class, "nonExistentField", null);
67      }
68  
69      @Test(expected = IllegalArgumentException.class)
70      public void testFindMethodClassNull() {
71          ReflectionUtils.findMethod(null, null);
72      }
73  
74      @Test(expected = IllegalArgumentException.class)
75      public void testFindMethodNameNull() {
76          ReflectionUtils.findMethod(TestClass.class, null);
77      }
78  
79      @Test(expected = IllegalArgumentException.class)
80      public void testFindMethodNotFound() {
81          ReflectionUtils.findMethod(TestClass.class, "nonExistentMethod");
82      }
83  
84      @Test(expected = IllegalArgumentException.class)
85      public void testGetEnumConstantNonEnumClass() {
86          ReflectionUtils.getEnumValue(TestClass.class, "VALUE");
87      }
88  
89      @Test(expected = IllegalArgumentException.class)
90      public void testGetEnumConstantValueNotFound() {
91          ReflectionUtils.getEnumValue(TestClass.ENUM.class, "NON_EXISTENT_VALUE");
92      }
93  
94      @Test(expected = IllegalArgumentException.class)
95      public void testGetStaticFieldNotStatic() throws Exception {
96          ReflectionUtils.getStaticField(ReflectionUtils.findField(TestClass.class, "stringField", String.class));
97      }
98  
99      @Test(expected = IllegalArgumentException.class)
100     public void testInvokeConstructorNull() throws Exception {
101         ReflectionUtils.invokeConstructor(null);
102     }
103 
104     @Test(expected = IllegalArgumentException.class)
105     public void testInvokeMethodMethodNull() throws Exception {
106         ReflectionUtils.invokeMethod(null, null);
107     }
108 
109     @Test(expected = IllegalArgumentException.class)
110     public void testInvokeMethodObjectNull() throws Exception {
111         ReflectionUtils.invokeMethod(TestClass.class.getMethod("getStringField"), null);
112     }
113 
114     @Test(expected = IllegalArgumentException.class)
115     public void testInvokeStaticMethodMethodNull() throws Exception {
116         ReflectionUtils.invokeStaticMethod(null);
117     }
118 
119     @Test(expected = IllegalArgumentException.class)
120     public void testInvokeStaticMethodMethodNotStatic() throws Exception {
121         ReflectionUtils.invokeStaticMethod(TestClass.class.getMethod("getStringField"));
122     }
123 
124     @Test
125     public void testConstructor() throws Exception {
126         ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(ReflectionUtils.class));
127     }
128 
129     public static class TestClass {
130         public static final String HELLO_WORLD = "Hello world!";
131         public String stringField;
132 
133         public TestClass() {
134         }
135 
136         public TestClass(String newStringField) {
137             stringField = newStringField;
138         }
139 
140         public String getStringField() {
141             return stringField;
142         }
143 
144         public void setStringField(String newStringField) {
145             stringField = newStringField;
146         }
147 
148         public static String helloWorld() {
149             return HELLO_WORLD;
150         }
151 
152         protected enum ENUM {
153             VALUE
154         }
155     }
156 
157 }