diff --git a/src/main/java/io/github/nstdio/http/ext/SimpleStreamFactory.java b/src/main/java/io/github/nstdio/http/ext/SimpleStreamFactory.java index 0ef63ef..0895960 100644 --- a/src/main/java/io/github/nstdio/http/ext/SimpleStreamFactory.java +++ b/src/main/java/io/github/nstdio/http/ext/SimpleStreamFactory.java @@ -19,13 +19,16 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; import java.nio.file.Files; import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.StandardOpenOption; +import static java.nio.file.StandardOpenOption.APPEND; import static java.nio.file.StandardOpenOption.READ; +import static java.nio.file.StandardOpenOption.WRITE; class SimpleStreamFactory implements StreamFactory { private static void assertNotContains(OpenOption[] options, StandardOpenOption needle) { @@ -52,4 +55,12 @@ public WritableByteChannel writable(Path path, OpenOption... options) throws IOE public InputStream input(Path path, OpenOption... options) throws IOException { return Files.newInputStream(path, options); } + + @Override + public ReadableByteChannel readable(Path path, OpenOption... options) throws IOException { + assertNotContains(options, WRITE); + assertNotContains(options, APPEND); + + return Files.newByteChannel(path, options); + } } diff --git a/src/test/kotlin/io/github/nstdio/http/ext/SimpleStreamFactoryTest.kt b/src/test/kotlin/io/github/nstdio/http/ext/SimpleStreamFactoryTest.kt index caf5d14..f2bf442 100644 --- a/src/test/kotlin/io/github/nstdio/http/ext/SimpleStreamFactoryTest.kt +++ b/src/test/kotlin/io/github/nstdio/http/ext/SimpleStreamFactoryTest.kt @@ -17,13 +17,25 @@ package io.github.nstdio.http.ext import io.kotest.assertions.throwables.shouldThrowExactly +import io.kotest.matchers.shouldBe import io.kotest.matchers.throwable.shouldHaveMessage import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import java.nio.ByteBuffer +import java.nio.file.Files import java.nio.file.Path +import java.nio.file.StandardOpenOption import java.nio.file.StandardOpenOption.READ import java.nio.file.StandardOpenOption.WRITE class SimpleStreamFactoryTest { + private val anyPath = Path.of("any") + + @TempDir + private lateinit var tempDir: Path + @Test fun `Should not allow read option on write method`() { //given @@ -31,7 +43,34 @@ class SimpleStreamFactoryTest { //when + then shouldThrowExactly { - factory.writable(Path.of("any"), READ, WRITE) + factory.writable(anyPath, READ, WRITE) }.shouldHaveMessage("READ not allowed") } + + @ParameterizedTest + @ValueSource(strings = ["WRITE", "APPEND"]) + fun `Should not allow write option on read method`(option: StandardOpenOption) { + //given + val factory = SimpleStreamFactory() + + //when + then + shouldThrowExactly { + factory.readable(anyPath, READ, option) + }.shouldHaveMessage("$option not allowed") + } + + @Test + fun `Should create channel`() { + //given + val file = tempDir.resolve("temp") + Files.write(file, listOf("a"), StandardOpenOption.CREATE) + + val factory = SimpleStreamFactory() + + //when + val channel = factory.readable(file) + + //then + channel.read(ByteBuffer.allocate(1)) shouldBe 1 + } } \ No newline at end of file