public class LogWriterTest extends TestCase {
public void testCharacter() {
MockLogger logger = new MockLogger();
LogWriter logWriter = new LogWriter(logger);
// ensure logger is empty
logger.getLastMessage();
// empty flush
logWriter.flush();
String msg = logger.getLastMessage();
assertNull(msg);
// write a single character, message only after flush
logWriter.write('a');
assertNull(logger.getLastMessage());
logWriter.flush();
msg = logger.getLastMessage();
assertNotNull(msg);
assertEquals("a", msg);
// write a single CR, no message
logWriter.write('\r');
assertNull(logger.getLastMessage());
// write a single LF, no message
logWriter.write('\n');
assertNull(logger.getLastMessage());
// write three characters (one is CR)
logWriter.write('a');
logWriter.write('\r');
logWriter.write('b');
msg = logger.getLastMessage();
assertNotNull(msg);
assertEquals("a", msg);
logWriter.flush();
msg = logger.getLastMessage();
assertNotNull(msg);
assertEquals("b", msg);
// write three characters (one is LF)
logWriter.write('a');
logWriter.write('\n');
logWriter.write('b');
msg = logger.getLastMessage();
assertNotNull(msg);
assertEquals("a", msg);
logWriter.flush();
msg = logger.getLastMessage();
assertNotNull(msg);
assertEquals("b", msg);
}