Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/PrintWriter/FailingConstructors.java
41149 views
1
/*
2
* Copyright (c) 2011, 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 7000511
27
* @summary PrintStream, PrintWriter, Formatter, Scanner leave files open when
28
* exception thrown
29
*/
30
31
import java.io.File;
32
import java.io.FileInputStream;
33
import java.io.FileOutputStream;
34
import java.io.FileNotFoundException;
35
import java.io.IOException;
36
import java.io.PrintWriter;
37
import java.io.UnsupportedEncodingException;
38
39
public class FailingConstructors {
40
static final String fileName = "FailingConstructorsTest";
41
static final String UNSUPPORTED_CHARSET = "unknownCharset";
42
static final String FILE_CONTENTS = "This is a small file!";
43
44
private static void realMain(String[] args) throws Throwable {
45
test(false, new File(fileName));
46
47
/* create the file and write its contents */
48
File file = File.createTempFile(fileName, null);
49
file.deleteOnExit();
50
FileOutputStream fos = new FileOutputStream(file);
51
fos.write(FILE_CONTENTS.getBytes());
52
fos.close();
53
54
test(true, file);
55
file.delete();
56
}
57
58
private static void test(boolean exists, File file) throws Throwable {
59
/* PrintWriter(File file, String csn) */
60
try {
61
new PrintWriter(file, UNSUPPORTED_CHARSET);
62
fail();
63
} catch(FileNotFoundException|UnsupportedEncodingException e) {
64
pass();
65
}
66
67
check(exists, file);
68
69
try {
70
new PrintWriter(file, (String)null);
71
fail();
72
} catch(FileNotFoundException|NullPointerException e) {
73
pass();
74
}
75
76
check(exists, file);
77
78
/* PrintWriter(String fileName, String csn) */
79
try {
80
new PrintWriter(file.getName(), UNSUPPORTED_CHARSET);
81
fail();
82
} catch(FileNotFoundException|UnsupportedEncodingException e) {
83
pass();
84
}
85
86
check(exists, file);
87
88
try {
89
new PrintWriter(file.getName(), (String)null);
90
fail();
91
} catch(FileNotFoundException|NullPointerException e) {
92
pass();
93
}
94
95
check(exists, file);
96
}
97
98
private static void check(boolean exists, File file) {
99
if (exists) {
100
/* the file should be unchanged */
101
verifyContents(file);
102
} else {
103
/* the file should not have been created */
104
if (file.exists()) { fail(file + " should not have been created"); }
105
}
106
}
107
108
private static void verifyContents(File file) {
109
try (FileInputStream fis = new FileInputStream(file)) {
110
byte[] contents = FILE_CONTENTS.getBytes();
111
int read, count = 0;
112
while ((read = fis.read()) != -1) {
113
if (read != contents[count++]) {
114
fail("file contents have been altered");
115
return;
116
}
117
}
118
} catch (IOException ioe) {
119
unexpected(ioe);
120
}
121
}
122
123
//--------------------- Infrastructure ---------------------------
124
static volatile int passed = 0, failed = 0;
125
static void pass() {passed++;}
126
static void fail() {failed++; Thread.dumpStack();}
127
static void fail(String message) {System.out.println(message); fail(); }
128
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
129
public static void main(String[] args) throws Throwable {
130
try {realMain(args);} catch (Throwable t) {unexpected(t);}
131
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
132
if (failed > 0) throw new AssertionError("Some tests failed");}
133
}
134
135