/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.lang.ref.Reference; import java.lang.ref.WeakReference; import java.nio.channels.FileLock; import java.nio.channels.OverlappingFileLockException; import java.nio.file.Files; import java.nio.file.Path; /* * @test * @bug 8166253 * @summary Verify that OverlappingFileLockException is thrown when expected. */ public class FileLockGC { public enum TestType { DEFAULT(true), GC(true), RELEASE(false), GC_THEN_RELEASE(true), RELEASE_THEN_GC(false); private final boolean exceptionExpected; TestType(boolean exceptionExpected) { this.exceptionExpected = exceptionExpected; } boolean exceptionExpected() { return exceptionExpected; } } public static void main(String[] args) throws Exception { final File f = new File(System.getProperty("test.dir", ".") + File.separator + "junk.txt"); final Path p = f.toPath(); int failures = 0; for (TestType t : TestType.values()) { try { if (!testFileLockGC(f, t)) { failures++; } } finally { Files.delete(p); } } if (failures != 0) { throw new RuntimeException("Test had " + failures + " failure(s)"); } } private static void gc() { System.out.println("Performing GC"); System.gc(); } private static void release(Reference ref) throws IOException { Object lock = ref.get(); if (lock != null) { System.out.println("Releasing lock"); ((FileLock)lock).release(); } } private static boolean testFileLockGC(File f, TestType type) throws IOException { System.out.printf("Test %s starting%n", type.toString()); final RandomAccessFile raf1 = new RandomAccessFile(f, "rw"); WeakReference ref1 = new WeakReference(raf1.getChannel().tryLock()); switch (type) { case GC: gc(); break; case RELEASE: release(ref1); break; case GC_THEN_RELEASE: // this case should usually be the same as the GC-only case // as the reference should have been cleared such that // release() will have no effect gc(); release(ref1); break; case RELEASE_THEN_GC: release(ref1); gc(); break; default: break; } final RandomAccessFile raf2 = new RandomAccessFile(f, "rw"); boolean success = true; WeakReference ref2 = null; try { ref2 = new WeakReference(raf2.getChannel().tryLock()); if (type.exceptionExpected()) { System.err.printf ("No expected OverlappingFileLockException for test %s%n", type.toString()); success = false; } } catch (OverlappingFileLockException ofe) { if (!type.exceptionExpected()) { System.err.printf ("Unexpected OverlappingFileLockException for test %s%n", type.toString()); success = false; } } finally { if (type == TestType.DEFAULT | type == TestType.GC) { release(ref1); } if (ref2 != null) { release(ref2); } raf2.close(); raf1.close(); System.out.printf("Test %s finished%n", type.toString()); } return success; } }