test/jdk/java/nio/channels/FileChannel/Lock.java
author bpb
Mon, 30 Apr 2018 13:40:39 -0700
changeset 49930 3aaaa5370999
parent 47216 71c04702a3d5
permissions -rw-r--r--
8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13 Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
     2
 * Copyright (c) 2001, 2016, 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
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    25
 * @bug 4429043 4493595 6332756 6709457 7146506
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    26
 * @summary Test FileChannel file locking
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.*;
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
    31
import static java.nio.file.StandardOpenOption.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Testing FileChannel's lock method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class Lock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public static void main(String[] args) throws Exception {
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    39
        if (args.length == 2) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    40
            attemptLock(args[1], args[0].equals("2"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            return;
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    42
        } else if (args.length != 0) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    43
            throw new RuntimeException("Wrong number of parameters.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        File blah = File.createTempFile("blah", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        blah.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        RandomAccessFile raf = new RandomAccessFile(blah, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        raf.write(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        test1(blah, "1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        test1(blah, "2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        test2(blah, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        test2(blah, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        test3(blah);
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
    55
        test4(blah);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    58
    /**
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    59
     * Test mutual locking with other process
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    60
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static void test1(File blah, String str) throws Exception {
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    62
        try (RandomAccessFile fis = new RandomAccessFile(blah, "rw")) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    63
            FileChannel fc = fis.getChannel();
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    64
            FileLock lock = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    66
            // grab the lock
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    67
            if (str.equals("1")) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    68
                lock = fc.lock(0, 10, false);
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    69
                if (lock == null)
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    70
                    throw new RuntimeException("Lock should not return null");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    71
                try {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    72
                    fc.lock(5, 10, false);
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    73
                    throw new RuntimeException("Overlapping locks allowed");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    74
                } catch (OverlappingFileLockException e) {} // correct result
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    75
            }
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    76
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    77
            // execute the tamperer
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    78
            String command = System.getProperty("java.home") +
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    79
                File.separator + "bin" + File.separator + "java";
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    80
            String testClasses = System.getProperty("test.classes");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    81
            if (testClasses != null)
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    82
                command += " -cp " + testClasses;
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    83
            command += " Lock " + str + " " + blah;
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    84
            Process p = Runtime.getRuntime().exec(command);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    86
            // evaluate System.out of child process
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    87
            String s;
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    88
            boolean hasOutput = false;
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    89
            InputStreamReader isr;
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    90
            isr = new InputStreamReader(p.getInputStream());
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    91
            BufferedReader br = new BufferedReader(isr);
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    92
            while ((s = br.readLine()) != null) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    93
                // only throw on Unix as windows over NFS fails...
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    94
                if ((File.separatorChar == '/') && !s.equals("good")) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    95
                    throw new RuntimeException("Failed: " + s);
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    96
                }
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    97
                hasOutput = true;
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    98
            }
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
    99
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   100
            // evaluate System.err in case of System.out of child process
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   101
            // was empty
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   102
            if (!hasOutput) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   103
                isr = new InputStreamReader(p.getErrorStream());
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   104
                br = new BufferedReader(isr);
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   105
                if ((s = br.readLine()) != null) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   106
                    System.err.println("Error output:");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   107
                    System.err.println(s);
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   108
                    while ((s = br.readLine()) != null) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   109
                        System.err.println(s);
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   110
                    }
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   111
                }
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   112
                throw new RuntimeException("Failed, no output");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   113
            }
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   114
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   115
            // clean up, check multiple releases
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   116
            if (lock != null) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   117
                lock.release();
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   118
                lock.release();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   121
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   123
    /**
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   124
     * Basic test for FileChannel.lock() and FileChannel.tryLock()
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   125
     */
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   126
    static void test2(File blah, boolean b) throws Exception {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   127
        try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   128
            FileChannel channel = raf.getChannel();
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   129
            FileLock lock;
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   130
            if (b)
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   131
                lock = channel.lock();
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   132
            else
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   133
                lock = channel.tryLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            lock.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   138
    /**
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   139
     * Test that overlapping file locking is not possible when using different
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   140
     * FileChannel objects to the same file path
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   141
     */
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   142
    static void test3(File blah) throws Exception {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   143
        try (RandomAccessFile raf1 = new RandomAccessFile(blah, "rw");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   144
             RandomAccessFile raf2 = new RandomAccessFile(blah, "rw"))
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   145
        {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   146
            FileChannel fc1 = raf1.getChannel();
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   147
            FileChannel fc2 = raf2.getChannel();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   149
            // lock via one channel, and then attempt to lock the same file
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   150
            // using a second channel
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   151
            FileLock fl1 = fc1.lock();
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   152
            try {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   153
                fc2.tryLock();
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   154
                throw new RuntimeException("Overlapping locks allowed");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   155
            } catch (OverlappingFileLockException x) {}
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   156
            try {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   157
                fc2.lock();
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   158
                throw new RuntimeException("Overlapping locks allowed");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   159
            } catch (OverlappingFileLockException x) {}
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   160
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   161
            // release lock and the attempt to lock with the second channel
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   162
            // should succeed.
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   163
            fl1.release();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            fc2.lock();
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   165
            try {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   166
                fc1.lock();
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   167
                throw new RuntimeException("Overlapping locks allowed");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   168
            } catch (OverlappingFileLockException x) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   171
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   172
    /**
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   173
     * Test file locking when file is opened for append
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   174
     */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   175
    static void test4(File blah) throws Exception {
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   176
        try (FileOutputStream fos = new FileOutputStream(blah, true)) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   177
            FileChannel fc = fos.getChannel();
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   178
            fc.tryLock().release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   179
            fc.tryLock(0L, 1L, false).release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   180
            fc.lock().release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   181
            fc.lock(0L, 1L, false).release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   182
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   183
        try (FileChannel fc = FileChannel.open(blah.toPath(), APPEND)) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   184
            fc.tryLock().release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   185
            fc.tryLock(0L, 1L, false).release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   186
            fc.lock().release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   187
            fc.lock(0L, 1L, false).release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   188
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   189
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
40566
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   191
    /**
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   192
     * Utility method to be run in secondary process which tries to acquire a
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   193
     * lock on a FileChannel
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   194
     */
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   195
    static void attemptLock(String fileName,
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   196
                            boolean expectsLock) throws Exception
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   197
    {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   198
        File f = new File(fileName);
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   199
        try (RandomAccessFile raf = new RandomAccessFile(f, "rw")) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   200
            FileChannel fc = raf.getChannel();
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   201
            if (fc.tryLock(10, 10, false) == null) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   202
                System.out.println("bad: Failed to grab adjacent lock");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   203
            }
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   204
            if (fc.tryLock(0, 10, false) == null) {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   205
                if (expectsLock)
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   206
                    System.out.println("bad");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   207
                else
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   208
                    System.out.println("good");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   209
            } else {
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   210
                if (expectsLock)
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   211
                    System.out.println("good");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   212
                else
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   213
                    System.out.println("bad");
cf1f1b3822e7 8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents: 7668
diff changeset
   214
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
}