test/jdk/java/util/logging/LoggerSupplierAPIsTest.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:
15310
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 15310
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
15310
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
     4
 *
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
     8
 *
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    13
 * accompanied this code).
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    14
 *
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    18
 *
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    21
 * questions.
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    22
 */
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    23
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    24
/*
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    25
 * @test
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    26
 * @bug 8005263
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    27
 * @run testng LoggerSupplierAPIsTest
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    28
 */
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    29
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    30
import java.util.logging.Logger;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    31
import java.util.logging.Level;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    32
import java.util.logging.Handler;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    33
import java.util.logging.LogRecord;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    34
import java.util.function.Supplier;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    35
import java.util.concurrent.atomic.AtomicInteger;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    36
import java.util.ArrayList;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    37
import java.util.List;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    38
import java.util.Collections;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    39
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    40
import org.testng.annotations.Test;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    41
import static org.testng.Assert.assertEquals;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    42
import static org.testng.Assert.assertTrue;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    43
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    44
@Test(groups="unit")
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    45
public class LoggerSupplierAPIsTest {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    46
    static class CountingSupplier implements Supplier<String> {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    47
        AtomicInteger sno = new AtomicInteger();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    48
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    49
        @Override
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    50
        public String get() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    51
            return "Log message " + sno.getAndIncrement();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    52
        }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    53
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    54
        public int getCount() { return sno.get(); }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    55
        public void reset() { sno.set(0); }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    56
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    57
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    58
    static class CountingHandler extends Handler {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    59
        AtomicInteger count = new AtomicInteger();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    60
        ArrayList<LogRecord> ar = new ArrayList<>();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    61
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    62
        @Override
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    63
        public void close() { reset(); }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    64
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    65
        @Override
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    66
        public void flush() { reset(); }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    67
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    68
        @Override
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    69
        public void publish(LogRecord lr) {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    70
            // Can do some more assertion here?
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    71
            // System.out.println(lr.getMessage());
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    72
            count.incrementAndGet();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    73
        }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    74
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    75
        public int getCount() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    76
            return count.get();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    77
        }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    78
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    79
        public List<LogRecord> getLogs() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    80
            return Collections.unmodifiableList(ar);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    81
        }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    82
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    83
        public void reset() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    84
            count.set(0);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    85
            ar.clear();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    86
        }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    87
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    88
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    89
    static class HelperEx extends Exception {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    90
        final Level level;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    91
        HelperEx(Level l) { level = l; }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    92
        Level getLevel() { return level; }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    93
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    94
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    95
    static class SystemInfo {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    96
        public static String Java() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    97
            return "Java: " + System.getProperty("java.version") +
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    98
                   " installed at " + System.getProperty("java.home");
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
    99
        }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   100
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   101
        public static String OS() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   102
            return "OS: " + System.getProperty("os.name") +
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   103
                   " " + System.getProperty("os.version") +
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   104
                   " " + System.getProperty("os.arch");
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   105
        }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   106
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   107
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   108
    static final CountingSupplier supplier = new CountingSupplier();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   109
    static final CountingHandler handler = new CountingHandler();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   110
    static final Logger logger;
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   111
    static final Level[] levels = { Level.ALL, Level.OFF, Level.SEVERE,
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   112
                                    Level.WARNING, Level.INFO, Level.CONFIG,
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   113
                                    Level.FINE, Level.FINER, Level.FINEST };
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   114
    static final int[] invokes = { 7, 0, 1, 2, 3, 4, 5, 6, 7 };
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   115
    static final int[] log_count = { 10, 0, 1, 2, 3, 4, 6, 8, 10 };
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   116
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   117
    static {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   118
        logger = Logger.getLogger("LoggerSupplierApisTest");
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   119
        logger.setUseParentHandlers(false);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   120
        logger.addHandler(handler);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   121
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   122
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   123
    public void setup() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   124
        supplier.reset();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   125
        handler.reset();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   126
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   127
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   128
    private void testLog() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   129
        logger.log(Level.SEVERE, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   130
        logger.log(Level.WARNING, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   131
        logger.log(Level.INFO, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   132
        logger.log(Level.CONFIG, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   133
        logger.log(Level.FINE, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   134
        logger.log(Level.FINER, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   135
        logger.log(Level.FINEST, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   136
        // Lambda expression
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   137
        logger.log(Level.FINE, () ->
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   138
            "Timestamp: " + System.currentTimeMillis() +
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   139
            ", user home directory:  " + System.getProperty("user.home"));
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   140
        // Method reference
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   141
        logger.log(Level.FINER, SystemInfo::Java);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   142
        logger.log(Level.FINEST, SystemInfo::OS);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   143
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   144
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   145
    private void testLogThrown() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   146
        logger.log(Level.SEVERE, new HelperEx(Level.SEVERE), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   147
        logger.log(Level.WARNING, new HelperEx(Level.WARNING), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   148
        logger.log(Level.INFO, new HelperEx(Level.INFO), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   149
        logger.log(Level.CONFIG, new HelperEx(Level.CONFIG), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   150
        logger.log(Level.FINE, new HelperEx(Level.FINE), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   151
        logger.log(Level.FINER, new HelperEx(Level.FINER), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   152
        logger.log(Level.FINEST, new HelperEx(Level.FINEST), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   153
        // Lambda expression
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   154
        logger.log(Level.FINE, new HelperEx(Level.FINE), () ->
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   155
            "Timestamp: " + System.currentTimeMillis() +
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   156
            ", user home directory:  " + System.getProperty("user.home"));
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   157
        // Method reference
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   158
        logger.log(Level.FINER, new HelperEx(Level.FINER), SystemInfo::Java);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   159
        logger.log(Level.FINEST, new HelperEx(Level.FINEST), SystemInfo::OS);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   160
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   161
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   162
    private void testLogp() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   163
        final String cls = getClass().getName();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   164
        final String mtd = "testLogp";
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   165
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   166
        logger.logp(Level.SEVERE, cls, mtd, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   167
        logger.logp(Level.WARNING, cls, mtd, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   168
        logger.logp(Level.INFO, cls, mtd, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   169
        logger.logp(Level.CONFIG, cls, mtd, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   170
        logger.logp(Level.FINE, cls, mtd, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   171
        logger.logp(Level.FINER, cls, mtd, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   172
        logger.logp(Level.FINEST, cls, mtd, supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   173
        // Lambda expression
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   174
        logger.logp(Level.FINE, cls, mtd, () ->
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   175
            "Timestamp: " + System.currentTimeMillis() +
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   176
            ", user home directory:  " + System.getProperty("user.home"));
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   177
        // Method reference
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   178
        logger.logp(Level.FINER, cls, mtd, SystemInfo::Java);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   179
        logger.logp(Level.FINEST, cls, mtd, SystemInfo::OS);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   180
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   181
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   182
    private void testLogpThrown() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   183
        final String cls = getClass().getName();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   184
        final String mtd = "testLogpThrown";
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   185
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   186
        logger.logp(Level.SEVERE, cls, mtd, new HelperEx(Level.SEVERE), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   187
        logger.logp(Level.WARNING, cls, mtd, new HelperEx(Level.WARNING), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   188
        logger.logp(Level.INFO, cls, mtd, new HelperEx(Level.INFO), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   189
        logger.logp(Level.CONFIG, cls, mtd, new HelperEx(Level.CONFIG), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   190
        logger.logp(Level.FINE, cls, mtd, new HelperEx(Level.FINE), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   191
        logger.logp(Level.FINER, cls, mtd, new HelperEx(Level.FINER), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   192
        logger.logp(Level.FINEST, cls, mtd, new HelperEx(Level.FINEST), supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   193
        // Lambda expression
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   194
        logger.logp(Level.FINE, cls, mtd, new HelperEx(Level.FINE), () ->
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   195
            "Timestamp: " + System.currentTimeMillis() +
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   196
            ", user home directory:  " + System.getProperty("user.home"));
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   197
        // Method reference
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   198
        logger.logp(Level.FINER, cls, mtd, new HelperEx(Level.FINER), SystemInfo::Java);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   199
        logger.logp(Level.FINEST, cls, mtd, new HelperEx(Level.FINEST), SystemInfo::OS);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   200
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   201
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   202
    private void testLevelConvenientMethods() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   203
        logger.severe(supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   204
        logger.warning(supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   205
        logger.info(supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   206
        logger.config(supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   207
        logger.fine(supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   208
        logger.finer(supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   209
        logger.finest(supplier);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   210
        // Lambda expression
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   211
        logger.fine(() ->
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   212
            "Timestamp: " + System.currentTimeMillis() +
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   213
            ", user home directory:  " + System.getProperty("user.home"));
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   214
        // Method reference
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   215
        logger.finer(SystemInfo::Java);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   216
        logger.finest(SystemInfo::OS);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   217
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   218
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   219
    private void validate(int index, boolean thrown, String methodName) {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   220
        assertEquals(supplier.getCount(), invokes[index]);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   221
        assertEquals(handler.getCount(), log_count[index]);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   222
        // Verify associated Throwable is right
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   223
        if (thrown) {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   224
            for (LogRecord r: handler.getLogs()) {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   225
                assertTrue(r.getThrown() instanceof HelperEx, "Validate Thrown");
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   226
                HelperEx e = (HelperEx) r.getThrown();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   227
                assertEquals(r.getLevel(), e.getLevel(), "Validate Thrown Log Level");
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   228
            }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   229
        }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   230
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   231
        if (methodName != null) {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   232
            for (LogRecord r: handler.getLogs()) {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   233
                assertEquals(r.getSourceClassName(), getClass().getName());
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   234
                assertEquals(r.getSourceMethodName(), methodName);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   235
            }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   236
        }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   237
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   238
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   239
    public void verifyLogLevel() {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   240
        for (int i = 0; i < levels.length; i++) {
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   241
            logger.setLevel(levels[i]);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   242
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   243
            setup();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   244
            testLog();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   245
            validate(i, false, null);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   246
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   247
            setup();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   248
            testLogThrown();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   249
            validate(i, true, null);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   250
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   251
            setup();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   252
            testLogp();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   253
            validate(i, false, "testLogp");
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   254
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   255
            setup();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   256
            testLogpThrown();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   257
            validate(i, true, "testLogpThrown");
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   258
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   259
            setup();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   260
            testLevelConvenientMethods();
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   261
            validate(i, false, null);
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   262
        }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   263
    }
77bdd8c8467e 8005632: Extend java.util.Logger to use Supplier<String> for messages
mduigou
parents:
diff changeset
   264
}