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