Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/StringBuffer/Exceptions.java
41149 views
1
/*
2
* Copyright (c) 2002, 2005, 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 4414306 6248507
27
* @summary Verify that exceptions are thrown as expected.
28
*/
29
30
public class Exceptions {
31
private static boolean ok = true;
32
33
private static void fail(Throwable ex, String s, Throwable got) {
34
ok = false;
35
System.err.println("expected "
36
+ ex.getClass().getName() + ": " + ex.getMessage()
37
+ " for " + s
38
+ " got "
39
+ got.getClass().getName() + ": " + got.getMessage()
40
+ " - FAILED");
41
}
42
43
private static void pass(String s) {
44
System.out.println(s + " -- OK");
45
}
46
47
private static void tryCatch(String s, Throwable ex, Runnable thunk) {
48
Throwable t = null;
49
try {
50
thunk.run();
51
} catch (Throwable x) {
52
// x.printStackTrace();
53
if (ex.getClass().isAssignableFrom(x.getClass()))
54
t = x;
55
else
56
x.printStackTrace();
57
}
58
if ((t == null) && (ex != null))
59
fail(ex, s, t);
60
61
String msg = (ex == null ? null : ex.getMessage());
62
if ((msg != null) && !msg.equals(t.getMessage()))
63
fail(ex, s, t);
64
else
65
pass(s);
66
}
67
68
public static void main(String [] args) {
69
System.out.println("StringBuffer()");
70
tryCatch(" no args", null, new Runnable() {
71
public void run() {
72
new StringBuffer();
73
}});
74
75
System.out.println("StringBuffer(int length)");
76
tryCatch(" 1", null, new Runnable() {
77
public void run() {
78
new StringBuffer(1);
79
}});
80
tryCatch(" -1", new NegativeArraySizeException(), new Runnable() {
81
public void run() {
82
new StringBuffer(-1);
83
}});
84
85
System.out.println("StringBuffer(String str)");
86
tryCatch(" null", new NullPointerException(), new Runnable() {
87
public void run() {
88
new StringBuffer(null);
89
}});
90
tryCatch(" foo", null, new Runnable() {
91
public void run() {
92
new StringBuffer("foo");
93
}});
94
95
System.out.println("StringBuffer.replace(int start, int end, String str)");
96
tryCatch(" -1, 2, \" \"",
97
new StringIndexOutOfBoundsException("start -1, end 2, length 7"),
98
new Runnable() {
99
public void run() {
100
StringBuffer sb = new StringBuffer("hilbert");
101
sb.replace(-1, 2, " ");
102
}});
103
104
tryCatch(" 7, 8, \" \"",
105
new StringIndexOutOfBoundsException("start 7, end 6, length 6"),
106
new Runnable() {
107
public void run() {
108
StringBuffer sb = new StringBuffer("banach");
109
sb.replace(7, 8, " ");
110
}});
111
tryCatch(" 2, 1, \" \"",
112
new StringIndexOutOfBoundsException("start 2, end 1, length 7"),
113
new Runnable() {
114
public void run() {
115
StringBuffer sb = new StringBuffer("riemann");
116
sb.replace(2, 1, " ");
117
}});
118
119
if (!ok)
120
throw new RuntimeException("Some tests FAILED");
121
else
122
System.out.println("All tests PASSED");
123
}
124
}
125
126