test/java/util/logging/ParentLoggersTest.java

Print this page




  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 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 }


  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 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.lang.ref.WeakReference;
  45 import java.util.logging.*;
  46 
  47 public class ParentLoggersTest {
  48     static final LogManager  logMgr     = LogManager.getLogManager();
  49     static final PrintStream out        = System.out;
  50 
  51     static final boolean     PASSED     = true;
  52     static final boolean     FAILED     = false;
  53     static final String      MSG_PASSED = "ParentLoggersTest: passed";
  54     static final String      MSG_FAILED = "ParentLoggersTest: failed";
  55 
  56     /* Properties */
  57     static final String TST_SRC_PROP    = "test.src";
  58     static final String CFG_FILE_PROP   = "java.util.logging.config.file";
  59     static final String LM_PROP_FNAME   = "ParentLoggersTest.props";
  60 
  61     /* Logger names */
  62     static final String PARENT_NAME_1   = "myParentLogger";
  63     static final String PARENT_NAME_2   = "abc.xyz.foo";
  64     static final String LOGGER_NAME_1   = PARENT_NAME_1 + ".myLogger";
  65     static final String LOGGER_NAME_2   = PARENT_NAME_2 + ".myBar.myLogger";
  66 
  67     static final List<String> initialLoggerNames = new ArrayList<>();
  68     static final List<Logger> createdLoggers = new ArrayList<>();
  69 
  70     public static void main(String args[]) throws Exception {
  71         // cache the initial set of loggers before this test begins
  72         // to add any loggers
  73         Enumeration<String> e = logMgr.getLoggerNames();
  74         List<String> defaultLoggers = getDefaultLoggerNames();
  75         while (e.hasMoreElements()) {
  76             String logger = e.nextElement();
  77             if (!defaultLoggers.contains(logger)) {
  78                 initialLoggerNames.add(logger);
  79             }
  80         }
  81 
  82         String tstSrc = System.getProperty(TST_SRC_PROP);
  83         File   fname  = new File(tstSrc, LM_PROP_FNAME);
  84         String prop   = fname.getCanonicalPath();
  85         System.setProperty(CFG_FILE_PROP, prop);
  86         logMgr.readConfiguration();
  87 
  88         System.out.println();
  89         if (checkLoggers() == PASSED) {
  90             System.out.println(MSG_PASSED);
  91         } else {
  92             System.out.println(MSG_FAILED);
  93             throw new Exception(MSG_FAILED);
  94         }
  95     }
  96 
  97     public static List<String> getDefaultLoggerNames() {
  98         List<String> expectedLoggerNames = new ArrayList<>();
  99 
 100         // LogManager always creates two loggers:
 101         expectedLoggerNames.add("");       // root   logger: ""
 102         expectedLoggerNames.add("global"); // global logger: "global"
 103         return expectedLoggerNames;
 104     }
 105 
 106     /* Check: getLoggerNames() must return correct names
 107      *        for registered loggers and their parents.
 108      * Returns boolean values: PASSED or FAILED
 109      */
 110     public static boolean checkLoggers() {
 111         String failMsg = "# checkLoggers: getLoggerNames() returned unexpected loggers";
 112         List<String> expectedLoggerNames = new ArrayList<>(getDefaultLoggerNames());
 113 
 114         // Create the logger LOGGER_NAME_1
 115         createdLoggers.add(Logger.getLogger(LOGGER_NAME_1));
 116         expectedLoggerNames.add(PARENT_NAME_1);
 117         expectedLoggerNames.add(LOGGER_NAME_1);
 118 
 119         // Attempt to trigger garbage collection of loggers
 120         // => this is just to make 8031068 more likely to occur
 121         WeakReference<?> wk = new WeakReference<>(new Object());
 122         int remaining = 100;
 123         while (wk.get() != null && --remaining >= 0) {
 124             System.out.println("System.gc()");
 125             System.gc();
 126         }
 127 
 128         // Create the logger LOGGER_NAME_2
 129         createdLoggers.add(Logger.getLogger(LOGGER_NAME_2));
 130         expectedLoggerNames.add(PARENT_NAME_2);
 131         expectedLoggerNames.add(LOGGER_NAME_2);
 132 
 133 
 134         Enumeration<String> returnedLoggersEnum = logMgr.getLoggerNames();
 135         List<String>      returnedLoggerNames = new ArrayList<>(0);
 136         while (returnedLoggersEnum.hasMoreElements()) {
 137            String logger = returnedLoggersEnum.nextElement();
 138             if (!initialLoggerNames.contains(logger)) {
 139                 // filter out the loggers that have been added before this test runs
 140                 returnedLoggerNames.add(logger);
 141             }
 142 
 143         }
 144         System.out.println(returnedLoggerNames);
 145         return checkNames(expectedLoggerNames, returnedLoggerNames, failMsg);
 146     }
 147 
 148     // Returns boolean values: PASSED or FAILED
 149     private static boolean checkNames(List<String> expNames,
 150                                       List<String> retNames,
 151                                       String failMsg) {
 152         boolean status = PASSED;
 153 
 154         if (expNames.size() != retNames.size()) {
 155             status = FAILED;
 156         } else if (!new HashSet<>(expNames).equals(new HashSet<>(retNames))) {











 157             status = FAILED;



 158         }
 159         if (!status) {
 160             printFailMsg(expNames, retNames, failMsg);
 161         }
 162         return status;
 163     }
 164 
 165     private static void printFailMsg(List<String> expNames,
 166                                      List<String> retNames,
 167                                      String failMsg) {
 168         out.println();
 169         out.println(failMsg);
 170         if (expNames.isEmpty()) {
 171             out.println("# there are NO expected logger names");
 172         } else {
 173             out.println("# expected logger names (" + expNames.size() + "):");
 174             for (int i = 0; i < expNames.size(); i++) {
 175                out.println(" expNames[" + i + "] = " + expNames.get(i));
 176             }
 177         }
 178         if (retNames.isEmpty()) {
 179             out.println("# there are NO returned logger names");
 180         } else {
 181             out.println("# returned logger names (" + retNames.size() + "):");
 182             for (int i = 0; i < retNames.size(); i++) {
 183                out.println("  retNames[" + i + "] = " + retNames.get(i));
 184             }
 185         }
 186     }
 187 }