jdk/test/javax/management/ObjectName/ValueOfTest.java
changeset 4159 9e3aae7675f1
parent 4158 0b4d21bc8b5c
parent 4156 acaa49a2768a
child 4160 bda0a85afcb7
equal deleted inserted replaced
4158:0b4d21bc8b5c 4159:9e3aae7675f1
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6734813
       
    27  * @summary Test the ObjectName.valueOf methods
       
    28  * @author Eamonn McManus
       
    29  */
       
    30 
       
    31 import java.lang.reflect.Constructor;
       
    32 import java.lang.reflect.InvocationTargetException;
       
    33 import java.lang.reflect.Method;
       
    34 import java.util.Arrays;
       
    35 import java.util.Hashtable;
       
    36 import javax.management.MalformedObjectNameException;
       
    37 import javax.management.ObjectName;
       
    38 
       
    39 public class ValueOfTest {
       
    40     public static void main(String[] args) throws Exception {
       
    41         // Calls that should work
       
    42         testPositive("d:foo=bar,baz=buh");
       
    43         testPositive("foo", "bar", "baz");
       
    44         Hashtable<String, String> h = new Hashtable<String, String>();
       
    45         h.put("foo", "bar");
       
    46         h.put("baz", "buh");
       
    47         testPositive("domain", h);
       
    48 
       
    49         // Calls that should not work
       
    50         testNegative("d");
       
    51         testNegative("d:");
       
    52         testNegative("d::foo=bar");
       
    53         testNegative("d:", "foo", "bar");
       
    54         testNegative("d", "foo=", "bar");
       
    55         testNegative("d:", h);
       
    56         testNegative("d", new Hashtable<String, String>());
       
    57     }
       
    58 
       
    59     private static void testPositive(Object... args) throws Exception {
       
    60         Method valueOf = valueOfMethod(args);
       
    61         Method getInstance = getInstanceMethod(args);
       
    62         Constructor<?> constructor = constructor(args);
       
    63 
       
    64         Object valueOfValue = valueOf.invoke(null, args);
       
    65         Object getInstanceValue = getInstance.invoke(null, args);
       
    66         Object constructorValue = constructor.newInstance(args);
       
    67 
       
    68         String argString =
       
    69                 Arrays.toString(args).replace('[', '(').replace(']', ')');
       
    70 
       
    71         if (!valueOfValue.equals(getInstanceValue)) {
       
    72             throw new Exception(
       
    73                     "valueOf" + argString + " differs from getInstance" +
       
    74                     argString);
       
    75         }
       
    76 
       
    77         if (!valueOfValue.equals(constructorValue)) {
       
    78             throw new Exception(
       
    79                     "valueOf" + argString + " differs from new ObjectName " +
       
    80                     argString);
       
    81         }
       
    82 
       
    83         System.out.println("OK: valueOf" + argString);
       
    84     }
       
    85 
       
    86     private static void testNegative(Object... args) throws Exception {
       
    87         Method valueOf = valueOfMethod(args);
       
    88         Method getInstance = getInstanceMethod(args);
       
    89 
       
    90         String argString =
       
    91                 Arrays.toString(args).replace('[', '(').replace(']', ')');
       
    92 
       
    93         final Throwable valueOfException;
       
    94         try {
       
    95             valueOf.invoke(null, args);
       
    96             throw new Exception("valueOf" + argString + " did not fail but should");
       
    97         } catch (InvocationTargetException e) {
       
    98             valueOfException = e.getCause();
       
    99         }
       
   100         if (!(valueOfException instanceof IllegalArgumentException)) {
       
   101             throw new Exception(
       
   102                     "valueOf" + argString + " threw " +
       
   103                     valueOfException.getClass().getName() + " instead of " +
       
   104                     "IllegalArgumentException", valueOfException);
       
   105         }
       
   106 
       
   107         final Throwable valueOfCause = valueOfException.getCause();
       
   108         if (!(valueOfCause instanceof MalformedObjectNameException)) {
       
   109             throw new Exception(
       
   110                     "valueOf" + argString + " threw exception with wrong " +
       
   111                     "type of cause", valueOfCause);
       
   112         }
       
   113 
       
   114         if (!valueOfException.getMessage().equals(valueOfCause.getMessage())) {
       
   115             // The IllegalArgumentException should have the same message as
       
   116             // the MalformedObjectNameException it wraps.
       
   117             // This isn't specified but is desirable.
       
   118             throw new Exception(
       
   119                     "valueOf" + argString + ": message in wrapping " +
       
   120                     "IllegalArgumentException (" + valueOfException.getMessage() +
       
   121                     ") differs from message in wrapped " +
       
   122                     "MalformedObjectNameException (" + valueOfCause.getMessage() +
       
   123                     ")");
       
   124         }
       
   125 
       
   126         final Throwable getInstanceException;
       
   127         try {
       
   128             getInstance.invoke(null, args);
       
   129             throw new Exception("getInstance" + argString + " did not fail but should");
       
   130         } catch (InvocationTargetException e) {
       
   131             getInstanceException = e.getCause();
       
   132         }
       
   133         if (!(getInstanceException instanceof MalformedObjectNameException)) {
       
   134             throw new Exception(
       
   135                     "getInstance" + argString + " threw wrong exception",
       
   136                     getInstanceException);
       
   137         }
       
   138 
       
   139         if (!valueOfException.getMessage().equals(getInstanceException.getMessage())) {
       
   140             // Again this is not specified.
       
   141             throw new Exception(
       
   142                     "Exception message from valueOf" + argString + " (" +
       
   143                     valueOfException.getMessage() + ") differs from message " +
       
   144                     "from getInstance" + argString + " (" +
       
   145                     getInstanceException.getMessage() + ")");
       
   146         }
       
   147 
       
   148         System.out.println("OK (correct exception): valueOf" + argString);
       
   149     }
       
   150 
       
   151     private static Method valueOfMethod(Object[] args) throws Exception {
       
   152         return method("valueOf", args);
       
   153     }
       
   154 
       
   155     private static Method getInstanceMethod(Object[] args) throws Exception {
       
   156         return method("getInstance", args);
       
   157     }
       
   158 
       
   159     private static Method method(String name, Object[] args) throws Exception {
       
   160         Class<?>[] argTypes = argTypes(args);
       
   161         return ObjectName.class.getMethod(name, argTypes);
       
   162     }
       
   163 
       
   164     private static Constructor<?> constructor(Object[] args) throws Exception {
       
   165         Class<?>[] argTypes = argTypes(args);
       
   166         return ObjectName.class.getConstructor(argTypes);
       
   167     }
       
   168 
       
   169     private static Class<?>[] argTypes(Object[] args) {
       
   170         Class<?>[] argTypes = new Class<?>[args.length];
       
   171         for (int i = 0; i < args.length; i++)
       
   172             argTypes[i] = args[i].getClass();
       
   173         return argTypes;
       
   174     }
       
   175 }