Skip to content

Commit 17e616e

Browse files
author
Ilia Isakhin
authored
Merge pull request #26 from java-online-course/feature/test_upd
- upd date test
2 parents 940d41e + 2b25ecb commit 17e616e

File tree

3 files changed

+81
-41
lines changed

3 files changed

+81
-41
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test
2+
1
3+
2
4+
текст

src/test/java/com/epam/izh/rd/online/DateServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void testGetNextLeapYear() {
5555
.findFirst()
5656
.getAsInt();
5757

58-
assertEquals(2020, dateService.getNextLeapYear(),
58+
assertEquals(2024, dateService.getNextLeapYear(),
5959
"Для вызова метода: dateService.getNextLeapYear()");
6060
}
6161

src/test/java/com/epam/izh/rd/online/FileRepositoryTest.java

Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import com.epam.izh.rd.online.repository.FileRepository;
44
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;
510
import org.junit.jupiter.api.BeforeAll;
611
import org.junit.jupiter.api.BeforeEach;
712
import org.junit.jupiter.api.DisplayName;
@@ -15,58 +20,89 @@
1520

1621
public class FileRepositoryTest {
1722

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";
2128

22-
private static FileRepository fileRepository;
29+
private static FileRepository fileRepository;
2330

24-
@BeforeAll
25-
static void setup() {
26-
fileRepository = new SimpleFileRepository();
27-
}
31+
@BeforeAll
32+
static void setup() {
33+
fileRepository = new SimpleFileRepository();
34+
}
2835

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();
3541
}
42+
}
3643

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+
}
4249

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());
4866

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);
5368

54-
assertTrue(getFile(TEST_DIR_CREATE_PATH + "/" + TEST_FILE_TO_CREATE).exists());
69+
if (copyFile.exists()) {
70+
copyFile.delete();
5571
}
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();
5690

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();
6195
}
6296

97+
return contentBuilder.toString();
98+
}
6399

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());
71105
}
106+
return new File("");
107+
}
72108
}

0 commit comments

Comments
 (0)