Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/native2ascii/NativeErrors.java
38840 views
/*1* Copyright (c) 1998, 2014, 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 413635226* @summary Test Native2ASCII error messages27* @library /lib/testlibrary28* @build jdk.testlibrary.* NativeErrors29* @run main NativeErrors30*/313233import java.io.File;34import java.util.ResourceBundle;35import java.util.MissingResourceException;36import jdk.testlibrary.OutputAnalyzer;37import jdk.testlibrary.JDKToolLauncher;38import jdk.testlibrary.ProcessTools;3940public class NativeErrors {4142private static ResourceBundle rsrc;4344static {45try {46rsrc = ResourceBundle.getBundle(47"sun.tools.native2ascii.resources.MsgNative2ascii");48} catch (MissingResourceException e) {49throw new Error("Missing message file.");50}51}5253public static void main(String args[]) throws Throwable {54// Execute command in another vm. Verify stdout for expected err msg.5556// Test with no input file given.57checkResult(executeCmd("-encoding"), "err.bad.arg");5859File f0 = new File(System.getProperty("test.src", "."), "test123");60String path0 = f0.getPath();61if ( f0.exists() ) {62throw new Error("Input file should not exist: " + path0);63}64checkResult(executeCmd(path0), "err.cannot.read");6566File f1 = new File(System.getProperty("test.src", "."), "test1");67File f2 = File.createTempFile("test2", ".tmp");68String path1 = f1.getPath();69String path2 = f2.getPath();70if ( !f1.exists() ) {71throw new Error("Missing input file: " + path1);72}73if ( !f2.setWritable(false) ) {74throw new Error("Output file cannot be made read only: " + path2);75}76f2.deleteOnExit();77if ( f2.canWrite() ) {78String msg = "Output file is still writable. " +79"Probably because test is run as root. Read-only test skipped.";80System.out.println(msg);81} else {82// Test write to a read-only file.83checkResult(executeCmd(path1, path2), "err.cannot.write");84}85}8687private static String executeCmd(String... toolArgs) throws Throwable {88JDKToolLauncher cmd = JDKToolLauncher.createUsingTestJDK("native2ascii");89for (String s : toolArgs) {90cmd.addToolArg(s);91}92OutputAnalyzer output = ProcessTools.executeProcess(cmd.getCommand());93if (output == null || output.getStdout() == null) {94throw new Exception("Output was null. Process did not finish correctly.");95}96if (output.getExitValue() == 0) {97throw new Exception("Process exit code was 0, but error was expected.");98}99return output.getStdout();100}101102private static void checkResult(103String errorReceived, String errorKey) throws Exception {104String errorExpected = rsrc.getString(errorKey);105if (errorExpected == null) {106throw new Exception("No error message for key: " + errorKey);107}108// Remove template tag from error message.109errorExpected = errorExpected.replaceAll("\\{0\\}", "");110111System.out.println("received: " + errorReceived);112System.out.println("expected: " + errorExpected);113if (errorReceived.indexOf(errorExpected) < 0) {114throw new RuntimeException("Native2ascii bad arg error broken.");115}116}117118}119120121