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