[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() {
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;
import static org.mockito.ArgumentMatchers.any;
/*-
* #%L
* GCLC swt
@ -36,48 +37,128 @@ package net.bigeon.gclc.swt;
* knowledge of the CeCILL license and that you accept its terms.
* #L%
*/
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;
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.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import net.bigeon.gclc.swt.io.ConsolePromptManager;
/**
* @author Emmanuel Bigeon
*
*/
/** @author Emmanuel Bigeon */
public class ConsolePromptManagerTest {
private final Label label = mock(Label.class);
private final ConsolePromptManager manager = new ConsolePromptManager(label);
/**
* Test method for {@link net.bigeon.gclc.swt.io.ConsolePromptManager#setPrompt(java.lang.String)}.
*/
/** Test method for
* {@link net.bigeon.gclc.swt.io.ConsolePromptManager#setPrompt(java.lang.String)}. */
@Test
public void testSetPrompt() {
manager.setPrompt("abc");
verify(label).setText("abc");
}
/**
* Test method for {@link net.bigeon.gclc.swt.io.ConsolePromptManager#setStream(java.io.BufferedReader)}.
*/
/** Test method for
* {@link net.bigeon.gclc.swt.io.ConsolePromptManager#setStream(java.io.BufferedReader)}.
*
* @throws IOException if error */
@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);
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);
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);
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);
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(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.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.events.KeyEvent;
@ -145,11 +149,12 @@ public class HistoryTextKeyListenerTest {
}
@Test
public void testKeyPressed() {
public void testKeyPressed() throws IOException {
final ConsoleDelayIO io = mock(ConsoleDelayIO.class);
final KeyEvent event = mock(KeyEvent.class);
event.keyCode = 'a';
final HistoryTextKeyListener listener = new HistoryTextKeyListener(io);
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.assertNull;
import org.eclipse.swt.SWT;
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.PipedConsoleOutput;
import net.bigeon.gclc.swt.io.ConsoleOutputManager;
/** @author Emmanuel Bigeon */
public class SWTConsoleShellTest {
@ -70,6 +72,8 @@ public class SWTConsoleShellTest {
final SWTConsole console = new SWTConsole(new Shell(), SWT.NONE);
// Disconnection should work.
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.assertNotNull;
import static org.junit.Assert.assertNull;
import org.eclipse.swt.SWT;
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.PipedConsoleOutput;
import net.bigeon.gclc.swt.io.ConsoleOutputManager;
/** @author Emmanuel Bigeon */
public class SWTConsoleViewTest {
@ -74,5 +76,7 @@ public class SWTConsoleViewTest {
final SWTConsoleView view = new SWTConsoleView(new Shell(), SWT.NONE);
// Disconnection should work.
view.setManager(null, null);
assertNull("Disconnection should replace the input.",
((ConsoleOutputManager) view.getOutputManager()).getManager());
}
}