blob: d9a9bd8a646de1052e506082437146f7cf681f07 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
package org.the_jk.cleversync
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowLooper
import org.the_jk.cleversync.io.Directory
import org.the_jk.cleversync.io.ModifiableTree
import org.the_jk.cleversync.io.TreeFactory
@Config(manifest=Config.NONE)
@RunWith(RobolectricTestRunner::class)
class LocalTreeTest {
@get:Rule
val folder = TemporaryFolder()
private lateinit var tree: ModifiableTree
@Before
fun setUp() {
tree = TreeFactory.localModifiableTree(folder.root.toPath())
}
@Test
fun empty() {
val content = tree.list()
assertThat(content.directories.safeValue()).isEmpty()
assertThat(content.files.safeValue()).isEmpty()
assertThat(content.links.safeValue()).isEmpty()
}
@Test
fun createDirectory() {
val foo = tree.createDirectory("foo")
assertThat(foo.name).isEqualTo("foo")
val fooContent = foo.list()
assertThat(fooContent.directories.safeValue()).isEmpty()
assertThat(fooContent.files.safeValue()).isEmpty()
assertThat(fooContent.links.safeValue()).isEmpty()
val content = tree.list()
assertThat(content.directories.safeValue()).contains(foo)
assertThat(content.files.safeValue()).isEmpty()
assertThat(content.links.safeValue()).isEmpty()
}
@Test
fun observeCreateDirectory() {
val content = tree.list()
var dir: Directory? = null
content.directories.observeForever { list ->
if (list.size == 1) dir = list[0]
}
tree.createDirectory("foo")
while (dir == null) {
ShadowLooper.idleMainLooper()
}
assertThat(dir?.name).isEqualTo("foo")
}
@Test
fun createFile() {
val foo = tree.createFile("foo")
// Files are not created until you write to them.
assertThat(tree.list().files.safeValue()).isEmpty()
foo.write().use { os ->
os.write(byteArrayOf(1, 2, 3, 4))
}
assertThat(tree.list().files.safeValue()).contains(foo)
assertThat(foo.size).isEqualTo(4.toULong())
foo.read().use {
assertThat(it.readBytes()).isEqualTo(byteArrayOf(1, 2, 3, 4))
}
}
@Test
fun overwriteFile() {
val foo = tree.createFile("foo")
foo.write().use { os ->
os.write(byteArrayOf(1, 2, 3, 4))
}
foo.write().use { os ->
os.write(byteArrayOf(127))
assertThat(foo.size).isEqualTo(4.toULong())
}
assertThat(foo.size).isEqualTo(1.toULong())
assertThat(tree.list().files.safeValue()).hasSize(1)
foo.read().use {
assertThat(it.readBytes()).isEqualTo(byteArrayOf(127))
}
}
}
|