Skip to content

Commit

Permalink
Fix #653
Browse files Browse the repository at this point in the history
Added more Unit tests to FileUtils class
Improved code coverage from 70% to 95%
  • Loading branch information
Sweetdevil144 committed Oct 11, 2023
1 parent 0a56e8f commit 76ce81c
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/test/java/com/crowdin/cli/utils/file/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

public class FileUtilsTest extends WorkWithProjectTestPart {

Expand Down Expand Up @@ -46,4 +43,30 @@ public void testWriteToFile_2() throws IOException {
String result = org.apache.commons.io.FileUtils.readFileToString(file, "UTF-8");
assertEquals(text, result);
}
@Test
public void testReadYamlFile_UnexpectedException() {
File yamlFile = new File(tempProject.getBasePath() + "invalid.yaml");
try (FileOutputStream fos = new FileOutputStream(yamlFile)) {
fos.write("invalid_yaml_content: @#!@".getBytes());
} catch (IOException e) {
fail("Failed to setup test: " + e.getMessage());
}

assertThrows(RuntimeException.class, () -> FileUtils.readYamlFile(yamlFile));
}

@Test
public void testReadYamlFile_NullFile() {
assertThrows(NullPointerException.class, () -> FileUtils.readYamlFile(null));
}

@Test
public void testReadYamlFile_NonYamlExtension() {
File nonYamlFile = new File(tempProject.getBasePath() + "somefile.txt");
try {
FileUtils.readYamlFile(nonYamlFile);
} catch (RuntimeException e) {
assertFalse(e.getMessage().equals("error.configuration_file_not_exist"));
}
}
}

0 comments on commit 76ce81c

Please sign in to comment.