Path: blob/master/test/jdk/java/util/Formatter/Close.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 503006426* @summary Basic tests for close().27*/2829import java.io.Closeable;30import java.util.Formatter;31import java.util.FormatterClosedException;3233public class Close {3435private static class ExpectedException extends RuntimeException {}3637private static class C implements Appendable, Closeable {38public Appendable append(CharSequence csq) { return null; }39public Appendable append(char c) { return null; }40public Appendable append(CharSequence csq, int s, int e) {41return null;42}4344public void close() {45throw new ExpectedException();46}47}4849private static class NC implements Appendable {50public Appendable append(CharSequence csq) { return null; }51public Appendable append(char c) { return null; }52public Appendable append(CharSequence csq, int s, int e) {53return null;54}5556// method coincidentally called close()57public void close() {58throw new RuntimeException("NC.close should not be called");59}60}6162private static void test(Formatter f) {63if (f.out() instanceof C) {64// C.close() called since C implements Closeable65try {66f.close();67throw new RuntimeException("C.close not called");68} catch (ExpectedException x) {69System.out.println(" C.close called");70}71} else {72// NC.close() not called since NC does not implement Closeable73f.close();74}7576// Formatter is a Closeable77if (!(f instanceof Closeable))78throw new RuntimeException("Formatter is not a Closeable");7980// multiple close() does not throw a FormatterClosedException81f.close();82try {83f.close();84System.out.println(" FormatterClosedException not thrown");85} catch (FormatterClosedException x) {86throw new RuntimeException("FormatterClosedException thrown");87}88}8990public static void main(String [] args) {91System.out.println("testing Closeable");92test(new Formatter(new C()));93System.out.println("testing non-Closeable");94test(new Formatter(new NC()));95}96}979899