Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/native2ascii/NativeErrors.java
38840 views
1
/*
2
* Copyright (c) 1998, 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
* @summary Test Native2ASCII error messages
28
* @library /lib/testlibrary
29
* @build jdk.testlibrary.* NativeErrors
30
* @run main NativeErrors
31
*/
32
33
34
import java.io.File;
35
import java.util.ResourceBundle;
36
import java.util.MissingResourceException;
37
import jdk.testlibrary.OutputAnalyzer;
38
import jdk.testlibrary.JDKToolLauncher;
39
import jdk.testlibrary.ProcessTools;
40
41
public class NativeErrors {
42
43
private static ResourceBundle rsrc;
44
45
static {
46
try {
47
rsrc = ResourceBundle.getBundle(
48
"sun.tools.native2ascii.resources.MsgNative2ascii");
49
} catch (MissingResourceException e) {
50
throw new Error("Missing message file.");
51
}
52
}
53
54
public static void main(String args[]) throws Throwable {
55
// Execute command in another vm. Verify stdout for expected err msg.
56
57
// Test with no input file given.
58
checkResult(executeCmd("-encoding"), "err.bad.arg");
59
60
File f0 = new File(System.getProperty("test.src", "."), "test123");
61
String path0 = f0.getPath();
62
if ( f0.exists() ) {
63
throw new Error("Input file should not exist: " + path0);
64
}
65
checkResult(executeCmd(path0), "err.cannot.read");
66
67
File f1 = new File(System.getProperty("test.src", "."), "test1");
68
File f2 = File.createTempFile("test2", ".tmp");
69
String path1 = f1.getPath();
70
String path2 = f2.getPath();
71
if ( !f1.exists() ) {
72
throw new Error("Missing input file: " + path1);
73
}
74
if ( !f2.setWritable(false) ) {
75
throw new Error("Output file cannot be made read only: " + path2);
76
}
77
f2.deleteOnExit();
78
if ( f2.canWrite() ) {
79
String msg = "Output file is still writable. " +
80
"Probably because test is run as root. Read-only test skipped.";
81
System.out.println(msg);
82
} else {
83
// Test write to a read-only file.
84
checkResult(executeCmd(path1, path2), "err.cannot.write");
85
}
86
}
87
88
private static String executeCmd(String... toolArgs) throws Throwable {
89
JDKToolLauncher cmd = JDKToolLauncher.createUsingTestJDK("native2ascii");
90
for (String s : toolArgs) {
91
cmd.addToolArg(s);
92
}
93
OutputAnalyzer output = ProcessTools.executeProcess(cmd.getCommand());
94
if (output == null || output.getStdout() == null) {
95
throw new Exception("Output was null. Process did not finish correctly.");
96
}
97
if (output.getExitValue() == 0) {
98
throw new Exception("Process exit code was 0, but error was expected.");
99
}
100
return output.getStdout();
101
}
102
103
private static void checkResult(
104
String errorReceived, String errorKey) throws Exception {
105
String errorExpected = rsrc.getString(errorKey);
106
if (errorExpected == null) {
107
throw new Exception("No error message for key: " + errorKey);
108
}
109
// Remove template tag from error message.
110
errorExpected = errorExpected.replaceAll("\\{0\\}", "");
111
112
System.out.println("received: " + errorReceived);
113
System.out.println("expected: " + errorExpected);
114
if (errorReceived.indexOf(errorExpected) < 0) {
115
throw new RuntimeException("Native2ascii bad arg error broken.");
116
}
117
}
118
119
}
120
121