Add API Coverage for java.nio methods
API list:
java.nio.channels.AsynchronousFileChannel.lock(A, java.nio.channels.CompletionHandler)
java.nio.channels.FileLock.toString()
java.nio.file.ClosedFileSystemException.ClosedFileSystemException()
java.nio.file.ProviderNotFoundException.ProviderNotFoundException()
java.nio.file.ReadOnlyFileSystemException.ReadOnlyFileSystemException()
java.nio.file.attribute.AclEntry.newBuilder(java.nio.file.attribute.AclEntry)
java.nio.file.spi.FileSystemProvider.createLink(java.nio.file.Path, java.nio.file.Path)
java.nio.file.spi.FileSystemProvider.newAsynchronousFileChannel(java.nio.file.Path, java.util.Set, java.util.concurrent.ExecutorService, java.nio.file.attribute.FileAttribute[])
Bug: 182165666
Test: atest CtsLibcoreTestCases:libcore.java.nio.channels.AsynchronousFileChannelTest
Test: atest CtsLibcoreTestCases:libcore.java.nio.channels.FileLockTest
Test: atest CtsLibcoreTestCases:libcore.java.nio.file.ClosedFileSystemExceptionTest
Test: atest CtsLibcoreTestCases:libcore.java.nio.file.ProviderNotFoundExceptionTest
Test: atest CtsLibcoreTestCases:libcore.java.nio.file.ReadOnlyFileSystemExceptionTest
Test: atest CtsLibcoreTestCases:libcore.java.nio.file.attribute.AclEntryTest
Test: atest CtsLibcoreTestCases:libcore.java.nio.file.spi.FileSystemProviderTest
Change-Id: I6a41821f60746d8f4facaaec1e6076f74fdd4fb0
diff --git a/luni/src/test/java/libcore/java/nio/channels/AsynchronousFileChannelTest.java b/luni/src/test/java/libcore/java/nio/channels/AsynchronousFileChannelTest.java
index 3b072f2..43f5b7d 100644
--- a/luni/src/test/java/libcore/java/nio/channels/AsynchronousFileChannelTest.java
+++ b/luni/src/test/java/libcore/java/nio/channels/AsynchronousFileChannelTest.java
@@ -27,8 +27,10 @@
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.CompletionHandler;
+import java.nio.channels.FileLock;
import java.nio.channels.NonReadableChannelException;
import java.nio.channels.NonWritableChannelException;
+import java.nio.channels.OverlappingFileLockException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
@@ -701,4 +703,67 @@
} catch(ClosedChannelException expected) {}
}
+ static class LockHandler implements CompletionHandler<FileLock, Integer> {
+ public FileLock fileLock;
+ public Throwable exc;
+
+ private final CountDownLatch cdl = new CountDownLatch(1);
+
+ @Override
+ public void completed(FileLock fileLock, Integer attachment) {
+ this.fileLock = fileLock;
+
+ cdl.countDown();
+ }
+
+ @Override
+ public void failed(Throwable exc, Integer attachment) {
+ this.exc = exc;
+ }
+
+ public boolean awaitCompletion() throws InterruptedException {
+ return cdl.await(10, TimeUnit.SECONDS);
+ }
+ }
+
+
+ @Test
+ public void testLock() throws Exception {
+ File temp = createTemporaryFile(256);
+ assertEquals(256, temp.length());
+ AsynchronousFileChannel afc = AsynchronousFileChannel.open(temp.toPath(),
+ StandardOpenOption.WRITE);
+ LockHandler handler = new LockHandler();
+
+ afc.lock(null, handler);
+ assertTrue(handler.awaitCompletion());
+ assertNotNull(handler.fileLock);
+ assertNull(handler.exc);
+ assertTrue(handler.fileLock.isValid());
+ assertFalse(handler.fileLock.isShared());
+
+ AsynchronousFileChannel otherAfc = AsynchronousFileChannel.open(temp.toPath(),
+ StandardOpenOption.WRITE);
+ LockHandler otherHandler = new LockHandler();
+ try {
+ otherAfc.lock(null, otherHandler);
+ fail();
+ } catch (OverlappingFileLockException expected) {
+ }
+
+ handler.fileLock.release();
+
+ otherHandler = new LockHandler();
+ otherAfc.lock(null, otherHandler);
+ assertTrue(otherHandler.awaitCompletion());
+ assertNotNull(otherHandler.fileLock);
+ assertNull(otherHandler.exc);
+ assertTrue(otherHandler.fileLock.isValid());
+
+ otherHandler.fileLock.release();
+
+ afc.close();
+ otherAfc.close();
+ }
+
}
diff --git a/luni/src/test/java/libcore/java/nio/channels/FileLockTest.java b/luni/src/test/java/libcore/java/nio/channels/FileLockTest.java
new file mode 100644
index 0000000..dbf72f9
--- /dev/null
+++ b/luni/src/test/java/libcore/java/nio/channels/FileLockTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package libcore.java.nio.channels;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public final class FileLockTest {
+
+ @Test
+ public void testToString() throws Exception {
+ File tmp = File.createTempFile("FileLockTest", "tmp");
+
+ try(FileOutputStream fos = new FileOutputStream(tmp)) {
+ try(FileChannel fc = fos.getChannel()) {
+ FileLock lock = fc.lock(0, 42, false);
+ String strLock = lock.toString();
+ assertTrue(strLock.contains("0"));
+ assertTrue(strLock.contains("42"));
+ assertTrue(strLock.contains("exclusive"));
+ assertTrue(strLock.contains("valid"));
+ assertFalse(strLock.contains("invalid"));
+
+ lock.release();
+ strLock = lock.toString();
+ assertTrue(strLock.contains("0"));
+ assertTrue(strLock.contains("42"));
+ assertTrue(strLock.contains("exclusive"));
+ assertTrue(strLock.contains("invalid"));
+ }
+ }
+
+ try(FileInputStream fis = new FileInputStream(tmp)) {
+ try(FileChannel fc = fis.getChannel()) {
+ FileLock lock = fc.lock(0, 42, true);
+ String strLock = lock.toString();
+ assertTrue(strLock.contains("0"));
+ assertTrue(strLock.contains("42"));
+ assertTrue(strLock.contains("shared"));
+ assertTrue(strLock.contains("valid"));
+ assertFalse(strLock.contains("invalid"));
+
+ lock.release();
+ strLock = lock.toString();
+ assertTrue(strLock.contains("0"));
+ assertTrue(strLock.contains("42"));
+ assertTrue(strLock.contains("shared"));
+ assertTrue(strLock.contains("invalid"));
+ }
+ }
+ }
+}
diff --git a/luni/src/test/java/libcore/java/nio/file/ClosedFileSystemExceptionTest.java b/luni/src/test/java/libcore/java/nio/file/ClosedFileSystemExceptionTest.java
new file mode 100644
index 0000000..f209899
--- /dev/null
+++ b/luni/src/test/java/libcore/java/nio/file/ClosedFileSystemExceptionTest.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.java.nio.file;
+
+import static org.junit.Assert.assertNull;
+
+import java.nio.file.ClosedFileSystemException;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public final class ClosedFileSystemExceptionTest {
+
+ @Test
+ public void testEmptyConstructor() {
+ ClosedFileSystemException exception = new ClosedFileSystemException();
+ assertNull(exception.getMessage());
+ assertNull(exception.getCause());
+ }
+}
diff --git a/luni/src/test/java/libcore/java/nio/file/ProviderNotFoundExceptionTest.java b/luni/src/test/java/libcore/java/nio/file/ProviderNotFoundExceptionTest.java
index 277db4f..480d9b4 100644
--- a/luni/src/test/java/libcore/java/nio/file/ProviderNotFoundExceptionTest.java
+++ b/luni/src/test/java/libcore/java/nio/file/ProviderNotFoundExceptionTest.java
@@ -22,6 +22,12 @@
public class ProviderNotFoundExceptionTest extends TestCase {
+ public void test_constructor$() {
+ ProviderNotFoundException exception = new ProviderNotFoundException();
+ assertNull(exception.getMessage());
+ assertNull(exception.getCause());
+ }
+
public void test_constructor$String() {
String message = "message";
ProviderNotFoundException exception = new ProviderNotFoundException(message);
diff --git a/luni/src/test/java/libcore/java/nio/file/ReadOnlyFileSystemExceptionTest.java b/luni/src/test/java/libcore/java/nio/file/ReadOnlyFileSystemExceptionTest.java
new file mode 100644
index 0000000..b89eccd
--- /dev/null
+++ b/luni/src/test/java/libcore/java/nio/file/ReadOnlyFileSystemExceptionTest.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.java.nio.file;
+
+import static org.junit.Assert.assertNull;
+
+import java.nio.file.ReadOnlyFileSystemException;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public final class ReadOnlyFileSystemExceptionTest {
+
+ @Test
+ public void testEmptyConstructor() {
+ ReadOnlyFileSystemException exception = new ReadOnlyFileSystemException();
+ assertNull(exception.getMessage());
+ assertNull(exception.getCause());
+ }
+}
diff --git a/luni/src/test/java/libcore/java/nio/file/attribute/AclEntryTest.java b/luni/src/test/java/libcore/java/nio/file/attribute/AclEntryTest.java
index 6e0ccd8..0464faf 100644
--- a/luni/src/test/java/libcore/java/nio/file/attribute/AclEntryTest.java
+++ b/luni/src/test/java/libcore/java/nio/file/attribute/AclEntryTest.java
@@ -55,4 +55,31 @@
assertEquals(1, flags.size());
assertTrue(flags.contains(AclEntryFlag.INHERIT_ONLY));
}
+
+ @Test
+ public void testBuilder() throws Exception {
+ UserPrincipal user = Files.getOwner(Paths.get("."));
+
+ AclEntry aclEntry = AclEntry.newBuilder()
+ .setType(AclEntryType.ALLOW)
+ .setPrincipal(user)
+ .setFlags(AclEntryFlag.INHERIT_ONLY)
+ .setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.READ_ATTRIBUTES)
+ .build();
+
+ AclEntry.Builder builder = AclEntry.newBuilder(aclEntry);
+ aclEntry = builder.build();
+
+ assertEquals(AclEntryType.ALLOW, aclEntry.type());
+ assertEquals(user, aclEntry.principal());
+
+ Set<AclEntryPermission> permissions = aclEntry.permissions();
+ assertEquals(2, permissions.size());
+ assertTrue(permissions.contains(AclEntryPermission.READ_DATA));
+ assertTrue(permissions.contains(AclEntryPermission.READ_ATTRIBUTES));
+
+ Set<AclEntryFlag> flags = aclEntry.flags();
+ assertEquals(1, flags.size());
+ assertTrue(flags.contains(AclEntryFlag.INHERIT_ONLY));
+ }
}
diff --git a/luni/src/test/java/libcore/java/nio/file/spi/FileSystemProviderTest.java b/luni/src/test/java/libcore/java/nio/file/spi/FileSystemProviderTest.java
new file mode 100644
index 0000000..82c58a1
--- /dev/null
+++ b/luni/src/test/java/libcore/java/nio/file/spi/FileSystemProviderTest.java
@@ -0,0 +1,173 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package libcore.java.nio.file.spi;
+
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.net.URI;
+import java.nio.channels.SeekableByteChannel;
+import java.nio.file.AccessMode;
+import java.nio.file.CopyOption;
+import java.nio.file.DirectoryStream;
+import java.nio.file.FileStore;
+import java.nio.file.FileSystem;
+import java.nio.file.LinkOption;
+import java.nio.file.OpenOption;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.nio.file.attribute.FileAttribute;
+import java.nio.file.attribute.FileAttributeView;
+import java.nio.file.spi.FileSystemProvider;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public final class FileSystemProviderTest {
+
+ class MockFileSystemProvider extends FileSystemProvider {
+ @Override
+ public String getScheme() {
+ return "mock";
+ }
+
+ @Override
+ public FileSystem newFileSystem(URI uri, Map<String,?> env) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public FileSystem getFileSystem(URI uri) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Path getPath(URI uri) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public SeekableByteChannel newByteChannel(Path path,
+ Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public DirectoryStream<Path> newDirectoryStream(Path dir,
+ DirectoryStream.Filter<? super Path> filter) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void delete(Path path) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void copy(Path source, Path target, CopyOption... options) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void move(Path source, Path target, CopyOption... options) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean isSameFile(Path path, Path path2) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean isHidden(Path path) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public FileStore getFileStore(Path path) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void checkAccess(Path path, AccessMode... modes) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public <V extends FileAttributeView> V getFileAttributeView(
+ Path path, Class<V> type, LinkOption... options) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public <A extends BasicFileAttributes> A readAttributes(
+ Path path, Class<A> type, LinkOption... options) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Map<String,Object> readAttributes(
+ Path path, String attributes, LinkOption... options) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setAttribute(Path path, String attribute,
+ Object value, LinkOption... options) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+ @Test
+ public void testCreateLink() throws Exception {
+ MockFileSystemProvider provider = new MockFileSystemProvider();
+
+ Path link = Paths.get("testdir");
+ Path existing = Paths.get("testfile");
+
+ try {
+ provider.createLink(link, existing);
+ fail("Expected UnsupportedOperationException");
+ } catch(UnsupportedOperationException expected) {
+ }
+ }
+
+ @Test
+ public void testNewAsynchronousFileChannel() throws Exception {
+ MockFileSystemProvider provider = new MockFileSystemProvider();
+
+ Path path = Paths.get("testfile");
+ Set<OpenOption> options = new TreeSet<OpenOption>();
+
+ try {
+ provider.newAsynchronousFileChannel(path, options, null);
+ fail("Expected UnsupportedOperationException");
+ } catch(UnsupportedOperationException expected) {
+ }
+ }
+}