893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A/* @test
893N/A * @bug 4313887
3471N/A * @summary Unit test for java.nio.file.Files.newByteChannel
893N/A * @library ..
893N/A */
893N/A
893N/Aimport java.nio.ByteBuffer;
893N/Aimport java.nio.file.*;
893N/Aimport static java.nio.file.StandardOpenOption.*;
893N/Aimport static com.sun.nio.file.ExtendedOpenOption.*;
893N/Aimport java.nio.file.attribute.FileAttribute;
893N/Aimport java.nio.channels.*;
893N/Aimport java.io.IOException;
893N/Aimport java.util.*;
893N/A
893N/Apublic class SBC {
893N/A
893N/A static boolean supportsLinks;
893N/A
893N/A public static void main(String[] args) throws Exception {
893N/A Path dir = TestUtil.createTemporaryDirectory();
893N/A try {
893N/A supportsLinks = TestUtil.supportsLinks(dir);
893N/A
893N/A // open options
893N/A createTests(dir);
893N/A appendTests(dir);
893N/A truncateExistingTests(dir);
893N/A noFollowLinksTests(dir);
893N/A
893N/A // SeekableByteChannel methods
893N/A sizeTruncatePositionTests(dir);
893N/A
893N/A // platform specific
893N/A if (System.getProperty("os.name").startsWith("Windows"))
893N/A dosSharingOptionTests(dir);
893N/A
893N/A // misc. tests
893N/A badCombinations(dir);
893N/A unsupportedOptions(dir);
893N/A nullTests(dir);
893N/A
893N/A } finally {
893N/A TestUtil.removeAll(dir);
893N/A }
893N/A }
893N/A
893N/A // test CREATE and CREATE_NEW options
893N/A static void createTests(Path dir) throws Exception {
893N/A Path file = dir.resolve("foo");
893N/A
893N/A // CREATE
893N/A try {
893N/A // create file (no existing file)
3471N/A Files.newByteChannel(file, CREATE, WRITE).close();
3471N/A if (Files.notExists(file))
893N/A throw new RuntimeException("File not created");
893N/A
893N/A // create file (existing file)
3471N/A Files.newByteChannel(file, CREATE, WRITE).close();
893N/A
893N/A // create file where existing file is a sym link
893N/A if (supportsLinks) {
3471N/A Path link = Files.createSymbolicLink(dir.resolve("link"), file);
893N/A try {
893N/A // file already exists
3471N/A Files.newByteChannel(link, CREATE, WRITE).close();
893N/A
893N/A // file does not exist
3471N/A Files.delete(file);
3471N/A Files.newByteChannel(link, CREATE, WRITE).close();
3471N/A if (Files.notExists(file))
893N/A throw new RuntimeException("File not created");
893N/A
893N/A } finally {
893N/A TestUtil.deleteUnchecked(link);
893N/A }
893N/A }
893N/A
893N/A } finally {
893N/A TestUtil.deleteUnchecked(file);
893N/A }
893N/A
893N/A // CREATE_NEW
893N/A try {
893N/A // create file
3471N/A Files.newByteChannel(file, CREATE_NEW, WRITE).close();
3471N/A if (Files.notExists(file))
893N/A throw new RuntimeException("File not created");
893N/A
893N/A // create should fail
893N/A try {
893N/A SeekableByteChannel sbc =
3471N/A Files.newByteChannel(file, CREATE_NEW, WRITE);
893N/A sbc.close();
893N/A throw new RuntimeException("FileAlreadyExistsException not thrown");
893N/A } catch (FileAlreadyExistsException x) { }
893N/A
893N/A // create should fail
893N/A if (supportsLinks) {
893N/A Path link = dir.resolve("link");
893N/A Path target = dir.resolve("thisDoesNotExist");
3471N/A Files.createSymbolicLink(link, target);
893N/A try {
893N/A
893N/A try {
893N/A SeekableByteChannel sbc =
3471N/A Files.newByteChannel(file, CREATE_NEW, WRITE);
893N/A sbc.close();
893N/A throw new RuntimeException("FileAlreadyExistsException not thrown");
893N/A } catch (FileAlreadyExistsException x) { }
893N/A
893N/A } finally {
893N/A TestUtil.deleteUnchecked(link);
893N/A }
893N/A }
893N/A
893N/A
893N/A } finally {
893N/A TestUtil.deleteUnchecked(file);
893N/A }
893N/A
893N/A // CREATE_NEW + SPARSE
893N/A try {
3471N/A try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, WRITE, SPARSE)) {
893N/A final long hole = 2L * 1024L * 1024L * 1024L;
893N/A sbc.position(hole);
893N/A write(sbc, "hello");
893N/A long size = sbc.size();
893N/A if (size != (hole + 5))
893N/A throw new RuntimeException("Unexpected size");
893N/A }
893N/A } finally {
893N/A TestUtil.deleteUnchecked(file);
893N/A }
893N/A }
893N/A
893N/A // test APPEND option
893N/A static void appendTests(Path dir) throws Exception {
893N/A Path file = dir.resolve("foo");
893N/A try {
893N/A // "hello there" should be written to file
3471N/A try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, WRITE, APPEND)) {
893N/A write(sbc, "hello ");
893N/A sbc.position(0L);
893N/A write(sbc, "there");
893N/A }
893N/A
893N/A // check file
3471N/A try (Scanner s = new Scanner(file)) {
893N/A String line = s.nextLine();
893N/A if (!line.equals("hello there"))
893N/A throw new RuntimeException("Unexpected file contents");
893N/A }
893N/A
893N/A // check that read is not allowed
3471N/A try (SeekableByteChannel sbc = Files.newByteChannel(file, APPEND)) {
893N/A sbc.read(ByteBuffer.allocate(100));
893N/A } catch (NonReadableChannelException x) {
893N/A }
893N/A } finally {
893N/A // clean-up
893N/A TestUtil.deleteUnchecked(file);
893N/A }
893N/A }
893N/A
893N/A // test TRUNCATE_EXISTING option
893N/A static void truncateExistingTests(Path dir) throws Exception {
893N/A Path file = dir.resolve("foo");
893N/A try {
3471N/A try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, WRITE)) {
893N/A write(sbc, "Have a nice day!");
893N/A }
893N/A
893N/A // re-open with truncate option
893N/A // write short message and check
3471N/A try (SeekableByteChannel sbc = Files.newByteChannel(file, WRITE, TRUNCATE_EXISTING)) {
893N/A write(sbc, "Hello there!");
893N/A }
3471N/A try (Scanner s = new Scanner(file)) {
893N/A String line = s.nextLine();
893N/A if (!line.equals("Hello there!"))
893N/A throw new RuntimeException("Unexpected file contents");
893N/A }
893N/A
893N/A // re-open with create + truncate option
893N/A // check file is of size 0L
3471N/A try (SeekableByteChannel sbc = Files.newByteChannel(file, WRITE, CREATE, TRUNCATE_EXISTING)) {
893N/A long size = ((FileChannel)sbc).size();
893N/A if (size != 0L)
893N/A throw new RuntimeException("File not truncated");
893N/A }
893N/A
893N/A } finally {
893N/A // clean-up
893N/A TestUtil.deleteUnchecked(file);
893N/A }
893N/A
893N/A }
893N/A
893N/A // test NOFOLLOW_LINKS option
893N/A static void noFollowLinksTests(Path dir) throws Exception {
893N/A if (!supportsLinks)
893N/A return;
3471N/A Path file = Files.createFile(dir.resolve("foo"));
893N/A try {
893N/A // ln -s foo link
3471N/A Path link = dir.resolve("link");
3471N/A Files.createSymbolicLink(link, file);
893N/A
893N/A // open with NOFOLLOW_LINKS option
893N/A try {
3471N/A Files.newByteChannel(link, READ, LinkOption.NOFOLLOW_LINKS);
893N/A throw new RuntimeException();
893N/A } catch (IOException x) {
893N/A } finally {
893N/A TestUtil.deleteUnchecked(link);
893N/A }
893N/A
893N/A } finally {
893N/A // clean-up
893N/A TestUtil.deleteUnchecked(file);
893N/A }
893N/A }
893N/A
893N/A // test size/truncate/position methods
893N/A static void sizeTruncatePositionTests(Path dir) throws Exception {
893N/A Path file = dir.resolve("foo");
893N/A try {
3471N/A try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, READ, WRITE)) {
893N/A if (sbc.size() != 0L)
893N/A throw new RuntimeException("Unexpected size");
893N/A
893N/A // check size
893N/A write(sbc, "hello");
893N/A if (sbc.size() != 5L)
893N/A throw new RuntimeException("Unexpected size");
893N/A
893N/A // truncate (size and position should change)
893N/A sbc.truncate(4L);
893N/A if (sbc.size() != 4L)
893N/A throw new RuntimeException("Unexpected size");
893N/A if (sbc.position() != 4L)
893N/A throw new RuntimeException("Unexpected position");
893N/A
893N/A // truncate (position should not change)
893N/A sbc.position(2L).truncate(3L);
893N/A if (sbc.size() != 3L)
893N/A throw new RuntimeException("Unexpected size");
893N/A if (sbc.position() != 2L)
893N/A throw new RuntimeException("Unexpected position");
893N/A }
893N/A } finally {
893N/A TestUtil.deleteUnchecked(file);
893N/A }
893N/A }
893N/A
893N/A // Windows specific options for the use by applications that really want
893N/A // to use legacy DOS sharing options
893N/A static void dosSharingOptionTests(Path dir) throws Exception {
3471N/A Path file = Files.createFile(dir.resolve("foo"));
893N/A try {
893N/A // no sharing
3471N/A try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_READ,
3471N/A NOSHARE_WRITE, NOSHARE_DELETE))
3471N/A {
893N/A try {
3471N/A Files.newByteChannel(file, READ);
893N/A throw new RuntimeException("Sharing violation expected");
893N/A } catch (IOException ignore) { }
893N/A try {
3471N/A Files.newByteChannel(file, WRITE);
893N/A throw new RuntimeException("Sharing violation expected");
893N/A } catch (IOException ignore) { }
893N/A try {
3471N/A Files.delete(file);
893N/A throw new RuntimeException("Sharing violation expected");
893N/A } catch (IOException ignore) { }
893N/A }
893N/A
893N/A // read allowed
3471N/A try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_WRITE, NOSHARE_DELETE)) {
3471N/A Files.newByteChannel(file, READ).close();
893N/A try {
3471N/A Files.newByteChannel(file, WRITE);
893N/A throw new RuntimeException("Sharing violation expected");
893N/A } catch (IOException ignore) { }
893N/A try {
3471N/A Files.delete(file);
893N/A throw new RuntimeException("Sharing violation expected");
893N/A } catch (IOException ignore) { }
893N/A }
893N/A
893N/A // write allowed
3471N/A try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_READ, NOSHARE_DELETE)) {
893N/A try {
3471N/A Files.newByteChannel(file, READ);
893N/A throw new RuntimeException("Sharing violation expected");
893N/A } catch (IOException ignore) { }
3471N/A Files.newByteChannel(file, WRITE).close();
893N/A try {
3471N/A Files.delete(file);
893N/A throw new RuntimeException("Sharing violation expected");
893N/A } catch (IOException ignore) { }
893N/A }
893N/A
893N/A // delete allowed
3471N/A try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_READ, NOSHARE_WRITE)) {
893N/A try {
3471N/A Files.newByteChannel(file, READ);
893N/A throw new RuntimeException("Sharing violation expected");
893N/A } catch (IOException ignore) { }
893N/A try {
3471N/A Files.newByteChannel(file, WRITE);
893N/A throw new RuntimeException("Sharing violation expected");
893N/A } catch (IOException ignore) { }
3471N/A Files.delete(file);
893N/A }
893N/A
893N/A } finally {
893N/A TestUtil.deleteUnchecked(file);
893N/A }
893N/A }
893N/A
893N/A // invalid combinations of options
893N/A static void badCombinations(Path dir) throws Exception {
893N/A Path file = dir.resolve("bad");
893N/A
893N/A try {
3471N/A Files.newByteChannel(file, READ, APPEND);
893N/A throw new RuntimeException("IllegalArgumentException expected");
893N/A } catch (IllegalArgumentException x) { }
893N/A
893N/A try {
3471N/A Files.newByteChannel(file, WRITE, APPEND, TRUNCATE_EXISTING);
893N/A throw new RuntimeException("IllegalArgumentException expected");
893N/A } catch (IllegalArgumentException x) { }
893N/A }
893N/A
893N/A // unsupported operations
893N/A static void unsupportedOptions(Path dir) throws Exception {
893N/A Path file = dir.resolve("bad");
893N/A
893N/A OpenOption badOption = new OpenOption() { };
893N/A try {
3471N/A Files.newByteChannel(file, badOption);
893N/A throw new RuntimeException("UnsupportedOperationException expected");
893N/A } catch (UnsupportedOperationException e) { }
893N/A try {
3471N/A Files.newByteChannel(file, READ, WRITE, badOption);
893N/A throw new RuntimeException("UnsupportedOperationException expected");
893N/A } catch (UnsupportedOperationException e) { }
893N/A }
893N/A
893N/A // null handling
893N/A static void nullTests(Path dir) throws Exception {
893N/A Path file = dir.resolve("foo");
893N/A
893N/A try {
3471N/A OpenOption[] opts = { READ, null };
3471N/A Files.newByteChannel((Path)null, opts);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException x) { }
3471N/A
3471N/A try {
3471N/A Files.newByteChannel(file, (OpenOption[])null);
893N/A throw new RuntimeException("NullPointerException expected");
893N/A } catch (NullPointerException x) { }
893N/A
893N/A try {
893N/A OpenOption[] opts = { READ, null };
3471N/A Files.newByteChannel(file, opts);
893N/A throw new RuntimeException("NullPointerException expected");
893N/A } catch (NullPointerException x) { }
893N/A
893N/A try {
3471N/A Files.newByteChannel(file, (Set<OpenOption>)null);
893N/A throw new RuntimeException("NullPointerException expected");
893N/A } catch (NullPointerException x) { }
893N/A
893N/A try {
3471N/A Set<OpenOption> opts = new HashSet<>();
893N/A opts.add(READ);
893N/A opts.add(null);
3471N/A Files.newByteChannel(file, opts);
893N/A throw new RuntimeException("NullPointerException expected");
893N/A } catch (NullPointerException x) { }
893N/A
893N/A try {
893N/A EnumSet<StandardOpenOption> opts = EnumSet.of(READ);
3471N/A Files.newByteChannel(file, opts, (FileAttribute[])null);
893N/A throw new RuntimeException("NullPointerException expected");
893N/A } catch (NullPointerException x) { }
893N/A
893N/A try {
893N/A EnumSet<StandardOpenOption> opts = EnumSet.of(READ);
893N/A FileAttribute[] attrs = { null };
3471N/A Files.newByteChannel(file, opts, attrs);
893N/A throw new RuntimeException("NullPointerException expected");
893N/A } catch (NullPointerException x) { }
893N/A }
893N/A
893N/A static void write(WritableByteChannel wbc, String msg) throws IOException {
893N/A ByteBuffer buf = ByteBuffer.wrap(msg.getBytes());
893N/A while (buf.hasRemaining())
893N/A wbc.write(buf);
893N/A }
893N/A}