diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2024-07-15 00:33:29 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2024-07-15 00:34:42 +0200 |
| commit | e126a3425b007a6fe5bb3db50902e3b60e2e3120 (patch) | |
| tree | bf5096a5cc3ea8487688234cfb5fa3aab96912e8 /app/src/test/java/org/the_jk/cleversync/io | |
| parent | e62c1097806098a0258fd44c0fa66d7577b6ed35 (diff) | |
Move LocalTreeTest to io
Diffstat (limited to 'app/src/test/java/org/the_jk/cleversync/io')
| -rw-r--r-- | app/src/test/java/org/the_jk/cleversync/io/LocalTreeTest.kt | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/app/src/test/java/org/the_jk/cleversync/io/LocalTreeTest.kt b/app/src/test/java/org/the_jk/cleversync/io/LocalTreeTest.kt new file mode 100644 index 0000000..495f6d7 --- /dev/null +++ b/app/src/test/java/org/the_jk/cleversync/io/LocalTreeTest.kt @@ -0,0 +1,102 @@ +package org.the_jk.cleversync.io + +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.safeValue + +@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).isEmpty() + assertThat(content.files).isEmpty() + assertThat(content.links).isEmpty() + } + + @Test + fun emptyLive() { + val content = tree.liveList().safeValue() + assertThat(content?.directories).isEmpty() + assertThat(content?.files).isEmpty() + assertThat(content?.links).isEmpty() + } + + @Test + fun createDirectory() { + val foo = tree.createDirectory("foo") + assertThat(foo.name).isEqualTo("foo") + val fooContent = foo.list() + assertThat(fooContent.directories).isEmpty() + assertThat(fooContent.files).isEmpty() + assertThat(fooContent.links).isEmpty() + val content = tree.list() + assertThat(content.directories).contains(foo) + assertThat(content.files).isEmpty() + assertThat(content.links).isEmpty() + } + + @Test + fun observeCreateDirectory() { + val content = tree.liveList() + var dir: Directory? = null + content.observeForever { + if (it.directories.size == 1) dir = it.directories[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).isEmpty() + foo.write().use { os -> + os.write(byteArrayOf(1, 2, 3, 4)) + } + assertThat(tree.list().files).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).hasSize(1) + foo.read().use { + assertThat(it.readBytes()).isEqualTo(byteArrayOf(127)) + } + } +} |
