xref: /aosp_15_r20/external/okio/okio/src/jvmTest/kotlin/okio/OkioKotlinTest.kt (revision f9742813c14b702d71392179818a9e591da8620c)
1 /*
2  * Copyright (C) 2018 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package okio
18 
19 import java.io.ByteArrayInputStream
20 import java.io.ByteArrayOutputStream
21 import java.io.File
22 import java.net.Socket
23 import java.nio.file.StandardOpenOption
24 import java.nio.file.StandardOpenOption.APPEND
25 import org.assertj.core.api.Assertions.assertThat
26 import org.junit.Ignore
27 import org.junit.Rule
28 import org.junit.Test
29 import org.junit.rules.TemporaryFolder
30 
31 class OkioKotlinTest {
32   @get:Rule val temp = TemporaryFolder()
33 
outputStreamSinknull34   @Test fun outputStreamSink() {
35     val baos = ByteArrayOutputStream()
36     val sink = baos.sink()
37     sink.write(Buffer().writeUtf8("a"), 1L)
38     assertThat(baos.toByteArray()).isEqualTo(byteArrayOf(0x61))
39   }
40 
inputStreamSourcenull41   @Test fun inputStreamSource() {
42     val bais = ByteArrayInputStream(byteArrayOf(0x61))
43     val source = bais.source()
44     val buffer = Buffer()
45     source.read(buffer, 1)
46     assertThat(buffer.readUtf8()).isEqualTo("a")
47   }
48 
fileSinknull49   @Test fun fileSink() {
50     val file = temp.newFile()
51     val sink = file.sink()
52     sink.write(Buffer().writeUtf8("a"), 1L)
53     assertThat(file.readText()).isEqualTo("a")
54   }
55 
fileAppendingSinknull56   @Test fun fileAppendingSink() {
57     val file = temp.newFile()
58     file.writeText("a")
59     val sink = file.sink(append = true)
60     sink.write(Buffer().writeUtf8("b"), 1L)
61     sink.close()
62     assertThat(file.readText()).isEqualTo("ab")
63   }
64 
fileSourcenull65   @Test fun fileSource() {
66     val file = temp.newFile()
67     file.writeText("a")
68     val source = file.source()
69     val buffer = Buffer()
70     source.read(buffer, 1L)
71     assertThat(buffer.readUtf8()).isEqualTo("a")
72   }
73 
pathSinknull74   @Test fun pathSink() {
75     val file = temp.newFile()
76     val sink = file.toPath().sink()
77     sink.write(Buffer().writeUtf8("a"), 1L)
78     assertThat(file.readText()).isEqualTo("a")
79   }
80 
pathSinkWithOptionsnull81   @Test fun pathSinkWithOptions() {
82     val file = temp.newFile()
83     file.writeText("a")
84     val sink = file.toPath().sink(APPEND)
85     sink.write(Buffer().writeUtf8("b"), 1L)
86     assertThat(file.readText()).isEqualTo("ab")
87   }
88 
pathSourcenull89   @Test fun pathSource() {
90     val file = temp.newFile()
91     file.writeText("a")
92     val source = file.toPath().source()
93     val buffer = Buffer()
94     source.read(buffer, 1L)
95     assertThat(buffer.readUtf8()).isEqualTo("a")
96   }
97 
98   @Ignore("Not sure how to test this")
99   @Test
pathSourceWithOptionsnull100   fun pathSourceWithOptions() {
101     val folder = temp.newFolder()
102     val file = File(folder, "new.txt")
103     file.toPath().source(StandardOpenOption.CREATE_NEW)
104     // This still throws NoSuchFileException...
105   }
106 
socketSinknull107   @Test fun socketSink() {
108     val baos = ByteArrayOutputStream()
109     val socket = object : Socket() {
110       override fun getOutputStream() = baos
111     }
112     val sink = socket.sink()
113     sink.write(Buffer().writeUtf8("a"), 1L)
114     assertThat(baos.toByteArray()).isEqualTo(byteArrayOf(0x61))
115   }
116 
socketSourcenull117   @Test fun socketSource() {
118     val bais = ByteArrayInputStream(byteArrayOf(0x61))
119     val socket = object : Socket() {
120       override fun getInputStream() = bais
121     }
122     val source = socket.source()
123     val buffer = Buffer()
124     source.read(buffer, 1L)
125     assertThat(buffer.readUtf8()).isEqualTo("a")
126   }
127 }
128