NativeErrors.java 4.4 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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.
 *
19 20 21
 * 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.
D
duke 已提交
22 23 24 25 26 27
 */

/*
 * @test
 * @bug 4136352
 * @summary Test Native2ASCII error messages
28 29 30
 * @library /lib/testlibrary
 * @build jdk.testlibrary.* NativeErrors
 * @run main NativeErrors
D
duke 已提交
31 32
 */

33 34 35 36 37 38 39

import java.io.File;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
import jdk.testlibrary.OutputAnalyzer;
import jdk.testlibrary.JDKToolLauncher;
import jdk.testlibrary.ProcessTools;
D
duke 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53

public class NativeErrors {

    private static ResourceBundle rsrc;

    static {
        try {
            rsrc = ResourceBundle.getBundle(
                     "sun.tools.native2ascii.resources.MsgNative2ascii");
        } catch (MissingResourceException e) {
            throw new Error("Missing message file.");
        }
    }

54 55
    public static void main(String args[]) throws Throwable {
        // Execute command in another vm. Verify stdout for expected err msg.
D
duke 已提交
56

57 58
        // Test with no input file given.
        checkResult(executeCmd("-encoding"), "err.bad.arg");
D
duke 已提交
59

60 61 62 63 64
        File f0 = new File(System.getProperty("test.src", "."), "test123");
        String path0 = f0.getPath();
        if ( f0.exists() ) {
            throw new Error("Input file should not exist: " + path0);
        }
65
        checkResult(executeCmd(path0), "err.cannot.read");
D
duke 已提交
66 67

        File f1 = new File(System.getProperty("test.src", "."), "test1");
68
        File f2 = File.createTempFile("test2", ".tmp");
D
duke 已提交
69 70
        String path1 = f1.getPath();
        String path2 = f2.getPath();
71 72 73 74 75 76 77
        if ( !f1.exists() ) {
            throw new Error("Missing input file: " + path1);
        }
        if ( !f2.setWritable(false) ) {
            throw new Error("Output file cannot be made read only: " + path2);
        }
        f2.deleteOnExit();
78 79 80 81 82 83 84 85
        if ( f2.canWrite() ) {
            String msg = "Output file is still writable. " +
                    "Probably because test is run as root. Read-only test skipped.";
            System.out.println(msg);
        } else {
            // Test write to a read-only file.
            checkResult(executeCmd(path1, path2), "err.cannot.write");
        }
D
duke 已提交
86 87
    }

88 89 90 91
    private static String executeCmd(String... toolArgs) throws Throwable {
        JDKToolLauncher cmd = JDKToolLauncher.createUsingTestJDK("native2ascii");
        for (String s : toolArgs) {
            cmd.addToolArg(s);
D
duke 已提交
92
        }
93 94 95 96 97 98 99 100
        OutputAnalyzer output = ProcessTools.executeProcess(cmd.getCommand());
        if (output == null || output.getStdout() == null) {
            throw new Exception("Output was null. Process did not finish correctly.");
        }
        if (output.getExitValue() == 0) {
            throw new Exception("Process exit code was 0, but error was expected.");
        }
        return output.getStdout();
D
duke 已提交
101 102
    }

103 104 105 106 107
    private static void checkResult(
            String errorReceived, String errorKey) throws Exception {
        String errorExpected = rsrc.getString(errorKey);
        if (errorExpected == null) {
            throw new Exception("No error message for key: " + errorKey);
D
duke 已提交
108
        }
109 110 111 112 113 114 115
        // Remove template tag from error message.
        errorExpected = errorExpected.replaceAll("\\{0\\}", "");

        System.out.println("received: " + errorReceived);
        System.out.println("expected: " + errorExpected);
        if (errorReceived.indexOf(errorExpected) < 0) {
            throw new RuntimeException("Native2ascii bad arg error broken.");
D
duke 已提交
116 117 118 119
        }
    }

}