summaryrefslogtreecommitdiff
path: root/app/src/test/java/org
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2024-07-11 23:28:01 +0200
committerJoel Klinghed <the_jk@spawned.biz>2024-07-11 23:28:01 +0200
commit5ac1ae8525181ba86ac6c17ef2192a5f7b17a86c (patch)
treede8250a7f4a76cbf789f380a8a3a4ca9b2d16f37 /app/src/test/java/org
Initial commit
Local (Path based) implementation of Tree, Directory, File and Link.
Diffstat (limited to 'app/src/test/java/org')
-rw-r--r--app/src/test/java/org/the_jk/cleversync/LocalTreeTest.kt90
1 files changed, 90 insertions, 0 deletions
diff --git a/app/src/test/java/org/the_jk/cleversync/LocalTreeTest.kt b/app/src/test/java/org/the_jk/cleversync/LocalTreeTest.kt
new file mode 100644
index 0000000..e3f70d4
--- /dev/null
+++ b/app/src/test/java/org/the_jk/cleversync/LocalTreeTest.kt
@@ -0,0 +1,90 @@
+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())
+ }
+
+ @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(1))
+ assertThat(foo.size).isEqualTo(4.toULong())
+ }
+ assertThat(foo.size).isEqualTo(1.toULong())
+ assertThat(tree.list().files.safeValue()).hasSize(1)
+ }
+}