Skip to content

Commit

Permalink
End of episode 171
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesshore committed Mar 8, 2012
1 parent 284b2df commit f4beb19
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion scratchpad.txt
@@ -1,4 +1,4 @@
Move value types to their own package
Move value types to their own package - including SelfRenderable

Move save file data clump into a "configuration" object?

Expand Down
14 changes: 9 additions & 5 deletions src/com/jamesshore/finances/persistence/SaveFile.java
Expand Up @@ -14,15 +14,19 @@ public SaveFile(File path) {
public void save(UserEnteredDollars startingBalance, UserEnteredDollars costBasis, UserEnteredDollars yearlySpending) throws IOException {
Writer writer = new BufferedWriter(new FileWriter(path));
try {
writer.write("com.jamesshore.finances,1\n");
writer.write(startingBalance.getUserText() + "\n");
writer.write(costBasis.getUserText() + "\n");
writer.write(yearlySpending.getUserText() + "\n");
writeLine(writer, "com.jamesshore.finances,1");
writeLine(writer, startingBalance.getUserText());
writeLine(writer, costBasis.getUserText());
writeLine(writer, yearlySpending.getUserText());
}
finally {
writer.close();
}

}

public void writeLine(Writer writer, String line) throws IOException {
line = line.replace("\\", "\\\\");
line = line.replace("\n", "\\n");
writer.write(line + "\n");
}
}
25 changes: 15 additions & 10 deletions src/com/jamesshore/finances/persistence/_SaveFileTest.java
@@ -1,7 +1,6 @@
package com.jamesshore.finances.persistence;

import static org.junit.Assert.*;
import com.jamesshore.finances.util.*;
import java.io.*;
import org.junit.*;
import org.junit.rules.*;
Expand Down Expand Up @@ -43,24 +42,30 @@ public void saveOverwritesAnExistingFile() throws IOException {
@Test
public void saveWritesFileContents() throws IOException {
saveFile.save(new UserEnteredDollars("1.23"), new UserEnteredDollars("10.24"), new UserEnteredDollars("100.25"));

String expected = "com.jamesshore.finances,1\n1.23\n10.24\n100.25\n";
assertEquals(expected, readFile());
assertFileMatches("file", "1.23", "10.24", "100.25");
}

@Test
public void saveWritesOutUserEnteredValuesExactlyAsEntered() throws IOException {
saveFile.save(new UserEnteredDollars("foo"), new UserEnteredDollars(" bar"), new UserEnteredDollars("baz\t"));
assertFileMatches("file", "foo", " bar", "baz\t");
}

String expected = "com.jamesshore.finances,1\nfoo\n bar\nbaz\t\n";
assertEquals(expected, readFile());
@Test
public void saveHandlesDelimitersInUserInput() throws IOException {
saveFile.save(new UserEnteredDollars("\n\n\n \\n"), anyValue, anyValue);
assertFileMatches("file", "\\n\\n\\n \\\\n", "any", "any");
}

@Test(expected = RequireException.class);
public void lineFeedInUserDataIsAProgrammingError() throws IOException {
saveFile.save(new UserEnteredDollars("\n"), anyValue, anyValue);
private void assertFileMatches(String message, String expectedStartingBalance, String expectedCostBasis, String expectedYearlySpending) throws IOException {
String expected = "com.jamesshore.finances,1\n";
expected += expectedStartingBalance + "\n";
expected += expectedCostBasis + "\n";
expected += expectedYearlySpending + "\n";
assertEquals(message, expected, readFile());

}

private String readFile() throws IOException {
BufferedReader input = new BufferedReader(new FileReader(path));
try {
Expand Down

0 comments on commit f4beb19

Please sign in to comment.