Prechádzať zdrojové kódy

rozoczęto refaktoryzajcę

Adam Matuszewski 1 rok pred
rodič
commit
6ec405804c

+ 1 - 2
KonwersjaLiter2/src/main/java/pl/dmcs/FileConverter.java

@@ -9,7 +9,6 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 
-
 public class FileConverter {
 
     public void convertToUpperCase(String filePath) throws IOException {
@@ -34,4 +33,4 @@ public class FileConverter {
         Files.delete(originalPath);
         Files.move(tempFilePath, originalPath);
     }
-}
+}

+ 0 - 4
KonwersjaLiter2/src/main/resources/test.txt

@@ -1,4 +0,0 @@
-TEST
-PRZYKLADOWY TEKST MALYMI LITERAMI
-12345
-ABCDEF

+ 50 - 50
KonwersjaLiter2/src/test/java/pl/dmcs/FileConverterTest.java

@@ -1,4 +1,5 @@
 package pl.dmcs;
+
 import org.testng.annotations.Test;
 import static org.junit.jupiter.api.Assertions.*;
 import java.io.IOException;
@@ -10,46 +11,49 @@ import java.util.List;
 
 public class FileConverterTest {
 
-    @Test
-    void testConvertToUpperCase() throws IOException {
-        Path filePath = Paths.get("src/main/resources/test.txt");
-        String originalContent = Files.readString(filePath);
-        String expectedContent ="TEST\n" +
-                "PRZYKLADOWY TEKST MALYMI LITERAMI\n" +
-                "12345\n" +
-                "ABCDEF\n";
+    private void createFileWithContent(Path filePath, String content) throws IOException {
+        if (!Files.exists(filePath)) {
+            Files.createFile(filePath);
+        }
+        Files.writeString(filePath, content);
+    }
 
-        FileConverter converter = new FileConverter();
-        converter.convertToUpperCase(filePath.toString());
+    private void deleteFile(Path filePath) throws IOException {
+        Files.deleteIfExists(filePath);
+    }
 
+    private void assertFileContent(Path filePath, String expectedContent) throws IOException {
         String actualContent = Files.readString(filePath);
-
         expectedContent = expectedContent.replace("\r\n", "\n");
         actualContent = actualContent.replace("\r\n", "\n");
-
         assertEquals(expectedContent, actualContent);
-
-        //  Files.writeString(filePath, originalContent);
     }
 
-    // test na pusty plik
     @Test
-    void testConvertEmpty() throws IOException{
-        Path emptyFilePath = Paths.get("src/main/resources/empty.txt");
-        Files.createFile(emptyFilePath); // twrorzy plik
+    void testConvertToUpperCase() throws IOException {
+        Path filePath = Paths.get("src/main/resources/oneFile.txt");
+        createFileWithContent(filePath, "test\nprzykladowy tekst malymi literami\n12345\nabcdef\n");
+        String expectedContent = "TEST\nPRZYKLADOWY TEKST MALYMI LITERAMI\n12345\nABCDEF\n";
 
         FileConverter converter = new FileConverter();
-        converter.convertToUpperCase(emptyFilePath.toString());
+        converter.convertToUpperCase(filePath.toString());
 
-        String actualContent = Files.readString(emptyFilePath);
+        assertFileContent(filePath, expectedContent);
+        deleteFile(filePath);
+    }
 
-        assertEquals("",actualContent);
+    @Test
+    void testConvertEmpty() throws IOException {
+        Path emptyFilePath = Paths.get("src/main/resources/empty.txt");
+        createFileWithContent(emptyFilePath, "");
 
-        Files.deleteIfExists(emptyFilePath); // usuwa plik
+        FileConverter converter = new FileConverter();
+        converter.convertToUpperCase(emptyFilePath.toString());
 
+        assertFileContent(emptyFilePath, "");
+        deleteFile(emptyFilePath);
     }
 
-    // Kilka plików z jednakową zawartością
     @Test
     void testConvertManyFilesToUpperCase() throws IOException {
         List<Path> testFiles = Arrays.asList(
@@ -59,60 +63,56 @@ public class FileConverterTest {
         );
 
         for (Path filePath : testFiles) {
-            Files.writeString(filePath, "To plik testowy"); //Pamietać
+            createFileWithContent(filePath, "To plik testowy");
         }
 
         FileConverter converter = new FileConverter();
 
         for (Path filePath : testFiles) {
             converter.convertToUpperCase(filePath.toString());
-
-            String actualContent = Files.readString(filePath);
-            actualContent = actualContent.replace("\r\n", "\n");
-            String expectedContent = "TO PLIK TESTOWY\n";
-
-            assertEquals(expectedContent, actualContent);
-
-            Files.deleteIfExists(filePath);
+            assertFileContent(filePath, "TO PLIK TESTOWY\n");
+            deleteFile(filePath);
         }
-
     }
 
     @Test
-    void  textConvertDigitsOnly() throws IOException {
+    void testConvertDigitsOnly() throws IOException {
         Path digitsFilePath = Paths.get("src/main/resources/digits.txt");
-        Files.writeString(digitsFilePath, "1234567890\n0987654321\n");
+        createFileWithContent(digitsFilePath, "1234567890\n0987654321\n");
 
         FileConverter converter = new FileConverter();
         converter.convertToUpperCase(digitsFilePath.toString());
 
-        String actualContent = Files.readString(digitsFilePath);
-        actualContent = actualContent.replace("\r\n", "\n");
-        String expectedContent = "1234567890\n0987654321\n";
-
-        assertEquals(expectedContent, actualContent);
-        Files.deleteIfExists(digitsFilePath);
+        assertFileContent(digitsFilePath, "1234567890\n0987654321\n");
+        deleteFile(digitsFilePath);
     }
 
     @Test
-    void  textConvertSpecialCharacters() throws IOException {
+    void testConvertSpecialCharacters() throws IOException {
         Path specialCharactersFilePath = Paths.get("src/main/resources/specialChars.txt");
-        Files.writeString(specialCharactersFilePath, "@#$%^&*()_+[]{}|;:',.<>/?-=~`");
+        createFileWithContent(specialCharactersFilePath, "@#$%^&*()_+[]{}|;:',.<>/?\n-=~`\n");
 
         FileConverter converter = new FileConverter();
         converter.convertToUpperCase(specialCharactersFilePath.toString());
 
-        String actualContent = Files.readString(specialCharactersFilePath);
-        actualContent = actualContent.replace("\r\n", "\n");
-        String expectedContent = "@#$%^&*()_+[]{}|;:',.<>/?-=~`\n";
-
-        assertEquals(expectedContent, actualContent);
-        Files.deleteIfExists(specialCharactersFilePath);
+        assertFileContent(specialCharactersFilePath, "@#$%^&*()_+[]{}|;:',.<>/?\n-=~`\n");
+        deleteFile(specialCharactersFilePath);
     }
 
+    @Test
+    void testConvertNonExistentFile() {
+        Path nonExistentFilePath = Paths.get("src/main/resources/nonExistentFile.txt");
 
+        FileConverter converter = new FileConverter();
 
+        Exception exception = assertThrows(IOException.class, () -> {
+            converter.convertToUpperCase(nonExistentFilePath.toString());
+        });
 
+        String expectedMessage = "nonExistentFile.txt";
+        String actualMessage = exception.getMessage();
 
+        assertTrue(actualMessage.contains(expectedMessage));
+    }
 
-}
+}