1 /* 2 * Copyright (c) 2014, 2015, 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 import java.lang.reflect.Method; 25 import java.util.HashMap; 26 import java.util.Map; 27 import javax.management.loading.MLet; 28 import org.testng.annotations.Test; 29 import org.testng.annotations.BeforeClass; 30 import org.testng.annotations.BeforeTest; 31 32 import static org.testng.Assert.*; 33 34 /* 35 * @test 36 * @bug 8058089 37 * @summary Tests various internal functions provided by MLet for correctness 38 * @author Jaroslav Bachorik 39 * @modules java.management/javax.management.loading:open 40 * @run testng MLetInternalsTest 41 */ 42 public class MLetInternalsTest { 43 private final static String CONSTRUCT_PARAMETER = "constructParameter"; 44 45 private final static Map<String, Method> testedMethods = new HashMap<>(); 46 47 @BeforeClass 48 public static void setupClass() { 49 testedMethods.clear(); 50 try { 51 Method m = MLet.class.getDeclaredMethod( 52 CONSTRUCT_PARAMETER, 53 String.class, String.class 54 ); 55 m.setAccessible(true); 56 57 testedMethods.put(CONSTRUCT_PARAMETER, m); 58 } catch (Exception ex) { 59 throw new Error(ex); 60 } 61 } 62 63 private MLet mlet; 64 65 @BeforeTest 66 public void setupTest() { 67 mlet = new MLet(); 68 } 69 70 @Test 71 public void testConstructParameter() throws Exception { 72 assertEquals(constructParameter("120", "int"), 120); 73 assertEquals(constructParameter("120", "java.lang.Integer"), Integer.valueOf(120)); 74 assertEquals(constructParameter("120", "long"), 120L); 75 assertEquals(constructParameter("120", "java.lang.Long"), Long.valueOf(120)); 76 assertEquals(constructParameter("120.0", "float"), 120.0f); 77 assertEquals(constructParameter("120.0", "java.lang.Float"), Float.valueOf(120.0f)); 78 assertEquals(constructParameter("120.0", "double"), 120.0d); 79 assertEquals(constructParameter("120", "java.lang.Double"), Double.valueOf(120d)); 80 assertEquals(constructParameter("120", "java.lang.String"), "120"); 81 assertEquals(constructParameter("120", "byte"), (byte)120); 82 assertEquals(constructParameter("120", "java.lang.Byte"), (byte)120); 83 assertEquals(constructParameter("120", "short"), (short)120); 84 assertEquals(constructParameter("120", "java.lang.Short"), (short)120); 85 assertEquals(constructParameter("true", "boolean"), true); 86 assertEquals(constructParameter("true", "java.lang.Boolean"), Boolean.valueOf(true)); 87 } 88 89 private Object constructParameter(String param, String type) throws Exception { 90 return testedMethods.get(CONSTRUCT_PARAMETER).invoke(mlet, param, type); 91 } 92 }