1717package io.github.nstdio.http.ext
1818
1919import io.kotest.assertions.throwables.shouldThrowExactly
20+ import io.kotest.matchers.shouldBe
2021import io.kotest.matchers.throwable.shouldHaveMessage
2122import org.junit.jupiter.api.Test
23+ import org.junit.jupiter.api.io.TempDir
24+ import org.junit.jupiter.params.ParameterizedTest
25+ import org.junit.jupiter.params.provider.ValueSource
26+ import java.nio.ByteBuffer
27+ import java.nio.file.Files
2228import java.nio.file.Path
29+ import java.nio.file.StandardOpenOption
2330import java.nio.file.StandardOpenOption.READ
2431import java.nio.file.StandardOpenOption.WRITE
2532
2633class SimpleStreamFactoryTest {
34+ private val anyPath = Path .of(" any" )
35+
36+ @TempDir
37+ private lateinit var tempDir: Path
38+
2739 @Test
2840 fun `Should not allow read option on write method` () {
2941 // given
3042 val factory = SimpleStreamFactory ()
3143
3244 // when + then
3345 shouldThrowExactly<IllegalArgumentException > {
34- factory.writable(Path .of( " any " ) , READ , WRITE )
46+ factory.writable(anyPath , READ , WRITE )
3547 }.shouldHaveMessage(" READ not allowed" )
3648 }
49+
50+ @ParameterizedTest
51+ @ValueSource(strings = [" WRITE" , " APPEND" ])
52+ fun `Should not allow write option on read method` (option : StandardOpenOption ) {
53+ // given
54+ val factory = SimpleStreamFactory ()
55+
56+ // when + then
57+ shouldThrowExactly<IllegalArgumentException > {
58+ factory.readable(anyPath, READ , option)
59+ }.shouldHaveMessage(" $option not allowed" )
60+ }
61+
62+ @Test
63+ fun `Should create channel` () {
64+ // given
65+ val file = tempDir.resolve(" temp" )
66+ Files .write(file, listOf (" a" ), StandardOpenOption .CREATE )
67+
68+ val factory = SimpleStreamFactory ()
69+
70+ // when
71+ val channel = factory.readable(file)
72+
73+ // then
74+ channel.read(ByteBuffer .allocate(1 )) shouldBe 1
75+ }
3776}
0 commit comments