Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Formatter/Close.java
41152 views
1
/*
2
* Copyright (c) 2004, 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 5030064
27
* @summary Basic tests for close().
28
*/
29
30
import java.io.Closeable;
31
import java.util.Formatter;
32
import java.util.FormatterClosedException;
33
34
public class Close {
35
36
private static class ExpectedException extends RuntimeException {}
37
38
private static class C implements Appendable, Closeable {
39
public Appendable append(CharSequence csq) { return null; }
40
public Appendable append(char c) { return null; }
41
public Appendable append(CharSequence csq, int s, int e) {
42
return null;
43
}
44
45
public void close() {
46
throw new ExpectedException();
47
}
48
}
49
50
private static class NC implements Appendable {
51
public Appendable append(CharSequence csq) { return null; }
52
public Appendable append(char c) { return null; }
53
public Appendable append(CharSequence csq, int s, int e) {
54
return null;
55
}
56
57
// method coincidentally called close()
58
public void close() {
59
throw new RuntimeException("NC.close should not be called");
60
}
61
}
62
63
private static void test(Formatter f) {
64
if (f.out() instanceof C) {
65
// C.close() called since C implements Closeable
66
try {
67
f.close();
68
throw new RuntimeException("C.close not called");
69
} catch (ExpectedException x) {
70
System.out.println(" C.close called");
71
}
72
} else {
73
// NC.close() not called since NC does not implement Closeable
74
f.close();
75
}
76
77
// Formatter is a Closeable
78
if (!(f instanceof Closeable))
79
throw new RuntimeException("Formatter is not a Closeable");
80
81
// multiple close() does not throw a FormatterClosedException
82
f.close();
83
try {
84
f.close();
85
System.out.println(" FormatterClosedException not thrown");
86
} catch (FormatterClosedException x) {
87
throw new RuntimeException("FormatterClosedException thrown");
88
}
89
}
90
91
public static void main(String [] args) {
92
System.out.println("testing Closeable");
93
test(new Formatter(new C()));
94
System.out.println("testing non-Closeable");
95
test(new Formatter(new NC()));
96
}
97
}
98
99