FileExtensionsTest.kt 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* _____ _
  2. * |_ _| |_ _ _ ___ ___ _ __ __ _
  3. * | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. * |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. *
  6. * Threema for Android
  7. * Copyright (c) 2025 Threema GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package ch.threema.base
  22. import ch.threema.testhelpers.createTempDirectory
  23. import java.io.File
  24. import kotlin.test.AfterTest
  25. import kotlin.test.BeforeTest
  26. import kotlin.test.Test
  27. import kotlin.test.assertContentEquals
  28. import kotlin.test.assertFails
  29. import kotlin.test.assertFalse
  30. class FileExtensionsTest {
  31. private lateinit var directory: File
  32. @BeforeTest
  33. fun setUp() {
  34. directory = createTempDirectory()
  35. }
  36. @AfterTest
  37. fun tearDown() {
  38. directory.deleteRecursively()
  39. }
  40. @Test
  41. fun `write file successfully`() {
  42. val file = File(directory, "my-file")
  43. assertFalse(file.exists())
  44. file.writeAtomically { outputStream ->
  45. outputStream.write(byteArrayOf(1, 2, 3))
  46. outputStream.write(byteArrayOf(4, 5, 6))
  47. }
  48. assertContentEquals(byteArrayOf(1, 2, 3, 4, 5, 6), file.readBytes())
  49. }
  50. @Test
  51. fun `write file unsuccessfully`() {
  52. val file = File(directory, "my-file")
  53. assertFalse(file.exists())
  54. assertFails {
  55. file.writeAtomically { outputStream ->
  56. outputStream.write(byteArrayOf(1, 2, 3))
  57. error("something went wrong")
  58. }
  59. }
  60. assertFalse(file.exists())
  61. }
  62. }