1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3.internal.introspection;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21
22 import org.junit.jupiter.api.Test;
23
24
25
26
27 public class MethodKeyTest {
28
29 private static final Class<?>[] PRIMS = {
30 Boolean.TYPE,
31 Byte.TYPE,
32 Character.TYPE,
33 Double.TYPE,
34 Float.TYPE,
35 Integer.TYPE,
36 Long.TYPE,
37 Short.TYPE,
38 String.class,
39 java.util.Date.class
40 };
41
42 private static final Object[] ARGS = {
43 Boolean.TRUE,
44 (byte) 1,
45 '2',
46 4d,
47 8f,
48 16,
49 32L,
50 (short) 64,
51 "foobar",
52 new java.util.Date()
53 };
54
55 private static final String[] METHODS = {
56 "plus",
57 "minus",
58 "execute",
59 "activate",
60 "perform",
61 "apply",
62 "invoke",
63 "executeAction",
64 "activateAction",
65 "performAction",
66 "applyAction",
67 "invokeAction",
68 "executeFunctor",
69 "activateFunctor",
70 "performFunctor",
71 "applyFunctor",
72 "invokeFunctor",
73 "executeIt",
74 "activateIt",
75 "performIt",
76 "applyIt",
77 "invokeIt"
78 };
79
80 private static final java.util.Map< MethodKey, String> BY_KEY;
81
82 private static final java.util.Map<String, MethodKey> BY_STRING;
83
84 private static final MethodKey[] KEY_LIST;
85
86
87 static {
88 BY_KEY = new java.util.HashMap<>();
89 BY_STRING = new java.util.HashMap<>();
90 for (final String method : METHODS) {
91 for (final Class<?> value : PRIMS) {
92 final Class<?>[] arg0 = {value};
93 setUpKey(method, arg0);
94 for (final Class<?> aClass : PRIMS) {
95 final Class<?>[] arg1 = {value, aClass};
96 setUpKey(method, arg1);
97 for (final Class<?> prim : PRIMS) {
98 final Class<?>[] arg2 = {value, aClass, prim};
99 setUpKey(method, arg2);
100 }
101 }
102 }
103 }
104 KEY_LIST = BY_KEY.keySet().toArray(new MethodKey[0]);
105 }
106
107 private static final int LOOP = 3;
108
109
110 private static void setUpKey(final String name, final Class<?>[] parms) {
111 final MethodKey key = new MethodKey(name, parms);
112 final String str = key.toString();
113 BY_KEY.put(key, str);
114 BY_STRING.put(str, key);
115
116 }
117
118
119 void checkKey(final String method, final Class<?>... params) {
120 final MethodKey key = makeKey(method, params);
121 final String out = BY_KEY.get(key);
122 assertNotNull(out);
123 }
124
125
126 void checkStringKey(final String method, final Class<?>... params) {
127 final String key = makeStringKey(method, params);
128 final MethodKey out = BY_STRING.get(key);
129 assertNotNull(out);
130 }
131
132
133 MethodKey makeKey(final String method, final Class<?>... params) {
134 return new MethodKey(method, params);
135 }
136
137
138 String makeStringKey(final String method, final Class<?>... params) {
139 final StringBuilder builder = new StringBuilder(method);
140 for (final Class<?> param : params) {
141 builder.append(MethodKey.primitiveClass(param).getName());
142 }
143 return builder.toString();
144 }
145
146 @Test
147 public void testDebugString() throws Exception {
148 final MethodKey c = KEY_LIST[0];
149 final String str = c.debugString();
150 assertNotNull(str);
151 }
152
153 @Test
154 public void testObjectKey() throws Exception {
155 for (final MethodKey ctl : KEY_LIST) {
156 final MethodKey key = makeKey(ctl.getMethod(), ctl.getParameters());
157 final String out = BY_KEY.get(key);
158 assertNotNull(out);
159 assertEquals(ctl.toString(), out, () -> ctl.toString() + " != " + out);
160 }
161
162 }
163 @Test
164 public void testPerfKey() throws Exception {
165 for (int l = 0; l < LOOP; ++l) {
166 for (final MethodKey ctl : KEY_LIST) {
167 final MethodKey key = makeKey(ctl.getMethod(), ctl.getParameters());
168 final String out = BY_KEY.get(key);
169 assertNotNull(out);
170 }
171 }
172 }
173
174 @Test
175 public void testPerfKey2() throws Exception {
176 for (int l = 0; l < LOOP; ++l) {
177 for (final String method : METHODS) {
178 for (final Object value : ARGS) {
179 checkKey(method, value.getClass());
180 for (final Object o : ARGS) {
181 checkKey(method, value.getClass(), o.getClass());
182 for (final Object arg : ARGS) {
183 checkKey(method, value.getClass(), o.getClass(), arg.getClass());
184 }
185 }
186 }
187 }
188 }
189 }
190
191 @Test
192 public void testPerfString() throws Exception {
193 for (int l = 0; l < LOOP; ++l) {
194 for (final MethodKey ctl : KEY_LIST) {
195 final String key = makeStringKey(ctl.getMethod(), ctl.getParameters());
196 final MethodKey out = BY_STRING.get(key);
197 assertNotNull(out);
198 }
199 }
200 }
201
202 @Test
203 public void testPerfStringKey2() throws Exception {
204 for (int l = 0; l < LOOP; ++l) {
205 for (final String method : METHODS) {
206 for (final Object value : ARGS) {
207 checkStringKey(method, value.getClass());
208 for (final Object o : ARGS) {
209 checkStringKey(method, value.getClass(), o.getClass());
210 for (final Object arg : ARGS) {
211 checkStringKey(method, value.getClass(), o.getClass(), arg.getClass());
212 }
213 }
214 }
215 }
216 }
217 }
218
219 @Test
220 public void testStringKey() throws Exception {
221 for (final MethodKey ctl : KEY_LIST) {
222 final String key = makeStringKey(ctl.getMethod(), ctl.getParameters());
223 final MethodKey out = BY_STRING.get(key);
224 assertNotNull(out);
225 assertEquals(ctl, out, ctl.toString() + " != " + key);
226 }
227
228 }
229 }