|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8277964 |
| 27 | + * @summary Test IllegalArgumentException be thrown when an argument is invalid |
| 28 | + * @run testng/othervm IllegalArgumentsTest |
| 29 | + */ |
| 30 | + |
| 31 | +import java.lang.reflect.Constructor; |
| 32 | +import java.lang.reflect.Method; |
| 33 | +import org.testng.annotations.Test; |
| 34 | + |
| 35 | +public class IllegalArgumentsTest { |
| 36 | + static class T { |
| 37 | + public T(int i) {} |
| 38 | + |
| 39 | + public static void m(int i) {} |
| 40 | + |
| 41 | + public void m1(String s) {} |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void wrongArgumentType() throws ReflectiveOperationException { |
| 46 | + for (int i = 0; i < 100_000; ++i) { |
| 47 | + try { |
| 48 | + Constructor<T> ctor = T.class.getConstructor(int.class); |
| 49 | + ctor.newInstance(int.class); // wrong argument type |
| 50 | + throw new RuntimeException("Expected IAE not thrown"); |
| 51 | + } catch (IllegalArgumentException e) {} |
| 52 | + } |
| 53 | + |
| 54 | + for (int i = 0; i < 100_000; ++i) { |
| 55 | + try { |
| 56 | + Method method = T.class.getMethod("m", int.class); |
| 57 | + method.invoke(null, int.class); // wrong argument type |
| 58 | + throw new RuntimeException("Expected IAE not thrown"); |
| 59 | + } catch (IllegalArgumentException e) {} |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void nullArguments() throws ReflectiveOperationException { |
| 65 | + for (int i = 0; i < 100_000; ++i) { |
| 66 | + try { |
| 67 | + Constructor<T> ctor = T.class.getConstructor(int.class); |
| 68 | + ctor.newInstance(new Object[] {null}); |
| 69 | + throw new RuntimeException("Expected IAE not thrown"); |
| 70 | + } catch (IllegalArgumentException e) {} |
| 71 | + } |
| 72 | + |
| 73 | + for (int i = 0; i < 100_000; ++i) { |
| 74 | + try { |
| 75 | + Method method = T.class.getMethod("m", int.class); |
| 76 | + method.invoke(null, new Object[] {null}); |
| 77 | + throw new RuntimeException("Expected IAE not thrown"); |
| 78 | + } catch (IllegalArgumentException e) {} |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void illegalArguments() throws ReflectiveOperationException { |
| 84 | + for (int i = 0; i < 100_000; ++i) { |
| 85 | + try { |
| 86 | + Constructor<T> ctor = T.class.getConstructor(int.class); |
| 87 | + ctor.newInstance(new Object[] { 10, 20}); |
| 88 | + throw new RuntimeException("Expected IAE not thrown"); |
| 89 | + } catch (IllegalArgumentException e) {} |
| 90 | + } |
| 91 | + |
| 92 | + for (int i = 0; i < 100_000; ++i) { |
| 93 | + try { |
| 94 | + Method method = T.class.getMethod("m", int.class); |
| 95 | + method.invoke(null, new Object[] { 10, 20}); |
| 96 | + throw new RuntimeException("Expected IAE not thrown"); |
| 97 | + } catch (IllegalArgumentException e) {} |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void wrongReceiver() throws ReflectiveOperationException { |
| 103 | + for (int i = 0; i < 100_000; ++i) { |
| 104 | + try { |
| 105 | + Method method = T.class.getMethod("m1", String.class); |
| 106 | + method.invoke(this, "bad receiver"); |
| 107 | + throw new RuntimeException("Expected IAE not thrown"); |
| 108 | + } catch (IllegalArgumentException e) {} |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments