CP - Code - Proposed Solution (Java - The Tests)
Anyone who has discussed code or software business with me, it is painfully apparent (so I'm told) that I am a testing freak. I am a big believer in Test Driven Design (TDD). It should come as no surprise that I employed that technique on this project.
I used JUnit here only because it was the obvious candidate. I have not found a "good" Specification language to wrap JUnit in, but there are several options. Since I was not comfortable publishing the "Spec" versions of my tests, I show only the raw JUnit tests.
Of note is my use of Hamcrest for comparison tests.
I have deliberately left of a discussion of Continuous Integration (CI), but that would be the natural progeny of TDD. I would employ Jenkins as a CI server for this project. I would defer topics of code coverage to a discussion of Jenkins.
Finally, only Unit tests are shown here. Since the Statement of Problem omitted any mention of the implementation details, it would be presumptuous to show Functional or Integration testing here. If this project were going further, those tests would be included.
Reader Unit Tests
The reader tests are relatively straight forward. Shown here:
package com.happytrout.commit.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.happytrout.commit.main.Reader;
import com.happytrout.commit.main.Writer;
public class ReaderTest {
@Test(expected = RuntimeException.class)
public final void whenNullStringIsPassedInThenExceptionIsThrown() {
Reader r = new Reader("");
}
@Test(expected = RuntimeException.class)
public final void whenNullStringIsPassedForCIDThenExceptionIsThrown() {
Reader r = new Reader("deleteme");
r.setCid("");
}
@Test(expected = RuntimeException.class)
public final void whenCIDIsNotSetReadingCausesException() {
Reader r = new Reader("deleteme");
r.fetch();
}
@Test
public final void whenCIDAndMatchesLineIsReturnedFromFormatter() {
String fname = "deleteme";
// Prep a file
String source = "Now is the time for all good men\\n"
+ " to come to the aid of their country\\n"
+ " and pay their due taxes.";
char buffer[] = new char[source.length()];
source.getChars(0, source.length(), buffer, 0);
FileWriter f1;
try {
f1 = new FileWriter(fname);
f1.write(buffer);
f1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// File should be prepped here
String sample = "A:344:Hello World\r\n";
Reader r = new Reader(fname, "A");
assertEquals("Formatter did not recognize correctly formatted line", sample, r.parseLine(sample));
}
@Test
public final void whenCIDDoesNotMatchNullIsReturnedFromParsing() throws IOException {
String fname = "deleteme";
String sample = "A:344:Hello World\r\n";
Reader r = new Reader(fname, "B");
assertNull("Formatter did not recognize correctly formatted line", r.parseLine(sample));
}
@Test(expected = RuntimeException.class)
public final void whenLineIsMalformedWithoutDelimitersParserThrowsError() throws IOException {
String fname = "deleteme";
String sample = "A344Hello World\r\n";
Reader r = new Reader(fname, "A");
assertNull("Formatter did not recognize malformed line", r.parseLine(sample));
}
@Test(expected = RuntimeException.class)
public final void whenLineIsMalformedWithOnlyOneDelimiterParserThrowsError() throws IOException {
String fname = "deleteme";
String sample = "A344:Hello World\r\n";
Reader r = new Reader(fname, "A");
assertNull("Formatter did not recognize malformed line", r.parseLine(sample));
}
@Test
public final void whenFileWithProperFormatIsParsedReaderGivesProperOutput() {
String fname = "deleteme";
// Prep a file
String source = "A:12345:First Entry\r\n"
+ "A:12346:Second Entry\r\n"
+ "A:12347:Third Entry\r\n";
char buffer[] = new char[source.length()];
source.getChars(0, source.length(), buffer, 0);
FileWriter f1;
try {
f1 = new FileWriter(fname);
f1.write(buffer);
f1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// File should be prepped here
Reader r = new Reader(fname, "A");
r.fetch();
assertThat("Did not retrieve correct delimiter data from file",
outContent.toString(), CoreMatchers.containsString(":12346:"));
assertThat("Did not retrieve correct message data from file",
outContent.toString(), CoreMatchers.containsString(":Third Entry"));
}
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
@After
public void cleanUpStreams() {
System.setOut(null);
System.setErr(null);
}
}Writer Unit Tests
The writer tests are slightly more involved, but nothing out of the ordinary.
package com.happytrout.commit.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import com.happytrout.commit.main.Writer;
public class WriterTest {
@Test(expected = RuntimeException.class)
public final void whenNullStringIsPassedInThenExceptionIsThrown() {
Writer w = new Writer("deleteme");
w.commit("");
}
@Test(expected = RuntimeException.class)
public final void whenNullStringIsPassedForCIDThenExceptionIsThrown() {
Writer w = new Writer("deleteme");
w.setCid("");
}
@Test(expected = RuntimeException.class)
public final void whenCIDIsNotSetWritingCausesException() {
Writer w = new Writer("deleteme");
w.commit("test message");
}
@Test(expected = RuntimeException.class)
public final void whenCIDIsNullThenFormattingCausesException() {
Writer w = new Writer("deleteme");
w.formatLine("booger");
}
@Test
public final void whenCIDIsSetThenMessageIsFormattedProperly() {
Writer w = new Writer("deleteme");
String cid = "a";
String msg = "big row of chickens";
w.setCid(cid);
String fmt_msg = w.formatLine(msg);
assertThat(fmt_msg, CoreMatchers.containsString(cid+":"));
assertThat(fmt_msg, CoreMatchers.containsString(":"+msg));
assertEquals("string [["+fmt_msg+"]] is the wrong length", cid.length() + msg.length() + 2 + 18, fmt_msg.length());
}
@Test
public final void whenMessageIsCommittedTheFileGetsBigger() throws IOException {
Writer w = new Writer("deleteme");
File fh = new File("deleteme");
FileWriter fileWriter = new FileWriter(fh);
assertTrue("Writer failed to create log file", fh.exists());
assertEquals("Log file not empty at beginning", 0, fh.length());
String cid = "b";
String msg = "quick brown fox on a bench";
w.setCid(cid);
w.commit(msg);
File fh1 = new File("deleteme");
assertEquals("Message was not written to log file", msg.length() + 23, fh1.length());
}
@Test
public final void whenTwoMessagesAreCommittedFileGetsBigger() throws IOException {
Writer w = new Writer("deleteme");
File fh = new File("deleteme");
FileWriter fileWriter = new FileWriter(fh);
assertEquals("Log file not empty at beginning", 0, fh.length());
String cid = "c";
String msg_0 = "it was a dark and stormy night";
String msg_1 = "and the rain was lightly falling";
w.setCid(cid);
w.commit(msg_0);
w.commit(msg_1);
File fh1 = new File("deleteme");
assertEquals("Messages were not written to log file", msg_0.length() + msg_1.length() + 46, fh1.length());
}
}