11package fr.free.nrw.commons.utils
22
33import fr.free.nrw.commons.upload.FileUtils
4+ import fr.free.nrw.commons.upload.FileUtilsWrapper
45import org.junit.Assert.assertEquals
56import org.junit.Test
67import java.io.*
@@ -17,6 +18,41 @@ class FileUtilsTest {
1718 assertEquals(getString(source), getString(dest))
1819 }
1920
21+ @Test
22+ fun deleteFile () {
23+ val file = File .createTempFile(" testfile" , " " )
24+ writeToFile(file, " Hello, World" )
25+
26+ assertEquals(true , file.exists())
27+ assertEquals(true , FileUtils .deleteFile(file))
28+ assertEquals(false , file.exists())
29+ }
30+
31+ @Test
32+ fun testSHA1 () {
33+ val fileUtilsWrapper = FileUtilsWrapper ()
34+
35+ assertEquals(
36+ " 907d14fb3af2b0d4f18c2d46abe8aedce17367bd" ,
37+ fileUtilsWrapper.getSHA1(toInputStream(" Hello, World" ))
38+ )
39+
40+ assertEquals(
41+ " 8b971da6347bd126872ea2f4f8d394e70c74073a" ,
42+ fileUtilsWrapper.getSHA1(toInputStream(" apps-android-commons" ))
43+ )
44+
45+ assertEquals(
46+ " e9d30f5a3a82792b9d79c258366bd53207ceaeb3" ,
47+ fileUtilsWrapper.getSHA1(toInputStream(" domdomegg was here" ))
48+ )
49+
50+ assertEquals(
51+ " 96e733a3e59261c0621ba99be5bd10bb21abe53e" ,
52+ fileUtilsWrapper.getSHA1(toInputStream(" iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=" ))
53+ )
54+ }
55+
2056 private fun writeToFile (file : File , s : String ) {
2157 val buf = BufferedOutputStream (FileOutputStream (file))
2258 buf.write(s.toByteArray())
@@ -30,4 +66,8 @@ class FileUtilsTest {
3066 buf.close()
3167 return String (bytes)
3268 }
69+
70+ private fun toInputStream (str : String ) : InputStream {
71+ return ByteArrayInputStream (str.toByteArray(Charsets .UTF_8 ))
72+ }
3373}
0 commit comments