|
1 /* |
|
2 * Copyright 2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /** |
|
25 * @test |
|
26 * @bug 5030063 |
|
27 * @summary Basic tests for flush(). |
|
28 */ |
|
29 |
|
30 import java.io.Closeable; |
|
31 import java.io.Flushable; |
|
32 import java.util.Formatter; |
|
33 import java.util.FormatterClosedException; |
|
34 |
|
35 public class Flush { |
|
36 |
|
37 private static class ExpectedException extends RuntimeException {} |
|
38 |
|
39 private static class F implements Appendable, Closeable, Flushable { |
|
40 public Appendable append(CharSequence csq) { return null; } |
|
41 public Appendable append(char c) { return null; } |
|
42 public Appendable append(CharSequence csq, int s, int e) { |
|
43 return null; |
|
44 } |
|
45 public void close() {} |
|
46 |
|
47 public void flush() { |
|
48 throw new ExpectedException(); |
|
49 } |
|
50 } |
|
51 |
|
52 private static class NF implements Appendable, Closeable { |
|
53 public Appendable append(CharSequence csq) { return null; } |
|
54 public Appendable append(char c) { return null; } |
|
55 public Appendable append(CharSequence csq, int s, int e) { |
|
56 return null; |
|
57 } |
|
58 public void close() {} |
|
59 |
|
60 // method coincidentally called flush() |
|
61 public void flush() { |
|
62 throw new RuntimeException("NF.flush should not be called"); |
|
63 } |
|
64 } |
|
65 |
|
66 private static void test(Formatter f) { |
|
67 if (f.out() instanceof F) { |
|
68 // F.flush() called since F implements Flushable |
|
69 try { |
|
70 f.flush(); |
|
71 throw new RuntimeException("F.flush not called"); |
|
72 } catch (ExpectedException x) { |
|
73 System.out.println(" F.flush called"); |
|
74 } |
|
75 } else { |
|
76 // NF.flush() not called since NF does not implement Flushable |
|
77 f.flush(); |
|
78 } |
|
79 |
|
80 // Formatter is a Flushable |
|
81 if (!(f instanceof Flushable)) |
|
82 throw new RuntimeException("Formatter is not a Flushable"); |
|
83 |
|
84 // flush() after close() throws a FormatterClosedException |
|
85 f.close(); |
|
86 try { |
|
87 f.flush(); |
|
88 throw new RuntimeException("FormatterClosedException not thrown"); |
|
89 } catch (FormatterClosedException x) { |
|
90 System.out.println(" FormatterClosedException thrown"); |
|
91 } |
|
92 } |
|
93 |
|
94 public static void main(String [] args) { |
|
95 System.out.println("testing Flushable"); |
|
96 test(new Formatter(new F())); |
|
97 System.out.println("testing non-Flushable"); |
|
98 test(new Formatter(new NF())); |
|
99 } |
|
100 } |