Path: blob/master/test/jdk/java/nio/file/Files/CustomOptions.java
41153 views
/*1* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 708754926* @summary Test custom options with newInputStream.27* @author Brandon Passanisi28* @library ..29* @build CustomOptions PassThroughFileSystem30* @run main CustomOptions31*/3233import java.io.IOException;34import java.io.InputStream;35import java.net.URI;36import java.nio.file.*;37import java.nio.file.attribute.FileAttribute;38import java.nio.file.spi.FileSystemProvider;39import java.nio.channels.SeekableByteChannel;40import java.util.Collections;41import java.util.Set;42import java.util.Map;4344public class CustomOptions {4546// Create a custom option47static enum CustomOption implements OpenOption {48IGNORE,49}5051// number of times that IGNORE option is observed52static int ignoreCount;5354// A pass through provider that supports a custom open option55static class MyCustomProvider extends PassThroughFileSystem.PassThroughProvider {56public MyCustomProvider() { }5758@Override59public SeekableByteChannel newByteChannel(Path path,60Set<? extends OpenOption> options,61FileAttribute<?>... attrs)62throws IOException63{64if (options.contains(CustomOption.IGNORE)) {65ignoreCount++;66options.remove(CustomOption.IGNORE);67}68return super.newByteChannel(path, options, attrs);69}70}7172public static void main(String[] args) throws Exception {73FileSystemProvider provider = new MyCustomProvider();74Map<String,?> env = Collections.emptyMap();75URI uri = URI.create("pass:///");76FileSystem fs = provider.newFileSystem(uri, env);7778// Create temp dir for testing79Path dir = TestUtil.createTemporaryDirectory();80try {8182// Create temp file for testing83Path path = fs.getPath(dir.resolve("foo").toString());84Files.createFile(path);8586// Test custom option87Files.newInputStream(path, CustomOption.IGNORE).close();88if (ignoreCount != 1)89throw new RuntimeException("IGNORE option not passed through");9091// Test null option92try {93Files.newInputStream(path, new OpenOption[] { null }).close();94throw new RuntimeException("NullPointerException expected");95} catch (NullPointerException ignore) { }9697// Test unsupported options98try {99Files.newInputStream(path, StandardOpenOption.WRITE).close();100throw new RuntimeException("UnsupportedOperationException expected");101} catch (UnsupportedOperationException uoe) { }102try {103Files.newInputStream(path, StandardOpenOption.APPEND).close();104throw new RuntimeException("UnsupportedOperationException expected");105} catch (UnsupportedOperationException uoe) { }106107} finally {108// Cleanup109TestUtil.removeAll(dir);110}111}112}113114115