From fb3055e71699e183587020b2d976adbb96535172 Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Mon, 17 Dec 2018 15:45:01 +0000 Subject: [PATCH] Add FileUtils tests for SHA1 and deletion (#2152) * Add FileUtils tests for SHA1 and deletion * Avoid changing access modifier for SHA1 test --- .../free/nrw/commons/utils/FileUtilsTest.kt | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/app/src/test/kotlin/fr/free/nrw/commons/utils/FileUtilsTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/utils/FileUtilsTest.kt index c3c118709..f4d4ad51f 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/utils/FileUtilsTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/utils/FileUtilsTest.kt @@ -1,6 +1,7 @@ package fr.free.nrw.commons.utils import fr.free.nrw.commons.upload.FileUtils +import fr.free.nrw.commons.upload.FileUtilsWrapper import org.junit.Assert.assertEquals import org.junit.Test import java.io.* @@ -17,6 +18,41 @@ class FileUtilsTest { assertEquals(getString(source), getString(dest)) } + @Test + fun deleteFile() { + val file = File.createTempFile("testfile", "") + writeToFile(file, "Hello, World") + + assertEquals(true, file.exists()) + assertEquals(true, FileUtils.deleteFile(file)) + assertEquals(false, file.exists()) + } + + @Test + fun testSHA1() { + val fileUtilsWrapper = FileUtilsWrapper() + + assertEquals( + "907d14fb3af2b0d4f18c2d46abe8aedce17367bd", + fileUtilsWrapper.getSHA1(toInputStream("Hello, World")) + ) + + assertEquals( + "8b971da6347bd126872ea2f4f8d394e70c74073a", + fileUtilsWrapper.getSHA1(toInputStream("apps-android-commons")) + ) + + assertEquals( + "e9d30f5a3a82792b9d79c258366bd53207ceaeb3", + fileUtilsWrapper.getSHA1(toInputStream("domdomegg was here")) + ) + + assertEquals( + "96e733a3e59261c0621ba99be5bd10bb21abe53e", + fileUtilsWrapper.getSHA1(toInputStream("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=")) + ) + } + private fun writeToFile(file: File, s: String) { val buf = BufferedOutputStream(FileOutputStream(file)) buf.write(s.toByteArray()) @@ -30,4 +66,8 @@ class FileUtilsTest { buf.close() return String(bytes) } + + private fun toInputStream(str: String) : InputStream { + return ByteArrayInputStream(str.toByteArray(Charsets.UTF_8)) + } }