Răsfoiți Sursa

Stworzono testy weryfikacji stanu dla DirConverter

Adam Matuszewski 1 an în urmă
părinte
comite
28ed13e2c1

+ 7 - 0
KonwersjaLiter2/pom.xml

@@ -34,6 +34,13 @@
       <version>RELEASE</version>
       <scope>compile</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <version>4.0.0</version>
+      <scope>test</scope>
+    </dependency>
+
   </dependencies>
   <build>
     <pluginManagement>

+ 40 - 1
KonwersjaLiter2/src/test/java/pl/dmcs/DirConverterTest.java → KonwersjaLiter2/src/test/java/pl/dmcs/DirConverterStateTest.java

@@ -12,7 +12,7 @@ import java.nio.file.Paths;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
-public class DirConverterTest {
+public class DirConverterStateTest {
     private static final String TEST_DIR = "target/test-classes/";
     private static final String FILE_EXTENSION = ".txt";
 
@@ -50,4 +50,43 @@ public class DirConverterTest {
         assertFileContent(Paths.get(TEST_DIR, "file3.txt"), "HELLO WORLD 3");
     }
 
+    @Test
+    void testConvertDirEmptyFile() throws IOException {
+        Path emptyFilePath = Paths.get(TEST_DIR, "empty.txt");
+        Files.write(emptyFilePath, new byte[0]);
+
+        DirConverter converter = new DirConverter();
+        converter.convertDir(TEST_DIR, FILE_EXTENSION);
+
+        assertFileContent(emptyFilePath, "");
+    }
+
+    @Test
+    void testConvertDirNonExistentDirectory() {
+        DirConverter converter = new DirConverter();
+
+        Exception exception = assertThrows(IOException.class, () -> {
+            converter.convertDir("non/existent/directory", FILE_EXTENSION);
+        });
+
+        String expectedMessage = "non/existent/directory".replace("/", File.separator);
+        String actualMessage = exception.getMessage();
+
+        assertEquals(expectedMessage, actualMessage);
+    }
+
+    @Test
+    void testConvertDirNonMatchingExtension() throws IOException {
+        Files.write(Paths.get(TEST_DIR, "file1.md"), "markdown content".getBytes());
+
+        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");
+        assertFileContent(Paths.get(TEST_DIR, "file1.md"), "markdown content");
+
+
+    }
 }