|
2 | 2 |
|
3 | 3 | import com.epam.izh.rd.online.repository.FileRepository; |
4 | 4 | import com.epam.izh.rd.online.repository.SimpleFileRepository; |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Paths; |
| 9 | +import java.util.stream.Stream; |
5 | 10 | import org.junit.jupiter.api.BeforeAll; |
6 | 11 | import org.junit.jupiter.api.BeforeEach; |
7 | 12 | import org.junit.jupiter.api.DisplayName; |
|
15 | 20 |
|
16 | 21 | public class FileRepositoryTest { |
17 | 22 |
|
18 | | - private static final String TEST_DIR_COUNT_PATH = "testDirCountFiles"; |
19 | | - private static final String TEST_DIR_CREATE_PATH = "testDirCreateFile"; |
20 | | - private static final String TEST_FILE_TO_CREATE = "newFile.txt"; |
| 23 | + private static final String TEST_DIR_COUNT_PATH = "testDirCountFiles"; |
| 24 | + private static final String TEST_DIR_CREATE_PATH = "testDirCreateFile"; |
| 25 | + private static final String TEST_FILE_TO_CREATE = "newFile.txt"; |
| 26 | + private static final String SOURCE_FILE = "fileRepository/source/TestFileToCopy.txt"; |
| 27 | + private static final String COPY_FILE = "fileRepository/copy/TestFileToCopy.txt"; |
21 | 28 |
|
22 | | - private static FileRepository fileRepository; |
| 29 | + private static FileRepository fileRepository; |
23 | 30 |
|
24 | | - @BeforeAll |
25 | | - static void setup() { |
26 | | - fileRepository = new SimpleFileRepository(); |
27 | | - } |
| 31 | + @BeforeAll |
| 32 | + static void setup() { |
| 33 | + fileRepository = new SimpleFileRepository(); |
| 34 | + } |
28 | 35 |
|
29 | | - @BeforeEach |
30 | | - void clean() { |
31 | | - File file = getFile(TEST_DIR_CREATE_PATH + "/" + TEST_FILE_TO_CREATE); |
32 | | - if (file.exists()) { |
33 | | - file.delete(); |
34 | | - } |
| 36 | + @BeforeEach |
| 37 | + void clean() { |
| 38 | + File file = getFile(TEST_DIR_CREATE_PATH + "/" + TEST_FILE_TO_CREATE); |
| 39 | + if (file.exists()) { |
| 40 | + file.delete(); |
35 | 41 | } |
| 42 | + } |
36 | 43 |
|
37 | | - @Test |
38 | | - @DisplayName("Тест метода FileRepository.countDirsInDirectory(String path)") |
39 | | - void testCountDirsInDirectory() { |
40 | | - assertEquals(7, fileRepository.countDirsInDirectory(TEST_DIR_COUNT_PATH)); |
41 | | - } |
| 44 | + @Test |
| 45 | + @DisplayName("Тест метода FileRepository.countDirsInDirectory(String path)") |
| 46 | + void testCountDirsInDirectory() { |
| 47 | + assertEquals(7, fileRepository.countDirsInDirectory(TEST_DIR_COUNT_PATH)); |
| 48 | + } |
42 | 49 |
|
43 | | - @Test |
44 | | - @DisplayName("Тест метода FileRepository.countFilesInDirectory(String path)") |
45 | | - void testCountFilesInDirectory() { |
46 | | - assertEquals(10, fileRepository.countFilesInDirectory(TEST_DIR_COUNT_PATH)); |
47 | | - } |
| 50 | + @Test |
| 51 | + @DisplayName("Тест метода FileRepository.countFilesInDirectory(String path)") |
| 52 | + void testCountFilesInDirectory() { |
| 53 | + assertEquals(10, fileRepository.countFilesInDirectory(TEST_DIR_COUNT_PATH)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @DisplayName("Тест метода FileRepository.copyTXTFiles(String from, String to)") |
| 58 | + void testCopyTXTFiles() { |
| 59 | + final File emptyFile = getFile(COPY_FILE); |
| 60 | + assertEquals("", emptyFile.getName()); |
| 61 | + fileRepository.copyTXTFiles(SOURCE_FILE, COPY_FILE); |
| 62 | + final File sourceFile = new File(SOURCE_FILE); |
| 63 | + final String sourceContent = readLineByLineJava(sourceFile.getAbsolutePath()); |
| 64 | + final File copyFile = new File(COPY_FILE); |
| 65 | + final String copyContent = readLineByLineJava(copyFile.getAbsolutePath()); |
48 | 66 |
|
49 | | - @Test |
50 | | - @DisplayName("Тест метода FileRepository.createFile(String path)") |
51 | | - void testCreateFile() { |
52 | | - fileRepository.createFile(TEST_DIR_CREATE_PATH, TEST_FILE_TO_CREATE); |
| 67 | + assertEquals(sourceContent, copyContent); |
53 | 68 |
|
54 | | - assertTrue(getFile(TEST_DIR_CREATE_PATH + "/" + TEST_FILE_TO_CREATE).exists()); |
| 69 | + if (copyFile.exists()) { |
| 70 | + copyFile.delete(); |
55 | 71 | } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @DisplayName("Тест метода FileRepository.createFile(String path)") |
| 76 | + void testCreateFile() { |
| 77 | + fileRepository.createFile(TEST_DIR_CREATE_PATH, TEST_FILE_TO_CREATE); |
| 78 | + |
| 79 | + assertTrue(getFile(TEST_DIR_CREATE_PATH + "/" + TEST_FILE_TO_CREATE).exists()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + @DisplayName("Тест метода FileRepository.readFileFromResources(String fileName)") |
| 84 | + void testReadFileFromResources() { |
| 85 | + assertEquals("Ya-hoo!", fileRepository.readFileFromResources("readme.txt")); |
| 86 | + } |
| 87 | + |
| 88 | + private String readLineByLineJava(String filePath) { |
| 89 | + StringBuilder contentBuilder = new StringBuilder(); |
56 | 90 |
|
57 | | - @Test |
58 | | - @DisplayName("Тест метода FileRepository.readFileFromResources(String fileName)") |
59 | | - void testReadFileFromResources() { |
60 | | - assertEquals("Ya-hoo!", fileRepository.readFileFromResources("readme.txt")); |
| 91 | + try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) { |
| 92 | + stream.forEach(s -> contentBuilder.append(s).append("\n")); |
| 93 | + } catch (IOException e) { |
| 94 | + e.printStackTrace(); |
61 | 95 | } |
62 | 96 |
|
| 97 | + return contentBuilder.toString(); |
| 98 | + } |
63 | 99 |
|
64 | | - private File getFile(String path) { |
65 | | - ClassLoader classLoader = getClass().getClassLoader(); |
66 | | - URL resource = classLoader.getResource(path); |
67 | | - if (resource != null) { |
68 | | - return new File(resource.getFile()); |
69 | | - } |
70 | | - return new File(""); |
| 100 | + private File getFile(String path) { |
| 101 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 102 | + URL resource = classLoader.getResource(path); |
| 103 | + if (resource != null) { |
| 104 | + return new File(resource.getFile()); |
71 | 105 | } |
| 106 | + return new File(""); |
| 107 | + } |
72 | 108 | } |
0 commit comments