Paul Sandoz | 7dd146f | 2016-06-29 08:30:49 +0200 | [diff] [blame] | 1 | /* |
Paul Sandoz | 94d019e | 2017-01-04 17:20:41 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
Paul Sandoz | 7dd146f | 2016-06-29 08:30:49 +0200 | [diff] [blame] | 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 | * @run testng VarHandleTestAccessModeMethodNames |
Paul Sandoz | 94d019e | 2017-01-04 17:20:41 -0800 | [diff] [blame] | 27 | * @modules java.base/java.lang.invoke:open |
Paul Sandoz | 7dd146f | 2016-06-29 08:30:49 +0200 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | import org.testng.annotations.DataProvider; |
| 31 | import org.testng.annotations.Test; |
| 32 | |
| 33 | import java.lang.invoke.VarHandle; |
Paul Sandoz | 94d019e | 2017-01-04 17:20:41 -0800 | [diff] [blame] | 34 | import java.lang.reflect.Field; |
| 35 | import java.lang.reflect.Method; |
Paul Sandoz | 7dd146f | 2016-06-29 08:30:49 +0200 | [diff] [blame] | 36 | import java.util.stream.Stream; |
| 37 | |
| 38 | import static org.testng.Assert.assertEquals; |
| 39 | |
| 40 | public class VarHandleTestAccessModeMethodNames { |
| 41 | |
| 42 | @DataProvider |
| 43 | public static Object[][] accessModesProvider() { |
| 44 | return Stream.of(VarHandle.AccessMode.values()). |
| 45 | map(am -> new Object[]{am}). |
| 46 | toArray(Object[][]::new); |
| 47 | } |
| 48 | |
Paul Sandoz | 7dd146f | 2016-06-29 08:30:49 +0200 | [diff] [blame] | 49 | @Test(dataProvider = "accessModesProvider") |
| 50 | public void testMethodName(VarHandle.AccessMode am) { |
| 51 | assertEquals(am.methodName(), toMethodName(am.name())); |
| 52 | } |
| 53 | |
| 54 | private static String toMethodName(String name) { |
| 55 | StringBuilder s = new StringBuilder(name.toLowerCase()); |
| 56 | int i; |
| 57 | while ((i = s.indexOf("_")) != -1) { |
| 58 | s.deleteCharAt(i); |
| 59 | s.setCharAt(i, Character.toUpperCase(s.charAt(i))); |
| 60 | } |
| 61 | return s.toString(); |
| 62 | } |
Paul Sandoz | 94d019e | 2017-01-04 17:20:41 -0800 | [diff] [blame] | 63 | |
| 64 | |
| 65 | @Test(dataProvider = "accessModesProvider") |
| 66 | public void testReturnType(VarHandle.AccessMode am) throws Exception { |
| 67 | assertEquals(getReturnType(am.methodName()), getAccessModeReturnType(am)); |
| 68 | } |
| 69 | |
| 70 | private static Class<?> getReturnType(String name) throws Exception { |
| 71 | return VarHandle.class.getMethod(name, Object[].class).getReturnType(); |
| 72 | } |
| 73 | |
| 74 | private static Class<?> getAccessModeReturnType(VarHandle.AccessMode am) throws Exception { |
| 75 | Field field_am_at = VarHandle.AccessMode.class.getDeclaredField("at"); |
| 76 | field_am_at.setAccessible(true); |
| 77 | Field field_at_returnType = field_am_at.getType().getDeclaredField("returnType"); |
| 78 | field_at_returnType.setAccessible(true); |
| 79 | return (Class<?>) field_at_returnType.get(field_am_at.get(am)); |
| 80 | } |
Paul Sandoz | 7dd146f | 2016-06-29 08:30:49 +0200 | [diff] [blame] | 81 | } |