1 /*
   2  * Copyright (c) 2012, 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 
  24 /*
  25  * @test
  26  * @bug     6498300
  27  *
  28  * @summary regression: parent loggers are not properly registered
  29  * @author  ss45998
  30  *
  31  * @build ParentLoggersTest
  32  * @run main/othervm ParentLoggersTest
  33  */
  34 
  35 /*
  36  * There are several original tests which were failed when
  37  * this regression was introduced. This is an extra test
  38  * to ensure that the parent loggers with the defined
  39  * .level property are implicitly registered.
  40  */
  41 
  42 import java.util.*;
  43 import java.io.*;
  44 import java.util.logging.*;
  45 
  46 public class ParentLoggersTest {
  47     static final LogManager  logMgr     = LogManager.getLogManager();
  48     static final PrintStream out        = System.out;
  49 
  50     static final boolean     PASSED     = true;
  51     static final boolean     FAILED     = false;
  52     static final String      MSG_PASSED = "ParentLoggersTest: passed";
  53     static final String      MSG_FAILED = "ParentLoggersTest: failed";
  54 
  55     /* Properties */
  56     static final String TST_SRC_PROP    = "test.src";
  57     static final String CFG_FILE_PROP   = "java.util.logging.config.file";
  58     static final String LM_PROP_FNAME   = "ParentLoggersTest.props";
  59 
  60     /* Logger names */
  61     static final String PARENT_NAME_1   = "myParentLogger";
  62     static final String PARENT_NAME_2   = "abc.xyz.foo";
  63     static final String LOGGER_NAME_1   = PARENT_NAME_1 + ".myLogger";
  64     static final String LOGGER_NAME_2   = PARENT_NAME_2 + ".myBar.myLogger";
  65 
  66     static final List<String> initialLoggerNames = new ArrayList<String>();
  67     public static void main(String args[]) throws Exception {
  68         // cache the initial set of loggers before this test begins
  69         // to add any loggers
  70         Enumeration<String> e = logMgr.getLoggerNames();
  71         List<String> defaultLoggers = getDefaultLoggerNames();
  72         while (e.hasMoreElements()) {
  73             String logger = e.nextElement();
  74             if (!defaultLoggers.contains(logger)) {
  75                 initialLoggerNames.add(logger);
  76             }
  77         };
  78 
  79         String tstSrc = System.getProperty(TST_SRC_PROP);
  80         File   fname  = new File(tstSrc, LM_PROP_FNAME);
  81         String prop   = fname.getCanonicalPath();
  82         System.setProperty(CFG_FILE_PROP, prop);
  83         logMgr.readConfiguration();
  84 
  85         System.out.println();
  86         if (checkLoggers() == PASSED) {
  87             System.out.println(MSG_PASSED);
  88         } else {
  89             System.out.println(MSG_FAILED);
  90             throw new Exception(MSG_FAILED);
  91         }
  92     }
  93 
  94     public static List<String> getDefaultLoggerNames() {
  95         List<String> expectedLoggerNames = new ArrayList<String>();
  96 
  97         // LogManager always creates two loggers:
  98         expectedLoggerNames.add("");       // root   logger: ""
  99         expectedLoggerNames.add("global"); // global logger: "global"
 100         return expectedLoggerNames;
 101     }
 102 
 103     /* Check: getLoggerNames() must return correct names
 104      *        for registered loggers and their parents.
 105      * Returns boolean values: PASSED or FAILED
 106      */
 107     public static boolean checkLoggers() {
 108         String failMsg = "# checkLoggers: getLoggerNames() returned unexpected loggers";
 109         Vector<String> expectedLoggerNames = new Vector<String>(getDefaultLoggerNames());
 110 
 111         // Create the logger LOGGER_NAME_1
 112         Logger.getLogger(LOGGER_NAME_1);
 113         expectedLoggerNames.addElement(PARENT_NAME_1);
 114         expectedLoggerNames.addElement(LOGGER_NAME_1);
 115 
 116         // Create the logger LOGGER_NAME_2
 117         Logger.getLogger(LOGGER_NAME_2);
 118         expectedLoggerNames.addElement(PARENT_NAME_2);
 119         expectedLoggerNames.addElement(LOGGER_NAME_2);
 120 
 121         Enumeration<String> returnedLoggersEnum = logMgr.getLoggerNames();
 122         Vector<String>      returnedLoggerNames = new Vector<String>(0);
 123         while (returnedLoggersEnum.hasMoreElements()) {
 124            String logger = returnedLoggersEnum.nextElement();
 125             if (!initialLoggerNames.contains(logger)) {
 126                 // filter out the loggers that have been added before this test runs
 127                 returnedLoggerNames.addElement(logger);
 128             }
 129 
 130         };
 131 
 132         return checkNames(expectedLoggerNames, returnedLoggerNames, failMsg);
 133     }
 134 
 135     // Returns boolean values: PASSED or FAILED
 136     private static boolean checkNames(Vector<String> expNames,
 137                                       Vector<String> retNames,
 138                                       String failMsg) {
 139         boolean status = PASSED;
 140 
 141         if (expNames.size() != retNames.size()) {
 142             status = FAILED;
 143         } else {
 144             boolean checked[] = new boolean[retNames.size()];
 145             for (int i = 0; i < expNames.size(); i++) {
 146                  int j = 0;
 147                 for (; j < retNames.size(); j++) {
 148                     if (!checked[j] &&
 149                         expNames.elementAt(i).equals(retNames.elementAt(j))) {
 150                         checked[j] = true;
 151                         break;
 152                     }
 153                 }
 154                 if (j >= retNames.size()) {
 155                     status = FAILED;
 156                     break;
 157                 }
 158             }
 159         }
 160         if (!status) {
 161             printFailMsg(expNames, retNames, failMsg);
 162         }
 163         return status;
 164     }
 165 
 166     private static void printFailMsg(Vector<String> expNames,
 167                                      Vector<String> retNames,
 168                                      String failMsg) {
 169         out.println();
 170         out.println(failMsg);
 171         if (expNames.size() == 0) {
 172             out.println("# there are NO expected logger names");
 173         } else {
 174             out.println("# expected logger names (" + expNames.size() + "):");
 175             for (int i = 0; i < expNames.size(); i++) {
 176                out.println(" expNames[" + i + "] = " + expNames.elementAt(i));
 177             }
 178         }
 179         if (retNames.size() == 0) {
 180             out.println("# there are NO returned logger names");
 181         } else {
 182             out.println("# returned logger names (" + retNames.size() + "):");
 183             for (int i = 0; i < retNames.size(); i++) {
 184                out.println("  retNames[" + i + "] = " + retNames.elementAt(i));
 185             }
 186         }
 187     }
 188 }