-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileToByteArrayReaderTest.java
62 lines (51 loc) · 1.96 KB
/
FileToByteArrayReaderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.stackroute.pe2;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.FileNotFoundException;
import static org.junit.Assert.*;
/**
* Practice Exercise - Question 8
* Write a program to find all files of a folder and select only given extension fileName and
* read content of this file using byte array
*/
public class FileToByteArrayReaderTest {
private FileToByteArrayReader fileToByteArrayReader;
@Before
public void setUp() {
fileToByteArrayReader = new FileToByteArrayReader();
}
@After
public void tearDown() {
fileToByteArrayReader = null;
}
/**
* Test readFile() by giving a file name and extension method should return
* contents of the file in byte array.
*/
@Test
public void givenFileNameAndExtensionShouldReturnByteArray() {
/*Expected test string which is converted into byte array*/
String testString = "Hello. This is a test string";
byte[] testByteArray = testString.getBytes();
assertArrayEquals("testReadFileSuccessGivenFileNameAndExtensionShouldReturnByteArray: check readFile()",
testByteArray, fileToByteArrayReader.readFile("test", "txt"));
}
/**
* Test for readFile() to throw FileNotFoundException
* when the wrong(non-existent) file path is given.
*/
@Test(expected = FileNotFoundException.class)
public void givenFileNameAndExtensionShouldReturnFileNotFoundException() {
fileToByteArrayReader.readFile("test1", "txt");
}
/**
* Test readFile() method with an empty file. The method should return null as
* the file is empty.
*/
@Test
public void givenFileNameAndExtensionWithAnEmptyFileShouldReturnNull() {
assertNull("givenFileNameAndExtensionWithAnEmptyFileShouldReturnNull: check readFile(). The method should read a empty file and return null"
,fileToByteArrayReader.readFile("nullFileTest","txt"));
}
}