1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.File; 25 import java.io.IOException; 26 import java.nio.channels.AsynchronousFileChannel; 27 import java.nio.channels.FileChannel; 28 import java.nio.channels.FileLock; 29 import java.nio.file.StandardOpenOption; 30 31 /* 32 * @test 33 * @bug 6880737 34 * @summary Test FileLock constructor parameter validation. 35 */ 36 public class FileLockConstructor { 37 public static void main(String[] args) throws IOException { 38 FileLock fileLock = null; 39 int failures = 0; 40 41 // null FileChannel 42 boolean exceptionThrown = false; 43 try { 44 fileLock = new FileLockSub((FileChannel)null, 0, 0, false); 45 } catch (NullPointerException npe) { 46 exceptionThrown = true; 47 } 48 if (!exceptionThrown) { 49 System.err.println("FileLock constructor did not throw NPE for null FileChannel"); 50 failures++; 51 } 52 53 // null AsynchronousFileChannel 54 exceptionThrown = false; 55 try { 56 fileLock = new FileLockSub((AsynchronousFileChannel)null, 0, 0, true); 57 } catch (NullPointerException npe) { 58 exceptionThrown = true; 59 } 60 if (!exceptionThrown) { 61 System.err.println("FileLock constructor did not throw NPE for null AsynchronousFileChannel"); 62 failures++; 63 } 64 65 // create temporary file 66 File tmpFile = File.createTempFile("FileLock", "tmp"); 67 tmpFile.deleteOnExit(); 68 69 // position and size preconditions 70 long[][] posAndSize = new long[][] { 71 {0, 42}, // valid 72 {-1, 42}, // invalid: position < 0 73 {0, -1}, // invalid: size < 0 74 {Long.MAX_VALUE, 1} // invalid: position + size < 0 75 }; 76 77 // test position and size preconditions for FileChannel case 78 try (FileChannel syncChannel = FileChannel.open(tmpFile.toPath(), 79 StandardOpenOption.READ, StandardOpenOption.WRITE)) { 80 81 for (int i = 0; i < posAndSize.length; i++) { 82 boolean preconditionsHold = i == 0; 83 exceptionThrown = false; 84 try { 85 fileLock = new FileLockSub(syncChannel, posAndSize[i][0], 86 posAndSize[i][1], true); 87 } catch (IllegalArgumentException iae) { 88 exceptionThrown = true; 89 } catch (Exception e) { 90 System.err.println("Unexpected exception \"" + e + "\" caught" 91 + " for position " + posAndSize[i][0] + " and size " 92 + posAndSize[i][1] + " for FileChannel variant"); 93 failures++; 94 continue; 95 } 96 if (preconditionsHold && exceptionThrown) { 97 System.err.println("FileLock constructor incorrectly threw IAE" 98 + " for position " + posAndSize[i][0] + " and size " 99 + posAndSize[i][1] + " for FileChannel variant"); 100 failures++; 101 } else if (!preconditionsHold && !exceptionThrown) { 102 System.err.println("FileLock constructor did not throw IAE" 103 + " for position " + posAndSize[i][0] + " and size " 104 + posAndSize[i][1] + " for FileChannel variant"); 105 failures++; 106 } 107 } 108 } 109 110 // test position and size preconditions for AsynchronousFileChannel case 111 try (AsynchronousFileChannel asyncChannel 112 = AsynchronousFileChannel.open(tmpFile.toPath(), 113 StandardOpenOption.READ, StandardOpenOption.WRITE)) { 114 for (int i = 0; i < posAndSize.length; i++) { 115 boolean preconditionsHold = i == 0; 116 exceptionThrown = false; 117 try { 118 fileLock = new FileLockSub(asyncChannel, posAndSize[i][0], 119 posAndSize[i][1], true); 120 } catch (IllegalArgumentException iae) { 121 exceptionThrown = true; 122 } catch (Exception e) { 123 System.err.println("Unexpected exception \"" + e + "\" caught" 124 + " for position " + posAndSize[i][0] + " and size " 125 + posAndSize[i][1] + " for AsynchronousFileChannel variant"); 126 failures++; 127 continue; 128 } 129 if (preconditionsHold && exceptionThrown) { 130 System.err.println("FileLock constructor incorrectly threw IAE" 131 + " for position " + posAndSize[i][0] + " and size " 132 + posAndSize[i][1] + " for AsynchronousFileChannel variant"); 133 failures++; 134 } else if (!preconditionsHold && !exceptionThrown) { 135 System.err.println("FileLock constructor did not throw IAE" 136 + " for position " + posAndSize[i][0] + " and size " 137 + posAndSize[i][1] + " for AsynchronousFileChannel variant"); 138 failures++; 139 } 140 } 141 } 142 143 if (failures > 0) { 144 throw new RuntimeException("Incurred " + failures + 145 " failures while testing FileLock."); 146 } 147 } 148 } 149 150 class FileLockSub extends FileLock { 151 FileLockSub(FileChannel channel, long position, long size, boolean shared) { 152 super(channel, position, size, shared); 153 } 154 155 FileLockSub(AsynchronousFileChannel channel, long position, long size, 156 boolean shared) { 157 super(channel, position, size, shared); 158 } 159 160 @Override 161 public boolean isValid() { 162 return false; 163 } 164 165 @Override 166 public void release() throws IOException { 167 // do nothing 168 } 169 }