Skip to content

Replace functions in FileUtilsTest with ones from kotlin-stdlib #2943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions app/src/test/kotlin/fr/free/nrw/commons/utils/FileUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FileUtilsTest {
@Test
fun deleteFile() {
val file = File.createTempFile("testfile", "")
writeToFile(file, "Hello, World")
file.writeText("Hello, World")

assertEquals(true, file.exists())
assertEquals(true, FileUtils.deleteFile(file))
Expand All @@ -23,40 +23,22 @@ class FileUtilsTest {

assertEquals(
"907d14fb3af2b0d4f18c2d46abe8aedce17367bd",
fileUtilsWrapper.getSHA1(toInputStream("Hello, World"))
fileUtilsWrapper.getSHA1("Hello, World".byteInputStream())
)

assertEquals(
"8b971da6347bd126872ea2f4f8d394e70c74073a",
fileUtilsWrapper.getSHA1(toInputStream("apps-android-commons"))
fileUtilsWrapper.getSHA1("apps-android-commons".byteInputStream())
)

assertEquals(
"e9d30f5a3a82792b9d79c258366bd53207ceaeb3",
fileUtilsWrapper.getSHA1(toInputStream("domdomegg was here"))
fileUtilsWrapper.getSHA1("domdomegg was here".byteInputStream())
)

assertEquals(
"96e733a3e59261c0621ba99be5bd10bb21abe53e",
fileUtilsWrapper.getSHA1(toInputStream("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="))
fileUtilsWrapper.getSHA1("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=".byteInputStream())
)
}

private fun writeToFile(file: File, s: String) {
val buf = BufferedOutputStream(FileOutputStream(file))
buf.write(s.toByteArray())
buf.close()
}

private fun getString(file: File): String {
val bytes = ByteArray(file.length().toInt())
val buf = BufferedInputStream(FileInputStream(file))
buf.read(bytes, 0, bytes.size)
buf.close()
return String(bytes)
}

private fun toInputStream(str: String) : InputStream {
return ByteArrayInputStream(str.toByteArray(Charsets.UTF_8))
}
}