jdk/test/sun/tools/native2ascii/NativeErrors.java
changeset 30789 9eca83469588
parent 30788 ea573d35531a
child 30790 f81f9725a1c6
equal deleted inserted replaced
30788:ea573d35531a 30789:9eca83469588
     1 /*
       
     2  * Copyright (c) 1998, 1999, 2014 Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4136352
       
    27  * @library /lib/testlibrary
       
    28  * @summary Test Native2ASCII error messages
       
    29  *
       
    30  */
       
    31 
       
    32 import java.io.File;
       
    33 import java.util.ResourceBundle;
       
    34 import java.util.MissingResourceException;
       
    35 import jdk.testlibrary.OutputAnalyzer;
       
    36 import jdk.testlibrary.JDKToolLauncher;
       
    37 import jdk.testlibrary.ProcessTools;
       
    38 
       
    39 public class NativeErrors {
       
    40 
       
    41     private static ResourceBundle rsrc;
       
    42 
       
    43     static {
       
    44         try {
       
    45             rsrc = ResourceBundle.getBundle(
       
    46                      "sun.tools.native2ascii.resources.MsgNative2ascii");
       
    47         } catch (MissingResourceException e) {
       
    48             throw new Error("Missing message file.");
       
    49         }
       
    50     }
       
    51 
       
    52     public static void main(String args[]) throws Throwable {
       
    53         // Execute command in another vm. Verify stdout for expected err msg.
       
    54 
       
    55         // Test with no input file given.
       
    56         checkResult(executeCmd("-encoding"), "err.bad.arg");
       
    57 
       
    58         File f0 = new File(System.getProperty("test.src", "."), "test123");
       
    59         String path0 = f0.getPath();
       
    60         if ( f0.exists() ) {
       
    61             throw new Error("Input file should not exist: " + path0);
       
    62         }
       
    63         checkResult(executeCmd(path0), "err.cannot.read");
       
    64 
       
    65         File f1 = new File(System.getProperty("test.src", "."), "test1");
       
    66         File f2 = File.createTempFile("test2", ".tmp");
       
    67         String path1 = f1.getPath();
       
    68         String path2 = f2.getPath();
       
    69         if ( !f1.exists() ) {
       
    70             throw new Error("Missing input file: " + path1);
       
    71         }
       
    72         if ( !f2.setWritable(false) ) {
       
    73             throw new Error("Output file cannot be made read only: " + path2);
       
    74         }
       
    75         f2.deleteOnExit();
       
    76         if ( f2.canWrite() ) {
       
    77             String msg = "Output file is still writable. " +
       
    78                     "Probably because test is run as root. Read-only test skipped.";
       
    79             System.out.println(msg);
       
    80         } else {
       
    81             // Test write to a read-only file.
       
    82             checkResult(executeCmd(path1, path2), "err.cannot.write");
       
    83         }
       
    84     }
       
    85 
       
    86     private static String executeCmd(String... toolArgs) throws Throwable {
       
    87         JDKToolLauncher cmd = JDKToolLauncher.createUsingTestJDK("native2ascii");
       
    88         for (String s : toolArgs) {
       
    89             cmd.addToolArg(s);
       
    90         }
       
    91         OutputAnalyzer output = ProcessTools.executeProcess(cmd.getCommand());
       
    92         if (output == null || output.getStdout() == null) {
       
    93             throw new Exception("Output was null. Process did not finish correctly.");
       
    94         }
       
    95         if (output.getExitValue() == 0) {
       
    96             throw new Exception("Process exit code was 0, but error was expected.");
       
    97         }
       
    98         return output.getStdout();
       
    99     }
       
   100 
       
   101     private static void checkResult(
       
   102             String errorReceived, String errorKey) throws Exception {
       
   103         String errorExpected = rsrc.getString(errorKey);
       
   104         if (errorExpected == null) {
       
   105             throw new Exception("No error message for key: " + errorKey);
       
   106         }
       
   107         // Remove template tag from error message.
       
   108         errorExpected = errorExpected.replaceAll("\\{0\\}", "");
       
   109 
       
   110         System.out.println("received: " + errorReceived);
       
   111         System.out.println("expected: " + errorExpected);
       
   112         if (errorReceived.indexOf(errorExpected) < 0) {
       
   113             throw new RuntimeException("Native2ascii bad arg error broken.");
       
   114         }
       
   115     }
       
   116 
       
   117 }