Skip to content

Commit fb3055e

Browse files
domdomeggmaskaravivek
authored andcommitted
Add FileUtils tests for SHA1 and deletion (commons-app#2152)
* Add FileUtils tests for SHA1 and deletion * Avoid changing access modifier for SHA1 test
1 parent 647c679 commit fb3055e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

app/src/test/kotlin/fr/free/nrw/commons/utils/FileUtilsTest.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fr.free.nrw.commons.utils
22

33
import fr.free.nrw.commons.upload.FileUtils
4+
import fr.free.nrw.commons.upload.FileUtilsWrapper
45
import org.junit.Assert.assertEquals
56
import org.junit.Test
67
import 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

Comments
 (0)