1 /*
2  * Copyright (c) 2013, 2016, 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  * @modules java.desktop
35  *          java.logging
36  * @run main/othervm TestLoggingWithMainAppContext
37  */
38 public class TestLoggingWithMainAppContext {
39 
main(String[] args)40     public static void main(String[] args) throws IOException {
41         System.out.println("Creating loggers.");
42 
43         // These loggers will be created in the default user context.
44         final Logger foo1 = Logger.getLogger( "foo" );
45         final Logger bar1 = Logger.getLogger( "foo.bar" );
46         if (bar1.getParent() != foo1) {
47             throw new RuntimeException("Parent logger of bar1 "+bar1+" is not "+foo1);
48         }
49         System.out.println("bar1.getParent() is the same as foo1");
50 
51         // Set a security manager
52         System.setSecurityManager(new SecurityManager());
53         System.out.println("Now running with security manager");
54 
55         // Triggers the creation of the main AppContext
56         ByteArrayInputStream is = new ByteArrayInputStream(new byte[] { 0, 1 });
57         ImageIO.read(is); // triggers calls to system loggers & creation of main AppContext
58 
59         // verify that we're still using the default user context
60         final Logger bar2 = Logger.getLogger( "foo.bar" );
61         if (bar1 != bar2) {
62             throw new RuntimeException("bar2 "+bar2+" is not the same as bar1 "+bar1);
63         }
64         System.out.println("bar2 is the same as bar1");
65         if (bar2.getParent() != foo1) {
66             throw new RuntimeException("Parent logger of bar2 "+bar2+" is not foo1 "+foo1);
67         }
68         System.out.println("bar2.getParent() is the same as foo1");
69         final Logger foo2 = Logger.getLogger("foo");
70         if (foo1 != foo2) {
71             throw new RuntimeException("foo2 "+foo2+" is not the same as foo1 "+foo1);
72         }
73         System.out.println("foo2 is the same as foo1");
74 
75         System.out.println("Test passed.");
76     }
77 }
78