package sis.report;
import static org.junit.Assert.*;
import java.io.*;
import org.junit.Before;
import org.junit.Test;
import sis.report.RosterReporter;
import sis.studentinfo.*;
import sis.studentinfo.util.DateUtil;
/**
* @author Carl Adler(C.A.)
* */
public class RosterReportTest {
private Session session;
@Before
public void setUp() {
session = CourseSession.create(
new Course("ENGL", "101"),
DateUtil.createDate(2013, 1, 6));
session.enroll(new Student("A"));
session.enroll(new Student("B"));
}
@Test
public void testRosterReport() throws IOException {
Writer writer = new StringWriter();
new RosterReporter(session).writeReport(writer);
assertReportContents(writer.toString());
}
@Test
public void testFileReport() throws IOException {
final String filename = "testFiledReport.txt";
try {
deleteFile(filename);
new RosterReporter(session).writeReport(filename);
StringBuffer buffer = new StringBuffer();
String line;
BufferedReader reader = new BufferedReader(new FileReader(filename));
while((line = reader.readLine()) != null)
buffer.append(String.format(line + "%n"));
reader.close();
assertReportContents(buffer.toString());
} finally {
deleteFile(filename);
}
}
private void assertReportContents(String rosterReport) {
assertEquals(
String.format(RosterReporter.ROSTER_REPORT_HEADER +
"A%n" +
"B%n" +
RosterReporter.ROSTER_REPORT_FOOTER, 2),
rosterReport);
}
private void deleteFile(String filename) {
File file = new File(filename);
if(file.exists())
assertTrue("unable to delete " + filename, file.delete());
}
}