Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/Files/SBC.java
38828 views
/*1* Copyright (c) 2008, 2011, 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*/2829import java.nio.ByteBuffer;30import java.nio.file.*;31import static java.nio.file.StandardOpenOption.*;32import static com.sun.nio.file.ExtendedOpenOption.*;33import java.nio.file.attribute.FileAttribute;34import java.nio.channels.*;35import java.io.IOException;36import java.util.*;3738public class SBC {3940static boolean supportsLinks;4142public static void main(String[] args) throws Exception {43Path dir = TestUtil.createTemporaryDirectory();44try {45supportsLinks = TestUtil.supportsLinks(dir);4647// open options48createTests(dir);49appendTests(dir);50truncateExistingTests(dir);51noFollowLinksTests(dir);5253// SeekableByteChannel methods54sizeTruncatePositionTests(dir);5556// platform specific57if (System.getProperty("os.name").startsWith("Windows"))58dosSharingOptionTests(dir);5960// misc. tests61badCombinations(dir);62unsupportedOptions(dir);63nullTests(dir);6465} finally {66TestUtil.removeAll(dir);67}68}6970// test CREATE and CREATE_NEW options71static void createTests(Path dir) throws Exception {72Path file = dir.resolve("foo");7374// CREATE75try {76// create file (no existing file)77Files.newByteChannel(file, CREATE, WRITE).close();78if (Files.notExists(file))79throw new RuntimeException("File not created");8081// create file (existing file)82Files.newByteChannel(file, CREATE, WRITE).close();8384// create file where existing file is a sym link85if (supportsLinks) {86Path link = Files.createSymbolicLink(dir.resolve("link"), file);87try {88// file already exists89Files.newByteChannel(link, CREATE, WRITE).close();9091// file does not exist92Files.delete(file);93Files.newByteChannel(link, CREATE, WRITE).close();94if (Files.notExists(file))95throw new RuntimeException("File not created");9697} finally {98TestUtil.deleteUnchecked(link);99}100}101102} finally {103TestUtil.deleteUnchecked(file);104}105106// CREATE_NEW107try {108// create file109Files.newByteChannel(file, CREATE_NEW, WRITE).close();110if (Files.notExists(file))111throw new RuntimeException("File not created");112113// create should fail114try {115SeekableByteChannel sbc =116Files.newByteChannel(file, CREATE_NEW, WRITE);117sbc.close();118throw new RuntimeException("FileAlreadyExistsException not thrown");119} catch (FileAlreadyExistsException x) { }120121// create should fail122if (supportsLinks) {123Path link = dir.resolve("link");124Path target = dir.resolve("thisDoesNotExist");125Files.createSymbolicLink(link, target);126try {127128try {129SeekableByteChannel sbc =130Files.newByteChannel(file, CREATE_NEW, WRITE);131sbc.close();132throw new RuntimeException("FileAlreadyExistsException not thrown");133} catch (FileAlreadyExistsException x) { }134135} finally {136TestUtil.deleteUnchecked(link);137}138}139140141} finally {142TestUtil.deleteUnchecked(file);143}144145// CREATE_NEW + SPARSE146try {147try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, WRITE, SPARSE)) {148final long hole = 2L * 1024L * 1024L * 1024L;149sbc.position(hole);150write(sbc, "hello");151long size = sbc.size();152if (size != (hole + 5))153throw new RuntimeException("Unexpected size");154}155} finally {156TestUtil.deleteUnchecked(file);157}158}159160// test APPEND option161static void appendTests(Path dir) throws Exception {162Path file = dir.resolve("foo");163try {164// "hello there" should be written to file165try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, WRITE, APPEND)) {166write(sbc, "hello ");167sbc.position(0L);168write(sbc, "there");169}170171// check file172try (Scanner s = new Scanner(file)) {173String line = s.nextLine();174if (!line.equals("hello there"))175throw new RuntimeException("Unexpected file contents");176}177178// check that read is not allowed179try (SeekableByteChannel sbc = Files.newByteChannel(file, APPEND)) {180sbc.read(ByteBuffer.allocate(100));181} catch (NonReadableChannelException x) {182}183} finally {184// clean-up185TestUtil.deleteUnchecked(file);186}187}188189// test TRUNCATE_EXISTING option190static void truncateExistingTests(Path dir) throws Exception {191Path file = dir.resolve("foo");192try {193try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, WRITE)) {194write(sbc, "Have a nice day!");195}196197// re-open with truncate option198// write short message and check199try (SeekableByteChannel sbc = Files.newByteChannel(file, WRITE, TRUNCATE_EXISTING)) {200write(sbc, "Hello there!");201}202try (Scanner s = new Scanner(file)) {203String line = s.nextLine();204if (!line.equals("Hello there!"))205throw new RuntimeException("Unexpected file contents");206}207208// re-open with create + truncate option209// check file is of size 0L210try (SeekableByteChannel sbc = Files.newByteChannel(file, WRITE, CREATE, TRUNCATE_EXISTING)) {211long size = ((FileChannel)sbc).size();212if (size != 0L)213throw new RuntimeException("File not truncated");214}215216} finally {217// clean-up218TestUtil.deleteUnchecked(file);219}220221}222223// test NOFOLLOW_LINKS option224static void noFollowLinksTests(Path dir) throws Exception {225if (!supportsLinks)226return;227Path file = Files.createFile(dir.resolve("foo"));228try {229// ln -s foo link230Path link = dir.resolve("link");231Files.createSymbolicLink(link, file);232233// open with NOFOLLOW_LINKS option234try {235Files.newByteChannel(link, READ, LinkOption.NOFOLLOW_LINKS);236throw new RuntimeException();237} catch (IOException | UnsupportedOperationException x) {238} finally {239TestUtil.deleteUnchecked(link);240}241242} finally {243// clean-up244TestUtil.deleteUnchecked(file);245}246}247248// test size/truncate/position methods249static void sizeTruncatePositionTests(Path dir) throws Exception {250Path file = dir.resolve("foo");251try {252try (SeekableByteChannel sbc = Files.newByteChannel(file, CREATE_NEW, READ, WRITE)) {253if (sbc.size() != 0L)254throw new RuntimeException("Unexpected size");255256// check size257write(sbc, "hello");258if (sbc.size() != 5L)259throw new RuntimeException("Unexpected size");260261// truncate (size and position should change)262sbc.truncate(4L);263if (sbc.size() != 4L)264throw new RuntimeException("Unexpected size");265if (sbc.position() != 4L)266throw new RuntimeException("Unexpected position");267268// truncate (position should not change)269sbc.position(2L).truncate(3L);270if (sbc.size() != 3L)271throw new RuntimeException("Unexpected size");272if (sbc.position() != 2L)273throw new RuntimeException("Unexpected position");274}275} finally {276TestUtil.deleteUnchecked(file);277}278}279280// Windows specific options for the use by applications that really want281// to use legacy DOS sharing options282static void dosSharingOptionTests(Path dir) throws Exception {283Path file = Files.createFile(dir.resolve("foo"));284try {285// no sharing286try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_READ,287NOSHARE_WRITE, NOSHARE_DELETE))288{289try {290Files.newByteChannel(file, READ);291throw new RuntimeException("Sharing violation expected");292} catch (IOException ignore) { }293try {294Files.newByteChannel(file, WRITE);295throw new RuntimeException("Sharing violation expected");296} catch (IOException ignore) { }297try {298Files.delete(file);299throw new RuntimeException("Sharing violation expected");300} catch (IOException ignore) { }301}302303// read allowed304try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_WRITE, NOSHARE_DELETE)) {305Files.newByteChannel(file, READ).close();306try {307Files.newByteChannel(file, WRITE);308throw new RuntimeException("Sharing violation expected");309} catch (IOException ignore) { }310try {311Files.delete(file);312throw new RuntimeException("Sharing violation expected");313} catch (IOException ignore) { }314}315316// write allowed317try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_READ, NOSHARE_DELETE)) {318try {319Files.newByteChannel(file, READ);320throw new RuntimeException("Sharing violation expected");321} catch (IOException ignore) { }322Files.newByteChannel(file, WRITE).close();323try {324Files.delete(file);325throw new RuntimeException("Sharing violation expected");326} catch (IOException ignore) { }327}328329// delete allowed330try (SeekableByteChannel ch = Files.newByteChannel(file, READ, NOSHARE_READ, NOSHARE_WRITE)) {331try {332Files.newByteChannel(file, READ);333throw new RuntimeException("Sharing violation expected");334} catch (IOException ignore) { }335try {336Files.newByteChannel(file, WRITE);337throw new RuntimeException("Sharing violation expected");338} catch (IOException ignore) { }339Files.delete(file);340}341342} finally {343TestUtil.deleteUnchecked(file);344}345}346347// invalid combinations of options348static void badCombinations(Path dir) throws Exception {349Path file = dir.resolve("bad");350351try {352Files.newByteChannel(file, READ, APPEND);353throw new RuntimeException("IllegalArgumentException expected");354} catch (IllegalArgumentException x) { }355356try {357Files.newByteChannel(file, WRITE, APPEND, TRUNCATE_EXISTING);358throw new RuntimeException("IllegalArgumentException expected");359} catch (IllegalArgumentException x) { }360}361362// unsupported operations363static void unsupportedOptions(Path dir) throws Exception {364Path file = dir.resolve("bad");365366OpenOption badOption = new OpenOption() { };367try {368Files.newByteChannel(file, badOption);369throw new RuntimeException("UnsupportedOperationException expected");370} catch (UnsupportedOperationException e) { }371try {372Files.newByteChannel(file, READ, WRITE, badOption);373throw new RuntimeException("UnsupportedOperationException expected");374} catch (UnsupportedOperationException e) { }375}376377// null handling378static void nullTests(Path dir) throws Exception {379Path file = dir.resolve("foo");380381try {382OpenOption[] opts = { READ, null };383Files.newByteChannel((Path)null, opts);384throw new RuntimeException("NullPointerException expected");385} catch (NullPointerException x) { }386387try {388Files.newByteChannel(file, (OpenOption[])null);389throw new RuntimeException("NullPointerException expected");390} catch (NullPointerException x) { }391392try {393OpenOption[] opts = { READ, null };394Files.newByteChannel(file, opts);395throw new RuntimeException("NullPointerException expected");396} catch (NullPointerException x) { }397398try {399Files.newByteChannel(file, (Set<OpenOption>)null);400throw new RuntimeException("NullPointerException expected");401} catch (NullPointerException x) { }402403try {404Set<OpenOption> opts = new HashSet<>();405opts.add(READ);406opts.add(null);407Files.newByteChannel(file, opts);408throw new RuntimeException("NullPointerException expected");409} catch (NullPointerException x) { }410411try {412EnumSet<StandardOpenOption> opts = EnumSet.of(READ);413Files.newByteChannel(file, opts, (FileAttribute[])null);414throw new RuntimeException("NullPointerException expected");415} catch (NullPointerException x) { }416417try {418EnumSet<StandardOpenOption> opts = EnumSet.of(READ);419FileAttribute[] attrs = { null };420Files.newByteChannel(file, opts, attrs);421throw new RuntimeException("NullPointerException expected");422} catch (NullPointerException x) { }423}424425static void write(WritableByteChannel wbc, String msg) throws IOException {426ByteBuffer buf = ByteBuffer.wrap(msg.getBytes());427while (buf.hasRemaining())428wbc.write(buf);429}430}431432433