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 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
81 private static final java.util.Map< MethodKey, String> BY_KEY;
82
83
84 private static final java.util.Map<String, MethodKey> BY_STRING;
85
86
87 private static final MethodKey[] KEY_LIST;
88
89
90 static {
91 BY_KEY = new java.util.HashMap<>();
92 BY_STRING = new java.util.HashMap<>();
93 for (final String method : METHODS) {
94 for (final Class<?> value : PRIMS) {
95 final Class<?>[] arg0 = {value};
96 setUpKey(method, arg0);
97 for (final Class<?> aClass : PRIMS) {
98 final Class<?>[] arg1 = {value, aClass};
99 setUpKey(method, arg1);
100 for (final Class<?> prim : PRIMS) {
101 final Class<?>[] arg2 = {value, aClass, prim};
102 setUpKey(method, arg2);
103 }
104 }
105 }
106 }
107 KEY_LIST = BY_KEY.keySet().toArray(new MethodKey[0]);
108 }
109
110 private static final int LOOP = 3;
111
112
113 private static void setUpKey(final String name, final Class<?>[] parms) {
114 final MethodKey key = new MethodKey(name, parms);
115 final String str = key.toString();
116 BY_KEY.put(key, str);
117 BY_STRING.put(str, key);
118
119 }
120
121
122 void checkKey(final String method, final Class<?>... params) {
123 final MethodKey key = makeKey(method, params);
124 final String out = BY_KEY.get(key);
125 assertNotNull(out);
126 }
127
128
129 void checkStringKey(final String method, final Class<?>... params) {
130 final String key = makeStringKey(method, params);
131 final MethodKey out = BY_STRING.get(key);
132 assertNotNull(out);
133 }
134
135
136 MethodKey makeKey(final String method, final Class<?>... params) {
137 return new MethodKey(method, params);
138 }
139
140
141 String makeStringKey(final String method, final Class<?>... params) {
142 final StringBuilder builder = new StringBuilder(method);
143 for (final Class<?> param : params) {
144 builder.append(MethodKey.primitiveClass(param).getName());
145 }
146 return builder.toString();
147 }
148
149 @Test
150 void testDebugString() throws Exception {
151 final MethodKey c = KEY_LIST[0];
152 final String str = c.debugString();
153 assertNotNull(str);
154 }
155
156 @Test
157 void testObjectKey() throws Exception {
158 for (final MethodKey ctl : KEY_LIST) {
159 final MethodKey key = makeKey(ctl.getMethod(), ctl.getParameters());
160 final String out = BY_KEY.get(key);
161 assertNotNull(out);
162 assertEquals(ctl.toString(), out, () -> ctl.toString() + " != " + out);
163 }
164
165 }
166 @Test
167 void testPerfKey() throws Exception {
168 for (int l = 0; l < LOOP; ++l) {
169 for (final MethodKey ctl : KEY_LIST) {
170 final MethodKey key = makeKey(ctl.getMethod(), ctl.getParameters());
171 final String out = BY_KEY.get(key);
172 assertNotNull(out);
173 }
174 }
175 }
176
177 @Test
178 void testPerfKey2() throws Exception {
179 for (int l = 0; l < LOOP; ++l) {
180 for (final String method : METHODS) {
181 for (final Object value : ARGS) {
182 checkKey(method, value.getClass());
183 for (final Object o : ARGS) {
184 checkKey(method, value.getClass(), o.getClass());
185 for (final Object arg : ARGS) {
186 checkKey(method, value.getClass(), o.getClass(), arg.getClass());
187 }
188 }
189 }
190 }
191 }
192 }
193
194 @Test
195 void testPerfString() throws Exception {
196 for (int l = 0; l < LOOP; ++l) {
197 for (final MethodKey ctl : KEY_LIST) {
198 final String key = makeStringKey(ctl.getMethod(), ctl.getParameters());
199 final MethodKey out = BY_STRING.get(key);
200 assertNotNull(out);
201 }
202 }
203 }
204
205 @Test
206 void testPerfStringKey2() throws Exception {
207 for (int l = 0; l < LOOP; ++l) {
208 for (final String method : METHODS) {
209 for (final Object value : ARGS) {
210 checkStringKey(method, value.getClass());
211 for (final Object o : ARGS) {
212 checkStringKey(method, value.getClass(), o.getClass());
213 for (final Object arg : ARGS) {
214 checkStringKey(method, value.getClass(), o.getClass(), arg.getClass());
215 }
216 }
217 }
218 }
219 }
220 }
221
222 @Test
223 void testStringKey() throws Exception {
224 for (final MethodKey ctl : KEY_LIST) {
225 final String key = makeStringKey(ctl.getMethod(), ctl.getParameters());
226 final MethodKey out = BY_STRING.get(key);
227 assertNotNull(out);
228 assertEquals(ctl, out, ctl.toString() + " != " + key);
229 }
230
231 }
232 }