jdk/test/sun/tools/native2ascii/NativeErrors.java
author ohair
Wed, 18 Nov 2009 11:10:18 -0800
changeset 4319 47bb128ae7b3
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java 6902325: Fix testcase sun/tools/jhat/HatHeapDump1Test.java Reviewed-by: tbell, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-1999 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4136352
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test Native2ASCII error messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.tools.native2ascii.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class NativeErrors {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private static ResourceBundle rsrc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            rsrc = ResourceBundle.getBundle(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                     "sun.tools.native2ascii.resources.MsgNative2ascii");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            throw new Error("Missing message file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        String[] command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Process p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        BufferedReader in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // Construct a command that runs the test in other vm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // Exec another vm to run test in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // Read the result to determine if test failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        command = getComString("-encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        p = Runtime.getRuntime().exec(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        in = new BufferedReader(new InputStreamReader(p.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        checkResult(in, "err.bad.arg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
4319
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    62
        File f0 = new File(System.getProperty("test.src", "."), "test123");
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    63
        String path0 = f0.getPath();
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    64
        if ( f0.exists() ) {
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    65
            throw new Error("Input file should not exist: " + path0);
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    66
        }
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    67
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    68
        command = getComString(path0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        p = Runtime.getRuntime().exec(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        in = new BufferedReader(new InputStreamReader(p.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        checkResult(in, "err.cannot.read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        File f1 = new File(System.getProperty("test.src", "."), "test1");
4319
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    74
        File f2 = File.createTempFile("test2", ".tmp");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String path1 = f1.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        String path2 = f2.getPath();
4319
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    77
        if ( !f1.exists() ) {
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    78
            throw new Error("Missing input file: " + path1);
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    79
        }
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    80
        if ( !f2.setWritable(false) ) {
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    81
            throw new Error("Output file cannot be made read only: " + path2);
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    82
        }
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    83
        f2.deleteOnExit();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        command = getComString(path1, path2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        p = Runtime.getRuntime().exec(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        in = new BufferedReader(new InputStreamReader(p.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        checkResult(in, "err.cannot.write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static void checkResult(BufferedReader in, String errorExpected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                                           throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        String errorReceived;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        errorReceived = in.readLine();
4319
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    96
        assert errorReceived != null : "First readline cannot be null";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        errorExpected = rsrc.getString(errorExpected);
4319
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
    98
        assert errorExpected != null : "Expected message cannot be null";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        StringBuffer error = new StringBuffer(errorExpected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        int start = errorExpected.indexOf("{0}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (start >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            error.delete(start, start+3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            errorExpected = error.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        //System.out.println("received: " + errorReceived);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        //System.out.println("expected: " + errorExpected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (!errorReceived.endsWith(errorExpected))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new RuntimeException("Native2ascii bad arg error broken.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static String[] getComString(String arg2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        String[] coms = new String[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        coms[0] = getPathString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        coms[1] = arg2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return coms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static String[] getComString(String arg2, String arg3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String[] coms = new String[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        coms[0] = getPathString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        coms[1] = arg2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        coms[2] = arg3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return coms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Search for path to native2ascii
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static String getPathString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String path = System.getProperty("java.home") + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            "bin" + File.separator + "native2ascii";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (File.separatorChar == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            path = path + ".exe";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        File f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.out.println("Cannot find native2ascii at "+path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            path = System.getProperty("java.home") + File.separator + ".." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                   File.separator + "bin" + File.separator + "native2ascii";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (File.separatorChar == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                path = path + ".exe";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (!f.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                throw new RuntimeException("Cannot find native2ascii at "+path);
4319
47bb128ae7b3 6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents: 2
diff changeset
   146
            System.out.println("Using native2ascii at "+path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}