test/jdk/java/nio/channels/FileChannel/Size.java
author jbhateja
Tue, 26 Nov 2019 16:09:25 +0300
changeset 59278 8375560db76b
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234394: C2: Dynamic register class support in ADLC Reviewed-by: vlivanov, sviswanathan, thartmann, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
21840
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4563125
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test size method of FileChannel
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    27
 * @run main/othervm Size
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 21840
diff changeset
    28
 * @key randomness
2
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
import java.nio.MappedByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Testing FileChannel's size method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class Size {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
21840
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    44
        testSmallFile();
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    45
        testLargeFile();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
21840
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    48
    private static void testSmallFile() throws Exception {
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    49
        File smallFile = new File("smallFileTest");
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    50
        Random generator = new Random();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        for(int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            long testSize = generator.nextInt(1000);
21840
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    53
            initTestFile(smallFile, testSize);
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    54
            try (FileChannel c = new FileInputStream(smallFile).getChannel()) {
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    55
                if (c.size() != testSize) {
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    56
                    throw new RuntimeException("Size failed in testSmallFile. "
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    57
                                             + "Expect size " + testSize
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    58
                                             + ", actual size " + c.size());
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    59
                }
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    60
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
21840
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    62
        smallFile.deleteOnExit();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Test for bug 4563125
21840
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    66
    private static void testLargeFile() throws Exception {
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    67
        File largeFile = new File("largeFileTest");
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    68
        long testSize = ((long)Integer.MAX_VALUE) * 2;
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    69
        initTestFile(largeFile, 10);
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    70
        try (FileChannel fc = new RandomAccessFile(largeFile, "rw").getChannel())
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    71
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            fc.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            fc.map(FileChannel.MapMode.READ_WRITE, testSize, 10);
21840
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    74
            if (fc.size() != testSize + 10) {
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    75
                throw new RuntimeException("Size failed in testLargeFile. "
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    76
                                         + "Expect size " + (testSize + 10)
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    77
                                         + ", actual size " + fc.size());
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    78
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
21840
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    80
        largeFile.deleteOnExit();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
21840
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    84
     * Create a file with the specified size in bytes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
21840
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    87
    private static void initTestFile(File f, long size) throws Exception {
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    88
        try (BufferedWriter awriter = new BufferedWriter(
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    89
                new OutputStreamWriter(new FileOutputStream(f), "8859_1")))
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    90
        {
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    91
            for(int i=0; i<size; i++) {
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    92
                awriter.write("e");
394835b0d057 8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents: 12538
diff changeset
    93
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
}