Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/PrintStream/EncodingTest.java
41149 views
1
/*
2
* Copyright (c) 2017, 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
import java.io.File;
25
import java.io.FileOutputStream;
26
import java.io.IOException;
27
import java.io.PrintStream;
28
import java.nio.charset.Charset;
29
import java.nio.charset.StandardCharsets;
30
import java.nio.file.Files;
31
import java.nio.file.Paths;
32
import org.testng.Assert;
33
import org.testng.annotations.DataProvider;
34
import org.testng.annotations.Test;
35
36
/**
37
* @test
38
* @bug 8183743
39
* @summary Test to verify the new overload method with Charset functions the same
40
* as the existing method that takes a charset name.
41
* @run testng EncodingTest
42
*/
43
public class EncodingTest {
44
static String USER_DIR = System.getProperty("user.dir", ".");
45
static boolean AUTOFLUSH = true;
46
public static enum ConstructorType {
47
STRING,
48
FILE,
49
OUTPUTSTREAM
50
}
51
52
/*
53
* DataProvider fields:
54
* Type of the constructor, a file to be written with a charset name,
55
* a file to be written with a charset, charset name, charset.
56
*/
57
@DataProvider(name = "parameters")
58
public Object[][] getParameters() throws IOException {
59
String csn = StandardCharsets.UTF_8.name();
60
Charset charset = StandardCharsets.UTF_8;
61
File file1 = new File(USER_DIR, "PSCharsetTest1.txt");
62
File file2 = new File(USER_DIR, "PSCharsetTest2.txt");
63
64
return new Object[][]{
65
{ConstructorType.STRING, file1, file2, csn, charset},
66
{ConstructorType.FILE, file1, file2, csn, charset},
67
{ConstructorType.OUTPUTSTREAM, file1, file2, csn, charset}
68
};
69
}
70
71
/**
72
* Verifies that the overloading constructor behaves the same as the existing
73
* one.
74
* @param type the type of the constructor
75
* @param file1 file1 written with the name of a charset
76
* @param file2 file2 written with a charset
77
* @param csn the charset name
78
* @param charset the charset
79
* @throws IOException
80
*/
81
@Test(dataProvider = "parameters")
82
public void test(ConstructorType type, File file1, File file2, String csn, Charset charset)
83
throws Exception {
84
createFile(getPrintStream(type, file1.getPath(), csn, null));
85
createFile(getPrintStream(type, file2.getPath(), null, charset));
86
87
Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
88
Files.readAllLines(Paths.get(file2.getPath()), charset));
89
}
90
91
public void createFile(PrintStream out) throws IOException {
92
out.println("high surrogate");
93
out.println(Character.MIN_HIGH_SURROGATE);
94
out.println("low surrogate");
95
out.println(Character.MIN_LOW_SURROGATE);
96
out.flush();
97
out.close();
98
}
99
100
PrintStream getPrintStream(ConstructorType type, String path, String csn, Charset charset)
101
throws IOException {
102
PrintStream out = null;
103
if (csn != null) {
104
switch (type) {
105
case STRING:
106
out = new PrintStream(path, csn);
107
break;
108
case FILE:
109
out = new PrintStream(new File(path), csn);
110
break;
111
case OUTPUTSTREAM:
112
FileOutputStream fout = new FileOutputStream(path);
113
out = new PrintStream(fout, AUTOFLUSH, csn);
114
break;
115
}
116
} else {
117
switch (type) {
118
case STRING:
119
out = new PrintStream(path, charset);
120
break;
121
case FILE:
122
out = new PrintStream(new File(path), charset);
123
break;
124
case OUTPUTSTREAM:
125
FileOutputStream fout = new FileOutputStream(path);
126
out = new PrintStream(fout, AUTOFLUSH, charset);
127
break;
128
}
129
}
130
131
return out;
132
}
133
134
}
135
136