1 /* 2 * Copyright (c) 2011, 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 /* 25 * @test 26 * @bug 7087549 27 * @summary Test custom options with newInputStream. 28 * @author Brandon Passanisi 29 * @library .. 30 * @build CustomOptions PassThroughFileSystem 31 */ 32 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.net.URI; 36 import java.nio.file.*; 37 import java.nio.file.attribute.FileAttribute; 38 import java.nio.file.spi.FileSystemProvider; 39 import java.nio.channels.SeekableByteChannel; 40 import java.util.Collections; 41 import java.util.Set; 42 import java.util.Map; 43 44 public class CustomOptions { 45 46 // Create a custom option 47 static enum CustomOption implements OpenOption { 48 IGNORE, 49 } 50 51 // number of times that IGNORE option is observed 52 static int ignoreCount; 53 54 // A pass through provider that supports a custom open option 55 static class MyCustomProvider extends PassThroughFileSystem.PassThroughProvider { 56 public MyCustomProvider() { } 57 58 @Override 59 public SeekableByteChannel newByteChannel(Path path, 60 Set<? extends OpenOption> options, 61 FileAttribute<?>... attrs) 62 throws IOException 63 { 64 if (options.contains(CustomOption.IGNORE)) { 65 ignoreCount++; 66 options.remove(CustomOption.IGNORE); 67 } 68 return super.newByteChannel(path, options, attrs); 69 } 70 } 71 72 public static void main(String[] args) throws Exception { 73 FileSystemProvider provider = new MyCustomProvider(); 74 Map<String,?> env = Collections.emptyMap(); 75 URI uri = URI.create("pass:///"); 76 FileSystem fs = provider.newFileSystem(uri, env); 77 78 // Create temp dir for testing 79 Path dir = TestUtil.createTemporaryDirectory(); 80 try { 81 82 // Create temp file for testing 83 Path path = fs.getPath(dir.resolve("foo").toString()); 84 Files.createFile(path); 85 86 // Test custom option 87 Files.newInputStream(path, CustomOption.IGNORE).close(); 88 if (ignoreCount != 1) 89 throw new RuntimeException("IGNORE option not passed through"); 90 91 // Test null option 92 try { 93 Files.newInputStream(path, new OpenOption[] { null }).close(); 94 throw new RuntimeException("NullPointerException expected"); 95 } catch (NullPointerException ignore) { } 96 97 // Test unsupported options 98 try { 99 Files.newInputStream(path, StandardOpenOption.WRITE).close(); 100 throw new RuntimeException("UnsupportedOperationException expected"); 101 } catch (UnsupportedOperationException uoe) { } 102 try { 103 Files.newInputStream(path, StandardOpenOption.APPEND).close(); 104 throw new RuntimeException("UnsupportedOperationException expected"); 105 } catch (UnsupportedOperationException uoe) { } 106 107 } finally { 108 // Cleanup 109 TestUtil.removeAll(dir); 110 } 111 } 112 }