Path: blob/master/test/jdk/java/nio/channels/unixdomain/FileAttributes.java
41153 views
/*1* Copyright (c) 2021, 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 825297126* @library /test/lib27* @run testng FileAttributes28*/2930import java.io.IOException;31import java.io.File;32import java.net.*;33import java.nio.channels.*;34import java.nio.file.Files;35import java.nio.file.Path;36import java.nio.file.attribute.BasicFileAttributes;37import java.util.Arrays;38import org.testng.annotations.Test;39import org.testng.SkipException;4041import static java.net.StandardProtocolFamily.UNIX;42import static org.testng.Assert.assertFalse;43import static org.testng.Assert.assertThrows;44import static org.testng.Assert.assertTrue;4546/**47*/48public class FileAttributes {4950@Test51public static void test() throws Exception {52checkSupported();53Path path = null;54try (var chan = SocketChannel.open(UNIX)) {55path = Path.of("foo.sock");56var addr = UnixDomainSocketAddress.of(path);5758chan.bind(addr);5960// Check file exists6162File f = path.toFile();63assertTrue(f.exists(), "File.exists failed");6465assertTrue(Files.exists(path), "Files.exists failed");6667// Check basic attributes68BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);6970assertFalse(attrs.isDirectory(), "file is not a directory");71assertTrue(attrs.isOther(), "file is other");72assertFalse(attrs.isRegularFile(), "file is not a regular file");73assertFalse(attrs.isSymbolicLink(), "file is not a symbolic link");7475// Check can't copy76final Path src = path;77final Path dest = Path.of("bar.sock");78assertThrows(IOException.class, () -> Files.copy(src, dest));7980// Check deletion81assertTrue(f.delete(), "File.delete failed");82} finally {83Files.deleteIfExists(path);84}85}8687static void checkSupported() {88try {89SocketChannel.open(UNIX).close();90} catch (UnsupportedOperationException e) {91throw new SkipException("Unix domain channels not supported");92} catch (Exception e) {93// continue test to see what problem is94}95}96}979899