JTextReporter { public static Connection showDialog(String title) { return showDialog(title, null); }  static Connection showDialog(String title); static Connection showDialog(String title, String initialText); static Connection showDialog(String title, 
            String initialText,
            int flags); static Connection showDialog(final String title, 
            final String initialText, 
            final int flags,
            final int textAreaRows, 
            final int textAreaCols); static final int FLAG_MODAL; static final int FLAG_ALWAYS_ON_TOP; static final int FLAG_RESIZABLE; static final int DEFAULT_FLAGS; static final int DEFAULT_TEXTAREA_ROWS; static final int DEFAULT_TEXTAREA_COLS; static String NEWLINE; static final char DEFAULT_SEPARATOR_CHAR; }
@Test public void showDefaultDialog() throws Exception { showDialog(TITLE); assertEquals(TITLE, windowFixture.component().getTitle()); JTextComponentFixture textBox = windowFixture.textBox(); assertTrue(textBox.component() instanceof JTextArea); JTextArea textArea = (JTextArea) textBox.component(); assertEquals(JTextReporter.DEFAULT_TEXTAREA_ROWS, textArea.getRows()); assertEquals(JTextReporter.DEFAULT_TEXTAREA_COLS, textArea.getColumns()); getButton("Copy to clipboard"); getButton("Save..."); getButton("Clear"); getButton("Close"); DialogFixture df = (DialogFixture) windowFixture; boolean expectModal = (JTextReporter.DEFAULT_FLAGS & JTextReporter.FLAG_MODAL) > 0; assertEquals(expectModal, df.component().isModal()); boolean expectResizable = (JTextReporter.DEFAULT_FLAGS & JTextReporter.FLAG_RESIZABLE) > 0; assertEquals(expectResizable, df.component().isResizable()); boolean expectAlwaysOnTop = (JTextReporter.DEFAULT_FLAGS & JTextReporter.FLAG_ALWAYS_ON_TOP) > 0; assertEquals(expectAlwaysOnTop, df.component().isAlwaysOnTop()); }
@Test public void nullTitleIsOK() throws Exception { showDialog(null); }
@Test public void nullInitialTextIsOK() throws Exception { showDialog(TITLE, null); }
@Test public void initialText() throws Exception { showDialog(TITLE, TEXT[0]); windowFixture.textBox().requireText(TEXT[0]); }
@Test public void connectionObjectIsReturned() throws Exception { Connection conn = null; try { conn = showDialog(TITLE).get(DISPLAY_TIMEOUT, TimeUnit.MILLISECONDS); } catch (TimeoutException ex) { fail("connection object not returned"); } assertNotNull(conn); }
@Test public void appendText() throws Exception { Connection conn = showDialog(TITLE).get(); conn.append(TEXT[0]); windowFixture.robot.waitForIdle(); windowFixture.textBox().requireText(TEXT[0]); }
@Test public void appendTextWithFormattedNewlines() throws Exception { final String text = getConcatenatedText(String.format("%n")); Connection conn = showDialog(TITLE).get(); conn.append(text); windowFixture.robot.waitForIdle(); String displayedText = windowFixture.textBox().text(); assertEquals(text, displayedText); }
@Test public void appendTextWithEmbeddedNewlines() throws Exception { final String text = getConcatenatedText("\n"); Connection conn = showDialog(TITLE).get(); conn.append(text); windowFixture.robot.waitForIdle(); String displayedText = windowFixture.textBox().text(); String regex = "(\\n|\\r)+"; assertEquals(text.replaceAll(regex, "|"), displayedText.replaceAll(regex, "|")); }
@Test public void appendNewline() throws Exception { Connection conn = showDialog(TITLE, TEXT[0]).get(); conn.appendNewline(); conn.appendNewline(); conn.append(TEXT[1]); conn.appendNewline(); windowFixture.robot.waitForIdle(); String displayedText = windowFixture.textBox().text(); String expectedText = String.format("%s%n%n%s%n", TEXT[0], TEXT[1]); assertEquals(expectedText, displayedText); }
@Test public void appendDefaultSeparatorLine() throws Exception { Connection conn = showDialog(TITLE, TEXT[0] + "\n").get(); final int N = 10; conn.appendSeparatorLine(N); char[] chars = new char[N]; Arrays.fill(chars, JTextReporter.DEFAULT_SEPARATOR_CHAR); String expected = String.format("%s%n%s%n", TEXT[0], String.valueOf(chars)); String displayedText = windowFixture.textBox().text(); assertEquals(expected, displayedText); }
@Test public void appendCustomSeparatorLine() throws Exception { Connection conn = showDialog(TITLE, TEXT[0] + "\n").get(); final int N = 10; final char c = '#'; conn.appendSeparatorLine(N, c); char[] chars = new char[N]; Arrays.fill(chars, c); String expected = String.format("%s%n%s%n", TEXT[0], String.valueOf(chars)); String displayedText = windowFixture.textBox().text(); assertEquals(expected, displayedText); }
@Test public void appendMethodsCanBeChained() throws Exception { Connection conn = showDialog(TITLE).get(); conn.append(TEXT[0]).appendNewline().append(TEXT[1]); windowFixture.robot.waitForIdle(); String actual = windowFixture.textBox().text(); String expected = String.format("%s%n%s", TEXT[0], TEXT[1]); assertEquals(expected, actual); }
@Test public void appendAfterDialogDismissedCausesLoggerMessage() throws Exception { Connection conn = showDialog(TITLE).get(); windowFixture.close(); windowFixture.robot.waitForIdle(); captureLogger(); conn.append("This should not work"); assertLogMessage(Level.SEVERE.toString()); assertLogMessage("appending text to an expired JTextReporter connection"); releaseLogger(); }
@Test public void getTextFromConnection() throws Exception { final String text = getConcatenatedText(String.format("%n")); Connection conn = showDialog(TITLE, text).get(); assertEquals(text, conn.getText()); }
@Test public void getTextWhenNoneIsDisplayedReturnsEmptyString() throws Exception { Connection conn = showDialog(TITLE).get(); String text = conn.getText(); assertNotNull(text); assertTrue(text.length() == 0); }
@Test public void listenerInformedWhenTextIsUpdated() throws Exception { Connection conn = showDialog(TITLE).get(); final CountDownLatch latch = new CountDownLatch(1); conn.addListener(new TextReporterListener() { @Override public void onReporterClosed() {} @Override public void onReporterUpdated() { latch.countDown(); } }); conn.append(TEXT[0]); assertTrue( latch.await(LISTENER_TIMEOUT, TimeUnit.MILLISECONDS) ); }
@Test public void listenerInformedWhenDialogIsClosedViaSystemButton() throws Exception { Connection conn = showDialog(TITLE).get(); final CountDownLatch latch = new CountDownLatch(1); conn.addListener(new TextReporterListener() { @Override public void onReporterClosed() { latch.countDown(); } @Override public void onReporterUpdated() {} }); windowFixture.close(); assertTrue( latch.await(LISTENER_TIMEOUT, TimeUnit.MILLISECONDS) ); }
@Test public void listenerInformedWhenDialogIsClosedViaCloseButton() throws Exception { Connection conn = showDialog(TITLE).get(); final CountDownLatch latch = new CountDownLatch(1); conn.addListener(new TextReporterListener() { @Override public void onReporterClosed() { latch.countDown(); } @Override public void onReporterUpdated() {} }); getButton("Close").click(); assertTrue( latch.await(LISTENER_TIMEOUT, TimeUnit.MILLISECONDS) ); }
@Ignore("have to work out how to do this one") @Test public void saveTextToFile() throws Exception { showDialog(TITLE, TEXT[0]).get(); getButton("Save...").click(); }
@Test public void clearText() throws Exception { showDialog(TITLE, TEXT[0]).get(); getButton("Clear").click(); windowFixture.robot.waitForIdle(); assertEquals(0, windowFixture.textBox().component().getDocument().getLength()); }
@Test public void copyTextToClipboard() throws Exception { final String text = getConcatenatedText(String.format("%n")); showDialog(TITLE, text).get(); getButton("Copy to clipboard").click(); windowFixture.robot.waitForIdle(); Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); String clipText = (String) clip.getData(DataFlavor.stringFlavor); assertEquals(text, clipText); }
@Test public void closeDialogViaConnection() throws Exception { Connection conn = showDialog(TITLE).get(); conn.closeDialog(); windowFixture.robot.waitForIdle(); windowFixture.requireNotVisible(); assertFalse(conn.isOpen()); }
@Test public void callingCloseDialogTwiceRaisesLogMessage() throws Exception { Connection conn = showDialog(TITLE).get(); conn.closeDialog(); windowFixture.robot.waitForIdle(); captureLogger(); conn.closeDialog(); assertLogMessage("INFO"); assertLogMessage("connection has expired"); releaseLogger(); }
@Test public void callingCloseDialogAfterGUICloseRaisesLogMessage() throws Exception { Connection conn = showDialog(TITLE).get(); getButton("Close").click(); windowFixture.robot.waitForIdle(); captureLogger(); conn.closeDialog(); assertLogMessage("INFO"); assertLogMessage("connection has expired"); releaseLogger(); }