Path: blob/master/test/jdk/java/util/ResourceBundle/Control/MissingResourceCauseTestRun.java
41155 views
/*1* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4354216 821312726* @summary Test for the cause support when throwing a27* MissingResourceBundle. (This test exists under28* ResourceBundle/Control because bad resource bundle data can be29* shared with other test cases.)30* @library /test/lib31* @build jdk.test.lib.JDKToolLauncher32* jdk.test.lib.Utils33* jdk.test.lib.process.ProcessTools34* MissingResourceCauseTest35* NonResourceBundle36* PrivateConstructorRB37* AbstractRB38* BadStaticInitRB39* NoNoArgConstructorRB40* @run main MissingResourceCauseTestRun41*/4243import java.io.File;44import java.io.FileWriter;45import java.nio.file.Files;46import java.nio.file.Path;47import java.nio.file.Paths;4849import jdk.test.lib.JDKToolLauncher;50import jdk.test.lib.Utils;51import jdk.test.lib.process.ProcessTools;5253public class MissingResourceCauseTestRun {54public static void main(String[] args) throws Throwable {55Path path = Paths.get("UnreadableRB.properties");56Files.deleteIfExists(path);57try {58writeFile(path);59runCmd();60} finally {61deleteFile(path);62}63}6465/**66* Create an unreadable properties file67*/68private static void writeFile(Path path) throws Throwable {69String str = "type=unreadable";70Files.createFile(path);71try (FileWriter fw = new FileWriter(path.toString())) {72fw.write(str);73}74ProcessTools.executeCommand("chmod", "000", path.toString())75.outputTo(System.out)76.errorTo(System.out)77.shouldHaveExitValue(0);78}7980private static void runCmd() throws Throwable {81// Class files are in Utils.TEST_CLASSES82// MalformedDataRB_en.properties is in Utils.TEST_SRC83// UnreadableRB.properties is in current directory84String cp = Utils.TEST_CLASSES + File.pathSeparator + Utils.TEST_SRC85+ File.pathSeparator + ".";86JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("java");87launcher.addToolArg("-ea")88.addToolArg("-esa")89.addToolArg("-cp")90.addToolArg(cp)91.addToolArg("MissingResourceCauseTest");9293int exitCode = ProcessTools.executeCommand(launcher.getCommand())94.getExitValue();95if (exitCode != 0) {96throw new RuntimeException("Execution of the test failed. "97+ "Unexpected exit code: " + exitCode);98}99}100101private static void deleteFile(Path path) throws Throwable {102if(path.toFile().exists()) {103ProcessTools.executeCommand("chmod", "666", path.toString())104.outputTo(System.out)105.errorTo(System.out)106.shouldHaveExitValue(0);107Files.delete(path);108}109}110}111112113