J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 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 4213822 |
| 27 | * @summary Test that checkError() returns a correct value |
| 28 | * when a PrintWriter is wrapped with another |
| 29 | * PrintWriter. |
| 30 | */ |
| 31 | |
| 32 | import java.io.*; |
| 33 | |
| 34 | public class CheckError { |
| 35 | public static void main(String[] args) throws Exception { |
| 36 | |
| 37 | boolean passTest1 = false; |
| 38 | File file = new File(System.getProperty("test.dir", "."), |
| 39 | "junkie.out"); |
| 40 | |
| 41 | FileWriter fw = new FileWriter(file); |
| 42 | |
| 43 | PrintWriter ppw = new PrintWriter( |
| 44 | new PrintWriter(fw)); |
| 45 | |
| 46 | fw.close(); |
| 47 | ppw.println("Hello World!"); |
| 48 | |
| 49 | file.deleteOnExit(); |
| 50 | |
| 51 | if (ppw.checkError()) { |
| 52 | System.out.println("Correct: An error occured in the" + |
| 53 | " underlying writer"); |
| 54 | passTest1 = true; |
| 55 | } |
| 56 | ppw.close(); |
| 57 | |
| 58 | // Test when the underlying stream is a PrintStream |
| 59 | FileOutputStream fos = new FileOutputStream(file); |
| 60 | PrintWriter pps = new PrintWriter( |
| 61 | new PrintStream(fos)); |
| 62 | |
| 63 | fos.close(); |
| 64 | pps.println("Hello World!"); |
| 65 | |
| 66 | if (pps.checkError()) { |
| 67 | System.out.println("Correct: An error occured in the" + |
| 68 | " underlying Stream"); |
| 69 | } else { |
| 70 | if (!passTest1) { |
| 71 | throw new Exception("CheckError() returned an incorrect value" + |
| 72 | " when error occured in the underlying Stream" + |
| 73 | " and when error occured in the underlying writer"); |
| 74 | } else { |
| 75 | throw new Exception("CheckError() returned an incorrect value" + |
| 76 | " when the error has occured in the underlying Stream"); |
| 77 | } |
| 78 | } |
| 79 | if (!passTest1) { |
| 80 | throw new Exception("CheckError() returned an incorrect value" + |
| 81 | " when the error has occured in the underlying Writer"); |
| 82 | } |
| 83 | pps.close(); |
| 84 | } |
| 85 | } |