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