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 5030623 27 * @summary Basic test of all append() methods in Writer and inherited classes. 28 */ 29 30 package test.java.io.Writer; 31 32 import java.io.*; 33 import java.lang.reflect.*; 34 35 public class Append { 36 // append methods throw IOException 37 private static Class [] io = { 38 Writer.class, BufferedWriter.class, FilterWriter.class, 39 OutputStreamWriter.class, FileWriter.class 40 }; 41 42 // append methods don't throw IOException 43 private static Class [] nio = { 44 CharArrayWriter.class, StringWriter.class, PrintWriter.class, 45 PrintStream.class 46 }; 47 main(String [] args)48 public static void main(String [] args) { 49 for (int i = 0; i < io.length; i++) 50 test(io[i], true); 51 for (int i = 0; i < nio.length; i++) 52 test(nio[i], false); 53 } 54 test(Class c, boolean io)55 private static void test(Class c, boolean io) { 56 try { 57 Class [] cparams = { char.class }; 58 test(c.getMethod("append", cparams), io); 59 Class [] csparams = { CharSequence.class }; 60 test(c.getMethod("append", csparams), io); 61 } catch (NoSuchMethodException x) { 62 throw new RuntimeException("No append method found"); 63 } 64 } 65 test(Method m, boolean io)66 private static void test(Method m, boolean io) { 67 Class [] ca = m.getExceptionTypes(); 68 boolean found = false; 69 for (int i = 0; i < ca.length; i++) { 70 if (ca[i].equals(IOException.class)) { 71 found = true; 72 break; 73 } 74 } 75 76 if (found && !io) 77 throw new RuntimeException("Unexpected IOException"); 78 if (!found && io) 79 throw new RuntimeException("Missing IOException"); 80 } 81 } 82