|
1 /* |
|
2 * Copyright 2009 Sun Microsystems, 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 |
|
26 * @bug 6882376 |
|
27 * @summary Test if java.util.logging.Logger is created before and after |
|
28 * logging is enabled. Also validate some basic PlatformLogger |
|
29 * operations. |
|
30 * |
|
31 * @build PlatformLoggerTest |
|
32 * @run main PlatformLoggerTest |
|
33 */ |
|
34 |
|
35 import java.util.logging.*; |
|
36 import sun.util.logging.PlatformLogger; |
|
37 |
|
38 public class PlatformLoggerTest { |
|
39 private static final int defaultEffectiveLevel = 0; |
|
40 public static void main(String[] args) throws Exception { |
|
41 final String FOO_PLATFORM_LOGGER = "test.platformlogger.foo"; |
|
42 final String BAR_PLATFORM_LOGGER = "test.platformlogger.bar"; |
|
43 final String GOO_PLATFORM_LOGGER = "test.platformlogger.goo"; |
|
44 final String BAR_LOGGER = "test.logger.bar"; |
|
45 PlatformLogger goo = PlatformLogger.getLogger(GOO_PLATFORM_LOGGER); |
|
46 |
|
47 // Create a platform logger using the default |
|
48 PlatformLogger foo = PlatformLogger.getLogger(FOO_PLATFORM_LOGGER); |
|
49 checkPlatformLogger(foo, FOO_PLATFORM_LOGGER); |
|
50 |
|
51 // create a java.util.logging.Logger |
|
52 // now java.util.logging.Logger should be created for each platform logger |
|
53 Logger logger = Logger.getLogger(BAR_LOGGER); |
|
54 logger.setLevel(Level.WARNING); |
|
55 |
|
56 PlatformLogger bar = PlatformLogger.getLogger(BAR_PLATFORM_LOGGER); |
|
57 checkPlatformLogger(bar, BAR_PLATFORM_LOGGER); |
|
58 |
|
59 checkLogger(FOO_PLATFORM_LOGGER, Level.FINER); |
|
60 checkLogger(BAR_PLATFORM_LOGGER, Level.FINER); |
|
61 |
|
62 checkLogger(GOO_PLATFORM_LOGGER, null); |
|
63 checkLogger(BAR_LOGGER, Level.WARNING); |
|
64 |
|
65 foo.setLevel(PlatformLogger.SEVERE); |
|
66 checkLogger(FOO_PLATFORM_LOGGER, Level.SEVERE); |
|
67 } |
|
68 |
|
69 private static void checkPlatformLogger(PlatformLogger logger, String name) { |
|
70 if (!logger.getName().equals(name)) { |
|
71 throw new RuntimeException("Invalid logger's name " + |
|
72 logger.getName() + " but expected " + name); |
|
73 } |
|
74 |
|
75 if (logger.getLevel() != defaultEffectiveLevel) { |
|
76 throw new RuntimeException("Invalid default level for logger " + |
|
77 logger.getName()); |
|
78 } |
|
79 |
|
80 if (logger.isLoggable(PlatformLogger.FINE) != false) { |
|
81 throw new RuntimeException("isLoggerable(FINE) returns true for logger " + |
|
82 logger.getName() + " but expected false"); |
|
83 } |
|
84 |
|
85 logger.setLevel(PlatformLogger.FINER); |
|
86 if (logger.getLevel() != Level.FINER.intValue()) { |
|
87 throw new RuntimeException("Invalid level for logger " + |
|
88 logger.getName() + " " + logger.getLevel()); |
|
89 } |
|
90 |
|
91 if (logger.isLoggable(PlatformLogger.FINE) != true) { |
|
92 throw new RuntimeException("isLoggerable(FINE) returns false for logger " + |
|
93 logger.getName() + " but expected true"); |
|
94 } |
|
95 |
|
96 logger.info("OK: Testing log message"); |
|
97 } |
|
98 |
|
99 private static void checkLogger(String name, Level level) { |
|
100 Logger logger = LogManager.getLogManager().getLogger(name); |
|
101 if (logger == null) { |
|
102 throw new RuntimeException("Logger " + name + |
|
103 " does not exist"); |
|
104 } |
|
105 |
|
106 if (logger.getLevel() != level) { |
|
107 throw new RuntimeException("Invalid level for logger " + |
|
108 logger.getName() + " " + logger.getLevel()); |
|
109 } |
|
110 } |
|
111 } |