author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 4319 | 47bb128ae7b3 |
child 22980 | 19675dcb8ec9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1998, 1999, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
||
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 | 69 |
p = Runtime.getRuntime().exec(command); |
70 |
in = new BufferedReader(new InputStreamReader(p.getInputStream())); |
|
71 |
checkResult(in, "err.cannot.read"); |
|
72 |
||
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 | 75 |
String path1 = f1.getPath(); |
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 | 84 |
|
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 |
} |
|
90 |
||
91 |
||
92 |
private static void checkResult(BufferedReader in, String errorExpected) |
|
93 |
throws Exception { |
|
94 |
String errorReceived; |
|
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 | 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 | 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 |
} |
|
105 |
//System.out.println("received: " + errorReceived); |
|
106 |
//System.out.println("expected: " + errorExpected); |
|
107 |
if (!errorReceived.endsWith(errorExpected)) |
|
108 |
throw new RuntimeException("Native2ascii bad arg error broken."); |
|
109 |
} |
|
110 |
||
111 |
private static String[] getComString(String arg2) { |
|
112 |
String[] coms = new String[2]; |
|
113 |
coms[0] = getPathString(); |
|
114 |
coms[1] = arg2; |
|
115 |
return coms; |
|
116 |
} |
|
117 |
||
118 |
private static String[] getComString(String arg2, String arg3) { |
|
119 |
String[] coms = new String[3]; |
|
120 |
coms[0] = getPathString(); |
|
121 |
coms[1] = arg2; |
|
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 |
} |
|
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); |
|
4319
47bb128ae7b3
6902323: Fix testcase sun/tools/native2ascii/NativeErrors.java
ohair
parents:
2
diff
changeset
|
146 |
System.out.println("Using native2ascii at "+path); |
2 | 147 |
} |
148 |
return path; |
|
149 |
} |
|
150 |
||
151 |
} |