Skip to content
Snippets Groups Projects
Commit 43606f6e authored by Trym Hamer Gudvangen's avatar Trym Hamer Gudvangen
Browse files

test: add null value tests for StoryFileHandler

parent f11c0709
Branches
Tags
3 merge requests!11merge master into part-three,!10Merge feat/filehandling into feat/part-two,!9Feat/part two
Pipeline #215052 passed
...@@ -47,6 +47,7 @@ public class StoryFileHandler { ...@@ -47,6 +47,7 @@ public class StoryFileHandler {
*/ */
public void createStoryFile(Story story, String fileName) throws IOException { public void createStoryFile(Story story, String fileName) throws IOException {
Objects.requireNonNull(story, "Story cannot be null"); Objects.requireNonNull(story, "Story cannot be null");
Objects.requireNonNull(fileName, "File name cannot be null");
File file = FileHandler.createFile(fileName); File file = FileHandler.createFile(fileName);
if(FileHandler.fileExists(file)) throw new IllegalArgumentException("You cannot overwrite a pre-existing story file"); if(FileHandler.fileExists(file)) throw new IllegalArgumentException("You cannot overwrite a pre-existing story file");
try(BufferedWriter storyBufferedWriter = new BufferedWriter(new FileWriter(file))){ try(BufferedWriter storyBufferedWriter = new BufferedWriter(new FileWriter(file))){
...@@ -61,6 +62,7 @@ public class StoryFileHandler { ...@@ -61,6 +62,7 @@ public class StoryFileHandler {
* @throws IOException This exception is thrown if an I/O error occurs with the reader. * @throws IOException This exception is thrown if an I/O error occurs with the reader.
*/ */
public Story readStoryFromFile(String fileName) throws IOException, InstantiationException { public Story readStoryFromFile(String fileName) throws IOException, InstantiationException {
Objects.requireNonNull(fileName, "File name cannot be null");
File file = new File(FileHandler.getFileSourcePath(fileName)); File file = new File(FileHandler.getFileSourcePath(fileName));
if(!FileHandler.fileExists(file)) throw new IllegalArgumentException("There is no story file with that name!"); if(!FileHandler.fileExists(file)) throw new IllegalArgumentException("There is no story file with that name!");
Story story; Story story;
......
...@@ -211,6 +211,22 @@ class StoryFileHandlerTest { ...@@ -211,6 +211,22 @@ class StoryFileHandlerTest {
}); });
} }
@Test
void a_null_file_name_when_creating_new_file_will_throw_NullPointerException(){
Story story = validStory();
Assertions.assertThrows(NullPointerException.class, () ->{
storyFileHandler.createStoryFile(story, null);
});
}
@Test
void a_null_file_name_when_reading_file_will_throw_NullPointerException(){
Assertions.assertThrows(NullPointerException.class, () ->{
Story story = storyFileHandler.readStoryFromFile(null);
});
}
//TODO: change this actually test the link information //TODO: change this actually test the link information
@Test @Test
void corrupt_link_information_throws_CorruptLinkException_when_read(){ void corrupt_link_information_throws_CorruptLinkException_when_read(){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment