[test] Complete tests.

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2021-11-10 11:14:08 +01:00
parent 625cacb198
commit 8025122906
5 changed files with 118 additions and 16 deletions

View File

@ -96,4 +96,12 @@ public final class ConsoleOutputManager implements ConsoleOutputDisplay {
public Thread getForwardThread() { public Thread getForwardThread() {
return forwardThread; return forwardThread;
} }
/** @return the output consumer */
public PipedConsoleOutput getManager() {
if (forward == null) {
return null;
}
return forward.getOuput();
}
} }

View File

@ -3,6 +3,7 @@
*/ */
package net.bigeon.gclc.swt; package net.bigeon.gclc.swt;
import static org.mockito.ArgumentMatchers.any;
/*- /*-
* #%L * #%L
* GCLC swt * GCLC swt
@ -36,48 +37,128 @@ package net.bigeon.gclc.swt;
* knowledge of the CeCILL license and that you accept its terms. * knowledge of the CeCILL license and that you accept its terms.
* #L% * #L%
*/ */
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Label;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import net.bigeon.gclc.swt.io.ConsolePromptManager; import net.bigeon.gclc.swt.io.ConsolePromptManager;
/** /** @author Emmanuel Bigeon */
* @author Emmanuel Bigeon
*
*/
public class ConsolePromptManagerTest { public class ConsolePromptManagerTest {
private final Label label = mock(Label.class); private final Label label = mock(Label.class);
private final ConsolePromptManager manager = new ConsolePromptManager(label); private final ConsolePromptManager manager = new ConsolePromptManager(label);
/** /** Test method for
* Test method for {@link net.bigeon.gclc.swt.io.ConsolePromptManager#setPrompt(java.lang.String)}. * {@link net.bigeon.gclc.swt.io.ConsolePromptManager#setPrompt(java.lang.String)}. */
*/
@Test @Test
public void testSetPrompt() { public void testSetPrompt() {
manager.setPrompt("abc"); manager.setPrompt("abc");
verify(label).setText("abc"); verify(label).setText("abc");
} }
/** /** Test method for
* Test method for {@link net.bigeon.gclc.swt.io.ConsolePromptManager#setStream(java.io.BufferedReader)}. * {@link net.bigeon.gclc.swt.io.ConsolePromptManager#setStream(java.io.BufferedReader)}.
*/ *
* @throws IOException if error */
@Test @Test
public void testSetStream() { public void testSetStream() throws IOException {
// Create the dispaly, in case...
final Display d = mock(Display.class);
when(label.getDisplay()).thenReturn(d);
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(d).asyncExec(any(Runnable.class));
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(d).syncExec(any(Runnable.class));
// Test.
BufferedReader output = mock(BufferedReader.class); BufferedReader output = mock(BufferedReader.class);
final Object lock = new Object();
final AtomicInteger calls = new AtomicInteger(0);
Mockito.when(output.readLine()).then(new Answer<String>() {
String[] ans = { "A line" };
@Override
public String answer(final InvocationOnMock invocation) throws Throwable {
synchronized (lock) {
if (calls.get() >= ans.length) {
calls.incrementAndGet();
lock.notify();
return null;
}
lock.notify();
return ans[calls.getAndIncrement()];
}
}
});
manager.setStream(output); manager.setStream(output);
manager.setStream(output); manager.setStream(output);
synchronized (lock) {
while (calls.get() < 2) {
try {
lock.wait(10);
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
verify(output, times(2)).readLine();
output = mock(BufferedReader.class); output = mock(BufferedReader.class);
calls.set(0);
Mockito.when(output.readLine()).then(new Answer<String>() {
String[] ans = { "A line" };
@Override
public String answer(final InvocationOnMock invocation) throws Throwable {
synchronized (lock) {
if (calls.get() >= ans.length) {
calls.incrementAndGet();
lock.notify();
return null;
}
lock.notify();
return ans[calls.getAndIncrement()];
}
}
});
manager.setStream(output); manager.setStream(output);
while (calls.get() < 2) {
synchronized (lock) {
try {
lock.wait(10);
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
manager.setStream(null); manager.setStream(null);
manager.setStream(output); manager.setStream(output);
while (calls.get() < 3) {
synchronized (lock) {
try {
lock.wait(10);
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
verify(output, times(3)).readLine();
verify(label, times(2)).setText("A line");
} }
} }

View File

@ -70,6 +70,10 @@ package net.bigeon.gclc.swt;
*/ */
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.io.IOException;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.KeyEvent;
@ -145,11 +149,12 @@ public class HistoryTextKeyListenerTest {
} }
@Test @Test
public void testKeyPressed() { public void testKeyPressed() throws IOException {
final ConsoleDelayIO io = mock(ConsoleDelayIO.class); final ConsoleDelayIO io = mock(ConsoleDelayIO.class);
final KeyEvent event = mock(KeyEvent.class); final KeyEvent event = mock(KeyEvent.class);
event.keyCode = 'a'; event.keyCode = 'a';
final HistoryTextKeyListener listener = new HistoryTextKeyListener(io); final HistoryTextKeyListener listener = new HistoryTextKeyListener(io);
listener.keyPressed(event); listener.keyPressed(event);
verify(io, times(1)).validateInput();
} }
} }

View File

@ -38,6 +38,7 @@ package net.bigeon.gclc.swt;
*/ */
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
@ -46,6 +47,7 @@ import org.junit.Test;
import net.bigeon.gclc.manager.PipedConsoleInput; import net.bigeon.gclc.manager.PipedConsoleInput;
import net.bigeon.gclc.manager.PipedConsoleOutput; import net.bigeon.gclc.manager.PipedConsoleOutput;
import net.bigeon.gclc.swt.io.ConsoleOutputManager;
/** @author Emmanuel Bigeon */ /** @author Emmanuel Bigeon */
public class SWTConsoleShellTest { public class SWTConsoleShellTest {
@ -70,6 +72,8 @@ public class SWTConsoleShellTest {
final SWTConsole console = new SWTConsole(new Shell(), SWT.NONE); final SWTConsole console = new SWTConsole(new Shell(), SWT.NONE);
// Disconnection should work. // Disconnection should work.
console.connect(null, null, null); console.connect(null, null, null);
assertNull("Console should disconnect",
((ConsoleOutputManager) console.getOutputManager()).getManager());
} }
} }

View File

@ -39,6 +39,7 @@ package net.bigeon.gclc.swt;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Shell;
@ -46,6 +47,7 @@ import org.junit.Test;
import net.bigeon.gclc.manager.PipedConsoleInput; import net.bigeon.gclc.manager.PipedConsoleInput;
import net.bigeon.gclc.manager.PipedConsoleOutput; import net.bigeon.gclc.manager.PipedConsoleOutput;
import net.bigeon.gclc.swt.io.ConsoleOutputManager;
/** @author Emmanuel Bigeon */ /** @author Emmanuel Bigeon */
public class SWTConsoleViewTest { public class SWTConsoleViewTest {
@ -74,5 +76,7 @@ public class SWTConsoleViewTest {
final SWTConsoleView view = new SWTConsoleView(new Shell(), SWT.NONE); final SWTConsoleView view = new SWTConsoleView(new Shell(), SWT.NONE);
// Disconnection should work. // Disconnection should work.
view.setManager(null, null); view.setManager(null, null);
assertNull("Disconnection should replace the input.",
((ConsoleOutputManager) view.getOutputManager()).getManager());
} }
} }