|
1 /* |
|
2 * Copyright 1998-1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 4136352 |
|
27 * @summary Test Native2ASCII error messages |
|
28 * |
|
29 */ |
|
30 |
|
31 import java.io.*; |
|
32 import sun.tools.native2ascii.*; |
|
33 import java.util.*; |
|
34 |
|
35 public class NativeErrors { |
|
36 |
|
37 private static ResourceBundle rsrc; |
|
38 |
|
39 static { |
|
40 try { |
|
41 rsrc = ResourceBundle.getBundle( |
|
42 "sun.tools.native2ascii.resources.MsgNative2ascii"); |
|
43 } catch (MissingResourceException e) { |
|
44 throw new Error("Missing message file."); |
|
45 } |
|
46 } |
|
47 |
|
48 public static void main(String args[]) throws Exception { |
|
49 String[] command; |
|
50 Process p = null; |
|
51 BufferedReader in = null; |
|
52 |
|
53 // Construct a command that runs the test in other vm |
|
54 // Exec another vm to run test in |
|
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 |
|
62 command = getComString("test123"); |
|
63 p = Runtime.getRuntime().exec(command); |
|
64 in = new BufferedReader(new InputStreamReader(p.getInputStream())); |
|
65 checkResult(in, "err.cannot.read"); |
|
66 |
|
67 File f1 = new File(System.getProperty("test.src", "."), "test1"); |
|
68 File f2 = new File(System.getProperty("test.src", "."), "test2"); |
|
69 String path1 = f1.getPath(); |
|
70 String path2 = f2.getPath(); |
|
71 |
|
72 command = getComString(path1, path2); |
|
73 p = Runtime.getRuntime().exec(command); |
|
74 in = new BufferedReader(new InputStreamReader(p.getInputStream())); |
|
75 checkResult(in, "err.cannot.write"); |
|
76 } |
|
77 |
|
78 |
|
79 private static void checkResult(BufferedReader in, String errorExpected) |
|
80 throws Exception { |
|
81 String errorReceived; |
|
82 errorReceived = in.readLine(); |
|
83 errorExpected = rsrc.getString(errorExpected); |
|
84 StringBuffer error = new StringBuffer(errorExpected); |
|
85 int start = errorExpected.indexOf("{0}"); |
|
86 if (start >= 0) { |
|
87 error.delete(start, start+3); |
|
88 errorExpected = error.toString(); |
|
89 } |
|
90 //System.out.println("received: " + errorReceived); |
|
91 //System.out.println("expected: " + errorExpected); |
|
92 if (!errorReceived.endsWith(errorExpected)) |
|
93 throw new RuntimeException("Native2ascii bad arg error broken."); |
|
94 } |
|
95 |
|
96 private static String[] getComString(String arg2) { |
|
97 String[] coms = new String[2]; |
|
98 coms[0] = getPathString(); |
|
99 coms[1] = arg2; |
|
100 return coms; |
|
101 } |
|
102 |
|
103 private static String[] getComString(String arg2, String arg3) { |
|
104 String[] coms = new String[3]; |
|
105 coms[0] = getPathString(); |
|
106 coms[1] = arg2; |
|
107 coms[2] = arg3; |
|
108 return coms; |
|
109 } |
|
110 |
|
111 /* |
|
112 * Search for path to native2ascii |
|
113 */ |
|
114 private static String getPathString() { |
|
115 String path = System.getProperty("java.home") + File.separator + |
|
116 "bin" + File.separator + "native2ascii"; |
|
117 if (File.separatorChar == '\\') { |
|
118 path = path + ".exe"; |
|
119 } |
|
120 File f = new File(path); |
|
121 if (!f.exists()) { |
|
122 System.out.println("Cannot find native2ascii at "+path); |
|
123 path = System.getProperty("java.home") + File.separator + ".." + |
|
124 File.separator + "bin" + File.separator + "native2ascii"; |
|
125 if (File.separatorChar == '\\') { |
|
126 path = path + ".exe"; |
|
127 } |
|
128 f = new File(path); |
|
129 if (!f.exists()) |
|
130 throw new RuntimeException("Cannot find native2ascii at "+path); |
|
131 } |
|
132 return path; |
|
133 } |
|
134 |
|
135 } |