jdk/test/java/io/FileOutputStream/OpsAfterClose.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 21596 0e3a39f29dbc
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21596
diff changeset
     2
 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     4
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     8
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    13
 * accompanied this code).
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    14
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    18
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    21
 * questions.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    22
 */
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    23
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *  @bug 6359397
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary Test if FileOutputStream methods will check if the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public enum OpsAfterClose {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        WRITE { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
                        r.write(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        WRITE_BUF { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                        byte buf[] = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                        r.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        WRITE_BUF_OFF { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                        byte buf[] = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                        int len = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                        r.write(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        GET_CHANNEL { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    r.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        GET_FD { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        r.getFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        CLOSE { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    return true; // No Exceptin thrown on Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    return true; // Exception thrown on solaris and linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
             } };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    abstract boolean check(FileOutputStream r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        File f = new File(System.getProperty("test.dir", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                          "f.txt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        f.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        FileOutputStream fis = new FileOutputStream(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (testFileOutputStream(fis)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new Exception("Test failed for some of the operation{s}" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                " on FileOutputStream, check the messages");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static boolean testFileOutputStream(FileOutputStream r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        boolean result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println("Testing File:" + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (OpsAfterClose op : OpsAfterClose.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            result = op.check(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (!result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
           System.out.println(op + ":" + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.out.println("Test failed for the failed operation{s}" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        " above for the FileOutputStream:" + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return failed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}