Skip to content

Commit

Permalink
Merge "Add unit tests for java.nio.channels.FileChannel open methods"
Browse files Browse the repository at this point in the history
  • Loading branch information
Treehugger Robot authored and Gerrit Code Review committed Nov 2, 2016
2 parents 04decd7 + 96d64bf commit 172e5ee
Show file tree
Hide file tree
Showing 3 changed files with 332 additions and 2 deletions.
5 changes: 3 additions & 2 deletions JavaLibrary.mk
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ LOCAL_STATIC_JAVA_LIBRARIES := \
core-tests-support \
mockwebserver \
nist-pkix-tests \
sqlite-jdbc
sqlite-jdbc \
junit-params
LOCAL_JAVACFLAGS := $(local_javac_flags)
LOCAL_ERROR_PRONE_FLAGS := -Xep:TryFailThrowable:ERROR
LOCAL_JAVA_LANGUAGE_VERSION := 1.8
Expand Down Expand Up @@ -360,7 +361,7 @@ ifeq ($(LIBCORE_SKIP_TESTS),)
LOCAL_JAVA_RESOURCE_DIRS := $(test_resource_dirs)
LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_JAVA_LIBRARIES := core-oj-hostdex core-libart-hostdex okhttp-hostdex bouncycastle-hostdex core-junit-hostdex junit4-target-hostdex core-tests-support-hostdex mockito-api-hostdex
LOCAL_STATIC_JAVA_LIBRARIES := sqlite-jdbc-host mockwebserver-host nist-pkix-tests-host core-test-rules-hostdex
LOCAL_STATIC_JAVA_LIBRARIES := sqlite-jdbc-host mockwebserver-host nist-pkix-tests-host core-test-rules-hostdex junit-params-hostdex
LOCAL_JAVACFLAGS := $(local_javac_flags)
LOCAL_MODULE_TAGS := optional
LOCAL_JAVA_LANGUAGE_VERSION := 1.8
Expand Down
63 changes: 63 additions & 0 deletions luni/src/test/java/libcore/java/nio/channels/FileChannelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,20 @@
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.spi.FileSystemProvider;
import java.util.HashSet;
import java.util.Set;
import libcore.io.IoUtils;

import static java.nio.file.StandardOpenOption.READ;
import static java.nio.file.StandardOpenOption.WRITE;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class FileChannelTest extends junit.framework.TestCase {
public void testReadOnlyByteArrays() throws Exception {
ByteBuffer readOnly = ByteBuffer.allocate(1).asReadOnlyBuffer();
Expand Down Expand Up @@ -255,4 +267,55 @@ private static FileChannel createFileContainingBytes(byte[] bytes) throws IOExce

return fc;
}

/**
* The test verifies that FileChannel#open(Path, Set<OpenOption>, FileAttribute ...) returns the
* same object returned by #newFileChannel(Path, Set<OpenOption>, FileAttribute ...) method
* in given Paths's FileSystemProvider.
*/
public void test_open_Path_Set_FileAttributes() throws IOException {
Path mockPath = mock(Path.class);
FileSystem mockFileSystem = mock(FileSystem.class);
FileSystemProvider mockFileSystemProvider = mock(FileSystemProvider.class);
FileChannel mockFileChannel = mock(FileChannel.class);

FileAttribute mockFileAttribute1 = mock(FileAttribute.class);
FileAttribute mockFileAttribute2 = mock(FileAttribute.class);

Set<StandardOpenOption> standardOpenOptions = new HashSet<>();
standardOpenOptions.add(READ);
standardOpenOptions.add(WRITE);

when(mockPath.getFileSystem()).thenReturn(mockFileSystem);
when(mockFileSystem.provider()).thenReturn(mockFileSystemProvider);
when(mockFileSystemProvider.newFileChannel(mockPath, standardOpenOptions,
mockFileAttribute1, mockFileAttribute2)).thenReturn(mockFileChannel);

assertEquals(mockFileChannel, FileChannel.open(mockPath, standardOpenOptions,
mockFileAttribute1, mockFileAttribute2));
}

/**
* The test verifies that FileChannel#open(Path, OpenOption ...) returns the
* same object returned by #newFileChannel(Path, OpenOption ...) method
* in given Paths's FileSystemProvider.
*/
public void test_open_Path_OpenOptions() throws IOException {

Path mockPath = mock(Path.class);
FileSystem mockFileSystem = mock(FileSystem.class);
FileSystemProvider mockFileSystemProvider = mock(FileSystemProvider.class);
FileChannel mockFileChannel = mock(FileChannel.class);

Set<StandardOpenOption> standardOpenOptions = new HashSet<>();
standardOpenOptions.add(READ);
standardOpenOptions.add(WRITE);

when(mockPath.getFileSystem()).thenReturn(mockFileSystem);
when(mockFileSystem.provider()).thenReturn(mockFileSystemProvider);
when(mockFileSystemProvider.newFileChannel(mockPath, standardOpenOptions))
.thenReturn(mockFileChannel);

assertEquals(mockFileChannel, FileChannel.open(mockPath, READ, WRITE));
}
}
Loading

0 comments on commit 172e5ee

Please sign in to comment.