/
pchelkin_g
/
homework6
Обзор
Документация
Войти
/
pchelkin_g
/
homework6
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
MyIOExample
src/test/java/sbp/io/MyIOExampleTests.java
180 строк
7 KB
pchelking
MyIOExample
23 дек 2025, 21:59
23 дек 2025, 21:59
0ad9c12
Код
Авторство
О чём код?
package sbp.io; import org.junit.jupiter.api.*; import org.junit.jupiter.api.io.TempDir; import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.FileTime; import java.time.Instant; import java.time.temporal.ChronoUnit; import static org.junit.jupiter.api.Assertions.*; /** * Тесты для класса MyIOExample. */ class MyIOExampleTest { private MyIOExample myIOExample; @TempDir Path tempDir; @BeforeEach void setUp() { myIOExample = new MyIOExample(); } @Test @DisplayName("Тест работы с существующим файлом") void testWorkWithFile_ExistingFile() throws IOException { // Создаем тестовый файл Path testFile = tempDir.resolve("test.txt"); String content = "Test content for file"; Files.writeString(testFile, content); // Устанавливаем время изменения Instant modifiedTime = Instant.now().minus(1, ChronoUnit.HOURS); Files.setLastModifiedTime(testFile, FileTime.from(modifiedTime)); assertTrue(myIOExample.workWithFile(testFile.toString())); } @Test @DisplayName("Тест работы с несуществующим файлом") void testWorkWithFile_NonExistingFile() { Path nonExistingFile = tempDir.resolve("non-existing.txt"); assertFalse(myIOExample.workWithFile(nonExistingFile.toString())); } @Test @DisplayName("Тест работы с директорией") void testWorkWithFile_Directory() throws IOException { Path testDir = tempDir.resolve("testDir"); Files.createDirectory(testDir); assertTrue(myIOExample.workWithFile(testDir.toString())); } @Test @DisplayName("Тест копирования файла") void testCopyFile() throws IOException { // Создаем исходный файл Path sourceFile = tempDir.resolve("source.txt"); String content = "Source file content for copy test"; Files.writeString(sourceFile, content); // Целевой файл Path destFile = tempDir.resolve("destination.txt"); assertTrue(myIOExample.copyFile(sourceFile.toString(), destFile.toString())); assertTrue(Files.exists(destFile)); // Проверяем содержимое String copiedContent = Files.readString(destFile); assertEquals(content, copiedContent); } @Test @DisplayName("Тест копирования несуществующего файла") void testCopyFile_NonExistingSource() { Path sourceFile = tempDir.resolve("non-existing.txt"); Path destFile = tempDir.resolve("destination.txt"); assertFalse(myIOExample.copyFile(sourceFile.toString(), destFile.toString())); assertFalse(Files.exists(destFile)); } @Test @DisplayName("Тест копирования с буферизацией") void testCopyBufferedFile() throws IOException { // Создаем исходный файл с большим содержимым Path sourceFile = tempDir.resolve("source_buffered.txt"); StringBuilder contentBuilder = new StringBuilder(); for (int i = 0; i < 10000; i++) { contentBuilder.append("Line ").append(i).append(": Test content for buffered copy\n"); } Files.writeString(sourceFile, contentBuilder.toString()); Path destFile = tempDir.resolve("destination_buffered.txt"); assertTrue(myIOExample.copyBufferedFile(sourceFile.toString(), destFile.toString())); assertTrue(Files.exists(destFile)); // Проверяем размер assertEquals(Files.size(sourceFile), Files.size(destFile)); } @Test @DisplayName("Тест копирования в несуществующую директорию") void testCopyFile_ToNonExistingDirectory() throws IOException { Path sourceFile = tempDir.resolve("source.txt"); Files.writeString(sourceFile, "Test content"); Path destDir = tempDir.resolve("subdir").resolve("destination.txt"); assertTrue(myIOExample.copyFile(sourceFile.toString(), destDir.toString())); assertTrue(Files.exists(destDir)); } @Test @DisplayName("Тест копирования текстового файла с Reader/Writer") void testCopyFileWithReaderAndWriter() throws IOException { // Создаем текстовый файл с разными символами Path sourceFile = tempDir.resolve("source_text.txt"); String content = "Текстовый файл с разными символами:\n" + "English text\n" + "Русский текст\n" + "Special characters: !@#$%^&*()\n" + "Unicode: \u00A9 \u20AC \u03B1 \u03B2"; Files.writeString(sourceFile, content); Path destFile = tempDir.resolve("destination_text.txt"); assertTrue(myIOExample.copyFileWithReaderAndWriter(sourceFile.toString(), destFile.toString())); assertTrue(Files.exists(destFile)); // Проверяем содержимое String copiedContent = Files.readString(destFile); assertEquals(content, copiedContent); } @Test @DisplayName("Тест перезаписи существующего файла") void testCopyFile_OverwriteExisting() throws IOException { Path sourceFile = tempDir.resolve("source.txt"); Files.writeString(sourceFile, "New content"); Path destFile = tempDir.resolve("destination.txt"); Files.writeString(destFile, "Old content that should be overwritten"); assertTrue(myIOExample.copyFile(sourceFile.toString(), destFile.toString())); String copiedContent = Files.readString(destFile); assertEquals("New content", copiedContent); } @Test @DisplayName("Тест с некорректным путем") void testWorkWithFile_InvalidPath() { assertFalse(myIOExample.workWithFile("invalid:path?*.txt")); } @Test @DisplayName("Тест копирования директории вместо файла") void testCopyFile_DirectoryInsteadOfFile() throws IOException { Path sourceDir = tempDir.resolve("directory"); Files.createDirectory(sourceDir); Path destFile = tempDir.resolve("destination.txt"); assertFalse(myIOExample.copyFile(sourceDir.toString(), destFile.toString())); } @AfterEach void tearDown() { // Очистка не требуется, так как @TempDir автоматически удаляет временные файлы } }