@Test public void testPersist() { List<HighScoreEntry> entries = new ArrayList<HighScoreEntry>(); entries.add(new HighScoreEntry(1, "yoda", 402)); entries.add(new HighScoreEntry(2, "luke", 212)); entries.add(new HighScoreEntry(3, "jabba", 123)); File file = filepath.toFile(); assertThat(file).doesNotExist(); persistence.persistHighScores(entries); assertThat(file).exists().isFile(); String expectedContent = "1,yoda,402;" + LINE_SEPARATOR + "2,luke,212;" + LINE_SEPARATOR + "3,jabba,123;" + LINE_SEPARATOR; assertThat(file).hasContent(expectedContent); }
@Test public void testPersistAlreadyExistingFileIsOverwritten() throws IOException { List<String> csvLines = new ArrayList<String>(); csvLines.add("1,yoda,123;"); csvLines.add("2,luke,234;"); Files.write(filepath, csvLines, StandardCharsets.UTF_8); File file = filepath.toFile(); assertThat(file).exists().isFile(); String expectedContentBefore = "1,yoda,123;" + LINE_SEPARATOR + "2,luke,234;" + LINE_SEPARATOR; assertThat(file).hasContent(expectedContentBefore); List<HighScoreEntry> entries = new ArrayList<HighScoreEntry>(); entries.add(new HighScoreEntry(3, "jabba", 402)); persistence.persistHighScores(entries); assertThat(file).exists().isFile(); String expectedContentAfter = "3,jabba,402;" + LINE_SEPARATOR; assertThat(file).hasContent(expectedContentAfter); }