< prev index next >

test/jdk/java/util/logging/ParentLoggersTest.java

Print this page

 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<>();
 67     static final List<Logger> createdLoggers = new ArrayList<>();
 68 
 69     public static void main(String args[]) throws Exception {
 70         // cache the initial set of loggers before this test begins
 71         // to add any loggers
 72         Enumeration<String> e = logMgr.getLoggerNames();
 73         List<String> defaultLoggers = getDefaultLoggerNames();
 74         while (e.hasMoreElements()) {
 75             String logger = e.nextElement();
 76             if (!defaultLoggers.contains(logger)) {
 77                 initialLoggerNames.add(logger);
 78             }
 79         }


 80 
 81         String tstSrc = System.getProperty(TST_SRC_PROP);
 82         File   fname  = new File(tstSrc, LM_PROP_FNAME);
 83         String prop   = fname.getCanonicalPath();
 84         System.setProperty(CFG_FILE_PROP, prop);
 85         logMgr.readConfiguration();
 86 
 87         System.out.println();
 88         if (checkLoggers() == PASSED) {
 89             System.out.println(MSG_PASSED);
 90         } else {
 91             System.out.println(MSG_FAILED);
 92             throw new Exception(MSG_FAILED);
 93         }
 94     }
 95 
 96     public static List<String> getDefaultLoggerNames() {
 97         List<String> expectedLoggerNames = new ArrayList<>();
 98 
 99         // LogManager always creates two loggers:

114         createdLoggers.add(Logger.getLogger(LOGGER_NAME_1));
115         expectedLoggerNames.add(PARENT_NAME_1);
116         expectedLoggerNames.add(LOGGER_NAME_1);
117 
118         // Create the logger LOGGER_NAME_2
119         createdLoggers.add(Logger.getLogger(LOGGER_NAME_2));
120         expectedLoggerNames.add(PARENT_NAME_2);
121         expectedLoggerNames.add(LOGGER_NAME_2);
122 
123 
124         Enumeration<String> returnedLoggersEnum = logMgr.getLoggerNames();
125         List<String>      returnedLoggerNames = new ArrayList<>(0);
126         while (returnedLoggersEnum.hasMoreElements()) {
127            String logger = returnedLoggersEnum.nextElement();
128             if (!initialLoggerNames.contains(logger)) {
129                 // filter out the loggers that have been added before this test runs
130                 returnedLoggerNames.add(logger);
131             }
132 
133         }
134         System.out.println(returnedLoggerNames);



135         return checkNames(expectedLoggerNames, returnedLoggerNames, failMsg);
136     }
137 
138     // Returns boolean values: PASSED or FAILED
139     private static boolean checkNames(List<String> expNames,
140                                       List<String> retNames,
141                                       String failMsg) {
142         boolean status = PASSED;
143 
144         if (expNames.size() != retNames.size()) {
145             status = FAILED;
146         } else if (!new HashSet<>(expNames).equals(new HashSet<>(retNames))) {
147             status = FAILED;
148         }
149         if (!status) {
150             printFailMsg(expNames, retNames, failMsg);
151         }
152         return status;
153     }
154 

 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<>();
 67     static final List<Logger> createdLoggers = new ArrayList<>();
 68 
 69     public static void main(String args[]) throws Exception {
 70         // cache the initial set of loggers before this test begins
 71         // to add any loggers
 72         Enumeration<String> e = logMgr.getLoggerNames();
 73         List<String> defaultLoggers = getDefaultLoggerNames();
 74         while (e.hasMoreElements()) {
 75             String logger = e.nextElement();
 76             if (!defaultLoggers.contains(logger)) {
 77                 initialLoggerNames.add(logger);
 78             }
 79         }
 80         System.out.println("# default loggers are: " + defaultLoggers);
 81         System.out.println("# initial loggers are: " + initialLoggerNames);
 82 
 83         String tstSrc = System.getProperty(TST_SRC_PROP);
 84         File   fname  = new File(tstSrc, LM_PROP_FNAME);
 85         String prop   = fname.getCanonicalPath();
 86         System.setProperty(CFG_FILE_PROP, prop);
 87         logMgr.readConfiguration();
 88 
 89         System.out.println();
 90         if (checkLoggers() == PASSED) {
 91             System.out.println(MSG_PASSED);
 92         } else {
 93             System.out.println(MSG_FAILED);
 94             throw new Exception(MSG_FAILED);
 95         }
 96     }
 97 
 98     public static List<String> getDefaultLoggerNames() {
 99         List<String> expectedLoggerNames = new ArrayList<>();
100 
101         // LogManager always creates two loggers:

116         createdLoggers.add(Logger.getLogger(LOGGER_NAME_1));
117         expectedLoggerNames.add(PARENT_NAME_1);
118         expectedLoggerNames.add(LOGGER_NAME_1);
119 
120         // Create the logger LOGGER_NAME_2
121         createdLoggers.add(Logger.getLogger(LOGGER_NAME_2));
122         expectedLoggerNames.add(PARENT_NAME_2);
123         expectedLoggerNames.add(LOGGER_NAME_2);
124 
125 
126         Enumeration<String> returnedLoggersEnum = logMgr.getLoggerNames();
127         List<String>      returnedLoggerNames = new ArrayList<>(0);
128         while (returnedLoggersEnum.hasMoreElements()) {
129            String logger = returnedLoggersEnum.nextElement();
130             if (!initialLoggerNames.contains(logger)) {
131                 // filter out the loggers that have been added before this test runs
132                 returnedLoggerNames.add(logger);
133             }
134 
135         }
136         System.out.println("# Created loggers are: " +
137                 createdLoggers.stream().map(Logger::getName).toList());
138         System.out.println("# Expected loggers are: " + expectedLoggerNames);
139         System.out.println("# Returned loggers are: " + returnedLoggerNames);
140         return checkNames(expectedLoggerNames, returnedLoggerNames, failMsg);
141     }
142 
143     // Returns boolean values: PASSED or FAILED
144     private static boolean checkNames(List<String> expNames,
145                                       List<String> retNames,
146                                       String failMsg) {
147         boolean status = PASSED;
148 
149         if (expNames.size() != retNames.size()) {
150             status = FAILED;
151         } else if (!new HashSet<>(expNames).equals(new HashSet<>(retNames))) {
152             status = FAILED;
153         }
154         if (!status) {
155             printFailMsg(expNames, retNames, failMsg);
156         }
157         return status;
158     }
159 
< prev index next >