jdk/test/javax/management/openmbean/GenericMBeanExceptionTest.java
changeset 1712 8664f15a755b
equal deleted inserted replaced
1711:d9d3b0002726 1712:8664f15a755b
       
     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 6456269
       
    27  * @summary Test GenericMBeanException
       
    28  * @author Eamonn McManus
       
    29  */
       
    30 
       
    31 import java.beans.ConstructorProperties;
       
    32 import java.lang.management.ManagementFactory;
       
    33 import java.lang.reflect.InvocationTargetException;
       
    34 import java.lang.reflect.Method;
       
    35 import javax.management.GenericMBeanException;
       
    36 import javax.management.JMX;
       
    37 import javax.management.MBeanServer;
       
    38 import javax.management.MBeanServerConnection;
       
    39 import javax.management.ObjectName;
       
    40 import javax.management.openmbean.CompositeData;
       
    41 import javax.management.openmbean.CompositeType;
       
    42 import javax.management.openmbean.MXBeanMapping;
       
    43 import javax.management.openmbean.MXBeanMappingFactory;
       
    44 import javax.management.remote.JMXConnector;
       
    45 import javax.management.remote.JMXConnectorFactory;
       
    46 import javax.management.remote.JMXConnectorServer;
       
    47 import javax.management.remote.JMXConnectorServerFactory;
       
    48 import javax.management.remote.JMXServiceURL;
       
    49 
       
    50 public class GenericMBeanExceptionTest {
       
    51     private static volatile String failure = null;
       
    52 
       
    53     public static interface ThrowerMBean {
       
    54         public void throwGeneric() throws GenericMBeanException;
       
    55         public void throwGeneric(Throwable cause) throws GenericMBeanException;
       
    56         public void throwGeneric(String errorCode) throws GenericMBeanException;
       
    57         public void throwGeneric(CompositeData userData) throws GenericMBeanException;
       
    58         public void throwGeneric(String errorCode, CompositeData userData)
       
    59                 throws GenericMBeanException;
       
    60         public void throwGeneric(String errorCode, CompositeData userData, Throwable cause)
       
    61                 throws GenericMBeanException;
       
    62     }
       
    63 
       
    64     public static class Thrower implements ThrowerMBean {
       
    65 
       
    66         public void throwGeneric() throws GenericMBeanException {
       
    67             throw new GenericMBeanException("Message");
       
    68         }
       
    69 
       
    70         public void throwGeneric(Throwable cause) throws GenericMBeanException {
       
    71             throw new GenericMBeanException("Message", cause);
       
    72         }
       
    73 
       
    74         public void throwGeneric(String errorCode) throws GenericMBeanException {
       
    75             throw new GenericMBeanException("Message", errorCode, null);
       
    76         }
       
    77 
       
    78         public void throwGeneric(CompositeData userData) throws GenericMBeanException {
       
    79             throw new GenericMBeanException("Message", null, userData);
       
    80         }
       
    81 
       
    82         public void throwGeneric(String errorCode, CompositeData userData)
       
    83                 throws GenericMBeanException {
       
    84             throw new GenericMBeanException("Message", errorCode, userData);
       
    85         }
       
    86 
       
    87         public void throwGeneric(String errorCode, CompositeData userData,
       
    88                                  Throwable cause) throws GenericMBeanException {
       
    89             throw new GenericMBeanException("Message", errorCode, userData, cause);
       
    90         }
       
    91     }
       
    92 
       
    93     public static class Payload {
       
    94         private final int severity;
       
    95         private final String subsystem;
       
    96 
       
    97         @ConstructorProperties({"severity", "subsystem"})
       
    98         public Payload(int severity, String subsystem) {
       
    99             this.severity = severity;
       
   100             this.subsystem = subsystem;
       
   101         }
       
   102 
       
   103         public int getSeverity() {
       
   104             return severity;
       
   105         }
       
   106 
       
   107         public String getSubsystem() {
       
   108             return subsystem;
       
   109         }
       
   110 
       
   111         @Override
       
   112         public boolean equals(Object x) {
       
   113             if (!(x instanceof Payload))
       
   114                 return false;
       
   115             Payload p = (Payload) x;
       
   116             return (severity == p.severity &&
       
   117                     (subsystem == null) ?
       
   118                         p.subsystem == null : subsystem.equals(p.subsystem));
       
   119         }
       
   120 
       
   121         @Override
       
   122         public int hashCode() {
       
   123             return severity + subsystem.hashCode();
       
   124         }
       
   125 
       
   126         @Override
       
   127         public String toString() {
       
   128             return "Payload{severity: " + severity + ", subsystem: " + subsystem + "}";
       
   129         }
       
   130     }
       
   131 
       
   132     public static void main(String[] args) throws Exception {
       
   133         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
       
   134         ObjectName name = new ObjectName("test:type=Thrower");
       
   135         Thrower thrower = new Thrower();
       
   136         mbs.registerMBean(thrower, name);
       
   137 
       
   138         if (args.length > 0) {
       
   139             System.out.println("Attach client now, hit return to exit");
       
   140             System.in.read();
       
   141             return;
       
   142         }
       
   143 
       
   144         JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
       
   145         JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(
       
   146                 url, null, mbs);
       
   147         cs.start();
       
   148         JMXServiceURL addr = cs.getAddress();
       
   149 
       
   150         JMXConnector cc = JMXConnectorFactory.connect(addr);
       
   151         MBeanServerConnection mbsc = cc.getMBeanServerConnection();
       
   152 
       
   153         ThrowerMBean throwerProxy = JMX.newMBeanProxy(mbsc, name, ThrowerMBean.class);
       
   154 
       
   155         Payload payload = new Payload(5, "modular modulizer");
       
   156         MXBeanMapping payloadMapping = MXBeanMappingFactory.DEFAULT.mappingForType(
       
   157                 Payload.class, MXBeanMappingFactory.DEFAULT);
       
   158         CompositeData userData = (CompositeData)
       
   159                 payloadMapping.toOpenValue(payload);
       
   160         Throwable cause = new IllegalArgumentException("Badness");
       
   161 
       
   162         Object[][] testCases = {
       
   163             {},
       
   164             {"code1"},
       
   165             {userData},
       
   166             {"code2", userData},
       
   167             {(String) null, userData},
       
   168             {"code99", userData, cause},
       
   169             {(String) null, userData, cause},
       
   170         };
       
   171 
       
   172         for (Object[] testCase : testCases) {
       
   173             System.out.println("Test case: " + testCaseString(testCase));
       
   174 
       
   175             // Find which ThrowerMBean method it corresponds to
       
   176             Method testMethod = null;
       
   177 search:
       
   178             for (Method m : ThrowerMBean.class.getMethods()) {
       
   179                 Class<?>[] paramTypes = m.getParameterTypes();
       
   180                 if (paramTypes.length != testCase.length)
       
   181                     continue;
       
   182                 for (int i = 0; i < paramTypes.length; i++) {
       
   183                     if (testCase[i] != null && !paramTypes[i].isInstance(testCase[i]))
       
   184                         continue search;
       
   185                 }
       
   186                 testMethod = m;
       
   187             }
       
   188 
       
   189             if (testMethod == null) {
       
   190                 throw new Exception("TEST ERROR: no method corresponds: " +
       
   191                         testCaseString(testCase));
       
   192             }
       
   193 
       
   194             try {
       
   195                 testMethod.invoke(throwerProxy, testCase);
       
   196                 fail("Did not throw exception", testCase);
       
   197                 continue;
       
   198             } catch (InvocationTargetException e) {
       
   199                 Throwable iteCause = e.getCause();
       
   200                 if (!(iteCause instanceof GenericMBeanException)) {
       
   201                     iteCause.printStackTrace(System.out);
       
   202                     fail("Threw wrong exception " + iteCause, testCase);
       
   203                     continue;
       
   204                 }
       
   205                 GenericMBeanException ge = (GenericMBeanException) iteCause;
       
   206                 if (!ge.getMessage().equals("Message"))
       
   207                     fail("Wrong message: " + ge.getMessage(), testCase);
       
   208 
       
   209                 Class<?>[] paramTypes = testMethod.getParameterTypes();
       
   210                 for (int i = 0; i < paramTypes.length; i++) {
       
   211                     Class<?> paramType = paramTypes[i];
       
   212 
       
   213                     if (paramType == Throwable.class) { // cause
       
   214                         Throwable geCause = ge.getCause();
       
   215                         if (!(geCause instanceof IllegalArgumentException))
       
   216                             fail("Wrong cause: " + geCause, testCase);
       
   217                         else if (!geCause.getMessage().equals("Badness"))
       
   218                             fail("Wrong cause message: " + geCause.getMessage(), testCase);
       
   219                     } else if (paramType == String.class) { // errorCode
       
   220                         String errorCode = ge.getErrorCode();
       
   221                         String expectedErrorCode =
       
   222                                 (testCase[i] == null) ? "" : (String) testCase[i];
       
   223                         if (!expectedErrorCode.equals(errorCode))
       
   224                             fail("Wrong error code: " + ge.getErrorCode(), testCase);
       
   225                     } else if (paramType == CompositeData.class) { // userData
       
   226                         CompositeData userData2 = ge.getUserData();
       
   227                         if (!userData.equals(userData2))
       
   228                             fail("Wrong userData: " + userData2, testCase);
       
   229                         Payload payload2 = (Payload) payloadMapping.fromOpenValue(userData2);
       
   230                         if (!payload.equals(payload2))
       
   231                             fail("Wrong payload: " + payload2, testCase);
       
   232                     } else
       
   233                         throw new Exception("TEST ERROR: unknown parameter type: " + paramType);
       
   234                 }
       
   235             }
       
   236         }
       
   237 
       
   238         if (failure == null)
       
   239             System.out.println("TEST PASSED");
       
   240         else
       
   241             throw new Exception("TEST FAILED: " + failure);
       
   242     }
       
   243 
       
   244     private static String testCaseString(Object[] testCase) {
       
   245         StringBuilder sb = new StringBuilder("[");
       
   246         String sep = "";
       
   247         for (Object x : testCase) {
       
   248             sb.append(sep);
       
   249             String xs = (x instanceof CompositeData) ?
       
   250                 compositeDataString((CompositeData) x) : String.valueOf(x);
       
   251             sb.append(xs);
       
   252             sep = ", ";
       
   253         }
       
   254         sb.append("]");
       
   255         return sb.toString();
       
   256     }
       
   257 
       
   258     private static String compositeDataString(CompositeData cd) {
       
   259         StringBuilder sb = new StringBuilder("CompositeData{");
       
   260         CompositeType ct = cd.getCompositeType();
       
   261         String sep = "";
       
   262         for (String key : ct.keySet()) {
       
   263             sb.append(sep).append(key).append(": ").append(cd.get(key));
       
   264             sep = ", ";
       
   265         }
       
   266         sb.append("}");
       
   267         return sb.toString();
       
   268     }
       
   269 
       
   270     private static void fail(String why, Object[] testCase) {
       
   271         fail(testCaseString(testCase) + ": " + why);
       
   272     }
       
   273 
       
   274     private static void fail(String why) {
       
   275         failure = why;
       
   276         System.out.println("FAIL: " + why);
       
   277     }
       
   278 }