1
1
package fr.free.nrw.commons.utils
2
2
3
3
import fr.free.nrw.commons.upload.FileUtils
4
+ import fr.free.nrw.commons.upload.FileUtilsWrapper
4
5
import org.junit.Assert.assertEquals
5
6
import org.junit.Test
6
7
import java.io.*
@@ -17,6 +18,41 @@ class FileUtilsTest {
17
18
assertEquals(getString(source), getString(dest))
18
19
}
19
20
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
+
20
56
private fun writeToFile (file : File , s : String ) {
21
57
val buf = BufferedOutputStream (FileOutputStream (file))
22
58
buf.write(s.toByteArray())
@@ -30,4 +66,8 @@ class FileUtilsTest {
30
66
buf.close()
31
67
return String (bytes)
32
68
}
69
+
70
+ private fun toInputStream (str : String ) : InputStream {
71
+ return ByteArrayInputStream (str.toByteArray(Charsets .UTF_8 ))
72
+ }
33
73
}
0 commit comments