1 /*
2  * Copyright 2023 The Android Open Source Project
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 androidx.datastore
18 
19 import androidx.kruth.assertThat
20 import androidx.kruth.assertThrows
21 import kotlin.test.Test
22 
23 class OkioTestIOTest : TestIOTestBase(OkioTestIO())
24 
25 abstract class TestIOTestBase(private val testIO: TestIO<*, *>) {
26     @Test
writeNewFilenull27     fun writeNewFile() {
28         val file = testIO.newTempFile()
29         assertThat(file.exists()).isFalse()
30         file.write("body")
31         assertThat(file.exists()).isTrue()
32         assertThat(file.isDirectory()).isFalse()
33         assertThat(file.isRegularFile()).isTrue()
34     }
35 
36     @Test
createDirectorynull37     fun createDirectory() {
38         val file = testIO.newTempFile(relativePath = "some/sub/directory")
39         assertThat(file.exists()).isFalse()
40         file.mkdirs(mustCreate = true)
41         assertThat(file.isDirectory()).isTrue()
42 
43         // try to re-create over it, should fail
44         assertThrows<Throwable> { file.mkdirs(mustCreate = true) }
45         // don't need to create so should be fine
46         file.mkdirs(mustCreate = false)
47     }
48 
49     @Test
createDirectory_overFilenull50     fun createDirectory_overFile() {
51         val file = testIO.newTempFile(relativePath = "some/sub/file")
52         file.write("test")
53         assertThrows<Throwable> { file.mkdirs(mustCreate = false) }
54         assertThrows<Throwable> { file.mkdirs(mustCreate = true) }
55     }
56 
57     @Test
writeToDirectorynull58     fun writeToDirectory() {
59         val file = testIO.newTempFile(relativePath = "some/dir")
60         file.mkdirs()
61         assertThrows<Throwable> { file.write("some text") }
62     }
63 
64     @Test
resolvenull65     fun resolve() {
66         val file = testIO.newTempFile(relativePath = "dir1/dir2/file")
67         file.write("test")
68         val file2 = testIO.newTempFile(relativePath = "dir1/dir2/file")
69         assertThat(file2.readText()).isEqualTo("test")
70         val parent = testIO.newTempFile(relativePath = "dir1")
71         assertThat(parent.resolve("dir2/file").readText()).isEqualTo("test")
72     }
73 
74     @Test
delete_regularFilenull75     fun delete_regularFile() {
76         val file = testIO.newTempFile(relativePath = "foo")
77         file.write("foo")
78         assertThat(file.isRegularFile()).isTrue()
79         file.delete()
80         assertThat(file.exists()).isFalse()
81     }
82 
83     @Test
delete_directorynull84     fun delete_directory() {
85         val file = testIO.newTempFile(relativePath = "foo/bar")
86         file.mkdirs()
87         assertThat(file.isDirectory()).isTrue()
88         file.delete()
89         assertThat(file.exists()).isFalse()
90     }
91 
92     @Test
readBytesnull93     fun readBytes() {
94         val file = testIO.newTempFile()
95         // cannot read non-existing file
96         assertThrows<Throwable> { file.readBytes() }
97         file.mkdirs()
98         // cannot read a directory
99         assertThrows<Throwable> { file.readBytes() }
100     }
101 }
102