1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. 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 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.
  22  */
  23 import java.io.ByteArrayInputStream;
  24 import java.io.IOException;
  25 import java.util.logging.Logger;
  26 import javax.imageio.ImageIO;
  27 
  28 /**
  29  * @test
  30  * @bug 8019853 8023258
  31  * @summary Test that the default user context is used when in the main
  32  *          application context. This test must not be run in same VM or agent
  33  *          VM mode: it would not test the intended behavior.
  34  * @run main/othervm TestLoggingWithMainAppContext
  35  */
  36 public class TestLoggingWithMainAppContext {
  37 
  38     public static void main(String[] args) throws IOException {
  39         System.out.println("Creating loggers.");
  40 
  41         // These loggers will be created in the default user context.
  42         final Logger foo1 = Logger.getLogger( "foo" );
  43         final Logger bar1 = Logger.getLogger( "foo.bar" );
  44         if (bar1.getParent() != foo1) {
  45             throw new RuntimeException("Parent logger of bar1 "+bar1+" is not "+foo1);
  46         }
  47         System.out.println("bar1.getParent() is the same as foo1");
  48 
  49         // Set a security manager
  50         System.setSecurityManager(new SecurityManager());
  51         System.out.println("Now running with security manager");
  52 
  53         // Triggers the creation of the main AppContext
  54         ByteArrayInputStream is = new ByteArrayInputStream(new byte[] { 0, 1 });
  55         ImageIO.read(is); // triggers calls to system loggers & creation of main AppContext
  56 
  57         // verify that we're still using the default user context
  58         final Logger bar2 = Logger.getLogger( "foo.bar" );
  59         if (bar1 != bar2) {
  60             throw new RuntimeException("bar2 "+bar2+" is not the same as bar1 "+bar1);
  61         }
  62         System.out.println("bar2 is the same as bar1");
  63         if (bar2.getParent() != foo1) {
  64             throw new RuntimeException("Parent logger of bar2 "+bar2+" is not foo1 "+foo1);
  65         }
  66         System.out.println("bar2.getParent() is the same as foo1");
  67         final Logger foo2 = Logger.getLogger("foo");
  68         if (foo1 != foo2) {
  69             throw new RuntimeException("foo2 "+foo2+" is not the same as foo1 "+foo1);
  70         }
  71         System.out.println("foo2 is the same as foo1");
  72 
  73         System.out.println("Test passed.");
  74     }
  75 }