jdk/test/java/util/logging/LoggerSubclass.java
author martin
Mon, 20 Apr 2009 21:57:01 -0700
changeset 2632 9779b9cbae42
parent 2630 1088f346d108
child 5506 202f599c92aa
permissions -rw-r--r--
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id Summary: Make j.u.l. thread id a copy of Thread's id, for small values of thread id. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2630
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
     1
/*
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
     2
 * Copyright 2009 Google, Inc.  All Rights Reserved.
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
     4
 *
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
     8
 *
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    13
 * accompanied this code).
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    14
 *
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    18
 *
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    21
 * have any questions.
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    22
 */
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    23
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    24
/*
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    25
 * @test
2632
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2630
diff changeset
    26
 * @bug 6830220 6278014
2630
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    27
 * @summary Test Logger subclasses
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    28
 */
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    29
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    30
import java.util.logging.Handler;
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    31
import java.util.logging.Level;
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    32
import java.util.logging.Logger;
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    33
import java.util.logging.LogRecord;
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    34
import java.util.concurrent.atomic.AtomicInteger;
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    35
import java.util.concurrent.atomic.AtomicLong;
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    36
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    37
public class LoggerSubclass {
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    38
    void test(String[] args) {
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    39
        final String name = "myLogger";
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    40
        final String message = "myMessage";
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    41
        final AtomicInteger getHandlerCount = new AtomicInteger(0);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    42
        final AtomicLong lastSequenceNumber = new AtomicLong(-1L);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    43
        final AtomicInteger lastThreadID = new AtomicInteger(-1);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    44
        final Logger logger = new Logger(name, null) {
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    45
            public Handler[] getHandlers() {
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    46
                getHandlerCount.getAndIncrement();
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    47
                return super.getHandlers();
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    48
            }};
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    49
        equal(logger.getName(), name);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    50
        equal(logger.getResourceBundle(), null);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    51
        equal(logger.getFilter(), null);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    52
        equal(logger.getLevel(), null);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    53
        check(logger.isLoggable(Level.WARNING));
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    54
        logger.addHandler(new Handler() {
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    55
            public void close() {}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    56
            public void flush() {}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    57
            public void publish(LogRecord l) {
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    58
                equal(l.getLoggerName(), name);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    59
                equal(l.getMessage(), message);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    60
                equal(l.getResourceBundle(), null);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    61
                equal(l.getSourceClassName(), "LoggerSubclass");
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    62
                equal(l.getSourceMethodName(), "test");
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    63
                equal(l.getThrown(), null);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    64
                equal(l.getLevel(), Level.WARNING);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    65
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    66
                if (lastSequenceNumber.get() != -1) {
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    67
                    equal(lastSequenceNumber.get() + 1,
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    68
                          l.getSequenceNumber());
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    69
                    equal(lastThreadID.get(),
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    70
                          l.getThreadID());
2632
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2630
diff changeset
    71
                    equal((int) Thread.currentThread().getId(),
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2630
diff changeset
    72
                          l.getThreadID());
2630
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    73
                }
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    74
                lastSequenceNumber.set(l.getSequenceNumber());
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    75
                lastThreadID.set(l.getThreadID());
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    76
            }});
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    77
        for (int i = 1; i < 4; i++) {
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    78
            logger.warning(message); // Should invoke getHandlers()
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    79
            equal(i, getHandlerCount.get());
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    80
        }
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    81
    }
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    82
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    83
    //--------------------- Infrastructure ---------------------------
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    84
    volatile int passed = 0, failed = 0;
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    85
    void pass() {passed++;}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    86
    void fail() {failed++; Thread.dumpStack();}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    87
    void fail(String msg) {System.err.println(msg); fail();}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    88
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    89
    void check(boolean cond) {if (cond) pass(); else fail();}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    90
    void equal(Object x, Object y) {
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    91
        if (x == null ? y == null : x.equals(y)) pass();
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    92
        else fail(x + " not equal to " + y);}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    93
    public static void main(String[] args) throws Throwable {
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    94
        try {new LoggerSubclass().instanceMain(args);}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    95
        catch (Throwable e) {throw e.getCause();}}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    96
    public void instanceMain(String[] args) throws Throwable {
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    97
        try {test(args);} catch (Throwable t) {unexpected(t);}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    98
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
    99
        if (failed > 0) throw new AssertionError("Some tests failed");}
1088f346d108 6830220: Logging issues due to regression from bug fix 6797480
martin
parents:
diff changeset
   100
}