blob: b5d715238fe0d8765761181e4d4888ec80d4af9c [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
Gaurav Gupta36555072020-03-26 14:10:23 +05302 * Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved.
Vinay Vishal57171472018-09-18 20:22:00 +05303 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17package com.sun.s1asdev.ejb.util.methodmap.client;
18
19import com.sun.ejte.ccl.reporter.SimpleReporterAdapter;
20
21import com.sun.ejb.containers.util.MethodMap;
22import java.util.Map;
23import java.util.HashMap;
24import java.lang.reflect.Method;
25import java.util.Set;
26import java.util.Iterator;
27
28public class MethodMapTest {
29
30 private static SimpleReporterAdapter stat =
31 new SimpleReporterAdapter("appserv-tests");
32
33 private static final Class[] TEST_CLASSES = {
34 java.lang.Object.class,
35 MethodMapTest1.class, MethodMapTest2.class, MethodMapTest3.class,
36 MethodMapTest4.class, MethodMapTest3.class, MethodMapTest6.class,
37 MethodMapTest.class, javax.ejb.EJBHome.class,
38 javax.ejb.EJBObject.class, javax.ejb.EJBLocalObject.class,
39 javax.ejb.EJBLocalHome.class, javax.ejb.SessionBean.class,
40 javax.ejb.EnterpriseBean.class, java.util.Map.class,
Gaurav Gupta36555072020-03-26 14:10:23 +053041 jakarta.jms.QueueConnection.class, jakarta.jms.QueueSession.class,
42 jakarta.jms.Session.class, java.util.Date.class,
Vinay Vishal57171472018-09-18 20:22:00 +053043 javax.swing.Action.class, javax.swing.AbstractAction.class,
44 javax.swing.JComboBox.class, javax.swing.JTextArea.class
45
46 };
47
48 public static void main (String[] args) {
49
50 stat.addDescription("ejb-util-methodmap");
51 MethodMapTest test = new MethodMapTest(args);
52 test.doTest();
53 stat.printSummary("ejb-util-methodmapID");
54 }
55
56 public MethodMapTest (String[] args) {
57 }
58
59 public void doTest() {
60
61 for(int i = 0; i < TEST_CLASSES.length; i++) {
62 Class c = TEST_CLASSES[i];
63 System.out.println("Doing methodmap test for " + c);
64 boolean result = testClass(c);
65 if( result ) {
66 stat.addStatus("methodmapclient main", stat.PASS);
67 } else {
68 stat.addStatus("methodmapclient main", stat.FAIL);
69 }
70 }
71
72 testCtor();
73 testUnsupportedOperations();
74
75
76
77 }
78
79 private void testCtor() {
80
81 // negative bucket value is illegal
82 boolean ctorTest1Passed = false;
83 try {
84 new MethodMap(new HashMap(), -1);
85 System.out.println("bucketSize -1 should have thrown exception");
86 } catch(IllegalArgumentException e) {
87 ctorTest1Passed = true;
88 } catch(Exception e) {
89 e.printStackTrace();
90 }
91
92 // bucket value 0 is illegal
93 boolean ctorTest2Passed = false;
94 try {
95 new MethodMap(new HashMap(), 0);
96 System.out.println("bucketSize 0 should have thrown exception");
97 } catch(IllegalArgumentException e) {
98 ctorTest2Passed = true;
99 } catch(Exception e) {
100 e.printStackTrace();
101 }
102
103 // make sure empty hashmap doesn't cause problems
104 boolean ctorTest3Passed = false;
105 try {
106 new MethodMap(new HashMap());
107 ctorTest3Passed = true;
108 } catch(Exception e) {
109 e.printStackTrace();
110 }
111
112 // pass map containing something other than Method objects
113 Map otherMap = new HashMap();
114 otherMap.put("foo", "bar");
115 boolean ctorTest4Passed = false;
116 try {
117 new MethodMap(otherMap);
118 } catch(Exception e) {
119 ctorTest4Passed = true;
120 }
121
122 if( ctorTest1Passed && ctorTest2Passed & ctorTest3Passed
123 && ctorTest4Passed ) {
124 stat.addStatus("methodmapclient ctor", stat.PASS);
125 } else {
126 stat.addStatus("methodmapclient ctor", stat.FAIL);
127 }
128
129 }
130
131 private void testUnsupportedOperations() {
132 Map map = new HashMap();
133
134 Method[] methods = this.getClass().getDeclaredMethods();
135 Method method1 = methods[0];
136 Method method2 = methods[1];
137
138 map.put(method1, method1.toString());
139 Map methodMap = new MethodMap(map);
140
141 boolean uoeTest1Passed = false;
142 try {
143 methodMap.put(method1, method1.toString());
144 System.out.println("put should have failed");
145 } catch(UnsupportedOperationException uoe) {
146 uoeTest1Passed = true;
147 } catch(Exception e) {
148 e.printStackTrace();
149 }
150
151 boolean uoeTest2Passed = false;
152 try {
153 methodMap.putAll(map);
154 System.out.println("putAll should have failed");
155 } catch(UnsupportedOperationException uoe) {
156 uoeTest2Passed = true;
157 } catch(Exception e) {
158 e.printStackTrace();
159 }
160
161 boolean uoeTest3Passed = false;
162 try {
163 methodMap.remove(method1);
164 System.out.println("remove should have failed");
165 } catch(UnsupportedOperationException uoe) {
166 uoeTest3Passed = true;
167 } catch(Exception e) {
168 e.printStackTrace();
169 }
170
171 boolean uoeTest4Passed = false;
172 try {
173 map.put(null, "foo");
174 Map illegalMethodMap = new MethodMap(map);
175 System.out.println("MethodMap ctor should have failed b/c of " +
176 " null method value");
177 } catch(Exception e) {
178 uoeTest4Passed = true;
179 }
180
181 if( uoeTest1Passed && uoeTest2Passed && uoeTest3Passed &&
182 uoeTest4Passed ) {
183 stat.addStatus("methodmapclient unsupportedop", stat.PASS);
184 } else {
185 stat.addStatus("methodmapclient unsupportedop", stat.FAIL);
186 }
187
188 }
189
190 private boolean testClass(Class c) {
191
192 Map regularMap = new HashMap();
193
194 Method[] methods = c.getDeclaredMethods();
195
196 for(int i = 0; i < methods.length; i++) {
197 regularMap.put(methods[i], methods[i].toString());
198 }
199
200 Map defaultMethodMap = new MethodMap(regularMap);
201 System.out.println("Testing " + c + " with default MethodMap");
202 boolean defaultTest =
203 testClass(c, methods, false, regularMap, defaultMethodMap);
204 boolean defaultTestOpt =
205 testClass(c, methods, true, regularMap, defaultMethodMap);
206
207 // test degenerate case where there is only 1 bucket in MethodMap.
208 // unless there are less than 2 methods in the map, this should
209 // always defer to parent.
210 Map oneBucketMethodMap = new MethodMap(regularMap, 1);
211 System.out.println("Testing " + c + " with 1-bucket MethodMap");
212 boolean oneBucketTest =
213 testClass(c, methods, false, regularMap, oneBucketMethodMap);
214 boolean oneBucketTestOpt =
215 testClass(c, methods, true, regularMap, oneBucketMethodMap);
216
217 Map seventeenBucketMethodMap = new MethodMap(regularMap, 17);
218 System.out.println("Testing " + c + " with 17-bucket MethodMap");
219 boolean seventeenBucketTest =
220 testClass(c, methods, false, regularMap, seventeenBucketMethodMap);
221 boolean seventeenBucketTestOpt =
222 testClass(c, methods, true, regularMap, seventeenBucketMethodMap);
223
224
225 boolean clearTest = false;
226 try {
227 seventeenBucketMethodMap.clear();
228 if( methods.length > 0 ) {
229 Object obj = seventeenBucketMethodMap.get(methods[0]);
230 if( obj == null ) {
231 // make sure another clear op doesn't cause exception.
232 seventeenBucketMethodMap.clear();
233 clearTest = true;
234 } else {
235 System.out.println("Clear() should have returned null for " +
236 methods[0]
237 + " after clear(). instead it " +
238 " returned " + obj);
239 }
240 } else {
241 clearTest = true;
242 }
243 } catch(Exception e) {
244 clearTest = false;
245 System.out.println("clear() threw an exception");
246 e.printStackTrace();
247 }
248
249 return (defaultTest && defaultTestOpt &&
250 oneBucketTest && oneBucketTestOpt &&
251 seventeenBucketTest && seventeenBucketTestOpt &&
252 clearTest);
253 }
254
255 private boolean testClass(Class c, Method[] methods, boolean useParamOpt,
256 Map regularMap, Map methodMap) {
257
258 for(int i = 0; i < methods.length; i++) {
259 Method next = methods[i];
260 int numParams = next.getParameterTypes().length;
261 Object regularLookup = regularMap.get(next);
262
263 Object methodMapLookup = null;
264 if( useParamOpt ) {
265 MethodMap mm = (MethodMap) methodMap;
266 methodMapLookup = mm.get(next, numParams);
267 } else {
268 methodMapLookup = methodMap.get(next);
269 }
270
271 if( regularLookup != methodMapLookup ) {
272 System.out.println("Error. regularLookup = " + regularLookup
273 + " methodMapLookup = " + methodMapLookup
274 + " for Method " + next.toString());
275 return false;
276 }
277
278 // System.out.println("Got " + regularLookup + " for method " +
279 // next.toString());
280
281 }
282
283 return true;
284
285 }
286
287
288 private interface MethodMapTest1 {}
289
290 private interface MethodMapTest2 {
291 // overloadeds methods
292 void foo();
293 void foo(int i);
294
295 // short method names
296 void a();
297
298 // long method names
299 void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();
300
301 void ab();
302
303 // method that throws exception
304 void _ab() throws java.rmi.RemoteException;
305
306 // array params
307 int abc(int[] a);
308 int def(int[][][][][][][][] b);
309
310 // arbitrary unicode chars
311 void _B\u8001$();
312 void _CCMcx\u04e3\u05E90123();
313
314
315 }
316
317 private interface MethodMapTest3 {
318
319 void A();
320
321 }
322
323 private interface MethodMapTest4 extends MethodMapTest3 {}
324
325 private interface MethodMapTest5 extends MethodMapTest3 {
326 void A();
327 void B();
328 void B(String c);
329 }
330
331 private interface MethodMapTest6 extends MethodMapTest5 {
332 void B(String c);
333 }
334
335}