diff --git a/gclc-swt/src/main/java/net/bigeon/gclc/swt/ConsoleOutputManager.java b/gclc-swt/src/main/java/net/bigeon/gclc/swt/ConsoleOutputManager.java index bb95fb2..7126c82 100644 --- a/gclc-swt/src/main/java/net/bigeon/gclc/swt/ConsoleOutputManager.java +++ b/gclc-swt/src/main/java/net/bigeon/gclc/swt/ConsoleOutputManager.java @@ -35,7 +35,8 @@ public final class ConsoleOutputManager implements ConsoleOutputDisplay { * @see java.lang.Runnable#run() */ @Override public void run() { - if (!text.getText().isEmpty()) { + final String initialText = text.getText(); + if (initialText != null && !initialText.isEmpty()) { text.append(System.lineSeparator()); } text.append(next); diff --git a/gclc-swt/src/main/java/net/bigeon/gclc/swt/SWTConsoleShell.java b/gclc-swt/src/main/java/net/bigeon/gclc/swt/SWTConsole.java similarity index 90% rename from gclc-swt/src/main/java/net/bigeon/gclc/swt/SWTConsoleShell.java rename to gclc-swt/src/main/java/net/bigeon/gclc/swt/SWTConsole.java index 5481e1f..b23beba 100644 --- a/gclc-swt/src/main/java/net/bigeon/gclc/swt/SWTConsoleShell.java +++ b/gclc-swt/src/main/java/net/bigeon/gclc/swt/SWTConsole.java @@ -73,9 +73,8 @@ import java.io.BufferedReader; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; -import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import net.bigeon.gclc.utils.PipedConsoleInput; @@ -85,7 +84,7 @@ import net.bigeon.gclc.utils.PipedConsoleOutput; *

* * @author Emmanuel Bigeon */ -public final class SWTConsoleShell extends Shell { +public final class SWTConsole extends Composite { private ConsoleInputManager inputManager; private ConsoleOutputManager outputManager; @@ -93,19 +92,14 @@ public final class SWTConsoleShell extends Shell { /** Create the shell. * - * @param display the display + * @param parent the containing composite * @param style the shell style */ - public SWTConsoleShell(final Display display, final int style) { - super(display, style); + public SWTConsole(final Composite parent, final int style) { + super(parent, style); setLayout(new GridLayout(2, false)); createContents(); } - @Override - protected void checkSubclass() { - // Disable the check that prevents subclassing of SWT components - } - /** Create contents of the shell. */ private void createContents() { final Text output = new Text(this, @@ -119,7 +113,6 @@ public final class SWTConsoleShell extends Shell { inputManager = new ConsoleInputManager(input); outputManager = new ConsoleOutputManager(output); promptManager = new ConsolePromptManager(prompt); - setText("Console Application"); //$NON-NLS-1$ } /** Connect the console parts to the shell. diff --git a/gclc-swt/src/test/java/net/bigeon/gclc/swt/ConsoleOutputManagerTest.java b/gclc-swt/src/test/java/net/bigeon/gclc/swt/ConsoleOutputManagerTest.java index 26cdad3..0b5d8b1 100644 --- a/gclc-swt/src/test/java/net/bigeon/gclc/swt/ConsoleOutputManagerTest.java +++ b/gclc-swt/src/test/java/net/bigeon/gclc/swt/ConsoleOutputManagerTest.java @@ -21,8 +21,8 @@ import net.bigeon.gclc.utils.PipedConsoleOutput; /** @author Emmanuel Bigeon */ public class ConsoleOutputManagerTest { - private final Text text = mock(Text.class); - private final ConsoleOutputManager com = new ConsoleOutputManager(text); + private final Text text = mock(Text.class); + private final ConsoleOutputManager com = new ConsoleOutputManager(text); private final Display display = mock(Display.class); { when(text.getDisplay()).thenReturn(display); @@ -40,11 +40,16 @@ public class ConsoleOutputManagerTest { } /** Test method for - * {@link net.bigeon.gclc.swt.ConsoleOutputManager#appendConsoleOutput(java.lang.String)}. */ + * {@link net.bigeon.gclc.swt.ConsoleOutputManager#appendLine(java.lang.String)}. */ @Test public void testAppendConsoleOutput() { - com.appendConsoleOutput("abc"); + when(text.getText()).thenReturn("", "abc", + "abc" + System.lineSeparator() + "def"); + com.appendLine("abc"); verify(text).append("abc"); + com.appendLine("def"); + verify(text).append(System.lineSeparator()); + verify(text).append("def"); } @Test @@ -60,7 +65,7 @@ public class ConsoleOutputManagerTest { forwardThread.join(500); com.setManager(null); forwardThread.join(); - verify(text).append(System.lineSeparator() + "line"); + verify(text).append("line"); } } } diff --git a/gclc-swt/src/test/java/net/bigeon/gclc/swt/PromptReadingRunnableTest.java b/gclc-swt/src/test/java/net/bigeon/gclc/swt/PromptReadingRunnableTest.java new file mode 100644 index 0000000..d82300e --- /dev/null +++ b/gclc-swt/src/test/java/net/bigeon/gclc/swt/PromptReadingRunnableTest.java @@ -0,0 +1,71 @@ +/** + * + */ +package net.bigeon.gclc.swt; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +/** @author Emmanuel Bigeon */ +public class PromptReadingRunnableTest { + private final Label view = mock(Label.class); + private final Display display = mock(Display.class); + { + when(view.getDisplay()).thenReturn(display); + doAnswer(new Answer() { + /* (non-Javadoc) + * @see + * org.mockito.stubbing.Answer#answer(org.mockito.invocation.InvocationOnMock) */ + @Override + public Object answer(final InvocationOnMock invocation) throws Throwable { + final Runnable runnable = invocation.getArgument(0); + runnable.run(); + return null; + } + }).when(display).syncExec(any(Runnable.class)); + } + + /** Test method for {@link net.bigeon.gclc.swt.PromptReadingRunnable#run()}. + * + * @throws IOException if an error occurred */ + @Test + public void testRun() throws IOException { + final BufferedReader reader = mock(BufferedReader.class); + when(reader.readLine()).thenReturn("abc", "def", null); + + final PromptReadingRunnable runnable = new PromptReadingRunnable(reader, view); + + runnable.run(); + } + + /** Test method for {@link net.bigeon.gclc.swt.PromptReadingRunnable#run()}. + * + * @throws IOException if an error occurred */ + @Test + public void testRunWithIO() throws IOException { + final BufferedReader reader = mock(BufferedReader.class); + when(reader.readLine()).thenThrow(new IOException()); + + final PromptReadingRunnable runnable = new PromptReadingRunnable(reader, view); + final Logger logger = Logger.getLogger(PromptReadingRunnable.class.getName()); + final Level back = logger.getLevel(); + logger.setLevel(Level.OFF); + + runnable.run(); + logger.setLevel(back); + } + +} diff --git a/gclc-swt/src/test/java/net/bigeon/gclc/swt/SWTConsoleShellTest.java b/gclc-swt/src/test/java/net/bigeon/gclc/swt/SWTConsoleShellTest.java new file mode 100644 index 0000000..1d8d818 --- /dev/null +++ b/gclc-swt/src/test/java/net/bigeon/gclc/swt/SWTConsoleShellTest.java @@ -0,0 +1,38 @@ +/** + * + */ +package net.bigeon.gclc.swt; + +import static org.junit.Assert.assertNotNull; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Shell; +import org.junit.Test; + +/** @author Emmanuel Bigeon */ +public class SWTConsoleShellTest { + + /** Test method for + * {@link net.bigeon.gclc.swt.SWTConsole#SWTConsole(Composite, int)}. */ + @Test + public void testSWTConsoleShell() { + final SWTConsole console = new SWTConsole(new Shell(), SWT.NONE); + assertNotNull("View should be initialized with managers", + console.getInputManager()); + assertNotNull("View should be initialized with managers", + console.getOutputManager()); + assertNotNull("View should be initialized with managers", + console.getPromptManager()); + } + + /** Test method for + * {@link net.bigeon.gclc.swt.SWTConsole#connect(net.bigeon.gclc.utils.PipedConsoleInput, net.bigeon.gclc.utils.PipedConsoleOutput, java.io.BufferedReader)}. */ + @Test + public void testConnect() { + final SWTConsole console = new SWTConsole(new Shell(), SWT.NONE); + // Disconnection should work. + console.connect(null, null, null); + } + +} diff --git a/gclc-swt/src/test/java/net/bigeon/gclc/swt/SWTConsoleViewTest.java b/gclc-swt/src/test/java/net/bigeon/gclc/swt/SWTConsoleViewTest.java new file mode 100644 index 0000000..350e076 --- /dev/null +++ b/gclc-swt/src/test/java/net/bigeon/gclc/swt/SWTConsoleViewTest.java @@ -0,0 +1,41 @@ +/** + * + */ +package net.bigeon.gclc.swt; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Shell; +import org.junit.Test; + +/** @author Emmanuel Bigeon */ +public class SWTConsoleViewTest { + + /** Test method for {@link net.bigeon.gclc.swt.SWTConsoleView#setFocus()}. */ + @Test + public void testSetFocus() { + final SWTConsoleView view = new SWTConsoleView(new Shell(), SWT.NONE); + assertFalse("Non visible component set focus should fail", view.setFocus()); + } + + /** Test method for + * {@link net.bigeon.gclc.swt.SWTConsoleView#SWTConsoleView(org.eclipse.swt.widgets.Composite, int)}. */ + @Test + public void testSWTConsoleView() { + final SWTConsoleView view = new SWTConsoleView(new Shell(), SWT.NONE); + assertNotNull("View should be initialized with managers", view.getInputManager()); + assertNotNull("View should be initialized with managers", + view.getOutputManager()); + } + + /** Test method for + * {@link net.bigeon.gclc.swt.SWTConsoleView#setManager(net.bigeon.gclc.utils.PipedConsoleOutput, net.bigeon.gclc.utils.PipedConsoleInput)}. */ + @Test + public void testSetManager() { + final SWTConsoleView view = new SWTConsoleView(new Shell(), SWT.NONE); + // Disconnection should work. + view.setManager(null, null); + } +}