-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8820 from wmontwe/change-filehelper-to-kotlin
Change FileHelper to Kotlin
- Loading branch information
Showing
3 changed files
with
141 additions
and
147 deletions.
There are no files selected for viewing
147 changes: 0 additions & 147 deletions
147
legacy/core/src/main/java/com/fsck/k9/helper/FileHelper.java
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
legacy/core/src/main/java/com/fsck/k9/helper/FileHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.fsck.k9.helper | ||
|
||
import java.io.File | ||
import java.io.IOException | ||
import timber.log.Timber | ||
|
||
object FileHelper { | ||
|
||
@JvmStatic | ||
@Suppress("TooGenericExceptionCaught") | ||
fun touchFile(parentDir: File, name: String) { | ||
val file = File(parentDir, name) | ||
try { | ||
if (!file.exists()) { | ||
if (!file.createNewFile()) { | ||
Timber.d("Unable to create file: %s", file.absolutePath) | ||
} | ||
} else { | ||
if (!file.setLastModified(System.currentTimeMillis())) { | ||
Timber.d("Unable to change last modification date: %s", file.absolutePath) | ||
} | ||
} | ||
} catch (e: Exception) { | ||
Timber.d(e, "Unable to touch file: %s", file.absolutePath) | ||
} | ||
} | ||
|
||
@JvmStatic | ||
@Throws(IOException::class) | ||
fun renameOrMoveByCopying(from: File, to: File) { | ||
deleteFileIfExists(to) | ||
val renameFailed = !from.renameTo(to) | ||
if (renameFailed) { | ||
from.copyTo(target = to, overwrite = true) | ||
val deleteFromFailed = !from.delete() | ||
if (deleteFromFailed) { | ||
Timber.e("Unable to delete source file after copying to destination!") | ||
} | ||
} | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun deleteFileIfExists(file: File) { | ||
if (file.exists() && !file.delete()) { | ||
throw IOException("Unable to delete file: ${file.absolutePath}") | ||
} | ||
} | ||
|
||
@Suppress("TooGenericExceptionCaught") | ||
fun move(from: File, to: File): Boolean { | ||
if (to.exists()) { | ||
if (!to.delete()) { | ||
Timber.d("Unable to delete file: %s", to.absolutePath) | ||
} | ||
} | ||
|
||
val parent = to.parentFile | ||
if (parent != null && !parent.mkdirs()) { | ||
Timber.d("Unable to make directories: %s", parent.absolutePath) | ||
} | ||
return try { | ||
from.copyTo(target = to, overwrite = true) | ||
val deleteFromFailed = !from.delete() | ||
if (deleteFromFailed) { | ||
Timber.e("Unable to delete source file after copying to destination!") | ||
} | ||
true | ||
} catch (e: Exception) { | ||
Timber.w(e, "cannot move %s to %s", from.absolutePath, to.absolutePath) | ||
false | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
legacy/core/src/test/java/com/fsck/k9/helper/FileHelperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.fsck.k9.helper | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import assertk.assertions.isFalse | ||
import assertk.assertions.isNotEqualTo | ||
import assertk.assertions.isTrue | ||
import java.io.File | ||
import kotlin.test.Test | ||
import org.junit.Rule | ||
import org.junit.rules.TemporaryFolder | ||
|
||
class FileHelperTest { | ||
|
||
@Rule | ||
@JvmField | ||
val tempFolder = TemporaryFolder() | ||
|
||
@Test | ||
fun `touchFile should create file`() { | ||
val parent = tempFolder.newFolder("parent") | ||
|
||
FileHelper.touchFile(parent, "FileName") | ||
|
||
assertThat(File(parent, "FileName").exists()).isTrue() | ||
} | ||
|
||
@Test | ||
fun `touchFile should change file modification time`() { | ||
val parent = tempFolder.newFolder("parent") | ||
val lastModified = 12345L | ||
val file = File(parent, "FileName") | ||
file.createNewFile() | ||
file.setLastModified(lastModified) | ||
|
||
FileHelper.touchFile(parent, "FileName") | ||
|
||
assertThat(file.lastModified()).isNotEqualTo(lastModified) | ||
} | ||
|
||
@Test | ||
fun `renameOrMoveByCopying should move file when destination does not exist`() { | ||
val source = tempFolder.newFile("source") | ||
val destination = File(tempFolder.root, "destination") | ||
val sourceContent = "content" | ||
source.writeText(sourceContent) | ||
|
||
FileHelper.renameOrMoveByCopying(source, destination) | ||
|
||
assertThat(source.exists()).isFalse() | ||
assertThat(destination.exists()).isTrue() | ||
assertThat(destination.readText()).isEqualTo(sourceContent) | ||
} | ||
|
||
@Test | ||
fun `renameOrMoveByCopying should move file when destination exists`() { | ||
val source = tempFolder.newFile("source") | ||
val destination = tempFolder.newFile("destination") | ||
val sourceContent = "content" | ||
source.writeText(sourceContent) | ||
|
||
FileHelper.renameOrMoveByCopying(source, destination) | ||
|
||
assertThat(source.exists()).isFalse() | ||
assertThat(destination.exists()).isTrue() | ||
assertThat(destination.readText()).isEqualTo(sourceContent) | ||
} | ||
} |