LoggerSubclass.java revision 1145
1145N/A/*
1145N/A * Copyright 2009 Google, Inc. All Rights Reserved.
1145N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1145N/A *
1145N/A * This code is free software; you can redistribute it and/or modify it
1145N/A * under the terms of the GNU General Public License version 2 only, as
1145N/A * published by the Free Software Foundation.
1145N/A *
1145N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1145N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1145N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1145N/A * version 2 for more details (a copy is included in the LICENSE file that
1145N/A * accompanied this code).
1145N/A *
1145N/A * You should have received a copy of the GNU General Public License version
1145N/A * 2 along with this work; if not, write to the Free Software Foundation,
1145N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1145N/A *
1145N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1145N/A * CA 95054 USA or visit www.sun.com if you need additional information or
1145N/A * have any questions.
1145N/A */
1145N/A
1145N/A/*
1145N/A * @test
1145N/A * @bug 6830220
1145N/A * @summary Test Logger subclasses
1145N/A */
1145N/A
1145N/Aimport java.util.logging.Handler;
1145N/Aimport java.util.logging.Level;
1145N/Aimport java.util.logging.Logger;
1145N/Aimport java.util.logging.LogRecord;
1145N/Aimport java.util.concurrent.atomic.AtomicInteger;
1145N/Aimport java.util.concurrent.atomic.AtomicLong;
1145N/A
1145N/Apublic class LoggerSubclass {
1145N/A void test(String[] args) {
1145N/A final String name = "myLogger";
1145N/A final String message = "myMessage";
1145N/A final AtomicInteger getHandlerCount = new AtomicInteger(0);
1145N/A final AtomicLong lastSequenceNumber = new AtomicLong(-1L);
1145N/A final AtomicInteger lastThreadID = new AtomicInteger(-1);
1145N/A final Logger logger = new Logger(name, null) {
1145N/A public Handler[] getHandlers() {
1145N/A getHandlerCount.getAndIncrement();
1145N/A return super.getHandlers();
1145N/A }};
1145N/A equal(logger.getName(), name);
1145N/A equal(logger.getResourceBundle(), null);
1145N/A equal(logger.getFilter(), null);
1145N/A equal(logger.getLevel(), null);
1145N/A check(logger.isLoggable(Level.WARNING));
1145N/A logger.addHandler(new Handler() {
1145N/A public void close() {}
1145N/A public void flush() {}
1145N/A public void publish(LogRecord l) {
1145N/A equal(l.getLoggerName(), name);
1145N/A equal(l.getMessage(), message);
1145N/A equal(l.getResourceBundle(), null);
1145N/A equal(l.getSourceClassName(), "LoggerSubclass");
1145N/A equal(l.getSourceMethodName(), "test");
1145N/A equal(l.getThrown(), null);
1145N/A equal(l.getLevel(), Level.WARNING);
1145N/A
1145N/A if (lastSequenceNumber.get() != -1) {
1145N/A equal(lastSequenceNumber.get() + 1,
1145N/A l.getSequenceNumber());
1145N/A equal(lastThreadID.get(),
1145N/A l.getThreadID());
1145N/A }
1145N/A lastSequenceNumber.set(l.getSequenceNumber());
1145N/A lastThreadID.set(l.getThreadID());
1145N/A }});
1145N/A for (int i = 1; i < 4; i++) {
1145N/A logger.warning(message); // Should invoke getHandlers()
1145N/A equal(i, getHandlerCount.get());
1145N/A }
1145N/A }
1145N/A
1145N/A //--------------------- Infrastructure ---------------------------
1145N/A volatile int passed = 0, failed = 0;
1145N/A void pass() {passed++;}
1145N/A void fail() {failed++; Thread.dumpStack();}
1145N/A void fail(String msg) {System.err.println(msg); fail();}
1145N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
1145N/A void check(boolean cond) {if (cond) pass(); else fail();}
1145N/A void equal(Object x, Object y) {
1145N/A if (x == null ? y == null : x.equals(y)) pass();
1145N/A else fail(x + " not equal to " + y);}
1145N/A public static void main(String[] args) throws Throwable {
1145N/A try {new LoggerSubclass().instanceMain(args);}
1145N/A catch (Throwable e) {throw e.getCause();}}
1145N/A public void instanceMain(String[] args) throws Throwable {
1145N/A try {test(args);} catch (Throwable t) {unexpected(t);}
1145N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1145N/A if (failed > 0) throw new AssertionError("Some tests failed");}
1145N/A}