test/jdk/java/io/FileInputStream/OpsAfterClose.java
author rriggs
Fri, 01 Dec 2017 16:40:08 -0500
changeset 48224 be0df5ab3093
parent 47216 71c04702a3d5
permissions -rw-r--r--
8080225: FileInput/OutputStream/FileChannel cleanup should be improved Reviewed-by: mchung, plevart, bpb
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 FileInputStream 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
        READ { boolean check(FileInputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
                        r.read();
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
        READ_BUF { boolean check(FileInputStream 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.read(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
        READ_BUF_OFF { boolean check(FileInputStream 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.read(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(FileInputStream 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(FileInputStream 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
        SKIP { boolean check(FileInputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                        r.skip(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        CLOSE { boolean check(FileInputStream r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    return true; // No Exception thrown on windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    return true; // Exception thrown on solaris and linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
             } };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    abstract boolean check(FileInputStream r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        File f = new File(System.getProperty("test.dir", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                          "f.txt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        f.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        FileInputStream fis = new FileInputStream(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (testFileInputStream(fis)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw new Exception("Test failed for some of the operation{s}" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                " on FileInputStream, check the messages");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static boolean testFileInputStream(FileInputStream r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        boolean result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.out.println("Testing File:" + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (OpsAfterClose op : OpsAfterClose.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            result = op.check(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (!result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
           System.out.println(op + ":" + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.out.println("Test failed for the failed operation{s}" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        " above for the FileInputStream:" + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return failed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}