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  *
  40  * @modules java.management/javax.management.loading:open
  41  *
  42  * @run testng MLetInternalsTest
  43  */
  44 public class MLetInternalsTest {
  45     private final static String CONSTRUCT_PARAMETER = "constructParameter";
  46 
  47     private final static Map<String, Method> testedMethods = new HashMap<>();
  48 
  49     @BeforeClass
  50     public static void setupClass() {
  51         testedMethods.clear();
  52         try {
  53             Method m = MLet.class.getDeclaredMethod(
  54                     CONSTRUCT_PARAMETER,
  55                     String.class, String.class
  56             );
  57             m.setAccessible(true);
  58 
  59             testedMethods.put(CONSTRUCT_PARAMETER, m);
  60         } catch (Exception ex) {
  61             throw new Error(ex);
  62         }
  63     }
  64 
  65     private MLet mlet;
  66 
  67     @BeforeTest
  68     public void setupTest() {
  69         mlet = new MLet();
  70     }
  71 
  72     @Test
  73     public void testConstructParameter() throws Exception {
  74         assertEquals(constructParameter("120", "int"), 120);
  75         assertEquals(constructParameter("120", "java.lang.Integer"), Integer.valueOf(120));
  76         assertEquals(constructParameter("120", "long"), 120L);
  77         assertEquals(constructParameter("120", "java.lang.Long"), Long.valueOf(120));
  78         assertEquals(constructParameter("120.0", "float"), 120.0f);
  79         assertEquals(constructParameter("120.0", "java.lang.Float"), Float.valueOf(120.0f));
  80         assertEquals(constructParameter("120.0", "double"), 120.0d);
  81         assertEquals(constructParameter("120", "java.lang.Double"), Double.valueOf(120d));
  82         assertEquals(constructParameter("120", "java.lang.String"), "120");
  83         assertEquals(constructParameter("120", "byte"), (byte)120);
  84         assertEquals(constructParameter("120", "java.lang.Byte"), (byte)120);
  85         assertEquals(constructParameter("120", "short"), (short)120);
  86         assertEquals(constructParameter("120", "java.lang.Short"), (short)120);
  87         assertEquals(constructParameter("true", "boolean"), true);
  88         assertEquals(constructParameter("true", "java.lang.Boolean"), Boolean.valueOf(true));
  89     }
  90 
  91     private Object constructParameter(String param, String type) throws Exception {
  92         return testedMethods.get(CONSTRUCT_PARAMETER).invoke(mlet, param, type);
  93     }
  94 }