jdk/test/javax/management/context/LocalizableTest.java
changeset 4156 acaa49a2768a
parent 4155 460e37d40f12
child 4159 9e3aae7675f1
equal deleted inserted replaced
4155:460e37d40f12 4156:acaa49a2768a
     1 /*
       
     2  * Copyright 2007-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 LocalizableTest
       
    26  * @bug 5072267 6635499
       
    27  * @summary Test localizable MBeanInfo using LocalizableMBeanFactory.
       
    28  * @author Eamonn McManus
       
    29  */
       
    30 
       
    31 import java.lang.management.ManagementFactory;
       
    32 import java.util.Locale;
       
    33 import java.util.ResourceBundle;
       
    34 import javax.management.ClientContext;
       
    35 import javax.management.Description;
       
    36 import javax.management.JMX;
       
    37 import javax.management.MBeanAttributeInfo;
       
    38 import javax.management.MBeanConstructorInfo;
       
    39 import javax.management.MBeanInfo;
       
    40 import javax.management.MBeanOperationInfo;
       
    41 import javax.management.MBeanParameterInfo;
       
    42 import javax.management.MBeanServer;
       
    43 import javax.management.ObjectName;
       
    44 
       
    45 import localizable.MBeanDescriptions_fr;
       
    46 import localizable.Whatsit;
       
    47 
       
    48 import static localizable.WhatsitMBean.*;
       
    49 
       
    50 public class LocalizableTest {
       
    51     // If you change the order of the array elements or their number then
       
    52     // you must also change these constants.
       
    53     private static final int
       
    54             MBEAN = 0, ATTR = 1, OPER = 2, PARAM = 3, CONSTR = 4,
       
    55             CONSTR_PARAM = 5;
       
    56     private static final String[] englishDescriptions = {
       
    57         englishMBeanDescription, englishAttrDescription, englishOperDescription,
       
    58         englishParamDescription, englishConstrDescription,
       
    59         englishConstrParamDescription,
       
    60     };
       
    61     private static final String[] defaultDescriptions = englishDescriptions.clone();
       
    62     static {
       
    63         defaultDescriptions[MBEAN] = defaultMBeanDescription;
       
    64     }
       
    65     private static final String[] frenchDescriptions = {
       
    66         frenchMBeanDescription, frenchAttrDescription, frenchOperDescription,
       
    67         frenchParamDescription, frenchConstrDescription,
       
    68         frenchConstrParamDescription,
       
    69     };
       
    70 
       
    71     private static String failure;
       
    72 
       
    73     @Description(unlocalizedMBeanDescription)
       
    74     public static interface UnlocalizedMBean {}
       
    75     public static class Unlocalized implements UnlocalizedMBean {}
       
    76 
       
    77     public static void main(String[] args) throws Exception {
       
    78         ResourceBundle frenchBundle = new MBeanDescriptions_fr();
       
    79         // The purpose of the previous line is to force that class to be compiled
       
    80         // when the test is run so it will be available for reflection.
       
    81         // Yes, we could do this with a @build tag.
       
    82 
       
    83         MBeanServer plainMBS = ManagementFactory.getPlatformMBeanServer();
       
    84         MBeanServer unlocalizedMBS =
       
    85                 ClientContext.newContextForwarder(plainMBS, null);
       
    86         MBeanServer localizedMBS =
       
    87                 ClientContext.newLocalizeMBeanInfoForwarder(plainMBS);
       
    88         localizedMBS = ClientContext.newContextForwarder(localizedMBS, null);
       
    89         ObjectName name = new ObjectName("a:b=c");
       
    90 
       
    91         Whatsit whatsit = new Whatsit();
       
    92         Object[][] locales = {
       
    93             {null, englishDescriptions},
       
    94             {"en", englishDescriptions},
       
    95             {"fr", frenchDescriptions},
       
    96         };
       
    97 
       
    98         for (Object[] localePair : locales) {
       
    99             String locale = (String) localePair[0];
       
   100             String[] localizedDescriptions = (String[]) localePair[1];
       
   101             System.out.println("===Testing locale " + locale + "===");
       
   102             for (boolean localized : new boolean[] {false, true}) {
       
   103                 String[] descriptions = localized ?
       
   104                     localizedDescriptions : defaultDescriptions;
       
   105                 MBeanServer mbs = localized ? localizedMBS : unlocalizedMBS;
       
   106                 System.out.println("Testing MBean " + whatsit + " with " +
       
   107                         "localized=" + localized);
       
   108                 mbs.registerMBean(whatsit, name);
       
   109                 System.out.println(mbs.getMBeanInfo(name));
       
   110                 try {
       
   111                     test(mbs, name, locale, descriptions);
       
   112                 } catch (Exception e) {
       
   113                     fail("Caught exception: " + e);
       
   114                 } finally {
       
   115                     mbs.unregisterMBean(name);
       
   116                 }
       
   117             }
       
   118         }
       
   119 
       
   120         System.out.println("===Testing unlocalizable MBean===");
       
   121         Object mbean = new Unlocalized();
       
   122         localizedMBS.registerMBean(mbean, name);
       
   123         try {
       
   124             MBeanInfo mbi = localizedMBS.getMBeanInfo(name);
       
   125             assertEquals("MBean description", unlocalizedMBeanDescription,
       
   126                     mbi.getDescription());
       
   127         } finally {
       
   128             localizedMBS.unregisterMBean(name);
       
   129         }
       
   130 
       
   131         System.out.println("===Testing MBeanInfo.localizeDescriptions===");
       
   132         plainMBS.registerMBean(whatsit, name);
       
   133         MBeanInfo mbi = plainMBS.getMBeanInfo(name);
       
   134         Locale french = new Locale("fr");
       
   135         mbi = mbi.localizeDescriptions(french, whatsit.getClass().getClassLoader());
       
   136         checkDescriptions(mbi, frenchDescriptions);
       
   137 
       
   138         if (failure == null)
       
   139             System.out.println("TEST PASSED");
       
   140         else
       
   141             throw new Exception("TEST FAILED: Last failure: " + failure);
       
   142     }
       
   143 
       
   144     private static void test(MBeanServer mbs, ObjectName name, String locale,
       
   145                              String[] expectedDescriptions)
       
   146     throws Exception {
       
   147         if (locale != null)
       
   148             mbs = ClientContext.withLocale(mbs, new Locale(locale));
       
   149         MBeanInfo mbi = mbs.getMBeanInfo(name);
       
   150         checkDescriptions(mbi, expectedDescriptions);
       
   151     }
       
   152 
       
   153     private static void checkDescriptions(MBeanInfo mbi,
       
   154                                           String[] expectedDescriptions) {
       
   155         assertEquals("MBean description",
       
   156                      expectedDescriptions[MBEAN], mbi.getDescription());
       
   157         MBeanAttributeInfo mbai = mbi.getAttributes()[0];
       
   158         assertEquals("Attribute description",
       
   159                      expectedDescriptions[ATTR], mbai.getDescription());
       
   160         MBeanOperationInfo mboi = mbi.getOperations()[0];
       
   161         assertEquals("Operation description",
       
   162                      expectedDescriptions[OPER], mboi.getDescription());
       
   163         MBeanParameterInfo mbpi = mboi.getSignature()[0];
       
   164         assertEquals("Parameter description",
       
   165                      expectedDescriptions[PARAM], mbpi.getDescription());
       
   166         MBeanConstructorInfo[] mbcis = mbi.getConstructors();
       
   167         assertEquals("Number of constructors", 2, mbcis.length);
       
   168         for (MBeanConstructorInfo mbci : mbcis) {
       
   169             MBeanParameterInfo[] mbcpis = mbci.getSignature();
       
   170             String constrName = mbcpis.length + "-arg constructor";
       
   171             assertEquals(constrName + " description",
       
   172                     expectedDescriptions[CONSTR], mbci.getDescription());
       
   173             if (mbcpis.length > 0) {
       
   174                 assertEquals(constrName + " parameter description",
       
   175                         expectedDescriptions[CONSTR_PARAM],
       
   176                         mbcpis[0].getDescription());
       
   177             }
       
   178         }
       
   179     }
       
   180 
       
   181     private static void assertEquals(String what, Object expect, Object actual) {
       
   182         if (expect.equals(actual))
       
   183             System.out.println("...OK: " + what + " = " + expect);
       
   184         else
       
   185             fail(what + " should be " + expect + ", was " + actual);
       
   186     }
       
   187 
       
   188     private static void fail(String why) {
       
   189         System.out.println("FAIL: " + why);
       
   190         failure = why;
       
   191     }
       
   192 }