jdk/test/java/util/logging/ParentLoggersTest.java
changeset 5803 c3ddaebe216b
parent 2 90ce3da70b43
child 10607 1ba66093449c
equal deleted inserted replaced
5802:ea99d72d3c19 5803:c3ddaebe216b
    38     static final String PARENT_NAME_1   = "myParentLogger";
    38     static final String PARENT_NAME_1   = "myParentLogger";
    39     static final String PARENT_NAME_2   = "abc.xyz.foo";
    39     static final String PARENT_NAME_2   = "abc.xyz.foo";
    40     static final String LOGGER_NAME_1   = PARENT_NAME_1 + ".myLogger";
    40     static final String LOGGER_NAME_1   = PARENT_NAME_1 + ".myLogger";
    41     static final String LOGGER_NAME_2   = PARENT_NAME_2 + ".myBar.myLogger";
    41     static final String LOGGER_NAME_2   = PARENT_NAME_2 + ".myBar.myLogger";
    42 
    42 
       
    43     static final List<String> initialLoggerNames = new ArrayList<String>();
    43     public static void main(String args[]) throws Exception {
    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 
    44         String tstSrc = System.getProperty(TST_SRC_PROP);
    56         String tstSrc = System.getProperty(TST_SRC_PROP);
    45         File   fname  = new File(tstSrc, LM_PROP_FNAME);
    57         File   fname  = new File(tstSrc, LM_PROP_FNAME);
    46         String prop   = fname.getCanonicalPath();
    58         String prop   = fname.getCanonicalPath();
    47         System.setProperty(CFG_FILE_PROP, prop);
    59         System.setProperty(CFG_FILE_PROP, prop);
    48         logMgr.readConfiguration();
    60         logMgr.readConfiguration();
    54             System.out.println(MSG_FAILED);
    66             System.out.println(MSG_FAILED);
    55             throw new Exception(MSG_FAILED);
    67             throw new Exception(MSG_FAILED);
    56         }
    68         }
    57     }
    69     }
    58 
    70 
    59     public static Vector getDefaultLoggerNames() {
    71     public static List<String> getDefaultLoggerNames() {
    60         Vector expectedLoggerNames = new Vector(0);
    72         List<String> expectedLoggerNames = new ArrayList<String>();
    61 
    73 
    62         // LogManager always creates two loggers:
    74         // LogManager always creates two loggers:
    63         expectedLoggerNames.addElement("");       // root   logger: ""
    75         expectedLoggerNames.add("");       // root   logger: ""
    64         expectedLoggerNames.addElement("global"); // global logger: "global"
    76         expectedLoggerNames.add("global"); // global logger: "global"
    65         return expectedLoggerNames;
    77         return expectedLoggerNames;
    66     }
    78     }
    67 
    79 
    68     /* Check: getLoggerNames() must return correct names
    80     /* Check: getLoggerNames() must return correct names
    69      *        for registered loggers and their parents.
    81      *        for registered loggers and their parents.
    70      * Returns boolean values: PASSED or FAILED
    82      * Returns boolean values: PASSED or FAILED
    71      */
    83      */
    72     public static boolean checkLoggers() {
    84     public static boolean checkLoggers() {
    73         String failMsg = "# checkLoggers: getLoggerNames() returned unexpected loggers";
    85         String failMsg = "# checkLoggers: getLoggerNames() returned unexpected loggers";
    74         Vector expectedLoggerNames = getDefaultLoggerNames();
    86         Vector<String> expectedLoggerNames = new Vector<String>(getDefaultLoggerNames());
    75 
    87 
    76         // Create the logger LOGGER_NAME_1
    88         // Create the logger LOGGER_NAME_1
    77         Logger logger1 = Logger.getLogger(LOGGER_NAME_1);
    89         Logger logger1 = Logger.getLogger(LOGGER_NAME_1);
    78         expectedLoggerNames.addElement(PARENT_NAME_1);
    90         expectedLoggerNames.addElement(PARENT_NAME_1);
    79         expectedLoggerNames.addElement(LOGGER_NAME_1);
    91         expectedLoggerNames.addElement(LOGGER_NAME_1);
    81         // Create the logger LOGGER_NAME_2
    93         // Create the logger LOGGER_NAME_2
    82         Logger logger2 = Logger.getLogger(LOGGER_NAME_2);
    94         Logger logger2 = Logger.getLogger(LOGGER_NAME_2);
    83         expectedLoggerNames.addElement(PARENT_NAME_2);
    95         expectedLoggerNames.addElement(PARENT_NAME_2);
    84         expectedLoggerNames.addElement(LOGGER_NAME_2);
    96         expectedLoggerNames.addElement(LOGGER_NAME_2);
    85 
    97 
    86         Enumeration returnedLoggersEnum = logMgr.getLoggerNames();
    98         Enumeration<String> returnedLoggersEnum = logMgr.getLoggerNames();
    87         Vector      returnedLoggerNames = new Vector(0);
    99         Vector<String>      returnedLoggerNames = new Vector<String>(0);
    88         while (returnedLoggersEnum.hasMoreElements()) {
   100         while (returnedLoggersEnum.hasMoreElements()) {
    89             returnedLoggerNames.addElement(returnedLoggersEnum.nextElement());
   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 
    90         };
   107         };
    91 
   108 
    92         return checkNames(expectedLoggerNames, returnedLoggerNames, failMsg);
   109         return checkNames(expectedLoggerNames, returnedLoggerNames, failMsg);
    93     }
   110     }
    94 
   111 
    95     // Returns boolean values: PASSED or FAILED
   112     // Returns boolean values: PASSED or FAILED
    96     private static boolean checkNames(Vector expNames,
   113     private static boolean checkNames(Vector<String> expNames,
    97                                       Vector retNames,
   114                                       Vector<String> retNames,
    98                                       String failMsg) {
   115                                       String failMsg) {
    99         boolean status = PASSED;
   116         boolean status = PASSED;
   100 
   117 
   101         if (expNames.size() != retNames.size()) {
   118         if (expNames.size() != retNames.size()) {
   102             status = FAILED;
   119             status = FAILED;
   121             printFailMsg(expNames, retNames, failMsg);
   138             printFailMsg(expNames, retNames, failMsg);
   122         }
   139         }
   123         return status;
   140         return status;
   124     }
   141     }
   125 
   142 
   126     private static void printFailMsg(Vector expNames,
   143     private static void printFailMsg(Vector<String> expNames,
   127                                      Vector retNames,
   144                                      Vector<String> retNames,
   128                                      String failMsg) {
   145                                      String failMsg) {
   129         out.println();
   146         out.println();
   130         out.println(failMsg);
   147         out.println(failMsg);
   131         if (expNames.size() == 0) {
   148         if (expNames.size() == 0) {
   132             out.println("# there are NO expected logger names");
   149             out.println("# there are NO expected logger names");