test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java

Print this page




   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 /*
  25  * @test
  26  * @bug 7024172
  27  * @summary Test if proxy for PlatformLoggingMXBean is equivalent
  28  *          to proxy for LoggingMXBean
  29  *
  30  * @build LoggingMXBeanTest
  31  * @run main LoggingMXBeanTest
  32  */
  33 
  34 import java.lang.management.*;
  35 import javax.management.MBeanServer;
  36 import java.util.logging.*;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 
  40 public class LoggingMXBeanTest
  41 {
  42     static String LOGGER_NAME_1 = "com.sun.management.Logger";
  43     static String LOGGER_NAME_2 = "com.sun.management.Logger.Logger2";
  44     static String UNKNOWN_LOGGER_NAME = "com.sun.management.Unknown";
  45 







  46     public static void main(String[] argv) throws Exception {
  47         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  48         LoggingMXBean proxy =
  49             ManagementFactory.newPlatformMXBeanProxy(mbs,
  50                 LogManager.LOGGING_MXBEAN_NAME,
  51                 LoggingMXBean.class);
  52 
  53         // test LoggingMXBean proxy
  54         LoggingMXBeanTest p = new LoggingMXBeanTest(proxy);
  55 
  56         // check if the attributes implemented by PlatformLoggingMXBean
  57         // and LoggingMXBean return the same value
  58         PlatformLoggingMXBean mxbean =
  59             ManagementFactory.getPlatformMXBean(mbs, PlatformLoggingMXBean.class);
  60 
  61         checkAttributes(proxy, mxbean);
  62     }
  63 
  64     // same verification as in java/util/logging/LoggingMXBeanTest2
  65     public LoggingMXBeanTest(LoggingMXBean mbean) throws Exception {
  66 
  67         Logger logger1 = Logger.getLogger( LOGGER_NAME_1 );
  68         logger1.setLevel(Level.FINE);
  69         Logger logger2 = Logger.getLogger( LOGGER_NAME_2 );
  70         logger2.setLevel(null);
  71 
  72         /*
  73          *  Check for the existence of our new Loggers
  74          */
  75         System.out.println("Test Logger Name retrieval (getLoggerNames)");
  76         boolean log1 = false, log2 = false;
  77         List<String> loggers = mbean.getLoggerNames();
  78         if (loggers == null || loggers.size() < 2) {
  79             throw new RuntimeException(
  80                 "Could not Detect the presense of the new Loggers");
  81         }
  82 
  83         for (String logger : loggers) {
  84             if (logger.equals(LOGGER_NAME_1)) {
  85                 log1 = true;
  86                 System.out.println("  : Found new Logger : " + logger);
  87             }
  88             if (logger.equals(LOGGER_NAME_2)) {
  89                 log2 = true;


 190         System.out.println("  : Parent Logger for \"\" : " + p2);
 191         if (!p2.equals("")) {
 192             throw new RuntimeException(
 193                 "Expected parent for root logger \"\" = \"\"" +
 194                 " but got " + p2);
 195         }
 196         String p3 = mbean.getParentLoggerName(UNKNOWN_LOGGER_NAME);
 197         System.out.println("  : Parent Logger for unknown logger : " + p3);
 198         if (p3 != null) {
 199             throw new RuntimeException(
 200                 "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
 201                  " but got " + p3);
 202         }
 203     }
 204 
 205     private static void checkAttributes(LoggingMXBean mxbean1,
 206                                         PlatformLoggingMXBean mxbean2) {
 207         // verify logger names
 208         List<String> loggers1 = mxbean1.getLoggerNames();
 209         List<String> loggers2 = mxbean2.getLoggerNames();

 210         if (loggers1.size() != loggers2.size())
 211             throw new RuntimeException("LoggerNames: unmatched number of entries");
 212         List<String> loggers3 = new ArrayList<>(loggers1);
 213         loggers3.removeAll(loggers2);
 214         if (loggers3.size() != 0)
 215             throw new RuntimeException("LoggerNames: unmatched loggers");
 216 
 217         // verify logger's level  and parent
 218         for (String logger : loggers1) {
 219             if (!mxbean1.getLoggerLevel(logger)
 220                     .equals(mxbean2.getLoggerLevel(logger)))
 221                 throw new RuntimeException(
 222                     "LoggerLevel: unmatched level for " + logger);



 223             if (!mxbean1.getParentLoggerName(logger)
 224                     .equals(mxbean2.getParentLoggerName(logger)))
 225                 throw new RuntimeException(
 226                     "ParentLoggerName: unmatched parent logger's name for " + logger);
 227         }
 228     }
 229 }


   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 /*
  25  * @test
  26  * @bug 7024172 7067691
  27  * @summary Test if proxy for PlatformLoggingMXBean is equivalent
  28  *          to proxy for LoggingMXBean
  29  *
  30  * @build LoggingMXBeanTest
  31  * @run main LoggingMXBeanTest
  32  */
  33 
  34 import java.lang.management.*;
  35 import javax.management.MBeanServer;
  36 import java.util.logging.*;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 
  40 public class LoggingMXBeanTest
  41 {
  42     static String LOGGER_NAME_1 = "com.sun.management.Logger";
  43     static String LOGGER_NAME_2 = "com.sun.management.Logger.Logger2";
  44     static String UNKNOWN_LOGGER_NAME = "com.sun.management.Unknown";
  45 
  46     static LoggingMXBeanTest lmxbeantest;
  47 
  48     // These instance variables prevent premature logger garbage collection
  49     // See getLogger() weak reference warnings.
  50     Logger logger1;
  51     Logger logger2;
  52 
  53     public static void main(String[] argv) throws Exception {
  54         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  55         LoggingMXBean proxy =
  56             ManagementFactory.newPlatformMXBeanProxy(mbs,
  57                 LogManager.LOGGING_MXBEAN_NAME,
  58                 LoggingMXBean.class);
  59 
  60         // test LoggingMXBean proxy
  61         lmxbeantest = new LoggingMXBeanTest(proxy);
  62 
  63         // check if the attributes implemented by PlatformLoggingMXBean
  64         // and LoggingMXBean return the same value
  65         PlatformLoggingMXBean mxbean =
  66             ManagementFactory.getPlatformMXBean(mbs, PlatformLoggingMXBean.class);
  67 
  68         checkAttributes(proxy, mxbean);
  69     }
  70 
  71     // same verification as in java/util/logging/LoggingMXBeanTest2
  72     public LoggingMXBeanTest(LoggingMXBean mbean) throws Exception {
  73 
  74         logger1 = Logger.getLogger( LOGGER_NAME_1 );
  75         logger1.setLevel(Level.FINE);
  76         logger2 = Logger.getLogger( LOGGER_NAME_2 );
  77         logger2.setLevel(null);
  78 
  79         /*
  80          *  Check for the existence of our new Loggers
  81          */
  82         System.out.println("Test Logger Name retrieval (getLoggerNames)");
  83         boolean log1 = false, log2 = false;
  84         List<String> loggers = mbean.getLoggerNames();
  85         if (loggers == null || loggers.size() < 2) {
  86             throw new RuntimeException(
  87                 "Could not Detect the presense of the new Loggers");
  88         }
  89 
  90         for (String logger : loggers) {
  91             if (logger.equals(LOGGER_NAME_1)) {
  92                 log1 = true;
  93                 System.out.println("  : Found new Logger : " + logger);
  94             }
  95             if (logger.equals(LOGGER_NAME_2)) {
  96                 log2 = true;


 197         System.out.println("  : Parent Logger for \"\" : " + p2);
 198         if (!p2.equals("")) {
 199             throw new RuntimeException(
 200                 "Expected parent for root logger \"\" = \"\"" +
 201                 " but got " + p2);
 202         }
 203         String p3 = mbean.getParentLoggerName(UNKNOWN_LOGGER_NAME);
 204         System.out.println("  : Parent Logger for unknown logger : " + p3);
 205         if (p3 != null) {
 206             throw new RuntimeException(
 207                 "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
 208                  " but got " + p3);
 209         }
 210     }
 211 
 212     private static void checkAttributes(LoggingMXBean mxbean1,
 213                                         PlatformLoggingMXBean mxbean2) {
 214         // verify logger names
 215         List<String> loggers1 = mxbean1.getLoggerNames();
 216         List<String> loggers2 = mxbean2.getLoggerNames();
 217 
 218         if (loggers1.size() != loggers2.size())
 219             throw new RuntimeException("LoggerNames: unmatched number of entries");
 220         List<String> loggers3 = new ArrayList<>(loggers1);
 221         loggers3.removeAll(loggers2);
 222         if (loggers3.size() != 0)
 223             throw new RuntimeException("LoggerNames: unmatched loggers");
 224 
 225         // verify logger's level  and parent
 226         for (String logger : loggers1) {
 227             if (!mxbean1.getLoggerLevel(logger)
 228                     .equals(mxbean2.getLoggerLevel(logger)))
 229                 throw new RuntimeException(
 230                     "LoggerLevel: unmatched level for (" + logger
 231                     + ", " + mxbean1.getLoggerLevel(logger)
 232                     + ", " + mxbean2.getLoggerLevel(logger) + ")");
 233 
 234             if (!mxbean1.getParentLoggerName(logger)
 235                     .equals(mxbean2.getParentLoggerName(logger)))
 236                 throw new RuntimeException(
 237                     "ParentLoggerName: unmatched parent logger's name for " + logger);
 238         }
 239     }
 240 }