jdk/test/java/io/RandomAccessFile/OpsAfterClose.java
author duke
Wed, 05 Jul 2017 22:06:37 +0200
changeset 40471 ad45133ff99e
parent 23010 6dadb192ad81
permissions -rw-r--r--
Merge
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 RandomAccessFile 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(RandomAccessFile 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(RandomAccessFile 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
                        int len = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                        r.read(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        GET_CHANNEL { boolean check(RandomAccessFile r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                    r.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        GET_FD { boolean check(RandomAccessFile r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                        r.getFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        GET_FILE_PTR { boolean check(RandomAccessFile r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                        r.getFilePointer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    }
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
        GET_LENGTH { boolean check(RandomAccessFile r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                        r.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        SEEK { boolean check(RandomAccessFile r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        r.seek(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        SET_LENGTH { boolean check(RandomAccessFile r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        r.setLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        SKIP_BYTES { boolean check(RandomAccessFile r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        r.skipBytes(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        WRITE { boolean check(RandomAccessFile r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        r.write(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        WRITE_BUF { boolean check(RandomAccessFile r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        byte buf[] = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        int len = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        r.write(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
             } },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        CLOSE { boolean check(RandomAccessFile r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    return true; // No Exception thrown on windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                } catch (IOException io) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    System.out.print("Excep Msg: "+ io.getMessage() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    return true; // Exception thrown on solaris and linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             } };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    abstract boolean check(RandomAccessFile r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        File f = new File(System.getProperty("test.dir", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                          "raf.txt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        f.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (testRandomAccessFile(raf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new Exception("Test failed for some of the operation{s}" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                " on RandomAccessFile, check the messages");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private static boolean testRandomAccessFile(RandomAccessFile r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        boolean result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        System.out.println("Testing File:" + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        for (OpsAfterClose op : OpsAfterClose.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            result = op.check(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (!result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
           System.out.println(op + ":" + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            System.out.println("Test failed for the failed operation{s}" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        " above for the RandomAccessFile:" + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return failed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}