jdk/test/sun/util/logging/PlatformLoggerTest.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 7026 9f2ec5fad124
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3861
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4181
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3861
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
     4
 *
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
     8
 *
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    13
 * accompanied this code).
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    14
 *
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4181
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4181
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4181
diff changeset
    21
 * questions.
3861
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    22
 */
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    23
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    24
/*
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    25
 * @test
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    26
 * @bug     6882376
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    27
 * @summary Test if java.util.logging.Logger is created before and after
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    28
 *          logging is enabled.  Also validate some basic PlatformLogger
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    29
 *          operations.
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    30
 *
4181
12101ac6820d 6896829: test/sun/util/logging/PlatformLoggerTest.java needs @compile tag to be compiled by jtreg
alanb
parents: 3861
diff changeset
    31
 * @compile -XDignore.symbol.file PlatformLoggerTest.java
3861
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    32
 * @run main PlatformLoggerTest
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    33
 */
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    34
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    35
import java.util.logging.*;
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    36
import sun.util.logging.PlatformLogger;
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    37
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    38
public class PlatformLoggerTest {
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    39
    private static final int defaultEffectiveLevel = 0;
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    41
        final String FOO_PLATFORM_LOGGER = "test.platformlogger.foo";
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    42
        final String BAR_PLATFORM_LOGGER = "test.platformlogger.bar";
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    43
        final String GOO_PLATFORM_LOGGER = "test.platformlogger.goo";
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    44
        final String BAR_LOGGER = "test.logger.bar";
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    45
        PlatformLogger goo = PlatformLogger.getLogger(GOO_PLATFORM_LOGGER);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    46
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    47
        // Create a platform logger using the default
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    48
        PlatformLogger foo = PlatformLogger.getLogger(FOO_PLATFORM_LOGGER);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    49
        checkPlatformLogger(foo, FOO_PLATFORM_LOGGER);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    50
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    51
        // create a java.util.logging.Logger
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    52
        // now java.util.logging.Logger should be created for each platform logger
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    53
        Logger logger = Logger.getLogger(BAR_LOGGER);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    54
        logger.setLevel(Level.WARNING);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    55
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    56
        PlatformLogger bar = PlatformLogger.getLogger(BAR_PLATFORM_LOGGER);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    57
        checkPlatformLogger(bar, BAR_PLATFORM_LOGGER);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    58
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    59
        checkLogger(FOO_PLATFORM_LOGGER, Level.FINER);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    60
        checkLogger(BAR_PLATFORM_LOGGER, Level.FINER);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    61
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    62
        checkLogger(GOO_PLATFORM_LOGGER, null);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    63
        checkLogger(BAR_LOGGER, Level.WARNING);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    64
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    65
        foo.setLevel(PlatformLogger.SEVERE);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    66
        checkLogger(FOO_PLATFORM_LOGGER, Level.SEVERE);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    67
    }
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    68
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    69
    private static void checkPlatformLogger(PlatformLogger logger, String name) {
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    70
        if (!logger.getName().equals(name)) {
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    71
            throw new RuntimeException("Invalid logger's name " +
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    72
                logger.getName() + " but expected " + name);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    73
        }
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    74
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    75
        if (logger.getLevel() != defaultEffectiveLevel) {
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    76
            throw new RuntimeException("Invalid default level for logger " +
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    77
                logger.getName());
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    78
        }
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    79
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    80
        if (logger.isLoggable(PlatformLogger.FINE) != false) {
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    81
            throw new RuntimeException("isLoggerable(FINE) returns true for logger " +
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    82
                logger.getName() + " but expected false");
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    83
        }
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    84
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    85
        logger.setLevel(PlatformLogger.FINER);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    86
        if (logger.getLevel() != Level.FINER.intValue()) {
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    87
            throw new RuntimeException("Invalid level for logger " +
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    88
                logger.getName() + " " + logger.getLevel());
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    89
        }
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    90
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    91
        if (logger.isLoggable(PlatformLogger.FINE) != true) {
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    92
            throw new RuntimeException("isLoggerable(FINE) returns false for logger " +
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    93
                logger.getName() + " but expected true");
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    94
        }
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    95
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    96
        logger.info("OK: Testing log message");
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    97
    }
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    98
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
    99
    private static void checkLogger(String name, Level level) {
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   100
        Logger logger = LogManager.getLogManager().getLogger(name);
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   101
        if (logger == null) {
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   102
            throw new RuntimeException("Logger " + name +
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   103
                " does not exist");
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   104
        }
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   105
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   106
        if (logger.getLevel() != level) {
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   107
            throw new RuntimeException("Invalid level for logger " +
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   108
                logger.getName() + " " + logger.getLevel());
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   109
        }
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   110
    }
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
diff changeset
   111
}