Path: blob/master/test/jdk/java/nio/file/Files/SBC.java
41153 views
/*1* Copyright (c) 2008, 2016, 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/* @test24* @bug 431388725* @summary Unit test for java.nio.file.Files.newByteChannel26* @library ..27* @modules jdk.unsupported28*/2930import java.nio.ByteBuffer;31import java.nio.file.*;32import static java.nio.file.StandardOpenOption.*;33import static com.sun.nio.file.ExtendedOpenOption.*;34import java.nio.file.attribute.FileAttribute;35import java.nio.channels.*;36import java.io.IOException;37import java.util.*;3839public class SBC {4041static boolean supportsLinks;4243public static void main(String[] args) throws Exception {44Path dir = TestUtil.createTemporaryDirectory();45try {46supportsLinks = TestUtil.supportsLinks(dir);4748// open options49createTests(dir);50appendTests(dir);51truncateExistingTests(dir);52noFollowLinksTests(dir);5354// SeekableByteChannel methods55sizeTruncatePositionTests(dir);5657// platform specific58if (System.getProperty("os.name").startsWith("Windows"))59dosSharingOptionTests(dir);6061// misc. tests62badCombinations(dir);63unsupportedOptions(dir);64nullTests(dir);6566} finally {67TestUtil.removeAll(dir);68}69}7071// test CREATE and CREATE_NEW options72static void createTests(Path dir) throws Exception {73Path file = dir.resolve("foo");7475// CREATE76try {77// create file (no existing file)78Files.newByteChannel(file, CREATE, WRITE).close();79if (Files.notExists(file))80throw new RuntimeException("File not created");8182// create file (existing file)83Files.newByteChannel(file, CREATE, WRITE).close();8485// create file where existing file is a sym link86if (supportsLinks) {87Path link = Files.createSymbolicLink(dir.resolve("link"), file);88try {89// file already exists90Files.newByteChannel(link, CREATE, WRITE).close();9192// file does not exist93Files.delete(file);94Files.newByteChannel(link, CREATE, WRITE).close();95if (Files.notExists(file))96throw new RuntimeException("File not created");9798} finally {99TestUtil.deleteUnchecked(link);100}101}102103} finally {104TestUtil.deleteUnchecked(file);105}106107// CREATE_NEW108try {109// create file110Files.newByteChannel(file, CREATE_NEW, WRITE).close();111if (Files.notExists(file))112throw new RuntimeException("File not created");113114// create should fail115try {116SeekableByteChannel sbc =117Files.newByteChannel(file, CREATE_NEW, WRITE);118sbc.close();119throw new RuntimeException("FileAlreadyExistsException not thrown");120} catch (FileAlreadyExistsException x) { }121122// create should fail123if (supportsLinks) {124Path link = dir.resolve("link");125Path target = dir.resolve("thisDoesNotExist");126Files.createSymbolicLink(link, target);127try {128129try {130SeekableByteChannel sbc =131Files.newByteChannel(file, CREATE_NEW, WRITE);132sbc.close();133throw new RuntimeException("FileAlreadyExistsException not thrown");134} catch (FileAlreadyExistsException x) { }135136} finally {137TestUtil.deleteUnchecked(link);138}139}140141142} finally {143TestUtil.deleteUnchecked(file);144}145146// CREATE_NEW + SPARSE147try {148try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, WRITE, SPARSE)) {149final long hole = 2L * 1024L * 1024L * 1024L;150sbc.position(hole);151write(sbc, "hello");152long size = sbc.size();153if (size != (hole + 5))154throw new RuntimeException("Unexpected size");155}156} finally {157TestUtil.deleteUnchecked(file);158}159}160161// test APPEND option162static void appendTests(Path dir) throws Exception {163Path file = dir.resolve("foo");164try {165// "hello there" should be written to file166try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, WRITE, APPEND)) {167write(sbc, "hello ");168sbc.position(0L);169write(sbc, "there");170}171172// check file173try (Scanner s = new Scanner(file)) {174String line = s.nextLine();175if (!line.equals("hello there"))176throw new RuntimeException("Unexpected file contents");177}178179// check that read is not allowed180try (SeekableByteChannel sbc = Files.newByteChannel(file, APPEND)) {181sbc.read(ByteBuffer.allocate(100));182} catch (NonReadableChannelException x) {183}184} finally {185// clean-up186TestUtil.deleteUnchecked(file);187}188}189190// test TRUNCATE_EXISTING option191static void truncateExistingTests(Path dir) throws Exception {192Path file = dir.resolve("foo");193try {194try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, WRITE)) {195write(sbc, "Have a nice day!");196}197198// re-open with truncate option199// write short message and check200try (SeekableByteChannel sbc = Files.newByteChannel(file, WRITE, TRUNCATE_EXISTING)) {201write(sbc, "Hello there!");202}203try (Scanner s = new Scanner(file)) {204String line = s.nextLine();205if (!line.equals("Hello there!"))206throw new RuntimeException("Unexpected file contents");207}208209// re-open with create + truncate option210// check file is of size 0L211try (SeekableByteChannel sbc = Files.newByteChannel(file, WRITE, CREATE, TRUNCATE_EXISTING)) {212long size = ((FileChannel)sbc).size();213if (size != 0L)214throw new RuntimeException("File not truncated");215}216217} finally {218// clean-up219TestUtil.deleteUnchecked(file);220}221222}223224// test NOFOLLOW_LINKS option225static void noFollowLinksTests(Path dir) throws Exception {226if (!supportsLinks)227return;228Path file = Files.createFile(dir.resolve("foo"));229try {230// ln -s foo link231Path link = dir.resolve("link");232Files.createSymbolicLink(link, file);233234// open with NOFOLLOW_LINKS option235try {236Files.newByteChannel(link, READ, LinkOption.NOFOLLOW_LINKS);237throw new RuntimeException();238} catch (IOException | UnsupportedOperationException x) {239} finally {240TestUtil.deleteUnchecked(link);241}242243} finally {244// clean-up245TestUtil.deleteUnchecked(file);246}247}248249// test size/truncate/position methods250static void sizeTruncatePositionTests(Path dir) throws Exception {251Path file = dir.resolve("foo");252try {253try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, READ, WRITE)) {254if (sbc.size() != 0L)255throw new RuntimeException("Unexpected size");256257// check size258write(sbc, "hello");259if (sbc.size() != 5L)260throw new RuntimeException("Unexpected size");261262// truncate (size and position should change)263sbc.truncate(4L);264if (sbc.size() != 4L)265throw new RuntimeException("Unexpected size");266if (sbc.position() != 4L)267throw new RuntimeException("Unexpected position");268269// truncate (position should not change)270sbc.position(2L).truncate(3L);271if (sbc.size() != 3L)272throw new RuntimeException("Unexpected size");273if (sbc.position() != 2L)274throw new RuntimeException("Unexpected position");275}276} finally {277TestUtil.deleteUnchecked(file);278}279}280281// Windows specific options for the use by applications that really want282// to use legacy DOS sharing options283static void dosSharingOptionTests(Path dir) throws Exception {284Path file = Files.createFile(dir.resolve("foo"));285try {286// no sharing287try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_READ,288NOSHARE_WRITE, NOSHARE_DELETE))289{290try {291Files.newByteChannel(file, READ);292throw new RuntimeException("Sharing violation expected");293} catch (IOException ignore) { }294try {295Files.newByteChannel(file, WRITE);296throw new RuntimeException("Sharing violation expected");297} catch (IOException ignore) { }298try {299Files.delete(file);300throw new RuntimeException("Sharing violation expected");301} catch (IOException ignore) { }302}303304// read allowed305try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_WRITE, NOSHARE_DELETE)) {306Files.newByteChannel(file, READ).close();307try {308Files.newByteChannel(file, WRITE);309throw new RuntimeException("Sharing violation expected");310} catch (IOException ignore) { }311try {312Files.delete(file);313throw new RuntimeException("Sharing violation expected");314} catch (IOException ignore) { }315}316317// write allowed318try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_READ, NOSHARE_DELETE)) {319try {320Files.newByteChannel(file, READ);321throw new RuntimeException("Sharing violation expected");322} catch (IOException ignore) { }323Files.newByteChannel(file, WRITE).close();324try {325Files.delete(file);326throw new RuntimeException("Sharing violation expected");327} catch (IOException ignore) { }328}329330// delete allowed331try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_READ, NOSHARE_WRITE)) {332try {333Files.newByteChannel(file, READ);334throw new RuntimeException("Sharing violation expected");335} catch (IOException ignore) { }336try {337Files.newByteChannel(file, WRITE);338throw new RuntimeException("Sharing violation expected");339} catch (IOException ignore) { }340Files.delete(file);341}342343} finally {344TestUtil.deleteUnchecked(file);345}346}347348// invalid combinations of options349static void badCombinations(Path dir) throws Exception {350Path file = dir.resolve("bad");351352try {353Files.newByteChannel(file, READ, APPEND);354throw new RuntimeException("IllegalArgumentException expected");355} catch (IllegalArgumentException x) { }356357try {358Files.newByteChannel(file, WRITE, APPEND, TRUNCATE_EXISTING);359throw new RuntimeException("IllegalArgumentException expected");360} catch (IllegalArgumentException x) { }361}362363// unsupported operations364static void unsupportedOptions(Path dir) throws Exception {365Path file = dir.resolve("bad");366367OpenOption badOption = new OpenOption() { };368try {369Files.newByteChannel(file, badOption);370throw new RuntimeException("UnsupportedOperationException expected");371} catch (UnsupportedOperationException e) { }372try {373Files.newByteChannel(file, READ, WRITE, badOption);374throw new RuntimeException("UnsupportedOperationException expected");375} catch (UnsupportedOperationException e) { }376}377378// null handling379static void nullTests(Path dir) throws Exception {380Path file = dir.resolve("foo");381382try {383OpenOption[] opts = { READ, null };384Files.newByteChannel((Path)null, opts);385throw new RuntimeException("NullPointerException expected");386} catch (NullPointerException x) { }387388try {389Files.newByteChannel(file, (OpenOption[])null);390throw new RuntimeException("NullPointerException expected");391} catch (NullPointerException x) { }392393try {394OpenOption[] opts = { READ, null };395Files.newByteChannel(file, opts);396throw new RuntimeException("NullPointerException expected");397} catch (NullPointerException x) { }398399try {400Files.newByteChannel(file, (Set<OpenOption>)null);401throw new RuntimeException("NullPointerException expected");402} catch (NullPointerException x) { }403404try {405Set<OpenOption> opts = new HashSet<>();406opts.add(READ);407opts.add(null);408Files.newByteChannel(file, opts);409throw new RuntimeException("NullPointerException expected");410} catch (NullPointerException x) { }411412try {413EnumSet<StandardOpenOption> opts = EnumSet.of(READ);414Files.newByteChannel(file, opts, (FileAttribute[])null);415throw new RuntimeException("NullPointerException expected");416} catch (NullPointerException x) { }417418try {419EnumSet<StandardOpenOption> opts = EnumSet.of(READ);420FileAttribute[] attrs = { null };421Files.newByteChannel(file, opts, attrs);422throw new RuntimeException("NullPointerException expected");423} catch (NullPointerException x) { }424}425426static void write(WritableByteChannel wbc, String msg) throws IOException {427ByteBuffer buf = ByteBuffer.wrap(msg.getBytes());428while (buf.hasRemaining())429wbc.write(buf);430}431}432433434