Browse Source

Stworzono pierwszy test dla prawidłowych plików i prawidłowego folderu

Adam Matuszewski 1 năm trước cách đây
mục cha
commit
6802de247e

+ 14 - 9
KonwersjaLiter2/src/main/java/pl/dmcs/DirConverter.java

@@ -1,5 +1,6 @@
 package pl.dmcs;
-import java.io.File;
+
+import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -7,19 +8,23 @@ import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
-import java.io.IOException;
-
 public class DirConverter {
+    private final FileConverter fileConverter;
+
+    public DirConverter() {
+        this.fileConverter = new FileConverter();
+    }
+
     public void convertDir(String directoryPath, String fileExtension) throws IOException {
         try (Stream<Path> filePathStream = Files.walk(Paths.get(directoryPath))) {
-            List<Path> files = filePathStream.filter(Files::isRegularFile).filter(path -> path.getFileName().toString().endsWith(fileExtension)).collect(Collectors.toList());
+            List<Path> files = filePathStream
+                    .filter(Files::isRegularFile)
+                    .filter(path -> path.getFileName().toString().endsWith(fileExtension))
+                    .collect(Collectors.toList());
 
             for (Path file : files) {
-                List<String> lines = Files.readAllLines(file);
-                List<String> upperCasedLines = lines.stream().map(String::toUpperCase).collect(Collectors.toList());
-                Files.write(file, upperCasedLines);
-                }
+                fileConverter.convertToUpperCase(file.toString());
             }
         }
     }
-
+}

+ 30 - 3
KonwersjaLiter2/src/test/java/pl/dmcs/DirConverterTest.java

@@ -1,26 +1,53 @@
 package pl.dmcs;
 
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 
 import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.Paths;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
 public class DirConverterTest {
     private static final String TEST_DIR = "target/test-classes/";
-
     private static final String FILE_EXTENSION = ".txt";
 
     @BeforeEach
     void setUp() throws IOException {
-        Files.createDirectory(Paths.get(TEST_DIR));
+        Files.createDirectories(Paths.get(TEST_DIR));
+        Files.walk(Paths.get(TEST_DIR))
+                .filter(Files::isRegularFile)
+                .forEach(file -> {
+                    try {
+                        Files.delete(file);
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                });
 
         Files.write(Paths.get(TEST_DIR, "file1.txt"), "hello world".getBytes());
         Files.write(Paths.get(TEST_DIR, "file2.txt"), "hello world 2".getBytes());
         Files.write(Paths.get(TEST_DIR, "file3.txt"), "hello world 3".getBytes());
     }
 
+    private void assertFileContent(Path filePath, String expectedContent) throws IOException {
+        String actualContent = Files.readString(filePath).replace("\r\n", "\n").trim();
+        expectedContent = expectedContent.replace("\r\n", "\n").trim();
+        assertEquals(expectedContent, actualContent);
+    }
 
-}
+    @Test
+    void testConvertDir() throws IOException {
+        DirConverter converter = new DirConverter();
+        converter.convertDir(TEST_DIR, FILE_EXTENSION);
 
+        assertFileContent(Paths.get(TEST_DIR, "file1.txt"), "HELLO WORLD");
+        assertFileContent(Paths.get(TEST_DIR, "file2.txt"), "HELLO WORLD 2");
+        assertFileContent(Paths.get(TEST_DIR, "file3.txt"), "HELLO WORLD 3");
+    }
+
+}