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