# HG changeset patch # User dfuchs # Date 1389365484 -3600 # Node ID 191b8793a5325c521e70b4f63c228170efbd6e36 # Parent b4f108102ca9d689b7c8e249777f93418144f93b 8031068: java/util/logging/ParentLoggersTest.java: checkLoggers: getLoggerNames() returned unexpected loggers Summary: The test was not keeping any strong reference on the loggers it created allowing for them to be garbage collected too early. Reviewed-by: mchung, chegar diff -r b4f108102ca9 -r 191b8793a532 jdk/test/java/util/logging/ParentLoggersTest.java --- a/jdk/test/java/util/logging/ParentLoggersTest.java Fri Jan 10 08:22:00 2014 +0100 +++ b/jdk/test/java/util/logging/ParentLoggersTest.java Fri Jan 10 15:51:24 2014 +0100 @@ -63,7 +63,9 @@ static final String LOGGER_NAME_1 = PARENT_NAME_1 + ".myLogger"; static final String LOGGER_NAME_2 = PARENT_NAME_2 + ".myBar.myLogger"; - static final List initialLoggerNames = new ArrayList(); + static final List initialLoggerNames = new ArrayList<>(); + static final List createdLoggers = new ArrayList<>(); + public static void main(String args[]) throws Exception { // cache the initial set of loggers before this test begins // to add any loggers @@ -74,7 +76,7 @@ if (!defaultLoggers.contains(logger)) { initialLoggerNames.add(logger); } - }; + } String tstSrc = System.getProperty(TST_SRC_PROP); File fname = new File(tstSrc, LM_PROP_FNAME); @@ -92,7 +94,7 @@ } public static List getDefaultLoggerNames() { - List expectedLoggerNames = new ArrayList(); + List expectedLoggerNames = new ArrayList<>(); // LogManager always creates two loggers: expectedLoggerNames.add(""); // root logger: "" @@ -106,56 +108,43 @@ */ public static boolean checkLoggers() { String failMsg = "# checkLoggers: getLoggerNames() returned unexpected loggers"; - Vector expectedLoggerNames = new Vector(getDefaultLoggerNames()); + List expectedLoggerNames = new ArrayList<>(getDefaultLoggerNames()); // Create the logger LOGGER_NAME_1 - Logger.getLogger(LOGGER_NAME_1); - expectedLoggerNames.addElement(PARENT_NAME_1); - expectedLoggerNames.addElement(LOGGER_NAME_1); + createdLoggers.add(Logger.getLogger(LOGGER_NAME_1)); + expectedLoggerNames.add(PARENT_NAME_1); + expectedLoggerNames.add(LOGGER_NAME_1); // Create the logger LOGGER_NAME_2 - Logger.getLogger(LOGGER_NAME_2); - expectedLoggerNames.addElement(PARENT_NAME_2); - expectedLoggerNames.addElement(LOGGER_NAME_2); + createdLoggers.add(Logger.getLogger(LOGGER_NAME_2)); + expectedLoggerNames.add(PARENT_NAME_2); + expectedLoggerNames.add(LOGGER_NAME_2); + Enumeration returnedLoggersEnum = logMgr.getLoggerNames(); - Vector returnedLoggerNames = new Vector(0); + List returnedLoggerNames = new ArrayList<>(0); while (returnedLoggersEnum.hasMoreElements()) { String logger = returnedLoggersEnum.nextElement(); if (!initialLoggerNames.contains(logger)) { // filter out the loggers that have been added before this test runs - returnedLoggerNames.addElement(logger); + returnedLoggerNames.add(logger); } - }; - + } + System.out.println(returnedLoggerNames); return checkNames(expectedLoggerNames, returnedLoggerNames, failMsg); } // Returns boolean values: PASSED or FAILED - private static boolean checkNames(Vector expNames, - Vector retNames, + private static boolean checkNames(List expNames, + List retNames, String failMsg) { boolean status = PASSED; if (expNames.size() != retNames.size()) { status = FAILED; - } else { - boolean checked[] = new boolean[retNames.size()]; - for (int i = 0; i < expNames.size(); i++) { - int j = 0; - for (; j < retNames.size(); j++) { - if (!checked[j] && - expNames.elementAt(i).equals(retNames.elementAt(j))) { - checked[j] = true; - break; - } - } - if (j >= retNames.size()) { - status = FAILED; - break; - } - } + } else if (!new HashSet<>(expNames).equals(new HashSet<>(retNames))) { + status = FAILED; } if (!status) { printFailMsg(expNames, retNames, failMsg); @@ -163,25 +152,25 @@ return status; } - private static void printFailMsg(Vector expNames, - Vector retNames, + private static void printFailMsg(List expNames, + List retNames, String failMsg) { out.println(); out.println(failMsg); - if (expNames.size() == 0) { + if (expNames.isEmpty()) { out.println("# there are NO expected logger names"); } else { out.println("# expected logger names (" + expNames.size() + "):"); for (int i = 0; i < expNames.size(); i++) { - out.println(" expNames[" + i + "] = " + expNames.elementAt(i)); + out.println(" expNames[" + i + "] = " + expNames.get(i)); } } - if (retNames.size() == 0) { + if (retNames.isEmpty()) { out.println("# there are NO returned logger names"); } else { out.println("# returned logger names (" + retNames.size() + "):"); for (int i = 0; i < retNames.size(); i++) { - out.println(" retNames[" + i + "] = " + retNames.elementAt(i)); + out.println(" retNames[" + i + "] = " + retNames.get(i)); } } }