Path: blob/master/test/jdk/java/util/Formatter/Flush.java
41152 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 503006326* @summary Basic tests for flush().27*/2829import java.io.Closeable;30import java.io.Flushable;31import java.util.Formatter;32import java.util.FormatterClosedException;3334public class Flush {3536private static class ExpectedException extends RuntimeException {}3738private static class F implements Appendable, Closeable, Flushable {39public Appendable append(CharSequence csq) { return null; }40public Appendable append(char c) { return null; }41public Appendable append(CharSequence csq, int s, int e) {42return null;43}44public void close() {}4546public void flush() {47throw new ExpectedException();48}49}5051private static class NF implements Appendable, Closeable {52public Appendable append(CharSequence csq) { return null; }53public Appendable append(char c) { return null; }54public Appendable append(CharSequence csq, int s, int e) {55return null;56}57public void close() {}5859// method coincidentally called flush()60public void flush() {61throw new RuntimeException("NF.flush should not be called");62}63}6465private static void test(Formatter f) {66if (f.out() instanceof F) {67// F.flush() called since F implements Flushable68try {69f.flush();70throw new RuntimeException("F.flush not called");71} catch (ExpectedException x) {72System.out.println(" F.flush called");73}74} else {75// NF.flush() not called since NF does not implement Flushable76f.flush();77}7879// Formatter is a Flushable80if (!(f instanceof Flushable))81throw new RuntimeException("Formatter is not a Flushable");8283// flush() after close() throws a FormatterClosedException84f.close();85try {86f.flush();87throw new RuntimeException("FormatterClosedException not thrown");88} catch (FormatterClosedException x) {89System.out.println(" FormatterClosedException thrown");90}91}9293public static void main(String [] args) {94System.out.println("testing Flushable");95test(new Formatter(new F()));96System.out.println("testing non-Flushable");97test(new Formatter(new NF()));98}99}100101102