6435N/A/*
6435N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6435N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6435N/A *
6435N/A * This code is free software; you can redistribute it and/or modify it
6435N/A * under the terms of the GNU General Public License version 2 only, as
6435N/A * published by the Free Software Foundation.
6435N/A *
6435N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6435N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6435N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6435N/A * version 2 for more details (a copy is included in the LICENSE file that
6435N/A * accompanied this code).
6435N/A *
6435N/A * You should have received a copy of the GNU General Public License version
6435N/A * 2 along with this work; if not, write to the Free Software Foundation,
6435N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6435N/A *
6435N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6435N/A * or visit www.oracle.com if you need additional information or have any
6435N/A * questions.
6435N/A */
6435N/A
6435N/Aimport java.security.CodeSource;
6435N/Aimport java.security.Permission;
6435N/Aimport java.security.PermissionCollection;
6435N/Aimport java.security.Permissions;
6435N/Aimport java.security.Policy;
6435N/Aimport java.security.ProtectionDomain;
6435N/Aimport java.util.EnumSet;
6435N/Aimport java.util.HashMap;
6435N/Aimport java.util.Map;
6435N/Aimport java.util.logging.LogManager;
6435N/Aimport java.util.logging.Logger;
6435N/Aimport java.util.logging.LoggingPermission;
6435N/Aimport sun.misc.JavaAWTAccess;
6435N/Aimport sun.misc.SharedSecrets;
6435N/A
6435N/A/*
6435N/A * @test
6435N/A * @bug 8017174 8010727
6435N/A * @summary NPE when using Logger.getAnonymousLogger or
6435N/A * LogManager.getLogManager().getLogger
6435N/A *
6435N/A * @run main/othervm -Dtest.security=off TestAppletLoggerContext LoadingApplet
6435N/A * @run main/othervm -Dtest.security=on TestAppletLoggerContext LoadingApplet
6435N/A * @run main/othervm -Dtest.security=off TestAppletLoggerContext LoadingMain
6435N/A * @run main/othervm -Dtest.security=on TestAppletLoggerContext LoadingMain
6435N/A * @run main/othervm -Dtest.security=off TestAppletLoggerContext One
6435N/A * @run main/othervm -Dtest.security=on TestAppletLoggerContext One
6435N/A * @run main/othervm -Dtest.security=off TestAppletLoggerContext Two
6435N/A * @run main/othervm -Dtest.security=on TestAppletLoggerContext Two
6435N/A * @run main/othervm -Dtest.security=off TestAppletLoggerContext Three
6435N/A * @run main/othervm -Dtest.security=on TestAppletLoggerContext Three
6435N/A * @run main/othervm -Dtest.security=off TestAppletLoggerContext Four
6435N/A * @run main/othervm -Dtest.security=on TestAppletLoggerContext Four
6435N/A * @run main/othervm -Dtest.security=off TestAppletLoggerContext Five
6435N/A * @run main/othervm -Dtest.security=on TestAppletLoggerContext Five
6435N/A * @run main/othervm -Dtest.security=off TestAppletLoggerContext Six
6435N/A * @run main/othervm -Dtest.security=on TestAppletLoggerContext Six
6435N/A * @run main/othervm -Dtest.security=off TestAppletLoggerContext Seven
6435N/A * @run main/othervm -Dtest.security=on TestAppletLoggerContext Seven
6435N/A * @run main/othervm -Dtest.security=off TestAppletLoggerContext
6435N/A * @run main/othervm -Dtest.security=on TestAppletLoggerContext
6435N/A */
6435N/A
6435N/A// NOTE: We run in other VM in order to 1. switch security manager and 2. cause
6435N/A// LogManager class to be loaded anew.
6435N/Apublic class TestAppletLoggerContext {
6435N/A
6435N/A // Avoids the hassle of dealing with files and system props...
6435N/A static class SimplePolicy extends Policy {
6435N/A private final Permissions perms;
6435N/A public SimplePolicy(Permission... permissions) {
6435N/A perms = new Permissions();
6435N/A for (Permission permission : permissions) {
6435N/A perms.add(permission);
6435N/A }
6435N/A }
6435N/A @Override
6435N/A public PermissionCollection getPermissions(CodeSource cs) {
6435N/A return perms;
6435N/A }
6435N/A @Override
6435N/A public PermissionCollection getPermissions(ProtectionDomain pd) {
6435N/A return perms;
6435N/A }
6435N/A @Override
6435N/A public boolean implies(ProtectionDomain pd, Permission p) {
6435N/A return perms.implies(p);
6435N/A }
6435N/A }
6435N/A
6435N/A // The bridge class initializes the logging system.
6435N/A // It stubs the applet context in order to simulate context changes.
6435N/A //
6435N/A public static class Bridge {
6435N/A
6435N/A private static class JavaAWTAccessStub implements JavaAWTAccess {
6435N/A boolean active = true;
6435N/A
6435N/A private static class TestExc {
6435N/A private final Map<Object, Object> map = new HashMap<>();
6435N/A void put(Object key, Object v) { map.put(key, v); }
6435N/A Object get(Object key) { return map.get(key); }
6435N/A void remove(Object o) { map.remove(o); }
6435N/A public static TestExc exc(Object o) {
6435N/A return TestExc.class.cast(o);
6435N/A }
6435N/A }
6435N/A
6435N/A TestExc exc;
6435N/A TestExc global = new TestExc();
6435N/A
6435N/A @Override
6435N/A public Object getContext() { return active ? global : null; }
6435N/A @Override
6435N/A public Object getExecutionContext() { return active ? exc : null; }
6435N/A @Override
6435N/A public Object get(Object o, Object o1) { return TestExc.exc(o).get(o1); }
6435N/A @Override
6435N/A public void put(Object o, Object o1, Object o2) { TestExc.exc(o).put(o1, o2); }
6435N/A @Override
6435N/A public void remove(Object o, Object o1) { TestExc.exc(o).remove(o1); }
6435N/A @Override
6435N/A public Object get(Object o) { return global.get(o); }
6435N/A @Override
6435N/A public void put(Object o, Object o1) { global.put(o, o1); }
6435N/A @Override
6435N/A public void remove(Object o) { global.remove(o); }
6435N/A @Override
6435N/A public boolean isDisposed() { return false; }
6435N/A @Override
6435N/A public boolean isMainAppContext() { return exc == null; }
6435N/A }
6435N/A
6435N/A final static JavaAWTAccessStub javaAwtAccess = new JavaAWTAccessStub();
6435N/A public static void init() {
6435N/A SharedSecrets.setJavaAWTAccess(javaAwtAccess);
6435N/A if (System.getProperty("test.security", "on").equals("on")) {
6435N/A Policy p = new SimplePolicy(new LoggingPermission("control", null),
6435N/A new RuntimePermission("setContextClassLoader"),
6435N/A new RuntimePermission("shutdownHooks"));
6435N/A Policy.setPolicy(p);
6435N/A System.setSecurityManager(new SecurityManager());
6435N/A }
6435N/A }
6435N/A
6435N/A public static void changeContext() {
6435N/A System.out.println("... Switching to a new applet context ...");
6435N/A javaAwtAccess.active = true;
6435N/A javaAwtAccess.exc = new JavaAWTAccessStub.TestExc();
6435N/A }
6435N/A
6435N/A public static void desactivate() {
6435N/A System.out.println("... Running with no applet context ...");
6435N/A javaAwtAccess.exc = null;
6435N/A javaAwtAccess.active = false;
6435N/A }
6435N/A
6435N/A public static class CustomAnonymousLogger extends Logger {
6435N/A public CustomAnonymousLogger() {
6435N/A this("");
6435N/A }
6435N/A public CustomAnonymousLogger(String name) {
6435N/A super(null, null);
6435N/A System.out.println( " LogManager: " +LogManager.getLogManager());
6435N/A System.out.println( " getLogger: " +LogManager.getLogManager().getLogger(name));
6435N/A setParent(LogManager.getLogManager().getLogger(name));
6435N/A }
6435N/A }
6435N/A
6435N/A public static class CustomLogger extends Logger {
6435N/A CustomLogger(String name) {
6435N/A super(name, null);
6435N/A }
6435N/A }
6435N/A }
6435N/A
6435N/A public static enum TestCase {
6435N/A LoadingApplet, LoadingMain, One, Two, Three, Four, Five, Six, Seven;
6435N/A public void test() {
6435N/A switch(this) {
6435N/A // When run - each of these two tests must be
6435N/A // run before any other tests and before each other.
6435N/A case LoadingApplet: testLoadingApplet(); break;
6435N/A case LoadingMain: testLoadingMain(); break;
6435N/A case One: testOne(); break;
6435N/A case Two: testTwo(); break;
6435N/A case Three: testThree(); break;
6435N/A case Four: testFour(); break;
6435N/A case Five: testFive(); break;
6435N/A case Six: testSix(); break;
6435N/A case Seven: testSeven(); break;
6435N/A }
6435N/A }
6435N/A public String describe() {
6435N/A switch(this) {
6435N/A case LoadingApplet:
6435N/A return "Test that when the LogManager class is"
6435N/A + " loaded in an applet thread first,"
6435N/A + "\n all LoggerContexts are correctly initialized";
6435N/A case LoadingMain:
6435N/A return "Test that when the LogManager class is"
6435N/A + " loaded in the main thread first,"
6435N/A + "\n all LoggerContexts are correctly initialized";
6435N/A case One:
6435N/A return "Test that Logger.getAnonymousLogger()"
6435N/A + " and new CustomAnonymousLogger() don't throw NPE";
6435N/A case Two:
6435N/A return "Test that Logger.getLogger(\"\")"
6435N/A + " does not return null nor throws NPE";
6435N/A case Three:
6435N/A return "Test that LogManager.getLogManager().getLogger(\"\")"
6435N/A + " does not return null nor throws NPE";
6435N/A case Four:
6435N/A return "Test that Logger.getLogger(Logger.GLOBAL_LOGGER_NAME)"
6435N/A + " does not return null,\n and that"
6435N/A + " new CustomAnonymousLogger(Logger.GLOBAL_LOGGER_NAME)"
6435N/A + " does not throw NPE";
6435N/A case Five:
6435N/A return "Test that LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME)"
6435N/A + "\n does not return null nor throws NPE";
6435N/A case Six:
6435N/A return "Test that manager.getLogger(Logger.GLOBAL_LOGGER_NAME)"
6435N/A + " returns null\n when manager is not the default"
6435N/A + " LogManager instance.\n"
6435N/A + "Test adding a new logger named \"global\" in that"
6435N/A + " non default instance.";
6435N/A case Seven: return "Test that manager.getLogger(\"\")"
6435N/A + " returns null\n when manager is not the default"
6435N/A + " LogManager instance.\n"
6435N/A + "Test adding a new logger named \"\" in that"
6435N/A + " non default instance.";
6435N/A default: return "Undefined";
6435N/A }
6435N/A }
6435N/A };
6435N/A
6435N/A /**
6435N/A * @param args the command line arguments
6435N/A */
6435N/A public static void main(String[] args) {
6435N/A Bridge.init();
6435N/A EnumSet<TestCase> tests = EnumSet.noneOf(TestCase.class);
6435N/A for (String arg : args) {
6435N/A tests.add(TestCase.valueOf(arg));
6435N/A }
6435N/A if (args.length == 0) {
6435N/A tests = EnumSet.complementOf(EnumSet.of(TestCase.LoadingMain));
6435N/A }
6435N/A final EnumSet<TestCase> loadingTests =
6435N/A EnumSet.of(TestCase.LoadingApplet, TestCase.LoadingMain);
6435N/A int testrun = 0;
6435N/A for (TestCase test : tests) {
6435N/A if (loadingTests.contains(test)) {
6435N/A if (testrun > 0) {
6435N/A throw new UnsupportedOperationException("Test case "
6435N/A + test + " must be executed first!");
6435N/A }
6435N/A }
6435N/A System.out.println("Testing "+ test+": ");
6435N/A System.out.println(test.describe());
6435N/A try {
6435N/A test.test();
6435N/A } catch (Exception x) {
6435N/A throw new Error(String.valueOf(test)
6435N/A + (System.getSecurityManager() == null ? " without " : " with ")
6435N/A + "security failed: "+x+"\n "+"FAILED: "+test.describe()+"\n", x);
6435N/A } finally {
6435N/A testrun++;
6435N/A }
6435N/A Bridge.changeContext();
6435N/A System.out.println("PASSED: "+ test);
6435N/A }
6435N/A }
6435N/A
6435N/A public static void testLoadingApplet() {
6435N/A Bridge.changeContext();
6435N/A
6435N/A Logger bar = new Bridge.CustomLogger("com.foo.Bar");
6435N/A LogManager.getLogManager().addLogger(bar);
6435N/A assertNotNull(bar.getParent());
6435N/A testParent(bar);
6435N/A testParent(LogManager.getLogManager().getLogger("global"));
6435N/A testParent(LogManager.getLogManager().getLogger(bar.getName()));
6435N/A
6435N/A Bridge.desactivate();
6435N/A
6435N/A Logger foo = new Bridge.CustomLogger("com.foo.Foo");
6435N/A boolean b = LogManager.getLogManager().addLogger(foo);
6435N/A assertEquals(Boolean.TRUE, Boolean.valueOf(b));
6435N/A assertNotNull(foo.getParent());
6435N/A testParent(foo);
6435N/A testParent(LogManager.getLogManager().getLogger("global"));
6435N/A testParent(LogManager.getLogManager().getLogger(foo.getName()));
6435N/A }
6435N/A
6435N/A public static void testLoadingMain() {
6435N/A Bridge.desactivate();
6435N/A
6435N/A Logger bar = new Bridge.CustomLogger("com.foo.Bar");
6435N/A LogManager.getLogManager().addLogger(bar);
6435N/A assertNotNull(bar.getParent());
6435N/A testParent(bar);
6435N/A testParent(LogManager.getLogManager().getLogger("global"));
6435N/A testParent(LogManager.getLogManager().getLogger(bar.getName()));
6435N/A
6435N/A Bridge.changeContext();
6435N/A
6435N/A Logger foo = new Bridge.CustomLogger("com.foo.Foo");
6435N/A boolean b = LogManager.getLogManager().addLogger(foo);
6435N/A assertEquals(Boolean.TRUE, Boolean.valueOf(b));
6435N/A assertNotNull(foo.getParent());
6435N/A testParent(foo);
6435N/A testParent(LogManager.getLogManager().getLogger("global"));
6435N/A testParent(LogManager.getLogManager().getLogger(foo.getName()));
6435N/A
6435N/A }
6435N/A
6435N/A public static void testOne() {
6435N/A for (int i=0; i<3 ; i++) {
6435N/A Logger logger1 = Logger.getAnonymousLogger();
6435N/A Logger logger1b = Logger.getAnonymousLogger();
6435N/A Bridge.changeContext();
6435N/A Logger logger2 = Logger.getAnonymousLogger();
6435N/A Logger logger2b = Logger.getAnonymousLogger();
6435N/A Bridge.changeContext();
6435N/A Logger logger3 = new Bridge.CustomAnonymousLogger();
6435N/A Logger logger3b = new Bridge.CustomAnonymousLogger();
6435N/A Bridge.changeContext();
6435N/A Logger logger4 = new Bridge.CustomAnonymousLogger();
6435N/A Logger logger4b = new Bridge.CustomAnonymousLogger();
6435N/A }
6435N/A }
6435N/A
6435N/A
6435N/A public static void testTwo() {
6435N/A for (int i=0; i<3 ; i++) {
6435N/A Logger logger1 = Logger.getLogger("");
6435N/A Logger logger1b = Logger.getLogger("");
6435N/A assertNotNull(logger1);
6435N/A assertNotNull(logger1b);
6435N/A assertEquals(logger1, logger1b);
6435N/A Bridge.changeContext();
6435N/A Logger logger2 = Logger.getLogger("");
6435N/A Logger logger2b = Logger.getLogger("");
6435N/A assertNotNull(logger2);
6435N/A assertNotNull(logger2b);
6435N/A assertEquals(logger2, logger2b);
6435N/A assertEquals(logger1, logger2);
6435N/A }
6435N/A }
6435N/A
6435N/A public static void testThree() {
6435N/A for (int i=0; i<3 ; i++) {
6435N/A Logger logger1 = LogManager.getLogManager().getLogger("");
6435N/A Logger logger1b = LogManager.getLogManager().getLogger("");
6435N/A assertNotNull(logger1);
6435N/A assertNotNull(logger1b);
6435N/A assertEquals(logger1, logger1b);
6435N/A Bridge.changeContext();
6435N/A Logger logger2 = LogManager.getLogManager().getLogger("");
6435N/A Logger logger2b = LogManager.getLogManager().getLogger("");
6435N/A assertNotNull(logger2);
6435N/A assertNotNull(logger2b);
6435N/A assertEquals(logger2, logger2b);
6435N/A assertEquals(logger1, logger2);
6435N/A }
6435N/A }
6435N/A
6435N/A public static void testFour() {
6435N/A for (int i=0; i<3 ; i++) {
6435N/A Logger logger1 = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger1b = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A assertNotNull(logger1);
6435N/A assertNotNull(logger1b);
6435N/A assertEquals(logger1, logger1b);
6435N/A Bridge.changeContext();
6435N/A
6435N/A Logger logger2 = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger2b = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A assertNotNull(logger2);
6435N/A assertNotNull(logger2b);
6435N/A assertEquals(logger2, logger2b);
6435N/A
6435N/A assertEquals(logger1, logger2);
6435N/A
6435N/A Bridge.changeContext();
6435N/A Logger logger3 = new Bridge.CustomAnonymousLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger3b = new Bridge.CustomAnonymousLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Bridge.changeContext();
6435N/A Logger logger4 = new Bridge.CustomAnonymousLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger4b = new Bridge.CustomAnonymousLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A }
6435N/A }
6435N/A
6435N/A public static void testFive() {
6435N/A for (int i=0; i<3 ; i++) {
6435N/A Logger logger1 = LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger1b = LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A assertNotNull(logger1);
6435N/A assertNotNull(logger1b);
6435N/A assertEquals(logger1, logger1b);
6435N/A
6435N/A Bridge.changeContext();
6435N/A
6435N/A Logger logger2 = LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger2b = LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A assertNotNull(logger2);
6435N/A assertNotNull(logger2b);
6435N/A assertEquals(logger2, logger2b);
6435N/A
6435N/A assertEquals(logger1, logger2);
6435N/A }
6435N/A }
6435N/A
6435N/A /**
6435N/A * This test is designed to test the behavior of additional LogManager instances.
6435N/A * It must be noted that if the security manager is off, then calling
6435N/A * Bridge.changeContext() has actually no effect - which explains why we have
6435N/A * some differences between the cases security manager on & security manager
6435N/A * off.
6435N/A **/
6435N/A public static void testSix() {
6435N/A for (int i=0; i<3 ; i++) {
6435N/A Bridge.desactivate();
6435N/A LogManager manager = new LogManager() {};
6435N/A Logger logger1 = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger1b = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A assertNull(logger1);
6435N/A assertNull(logger1b);
6435N/A Logger global = new Bridge.CustomLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A manager.addLogger(global);
6435N/A Logger logger2 = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger2b = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A assertNotNull(logger2);
6435N/A assertNotNull(logger2b);
6435N/A assertEquals(logger2, global);
6435N/A assertEquals(logger2b, global);
6435N/A assertNull(manager.getLogger(""));
6435N/A assertNull(manager.getLogger(""));
6435N/A
6435N/A Bridge.changeContext();
6435N/A
6435N/A // this is not a supported configuration:
6435N/A // We are in an applet context with several log managers.
6435N/A // We however need to check our assumptions...
6435N/A
6435N/A // Applet context => root logger and global logger are not null.
6435N/A // root == LogManager.getLogManager().rootLogger
6435N/A // global == Logger.global
6435N/A
6435N/A Logger logger3 = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger3b = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A assertNotNull(logger3);
6435N/A assertNotNull(logger3b);
6435N/A Logger expected = (System.getSecurityManager() != null
6435N/A ? Logger.getGlobal()
6435N/A : global);
6435N/A assertEquals(logger3, expected); // in applet context, we will not see
6435N/A // the LogManager's custom global logger added above...
6435N/A assertEquals(logger3b, expected); // in applet context, we will not see
6435N/A // the LogManager's custom global logger added above...
6435N/A Logger global2 = new Bridge.CustomLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A manager.addLogger(global2); // adding a global logger will not work in applet context
6435N/A // we will always get back the global logger.
6435N/A // this could be considered as a bug...
6435N/A Logger logger4 = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger4b = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A assertNotNull(logger4);
6435N/A assertNotNull(logger4b);
6435N/A assertEquals(logger4, expected); // adding a global logger will not work in applet context
6435N/A assertEquals(logger4b, expected); // adding a global logger will not work in applet context
6435N/A
6435N/A Logger logger5 = manager.getLogger("");
6435N/A Logger logger5b = manager.getLogger("");
6435N/A Logger expectedRoot = (System.getSecurityManager() != null
6435N/A ? LogManager.getLogManager().getLogger("")
6435N/A : null);
6435N/A assertEquals(logger5, expectedRoot);
6435N/A assertEquals(logger5b, expectedRoot);
6435N/A
6435N/A }
6435N/A }
6435N/A
6435N/A /**
6435N/A * This test is designed to test the behavior of additional LogManager instances.
6435N/A * It must be noted that if the security manager is off, then calling
6435N/A * Bridge.changeContext() has actually no effect - which explains why we have
6435N/A * some differences between the cases security manager on & security manager
6435N/A * off.
6435N/A **/
6435N/A public static void testSeven() {
6435N/A for (int i=0; i<3 ; i++) {
6435N/A Bridge.desactivate();
6435N/A LogManager manager = new LogManager() {};
6435N/A Logger logger1 = manager.getLogger("");
6435N/A Logger logger1b = manager.getLogger("");
6435N/A assertNull(logger1);
6435N/A assertNull(logger1b);
6435N/A Logger global = new Bridge.CustomLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A manager.addLogger(global);
6435N/A Logger logger2 = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger2b = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A assertNotNull(logger2);
6435N/A assertNotNull(logger2b);
6435N/A assertEquals(logger2, global);
6435N/A assertEquals(logger2b, global);
6435N/A Logger logger3 = manager.getLogger("");
6435N/A Logger logger3b = manager.getLogger("");
6435N/A assertNull(logger3);
6435N/A assertNull(logger3b);
6435N/A Logger root = new Bridge.CustomLogger("");
6435N/A manager.addLogger(root);
6435N/A Logger logger4 = manager.getLogger("");
6435N/A Logger logger4b = manager.getLogger("");
6435N/A assertNotNull(logger4);
6435N/A assertNotNull(logger4b);
6435N/A assertEquals(logger4, root);
6435N/A assertEquals(logger4b, root);
6435N/A
6435N/A Bridge.changeContext();
6435N/A
6435N/A // this is not a supported configuration:
6435N/A // We are in an applet context with several log managers.
6435N/A // We haowever need to check our assumptions...
6435N/A
6435N/A // Applet context => root logger and global logger are not null.
6435N/A // root == LogManager.getLogManager().rootLogger
6435N/A // global == Logger.global
6435N/A
6435N/A Logger logger5 = manager.getLogger("");
6435N/A Logger logger5b = manager.getLogger("");
6435N/A Logger expectedRoot = (System.getSecurityManager() != null
6435N/A ? LogManager.getLogManager().getLogger("")
6435N/A : root);
6435N/A
6435N/A assertNotNull(logger5);
6435N/A assertNotNull(logger5b);
6435N/A assertEquals(logger5, expectedRoot);
6435N/A assertEquals(logger5b, expectedRoot);
6435N/A if (System.getSecurityManager() != null) {
6435N/A assertNotEquals(logger5, root);
6435N/A assertNotEquals(logger5b, root);
6435N/A }
6435N/A
6435N/A Logger global2 = new Bridge.CustomLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A manager.addLogger(global2); // adding a global logger will not work in applet context
6435N/A // we will always get back the global logger.
6435N/A // this could be considered as a bug...
6435N/A Logger logger6 = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger logger6b = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
6435N/A Logger expectedGlobal = (System.getSecurityManager() != null
6435N/A ? Logger.getGlobal()
6435N/A : global);
6435N/A assertNotNull(logger6);
6435N/A assertNotNull(logger6b);
6435N/A assertEquals(logger6, expectedGlobal); // adding a global logger will not work in applet context
6435N/A assertEquals(logger6b, expectedGlobal); // adding a global logger will not work in applet context
6435N/A
6435N/A Logger root2 = new Bridge.CustomLogger("");
6435N/A manager.addLogger(root2); // adding a root logger will not work in applet context
6435N/A // we will always get back the default manager's root logger.
6435N/A // this could be considered as a bug...
6435N/A Logger logger7 = manager.getLogger("");
6435N/A Logger logger7b = manager.getLogger("");
6435N/A assertNotNull(logger7);
6435N/A assertNotNull(logger7b);
6435N/A assertEquals(logger7, expectedRoot); // adding a global logger will not work in applet context
6435N/A assertEquals(logger7b, expectedRoot); // adding a global logger will not work in applet context
6435N/A assertNotEquals(logger7, root2);
6435N/A assertNotEquals(logger7b, root2);
6435N/A }
6435N/A }
6435N/A
6435N/A public static void testParent(Logger logger) {
6435N/A Logger l = logger;
6435N/A while (l.getParent() != null) {
6435N/A l = l.getParent();
6435N/A }
6435N/A assertEquals("", l.getName());
6435N/A }
6435N/A
6435N/A public static class TestError extends RuntimeException {
6435N/A public TestError(String msg) {
6435N/A super(msg);
6435N/A }
6435N/A }
6435N/A
6435N/A public static void assertNotNull(Object obj) {
6435N/A if (obj == null) throw new NullPointerException();
6435N/A }
6435N/A
6435N/A public static void assertNull(Object obj) {
6435N/A if (obj != null) throw new TestError("Null expected, got "+obj);
6435N/A }
6435N/A
6435N/A public static void assertEquals(Object o1, Object o2) {
6435N/A if (o1 != o2) {
6435N/A throw new TestError(o1 + " != " + o2);
6435N/A }
6435N/A }
6435N/A
6435N/A public static void assertNotEquals(Object o1, Object o2) {
6435N/A if (o1 == o2) {
6435N/A throw new TestError(o1 + " == " + o2);
6435N/A }
6435N/A }
6435N/A}