jdk/test/java/util/logging/LoggingMXBeanTest2.java
author jlahoda
Wed, 12 Dec 2012 20:26:56 +0100
changeset 14803 88347e495d34
parent 14181 f723098d438c
child 14683 38921a46c62d
permissions -rw-r--r--
8004504: ListBuffer could reuse List.nil() as the sentinel element Summary: ListBuffer.last now points to the last elements with client data, or null if none. Reviewed-by: jjg, mcimadamore
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static LoggingMXBean mbean = LogManager.getLoggingMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static String LOGGER_NAME_1 = "com.sun.management.Logger";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static String LOGGER_NAME_2 = "com.sun.management.Logger.Logger2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static String UNKNOWN_LOGGER_NAME = "com.sun.management.Unknown";
14181
f723098d438c 7146552: java/util/logging/LoggingMXBeanTest.java failing intermittently
jgish
parents: 5506
diff changeset
    45
    static Logger logger1;
f723098d438c 7146552: java/util/logging/LoggingMXBeanTest.java failing intermittently
jgish
parents: 5506
diff changeset
    46
    static Logger logger2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public LoggingMXBeanTest2() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
14181
f723098d438c 7146552: java/util/logging/LoggingMXBeanTest.java failing intermittently
jgish
parents: 5506
diff changeset
    50
        logger1 = Logger.getLogger( LOGGER_NAME_1 );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        logger1.setLevel(Level.FINE);
14181
f723098d438c 7146552: java/util/logging/LoggingMXBeanTest.java failing intermittently
jgish
parents: 5506
diff changeset
    52
        logger2 = Logger.getLogger( LOGGER_NAME_2 );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        logger2.setLevel(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         *  Check for the existence of our new Loggers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("Test Logger Name retrieval (getLoggerNames)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        boolean log1 = false, log2 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        List loggers = mbean.getLoggerNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (loggers == null || loggers.size() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                "Could not Detect the presense of the new Loggers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        for (ListIterator iter = loggers.listIterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            String logger = (String) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            if (logger.equals(LOGGER_NAME_1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                log1 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                System.out.println("  : Found new Logger : " + logger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (logger.equals(LOGGER_NAME_2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                log2 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                System.out.println("  : Found new Logger : " + logger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if ( log1 && log2 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.out.println("  : PASSED." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.out.println("  : FAILED.  Could not Detect the new Loggers." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                "Could not Detect the presense of the new Loggers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        System.out.println("Test getLoggerLevel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String l1 = mbean.getLoggerLevel(LOGGER_NAME_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        System.out.println("  : Level for Logger " + LOGGER_NAME_1 + " : " + l1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (!l1.equals(Level.FINE.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                "Expected level for " + LOGGER_NAME_1 + " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                 Level.FINE.getName() + " but got " + l1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        String l2 = mbean.getLoggerLevel(LOGGER_NAME_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.out.println("  : Level for Logger " + LOGGER_NAME_2 + " : " + l2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (!l2.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                "Expected level for " + LOGGER_NAME_2 + " = \"\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                 " but got " + l2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String l3 = mbean.getLoggerLevel(UNKNOWN_LOGGER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        System.out.println("  : Level for unknown logger : " + l3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (l3 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                 " but got " + l3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        System.out.println("Test setLoggerLevel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        mbean.setLoggerLevel(LOGGER_NAME_1, "INFO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: INFO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Level l = logger1.getLevel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (l != Level.INFO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                "Expected level for " + LOGGER_NAME_1 + " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                 Level.INFO + " but got " + l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        mbean.setLoggerLevel(LOGGER_NAME_2, "SEVERE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        System.out.println("  : Set Level for Logger " + LOGGER_NAME_2 + " to: SERVER");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        l = logger2.getLevel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (l != Level.SEVERE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                "Expected level for " + LOGGER_NAME_2 + " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                 Level.SEVERE+ " but got " + l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        mbean.setLoggerLevel(LOGGER_NAME_1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        l = logger1.getLevel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                "Expected level for " + LOGGER_NAME_1 + " = null " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                 " but got " + l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        boolean iaeCaught = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        System.out.println("  : Set Level for unknown Logger to: FINE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            mbean.setLoggerLevel(UNKNOWN_LOGGER_NAME, "FINE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            iaeCaught = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            System.out.println("      : IllegalArgumentException caught as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (!iaeCaught) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                "Expected IllegalArgumentException for setting level for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                UNKNOWN_LOGGER_NAME + " not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        iaeCaught = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: DUMMY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            mbean.setLoggerLevel(LOGGER_NAME_1, "DUMMY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            iaeCaught = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            System.out.println("      : IllegalArgumentException caught as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (!iaeCaught) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                "Expected IllegalArgumentException for invalid level.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        System.out.println("Test getParentLoggerName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        String p1 = mbean.getParentLoggerName(LOGGER_NAME_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        System.out.println("  : Parent Logger for " + LOGGER_NAME_2 + " : " + p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (!p1.equals(LOGGER_NAME_1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                "Expected parent for " + LOGGER_NAME_2 + " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                LOGGER_NAME_1 + " but got " + p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        String p2 = mbean.getParentLoggerName("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        System.out.println("  : Parent Logger for \"\" : " + p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (!p2.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                "Expected parent for root logger \"\" = \"\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                " but got " + p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        String p3 = mbean.getParentLoggerName(UNKNOWN_LOGGER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        System.out.println("  : Parent Logger for unknown logger : " + p3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (p3 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                 " but got " + p3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        LoggingMXBeanTest2 p = new LoggingMXBeanTest2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}