undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
|
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
||||
import static jdk.test.lib.process.ProcessTools.*;
|
||||
import jdk.test.lib.compiler.CompilerUtils;
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8129126 8136802 8137316 8137317 8136804 8139350
|
||||
* @library /test/lib
|
||||
* @modules jdk.compiler
|
||||
* java.logging
|
||||
* @build GetResourceBundleTest jdk.test.lib.process.ProcessTools
|
||||
* jdk.test.lib.compiler.CompilerUtils
|
||||
* @run testng GetResourceBundleTest
|
||||
* @summary Tests Logger.getLogger + logger.getResourceBundle in an named/unnamed module,
|
||||
* resources are in named and unnamed modules respectively.
|
||||
* Positive tests to ensure that a Logger can retrieve ResourceBundle in its current module.
|
||||
* Negative tests to ensure that a Logger cannot retrieve ResourceBundle in another module.
|
||||
* This test also verifies 8136802 8137316 8137317 8136804 8139350.
|
||||
*/
|
||||
|
||||
public class GetResourceBundleTest {
|
||||
|
||||
private static final String TEST_SRC = System.getProperty("test.src");
|
||||
|
||||
private static final Path MOD_SRC_DIR = Paths.get(TEST_SRC, "src");
|
||||
private static final Path MOD_DEST_DIR = Paths.get("mods");
|
||||
private static final Path PKG_SRC_DIR = Paths.get(TEST_SRC, "pkgs");
|
||||
private static final Path PKG_DEST_DIR = Paths.get("pkgs");
|
||||
|
||||
private static final String[] modules = new String[] {"m1", "m2"};
|
||||
|
||||
/**
|
||||
* Compiles all modules used by the test, copy resource files.
|
||||
*/
|
||||
@BeforeClass
|
||||
public void setup() throws Exception {
|
||||
// compile all modules
|
||||
for (String mn : modules) {
|
||||
Path msrc = MOD_SRC_DIR.resolve(mn);
|
||||
assertTrue(CompilerUtils.compile(msrc, MOD_DEST_DIR,
|
||||
"--module-source-path", MOD_SRC_DIR.toString()));
|
||||
}
|
||||
assertTrue(CompilerUtils.compile(PKG_SRC_DIR, PKG_DEST_DIR,
|
||||
"--module-path", MOD_DEST_DIR.toString(), "--add-modules", String.join(",", modules)));
|
||||
|
||||
// copy resource files
|
||||
String[] files = { "m1/p1/resource/p.properties", "m2/p2/resource/p.properties" };
|
||||
for(String f : files) {
|
||||
Files.copy(MOD_SRC_DIR.resolve(f), MOD_DEST_DIR.resolve(f), REPLACE_EXISTING);
|
||||
}
|
||||
String p3 = "p3/resource/p.properties";
|
||||
Files.copy(PKG_SRC_DIR.resolve(p3), PKG_DEST_DIR.resolve(p3), REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run() throws Exception {
|
||||
int exitValue = executeTestJava(
|
||||
"-cp", PKG_DEST_DIR.toString(),
|
||||
"--module-path", MOD_DEST_DIR.toString(),
|
||||
"--add-modules", String.join(",", modules),
|
||||
"p3.test.ResourceBundleTest")
|
||||
.outputTo(System.out)
|
||||
.errorTo(System.err)
|
||||
.getExitValue();
|
||||
assertTrue(exitValue == 0);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.nio.file.Paths;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogManager;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8172886
|
||||
* @summary Verifies that a custom LogManager or custom Handler can be
|
||||
* instantiated by the logging system if they are in a package
|
||||
* that is exported to java.logging by a module.
|
||||
* @build test.logmanager/test.logmanager.TestLogManager
|
||||
* test.handlers/test.handlers.TestHandler
|
||||
* test.config/test.config.LogConfig
|
||||
* LogManagerInModuleTest
|
||||
* @run main/othervm --add-modules test.logmanager,test.handlers
|
||||
* -Djava.util.logging.manager=test.logmanager.TestLogManager
|
||||
* LogManagerInModuleTest
|
||||
* @run main/othervm --add-modules test.logmanager,test.handlers,test.config
|
||||
* -Djava.util.logging.manager=test.logmanager.TestLogManager
|
||||
* -Djava.util.logging.config.class=test.config.LogConfig
|
||||
* LogManagerInModuleTest
|
||||
*
|
||||
* @author danielfuchs
|
||||
*/
|
||||
public class LogManagerInModuleTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (System.getProperty("java.util.logging.config.class", null) == null) {
|
||||
System.setProperty("java.util.logging.config.file",
|
||||
Paths.get(System.getProperty("test.src", "src"),
|
||||
"logging.properties").toString());
|
||||
}
|
||||
// sanity check
|
||||
if (LogManagerInModuleTest.class.getModule().isNamed()) {
|
||||
throw new RuntimeException("Unexpected named module for "
|
||||
+ LogManagerInModuleTest.class + ": "
|
||||
+ LogManagerInModuleTest.class.getModule().getName());
|
||||
}
|
||||
|
||||
// now check that the LogManager was correctly instantiated.
|
||||
LogManager manager = LogManager.getLogManager();
|
||||
System.out.println("LogManager: " + manager);
|
||||
Class<?> logManagerClass = manager.getClass();
|
||||
if (!"test.logmanager".equals(logManagerClass.getModule().getName())) {
|
||||
throw new RuntimeException("Bad module for log manager: "
|
||||
+ logManagerClass.getModule() + "; class is: "
|
||||
+ logManagerClass.getName());
|
||||
}
|
||||
|
||||
Logger logger = Logger.getLogger("com.xyz.foo");
|
||||
Handler[] handlers = logger.getHandlers();
|
||||
if (handlers.length != 1) {
|
||||
throw new RuntimeException("Expected 1 handler, found " + handlers.length);
|
||||
}
|
||||
Class<?> handlerClass = handlers[0].getClass();
|
||||
if (!"test.handlers".equals(handlerClass.getModule().getName())) {
|
||||
throw new RuntimeException("Bad module for handler: "
|
||||
+ handlerClass.getModule() + "; class is: "
|
||||
+ handlerClass.getName());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
############################################################
|
||||
# Global properties
|
||||
############################################################
|
||||
|
||||
# "handlers" specifies a comma separated list of log Handler
|
||||
# classes. These handlers will be installed during VM startup.
|
||||
# Note that these classes must be on the system classpath.
|
||||
# By default we only configure a ConsoleHandler, which will only
|
||||
# show messages at the INFO and above levels.
|
||||
handlers= java.util.logging.ConsoleHandler
|
||||
|
||||
# To also add the FileHandler, use the following line instead.
|
||||
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
|
||||
|
||||
# Default global logging level.
|
||||
# This specifies which kinds of events are logged across
|
||||
# all loggers. For any given facility this global level
|
||||
# can be overriden by a facility specific level
|
||||
# Note that the ConsoleHandler also has a separate level
|
||||
# setting to limit messages printed to the console.
|
||||
.level= INFO
|
||||
|
||||
############################################################
|
||||
# Handler specific properties.
|
||||
# Describes specific configuration info for Handlers.
|
||||
############################################################
|
||||
|
||||
# default file output is in user's home directory.
|
||||
java.util.logging.FileHandler.pattern = %h/java%u.log
|
||||
java.util.logging.FileHandler.limit = 50000
|
||||
java.util.logging.FileHandler.count = 1
|
||||
# Default number of locks FileHandler can obtain synchronously.
|
||||
# This specifies maximum number of attempts to obtain lock file by FileHandler
|
||||
# implemented by incrementing the unique field %u as per FileHandler API documentation.
|
||||
java.util.logging.FileHandler.maxLocks = 100
|
||||
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
|
||||
|
||||
# Limit the message that are printed on the console to INFO and above.
|
||||
java.util.logging.ConsoleHandler.level = INFO
|
||||
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
|
||||
|
||||
# Example to customize the SimpleFormatter output format
|
||||
# to print one-line log message like this:
|
||||
# <level>: <log message> [<date/time>]
|
||||
#
|
||||
# java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
|
||||
|
||||
############################################################
|
||||
# Facility specific properties.
|
||||
# Provides extra control for each logger.
|
||||
############################################################
|
||||
|
||||
# For example, set the com.xyz.foo logger to only log SEVERE
|
||||
# messages:
|
||||
com.xyz.foo.level = SEVERE
|
||||
com.xyz.foo.handlers = test.handlers.TestHandler
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
module test.config {
|
||||
requires java.logging;
|
||||
requires test.handlers;
|
||||
// makes it possible for java.logging to instantiate test.config.LogConfig;
|
||||
// this doesn't need to be a qualified export, but making it so will prevent
|
||||
// any other module from being able to instantiate the provided classes.
|
||||
exports test.config to java.logging;
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package test.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.LogManager;
|
||||
import java.util.logging.Logger;
|
||||
import test.handlers.TestHandler;
|
||||
|
||||
/**
|
||||
* A dummy class that configures the logging system.
|
||||
* @author danielfuchs
|
||||
*/
|
||||
public class LogConfig {
|
||||
private static final List<Logger> LOGGERS = new ArrayList<>();
|
||||
public LogConfig() {
|
||||
LogManager manager = LogManager.getLogManager();
|
||||
Logger logger = Logger.getLogger("com.xyz.foo");
|
||||
if (logger.getHandlers().length > 0) {
|
||||
System.err.println(this.getClass().getName() + ": "
|
||||
+ "Unexpected handlers: "
|
||||
+ List.of(logger.getHandlers()));
|
||||
throw new RuntimeException("Unexpected handlers: "
|
||||
+ List.of(logger.getHandlers()));
|
||||
}
|
||||
logger.addHandler(new TestHandler());
|
||||
LOGGERS.add(logger);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
module test.handlers {
|
||||
requires transitive java.logging;
|
||||
// makes it possible for java.logging and test.config to instantiate
|
||||
// test.handlers.TestHandler;
|
||||
// this doesn't need to be a qualified export, but making it so will prevent
|
||||
// any other module from being able to instantiate the provided classes.
|
||||
exports test.handlers to java.logging, test.config;
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package test.handlers;
|
||||
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
/**
|
||||
* A dummy Handler that does nothing.
|
||||
* @author danielfuchs
|
||||
*/
|
||||
public class TestHandler extends Handler {
|
||||
|
||||
@Override
|
||||
public void publish(LogRecord record) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SecurityException {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
module test.logmanager {
|
||||
requires java.logging;
|
||||
// makes it possible for java.logging to instantiate
|
||||
// test.logmanager.TestLogManager;
|
||||
// this doesn't need to be a qualified export, but making it so will prevent
|
||||
// any other module from being able to instantiate the provided classes.
|
||||
exports test.logmanager to java.logging;
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package test.logmanager;
|
||||
|
||||
import java.util.logging.LogManager;
|
||||
|
||||
/**
|
||||
* A dummy LogManager that simply extends the standard class.
|
||||
* @author danielfuchs
|
||||
*/
|
||||
public class TestLogManager extends LogManager {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package p3.resource;
|
||||
|
||||
import java.util.ListResourceBundle;
|
||||
|
||||
public class ClassResource extends ListResourceBundle {
|
||||
|
||||
protected Object[][] getContents() {
|
||||
return new Object[][] {
|
||||
{ "OkKey", "OK" },
|
||||
{ "CancelKey", "Cancel" }, };
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
OkKey=OK
|
||||
|
|
@ -0,0 +1,177 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package p3.test;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.PropertyResourceBundle;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import p2.test.ModuleLoggerAccess;
|
||||
|
||||
/*
|
||||
* Test Logger.getLogger + logger.getResourceBundle in unnamed/named module,
|
||||
* resources are in named and unnamed modules respectively.
|
||||
*/
|
||||
public class ResourceBundleTest {
|
||||
private static final String RESOURCE_KEY = "OkKey";
|
||||
private static final String RESOURCE_VALUE = "OK";
|
||||
|
||||
private static final String HELP_MSG =
|
||||
"Test that a class in a %s module %s obtain a logger " +
|
||||
"which uses a resource bundle %s bundled in " +
|
||||
"%s module. ( The package in which the resource is " +
|
||||
"contained is not exported by the module )";
|
||||
private static final String NAMED_POSITIVE_CLASSBUNDLE_MSG =
|
||||
String.format(HELP_MSG, "named", "can", "class", "the same");
|
||||
private static final String UNNAMED_POSITIVE_CLASSBUNDLE_MSG =
|
||||
String.format(HELP_MSG, "unnamed", "can", "class", "the same");
|
||||
private static final String NAMED_POSITIVE_PROPERTYBUNDLE_MSG =
|
||||
String.format(HELP_MSG, "named", "can", "property", "the same");
|
||||
private static final String UNNAMED_POSITIVE_PROPERTYBUNDLE_MSG =
|
||||
String.format(HELP_MSG, "unnamed", "can", "property", "the same");
|
||||
private static final String NAMED_NEGATIVE_CLASSBUNDLE_MSG =
|
||||
String.format(HELP_MSG, "named", "cannot", "class", "another");
|
||||
private static final String UNNAMED_NEGATIVE_CLASSBUNDLE_MSG =
|
||||
String.format(HELP_MSG, "unnamed", "cannot", "class", "another");
|
||||
private static final String NAMED_NEGATIVE_PROPERTYBUNDLE_MSG =
|
||||
String.format(HELP_MSG, "named", "cannot", "property", "another");
|
||||
private static final String UNNAMED_NEGATIVE_PROPERTYBUNDLE_MSG =
|
||||
String.format(HELP_MSG, "unnamed", "cannot", "property", "another");
|
||||
|
||||
public static void main(String[] args) {
|
||||
verifySetup();
|
||||
testLoggerRBs();
|
||||
failToLoadRBs();
|
||||
}
|
||||
|
||||
static void verifySetup() {
|
||||
Module m = ResourceBundleTest.class.getModule();
|
||||
System.out.println("Module Name for ResourceBundleTest : " + m.getName());
|
||||
assertTrue(!m.isNamed());
|
||||
m = ModuleLoggerAccess.class.getModule();
|
||||
System.out.println("Module Name for ModuleLoggerAccess : " + m.getName());
|
||||
assertTrue(m.isNamed());
|
||||
}
|
||||
|
||||
/*
|
||||
* Positive tests :
|
||||
* Should be able to access class/property resource bundle in current module,
|
||||
* no matter named or unnamed module.
|
||||
*/
|
||||
static void testLoggerRBs() {
|
||||
testLoggerClassRBs();
|
||||
testLoggerPropertyRBs();
|
||||
}
|
||||
|
||||
static void testLoggerClassRBs() {
|
||||
testLoggerResoureBundle(
|
||||
Logger.getLogger("mylogger.a", "p3.resource.ClassResource"),
|
||||
p3.resource.ClassResource.class,
|
||||
UNNAMED_POSITIVE_CLASSBUNDLE_MSG);
|
||||
testLoggerResoureBundle(
|
||||
ModuleLoggerAccess.getLogger("mylogger.b", "p2.resource.ClassResource"),
|
||||
ModuleLoggerAccess.getResourceClass(),
|
||||
NAMED_POSITIVE_CLASSBUNDLE_MSG);
|
||||
}
|
||||
|
||||
static void testLoggerPropertyRBs() {
|
||||
testLoggerResoureBundle(
|
||||
Logger.getLogger("mylogger.c", "p3.resource.p"),
|
||||
PropertyResourceBundle.class,
|
||||
UNNAMED_POSITIVE_PROPERTYBUNDLE_MSG);
|
||||
testLoggerResoureBundle(
|
||||
ModuleLoggerAccess.getLogger("mylogger.d", "p2.resource.p"),
|
||||
PropertyResourceBundle.class,
|
||||
NAMED_POSITIVE_PROPERTYBUNDLE_MSG);
|
||||
}
|
||||
|
||||
static void testLoggerResoureBundle(Logger logger, Class<?> rbType, String helpMsg) {
|
||||
System.out.println(helpMsg);
|
||||
ResourceBundle rb = logger.getResourceBundle();
|
||||
assertTrue(rbType.isInstance(rb));
|
||||
assertTrue(RESOURCE_VALUE.equals(rb.getString(RESOURCE_KEY)));
|
||||
}
|
||||
|
||||
/*
|
||||
* Negative tests :
|
||||
* MissingResourceException should be thrown when access class/property resource bundle
|
||||
* from another module, no matter named or unnamed module.
|
||||
*/
|
||||
static void failToLoadRBs() {
|
||||
failToLoadClassRBs();
|
||||
failToLoadPropertyRBs();
|
||||
}
|
||||
|
||||
static void failToLoadClassRBs() {
|
||||
// in an unnamed module, try to create a logger with
|
||||
// class resource bundle in named module m1 or m2.
|
||||
failToLoadResourceBundle("mylogger.e", "p1.resource.ClassResource",
|
||||
false, UNNAMED_NEGATIVE_CLASSBUNDLE_MSG);
|
||||
failToLoadResourceBundle("mylogger.f", "p2.resource.ClassResource",
|
||||
false, UNNAMED_NEGATIVE_CLASSBUNDLE_MSG);
|
||||
// in named module m2, try to create a logger with
|
||||
// class resource bundle in another named module m1.
|
||||
failToLoadResourceBundle("mylogger.g", "p1.resource.ClassResource",
|
||||
true, NAMED_NEGATIVE_CLASSBUNDLE_MSG);
|
||||
}
|
||||
|
||||
static void failToLoadPropertyRBs() {
|
||||
// in an unnamed module, try to create a logger with
|
||||
// property resource bundle in named module m1 or m2.
|
||||
failToLoadResourceBundle("mylogger.i", "p1.resource.p",
|
||||
false, UNNAMED_NEGATIVE_PROPERTYBUNDLE_MSG);
|
||||
failToLoadResourceBundle("mylogger.j", "p2.resource.p",
|
||||
false, UNNAMED_NEGATIVE_PROPERTYBUNDLE_MSG);
|
||||
// in named module m2, try to create a logger with
|
||||
// property resource bundle in another named module m1.
|
||||
failToLoadResourceBundle("mylogger.k", "p1.resource.p",
|
||||
true, NAMED_NEGATIVE_PROPERTYBUNDLE_MSG);
|
||||
}
|
||||
|
||||
static void failToLoadResourceBundle(String loggerName, String rbName,
|
||||
boolean getLoggerInNamedModule, String helpMsg) {
|
||||
String msg = String.format(
|
||||
"Logger : %s. Expected exception is not thrown for ResourceBundle : %s.",
|
||||
loggerName, rbName);
|
||||
System.out.println(helpMsg);
|
||||
try {
|
||||
if(getLoggerInNamedModule) {
|
||||
ModuleLoggerAccess.getLogger(loggerName, rbName);
|
||||
} else {
|
||||
Logger.getLogger(loggerName, rbName);
|
||||
}
|
||||
throw new RuntimeException(msg);
|
||||
} catch (MissingResourceException expected) {
|
||||
System.out.println("Get expected exception : " + expected);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public static void assertTrue(boolean b) {
|
||||
if (!b) {
|
||||
throw new RuntimeException("Expect true, get false!");
|
||||
}
|
||||
}
|
||||
}
|
||||
26
test/jdk/java/util/logging/modules/src/m1/module-info.java
Normal file
26
test/jdk/java/util/logging/modules/src/m1/module-info.java
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
module m1 {
|
||||
requires java.logging;
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package p1.resource;
|
||||
|
||||
import java.util.ListResourceBundle;
|
||||
|
||||
public class ClassResource extends ListResourceBundle {
|
||||
|
||||
protected Object[][] getContents() {
|
||||
return new Object[][] {
|
||||
{ "OkKey", "OK" },
|
||||
{ "CancelKey", "Cancel" }, };
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
OkKey=OK
|
||||
28
test/jdk/java/util/logging/modules/src/m2/module-info.java
Normal file
28
test/jdk/java/util/logging/modules/src/m2/module-info.java
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
module m2 {
|
||||
requires m1;
|
||||
requires java.logging;
|
||||
exports p2.test;
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package p2.resource;
|
||||
|
||||
import java.util.ListResourceBundle;
|
||||
|
||||
public class ClassResource extends ListResourceBundle {
|
||||
protected Object[][] getContents() {
|
||||
return new Object[][] {
|
||||
{ "OkKey", "OK" },
|
||||
{ "CancelKey", "Cancel" }, };
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
OkKey=OK
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package p2.test;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/*
|
||||
* Logger in this named module m2 loading a resource bundle
|
||||
* in m2, m1 or unnamed module.
|
||||
*/
|
||||
public class ModuleLoggerAccess {
|
||||
|
||||
public static Logger getLogger(String loggerName, String rbName) {
|
||||
return Logger.getLogger(loggerName, rbName);
|
||||
}
|
||||
|
||||
public static Class<?> getResourceClass() {
|
||||
return p2.resource.ClassResource.class;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue