blob: ba18fa36b2e4dd46a272a4890f8c4214808f807f [file] [log] [blame]
martinddfa90d2009-04-20 21:23:47 -07001/*
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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
martind3216de2009-04-20 21:57:01 -070026 * @bug 6830220 6278014
martinddfa90d2009-04-20 21:23:47 -070027 * @summary Test Logger subclasses
28 */
29
30import java.util.logging.Handler;
31import java.util.logging.Level;
32import java.util.logging.Logger;
33import java.util.logging.LogRecord;
34import java.util.concurrent.atomic.AtomicInteger;
35import java.util.concurrent.atomic.AtomicLong;
36
37public 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());
martind3216de2009-04-20 21:57:01 -070071 equal((int) Thread.currentThread().getId(),
72 l.getThreadID());
martinddfa90d2009-04-20 21:23:47 -070073 }
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}