PrintEventWriter extends Writer { @Override public void flush() throws IOException { final String text = buffer.toString(); if(text.length() == 0) { return; } final PrintEvent event = new PrintEvent(text); buffer.setLength(0); events.fireEvent(event); } PrintEventWriter(SoarEventManager events); @Override void close(); @Override void flush(); @Override void write(char[] cbuf, int off, int len);  }
@Test public void testPrintEventIsFiredOnFlush() { final AtomicReference<String> text = new AtomicReference<String>(); final SoarEventListener listener = new SoarEventListener() { @Override public void onEvent(SoarEvent event) { text.set(((PrintEvent) event).getText()); } }; events.addListener(PrintEvent.class, listener); printer.print("abcdefghijk"); assertNull(text.get()); printer.flush(); assertEquals("abcdefghijk", text.get()); }
@Test public void testDoesNotFirePrintEventIfNothingHasBeenPrinted() { final AtomicReference<String> text = new AtomicReference<String>(); final SoarEventListener listener = new SoarEventListener() { @Override public void onEvent(SoarEvent event) { text.set(((PrintEvent) event).getText()); } }; events.addListener(PrintEvent.class, listener); assertNull(text.get()); printer.flush(); assertNull(text.get()); }