jdk/test/sun/tools/native2ascii/NativeErrors.java
changeset 22980 19675dcb8ec9
parent 5506 202f599c92aa
child 24707 f1225d0f947c
equal deleted inserted replaced
22979:b6140042793d 22980:19675dcb8ec9
     1 /*
     1 /*
     2  * Copyright (c) 1998, 1999, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 4136352
    26  * @bug 4136352
       
    27  * @library /lib/testlibrary
    27  * @summary Test Native2ASCII error messages
    28  * @summary Test Native2ASCII error messages
    28  *
    29  *
    29  */
    30  */
    30 
    31 
    31 import java.io.*;
    32 import java.io.File;
    32 import sun.tools.native2ascii.*;
    33 import java.util.ResourceBundle;
    33 import java.util.*;
    34 import java.util.MissingResourceException;
       
    35 import jdk.testlibrary.OutputAnalyzer;
       
    36 import jdk.testlibrary.JDKToolLauncher;
       
    37 import jdk.testlibrary.ProcessTools;
    34 
    38 
    35 public class NativeErrors {
    39 public class NativeErrors {
    36 
    40 
    37     private static ResourceBundle rsrc;
    41     private static ResourceBundle rsrc;
    38 
    42 
    43         } catch (MissingResourceException e) {
    47         } catch (MissingResourceException e) {
    44             throw new Error("Missing message file.");
    48             throw new Error("Missing message file.");
    45         }
    49         }
    46     }
    50     }
    47 
    51 
    48     public static void main(String args[]) throws Exception {
    52     public static void main(String args[]) throws Throwable {
    49         String[] command;
    53         // Execute command in another vm. Verify stdout for expected err msg.
    50         Process p = null;
       
    51         BufferedReader in = null;
       
    52 
    54 
    53         // Construct a command that runs the test in other vm
    55         // Test with no input file given.
    54         // Exec another vm to run test in
    56         checkResult(executeCmd("-encoding"), "err.bad.arg");
    55         // Read the result to determine if test failed
       
    56 
       
    57         command = getComString("-encoding");
       
    58         p = Runtime.getRuntime().exec(command);
       
    59         in = new BufferedReader(new InputStreamReader(p.getInputStream()));
       
    60         checkResult(in, "err.bad.arg");
       
    61 
    57 
    62         File f0 = new File(System.getProperty("test.src", "."), "test123");
    58         File f0 = new File(System.getProperty("test.src", "."), "test123");
    63         String path0 = f0.getPath();
    59         String path0 = f0.getPath();
    64         if ( f0.exists() ) {
    60         if ( f0.exists() ) {
    65             throw new Error("Input file should not exist: " + path0);
    61             throw new Error("Input file should not exist: " + path0);
    66         }
    62         }
    67 
    63         checkResult(executeCmd(path0), "err.cannot.read");
    68         command = getComString(path0);
       
    69         p = Runtime.getRuntime().exec(command);
       
    70         in = new BufferedReader(new InputStreamReader(p.getInputStream()));
       
    71         checkResult(in, "err.cannot.read");
       
    72 
    64 
    73         File f1 = new File(System.getProperty("test.src", "."), "test1");
    65         File f1 = new File(System.getProperty("test.src", "."), "test1");
    74         File f2 = File.createTempFile("test2", ".tmp");
    66         File f2 = File.createTempFile("test2", ".tmp");
    75         String path1 = f1.getPath();
    67         String path1 = f1.getPath();
    76         String path2 = f2.getPath();
    68         String path2 = f2.getPath();
    79         }
    71         }
    80         if ( !f2.setWritable(false) ) {
    72         if ( !f2.setWritable(false) ) {
    81             throw new Error("Output file cannot be made read only: " + path2);
    73             throw new Error("Output file cannot be made read only: " + path2);
    82         }
    74         }
    83         f2.deleteOnExit();
    75         f2.deleteOnExit();
    84 
    76         checkResult(executeCmd(path1, path2), "err.cannot.write");
    85         command = getComString(path1, path2);
       
    86         p = Runtime.getRuntime().exec(command);
       
    87         in = new BufferedReader(new InputStreamReader(p.getInputStream()));
       
    88         checkResult(in, "err.cannot.write");
       
    89     }
    77     }
    90 
    78 
    91 
    79     private static String executeCmd(String... toolArgs) throws Throwable {
    92     private static void checkResult(BufferedReader in, String errorExpected)
    80         JDKToolLauncher cmd = JDKToolLauncher.createUsingTestJDK("native2ascii");
    93                                                            throws Exception {
    81         for (String s : toolArgs) {
    94         String errorReceived;
    82             cmd.addToolArg(s);
    95         errorReceived = in.readLine();
       
    96         assert errorReceived != null : "First readline cannot be null";
       
    97         errorExpected = rsrc.getString(errorExpected);
       
    98         assert errorExpected != null : "Expected message cannot be null";
       
    99         StringBuffer error = new StringBuffer(errorExpected);
       
   100         int start = errorExpected.indexOf("{0}");
       
   101         if (start >= 0) {
       
   102             error.delete(start, start+3);
       
   103             errorExpected = error.toString();
       
   104         }
    83         }
   105         //System.out.println("received: " + errorReceived);
    84         OutputAnalyzer output = ProcessTools.executeProcess(cmd.getCommand());
   106         //System.out.println("expected: " + errorExpected);
    85         if (output == null || output.getStdout() == null) {
   107         if (!errorReceived.endsWith(errorExpected))
    86             throw new Exception("Output was null. Process did not finish correctly.");
   108             throw new RuntimeException("Native2ascii bad arg error broken.");
    87         }
       
    88         if (output.getExitValue() == 0) {
       
    89             throw new Exception("Process exit code was 0, but error was expected.");
       
    90         }
       
    91         return output.getStdout();
   109     }
    92     }
   110 
    93 
   111     private static String[] getComString(String arg2) {
    94     private static void checkResult(
   112         String[] coms = new String[2];
    95             String errorReceived, String errorKey) throws Exception {
   113         coms[0] = getPathString();
    96         String errorExpected = rsrc.getString(errorKey);
   114         coms[1] = arg2;
    97         if (errorExpected == null) {
   115         return coms;
    98             throw new Exception("No error message for key: " + errorKey);
   116     }
    99         }
       
   100         // Remove template tag from error message.
       
   101         errorExpected = errorExpected.replaceAll("\\{0\\}", "");
   117 
   102 
   118     private static String[] getComString(String arg2, String arg3) {
   103         System.out.println("received: " + errorReceived);
   119         String[] coms = new String[3];
   104         System.out.println("expected: " + errorExpected);
   120         coms[0] = getPathString();
   105         if (errorReceived.indexOf(errorExpected) < 0) {
   121         coms[1] = arg2;
   106             throw new RuntimeException("Native2ascii bad arg error broken.");
   122         coms[2] = arg3;
       
   123         return coms;
       
   124     }
       
   125 
       
   126     /*
       
   127      * Search for path to native2ascii
       
   128      */
       
   129     private static String getPathString() {
       
   130         String path = System.getProperty("java.home") + File.separator +
       
   131             "bin" + File.separator + "native2ascii";
       
   132         if (File.separatorChar == '\\') {
       
   133             path = path + ".exe";
       
   134         }
   107         }
   135         File f = new File(path);
       
   136         if (!f.exists()) {
       
   137             System.out.println("Cannot find native2ascii at "+path);
       
   138             path = System.getProperty("java.home") + File.separator + ".." +
       
   139                    File.separator + "bin" + File.separator + "native2ascii";
       
   140             if (File.separatorChar == '\\') {
       
   141                 path = path + ".exe";
       
   142             }
       
   143             f = new File(path);
       
   144             if (!f.exists())
       
   145                 throw new RuntimeException("Cannot find native2ascii at "+path);
       
   146             System.out.println("Using native2ascii at "+path);
       
   147         }
       
   148         return path;
       
   149     }
   108     }
   150 
   109 
   151 }
   110 }