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