jdk/test/java/io/FileOutputStream/OpsAfterClose.java
author zgu
Tue, 16 Aug 2011 09:18:47 -0400
changeset 10248 62296fcbd7cb
parent 2 90ce3da70b43
child 21596 0e3a39f29dbc
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 *  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *  @bug 6359397
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *  @summary Test if FileOutputStream methods will check if the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 *          has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
public enum OpsAfterClose {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
        WRITE { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
                        r.write(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
        WRITE_BUF { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
                        byte buf[] = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
                        r.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
            } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
        WRITE_BUF_OFF { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
                        byte buf[] = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
                        int len = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
                        r.write(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        GET_CHANNEL { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                    r.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        GET_FD { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                        r.getFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
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
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        CLOSE { boolean check(FileOutputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    return true; // No Exceptin thrown on Windows
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; // Exception thrown on solaris and linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
             } };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    abstract boolean check(FileOutputStream r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        File f = new File(System.getProperty("test.dir", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                          "f.txt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        f.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        FileOutputStream fis = new FileOutputStream(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (testFileOutputStream(fis)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new Exception("Test failed for some of the operation{s}" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                " on FileOutputStream, check the messages");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static boolean testFileOutputStream(FileOutputStream r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        boolean result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.out.println("Testing File:" + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (OpsAfterClose op : OpsAfterClose.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            result = op.check(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (!result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
           System.out.println(op + ":" + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            System.out.println("Test failed for the failed operation{s}" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        " above for the FileOutputStream:" + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return failed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}