mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Add test for FileUtils
This commit is contained in:
parent
9c69539276
commit
4f4388b04e
1 changed files with 40 additions and 0 deletions
40
app/src/test/java/fr/free/nrw/commons/FileUtilsTest.java
Normal file
40
app/src/test/java/fr/free/nrw/commons/FileUtilsTest.java
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package fr.free.nrw.commons;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import fr.free.nrw.commons.upload.FileUtils;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
|
||||
public class FileUtilsTest {
|
||||
@Test public void copiedFileIsIdenticalToSource() throws IOException {
|
||||
File source = File.createTempFile("temp", "");
|
||||
File dest = File.createTempFile("temp", "");
|
||||
writeToFile(source, "Hello, World");
|
||||
FileUtils.copy(new FileInputStream(source), new FileOutputStream(dest));
|
||||
Assert.assertThat(getString(dest), is(getString(source)));
|
||||
}
|
||||
|
||||
private static void writeToFile(File file, String s) throws IOException {
|
||||
BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream(file));
|
||||
buf.write(s.getBytes());
|
||||
buf.close();
|
||||
}
|
||||
|
||||
private static String getString(File file) throws IOException {
|
||||
int size = (int) file.length();
|
||||
byte[] bytes = new byte[size];
|
||||
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
|
||||
buf.read(bytes, 0, bytes.length);
|
||||
buf.close();
|
||||
return new String(bytes);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue