test/jdk/java/util/logging/LoggingMXBeanTest2.java
author mikael
Mon, 29 Jul 2019 09:59:04 -0700
changeset 57584 9d82a35b6ff7
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14181
f723098d438c 7146552: java/util/logging/LoggingMXBeanTest.java failing intermittently
jgish
parents: 5506
diff changeset
     2
 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     5007165
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic Test for LoggingMXBean (direct access to MXBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @build LoggingMXBeanTest2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main LoggingMXBeanTest2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.logging.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.ListIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class LoggingMXBeanTest2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static LoggingMXBean mbean = LogManager.getLoggingMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static String LOGGER_NAME_1 = "com.sun.management.Logger";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static String LOGGER_NAME_2 = "com.sun.management.Logger.Logger2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static String UNKNOWN_LOGGER_NAME = "com.sun.management.Unknown";
14181
f723098d438c 7146552: java/util/logging/LoggingMXBeanTest.java failing intermittently
jgish
parents: 5506
diff changeset
    44
    static Logger logger1;
f723098d438c 7146552: java/util/logging/LoggingMXBeanTest.java failing intermittently
jgish
parents: 5506
diff changeset
    45
    static Logger logger2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public LoggingMXBeanTest2() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
14181
f723098d438c 7146552: java/util/logging/LoggingMXBeanTest.java failing intermittently
jgish
parents: 5506
diff changeset
    49
        logger1 = Logger.getLogger( LOGGER_NAME_1 );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        logger1.setLevel(Level.FINE);
14181
f723098d438c 7146552: java/util/logging/LoggingMXBeanTest.java failing intermittently
jgish
parents: 5506
diff changeset
    51
        logger2 = Logger.getLogger( LOGGER_NAME_2 );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        logger2.setLevel(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
         *  Check for the existence of our new Loggers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        System.out.println("Test Logger Name retrieval (getLoggerNames)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        boolean log1 = false, log2 = false;
14683
38921a46c62d 8003380: Compiler warnings in logging test code
jgish
parents: 14181
diff changeset
    59
        List<String> loggers = mbean.getLoggerNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (loggers == null || loggers.size() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                "Could not Detect the presense of the new Loggers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
14683
38921a46c62d 8003380: Compiler warnings in logging test code
jgish
parents: 14181
diff changeset
    65
        for (ListIterator<String> iter = loggers.listIterator(); iter.hasNext(); ) {
38921a46c62d 8003380: Compiler warnings in logging test code
jgish
parents: 14181
diff changeset
    66
            String logger = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (logger.equals(LOGGER_NAME_1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                log1 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                System.out.println("  : Found new Logger : " + logger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if (logger.equals(LOGGER_NAME_2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                log2 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                System.out.println("  : Found new Logger : " + logger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if ( log1 && log2 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            System.out.println("  : PASSED." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.out.println("  : FAILED.  Could not Detect the new Loggers." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                "Could not Detect the presense of the new Loggers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        System.out.println("Test getLoggerLevel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        String l1 = mbean.getLoggerLevel(LOGGER_NAME_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        System.out.println("  : Level for Logger " + LOGGER_NAME_1 + " : " + l1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (!l1.equals(Level.FINE.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                "Expected level for " + LOGGER_NAME_1 + " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                 Level.FINE.getName() + " but got " + l1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        String l2 = mbean.getLoggerLevel(LOGGER_NAME_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        System.out.println("  : Level for Logger " + LOGGER_NAME_2 + " : " + l2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (!l2.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                "Expected level for " + LOGGER_NAME_2 + " = \"\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                 " but got " + l2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        String l3 = mbean.getLoggerLevel(UNKNOWN_LOGGER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        System.out.println("  : Level for unknown logger : " + l3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (l3 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                 " but got " + l3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        System.out.println("Test setLoggerLevel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        mbean.setLoggerLevel(LOGGER_NAME_1, "INFO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: INFO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Level l = logger1.getLevel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (l != Level.INFO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                "Expected level for " + LOGGER_NAME_1 + " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                 Level.INFO + " but got " + l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        mbean.setLoggerLevel(LOGGER_NAME_2, "SEVERE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        System.out.println("  : Set Level for Logger " + LOGGER_NAME_2 + " to: SERVER");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        l = logger2.getLevel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (l != Level.SEVERE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                "Expected level for " + LOGGER_NAME_2 + " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                 Level.SEVERE+ " but got " + l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        mbean.setLoggerLevel(LOGGER_NAME_1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        l = logger1.getLevel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                "Expected level for " + LOGGER_NAME_1 + " = null " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                 " but got " + l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        boolean iaeCaught = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        System.out.println("  : Set Level for unknown Logger to: FINE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            mbean.setLoggerLevel(UNKNOWN_LOGGER_NAME, "FINE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            iaeCaught = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            System.out.println("      : IllegalArgumentException caught as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (!iaeCaught) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                "Expected IllegalArgumentException for setting level for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                UNKNOWN_LOGGER_NAME + " not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        iaeCaught = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: DUMMY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            mbean.setLoggerLevel(LOGGER_NAME_1, "DUMMY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            iaeCaught = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            System.out.println("      : IllegalArgumentException caught as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (!iaeCaught) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                "Expected IllegalArgumentException for invalid level.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        System.out.println("Test getParentLoggerName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        String p1 = mbean.getParentLoggerName(LOGGER_NAME_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        System.out.println("  : Parent Logger for " + LOGGER_NAME_2 + " : " + p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (!p1.equals(LOGGER_NAME_1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                "Expected parent for " + LOGGER_NAME_2 + " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                LOGGER_NAME_1 + " but got " + p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        String p2 = mbean.getParentLoggerName("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        System.out.println("  : Parent Logger for \"\" : " + p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (!p2.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                "Expected parent for root logger \"\" = \"\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                " but got " + p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        String p3 = mbean.getParentLoggerName(UNKNOWN_LOGGER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        System.out.println("  : Parent Logger for unknown logger : " + p3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (p3 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                 " but got " + p3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public static void main(String[] argv) throws Exception {
14683
38921a46c62d 8003380: Compiler warnings in logging test code
jgish
parents: 14181
diff changeset
   189
        new LoggingMXBeanTest2();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}