Move test thread in test runnable

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2018-10-27 09:40:56 -04:00
parent 579e85dcb3
commit cf29eb37cc
2 changed files with 39 additions and 31 deletions

View File

@ -93,6 +93,8 @@ import net.bigeon.gclc.manager.ConsoleInput;
import net.bigeon.gclc.manager.ConsoleOutput; import net.bigeon.gclc.manager.ConsoleOutput;
import net.bigeon.gclc.utils.PipedConsoleInput; import net.bigeon.gclc.utils.PipedConsoleInput;
import net.bigeon.gclc.utils.PipedConsoleOutput; import net.bigeon.gclc.utils.PipedConsoleOutput;
import net.bigeon.test.junitmt.ATestRunnable;
import net.bigeon.test.junitmt.ThreadTest;
/** /**
* <p> * <p>
@ -509,10 +511,10 @@ public class ParametrizedCommandTest {
cmd.execute(out, in, "-" + str1, str2, "-" + addParam); cmd.execute(out, in, "-" + str1, str2, "-" + addParam);
cmd.execute(out, in, "-" + str1, str2, "-" + addParam, addParam); cmd.execute(out, in, "-" + str1, str2, "-" + addParam, addParam);
final Thread th = new Thread(new Runnable() { final ATestRunnable testConsole = new ATestRunnable() {
@Override @Override
public void run() { protected void testRun() {
try { try {
assertEquals("value of " + str1 + "? ", buf.readLine()); assertEquals("value of " + str1 + "? ", buf.readLine());
in.type(""); in.type("");
@ -523,15 +525,17 @@ public class ParametrizedCommandTest {
buf.readLine()); buf.readLine());
in.type(str2); in.type(str2);
} catch (final IOException e) { } catch (final IOException e) {
assertNull(e); fail("IO exception in test");
} }
} }
}); };
final Thread th = new Thread(testConsole);
th.start(); th.start();
cmd.execute(out, in); cmd.execute(out, in);
th.join(); ThreadTest.assertRuns(th, testConsole);
} }
try (PipedConsoleOutput out = new PipedConsoleOutput(); try (PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream(); PipedOutputStream pout = new PipedOutputStream();
@ -540,23 +544,25 @@ public class ParametrizedCommandTest {
new InputStreamReader(pis, StandardCharsets.UTF_8)); new InputStreamReader(pis, StandardCharsets.UTF_8));
PipedConsoleInput in = new PipedConsoleInput(new PrintStream(pout))) { PipedConsoleInput in = new PipedConsoleInput(new PrintStream(pout))) {
final Thread th = new Thread(new Runnable() { final ATestRunnable testConsole = new ATestRunnable() {
@Override @Override
public void run() { protected void testRun() {
try { try {
assertEquals("value of " + str1 + "? ", buf.readLine()); assertEquals("value of " + str1 + "? ", buf.readLine());
in.type(str2); in.type(str2);
} catch (final IOException e) { } catch (final IOException e) {
assertNull(e); fail("IO exception in test");
} }
} }
}); };
final Thread th = new Thread(testConsole);
th.start(); th.start();
cmd.execute(out, in, "-" + addParam); cmd.execute(out, in, "-" + addParam);
th.join(); ThreadTest.assertRuns(th, testConsole);
} }
try { try {
final PipedConsoleOutput out = new PipedConsoleOutput(); final PipedConsoleOutput out = new PipedConsoleOutput();

View File

@ -94,6 +94,8 @@ import org.junit.Test;
import net.bigeon.gclc.utils.PipedConsoleInput; import net.bigeon.gclc.utils.PipedConsoleInput;
import net.bigeon.gclc.utils.PipedConsoleOutput; import net.bigeon.gclc.utils.PipedConsoleOutput;
import net.bigeon.test.junitmt.ATestRunnable;
import net.bigeon.test.junitmt.ThreadTest;
/** /**
* <p> * <p>
@ -115,19 +117,21 @@ public class CLIPrompterTest {
} }
/** Test method for /** Test method for
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptBoolean(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String)}. */ * {@link net.bigeon.gclc.prompt.CLIPrompter#promptBoolean(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String)}.
*
* @throws IOException if an io occurs */
@Test @Test
public final void testPromptBoolean() { public final void testPromptBoolean() throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream(); PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout); PipedInputStream pis = new PipedInputStream(pout);
BufferedReader buf = new BufferedReader( BufferedReader buf = new BufferedReader(
new InputStreamReader(pis, StandardCharsets.UTF_8)); new InputStreamReader(pis, StandardCharsets.UTF_8));
PipedConsoleInput in = new PipedConsoleInput(new PrintStream(pout))) { PipedConsoleInput in = new PipedConsoleInput(new PrintStream(pout))) {
final Thread th = new Thread(new Runnable() { final ATestRunnable target = new ATestRunnable() {
@Override @Override
public void run() { public void testRun() {
try { try {
assertTrue(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$ assertTrue(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
assertTrue(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$ assertTrue(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
@ -135,10 +139,10 @@ public class CLIPrompterTest {
assertFalse(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$ assertFalse(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
} }
} }
}); };
final Thread th = new Thread(target);
th.start(); th.start();
assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
in.type(""); //$NON-NLS-1$ in.type(""); //$NON-NLS-1$
@ -151,10 +155,7 @@ public class CLIPrompterTest {
in.type("N"); //$NON-NLS-1$ in.type("N"); //$NON-NLS-1$
assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
in.type("nO"); //$NON-NLS-1$ in.type("nO"); //$NON-NLS-1$
th.join(); ThreadTest.assertRuns(th, target);
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
} }
} }
@ -1080,10 +1081,10 @@ public class CLIPrompterTest {
choices.put(keys.get(1), "The actual other"); //$NON-NLS-1$ choices.put(keys.get(1), "The actual other"); //$NON-NLS-1$
final String message = "My message"; //$NON-NLS-1$ final String message = "My message"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() { final ATestRunnable target = new ATestRunnable() {
@Override @Override
public void run() { public void testRun() {
try { try {
assertTrue("Asserted provided value to be retrieved", CLIPrompter assertTrue("Asserted provided value to be retrieved", CLIPrompter
.promptMultiChoice(out, in, keys, choices, message) .promptMultiChoice(out, in, keys, choices, message)
@ -1102,7 +1103,8 @@ public class CLIPrompterTest {
e.printStackTrace(); e.printStackTrace();
} }
} }
}); };
final Thread th = new Thread(target);
th.start(); th.start();
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
final String readNextLine = out.readNextLine(); final String readNextLine = out.readNextLine();
@ -1135,8 +1137,8 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
in.type("0 1"); //$NON-NLS-1$ in.type("0 1"); //$NON-NLS-1$
th.join(); ThreadTest.assertRuns(th, target);
} catch (IOException | InterruptedException e) { } catch (final IOException e) {
fail("Unexpected excpetion, the other thread probably failed"); //$NON-NLS-1$ fail("Unexpected excpetion, the other thread probably failed"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
} }
@ -1152,28 +1154,28 @@ public class CLIPrompterTest {
new InputStreamReader(pis, StandardCharsets.UTF_8)); new InputStreamReader(pis, StandardCharsets.UTF_8));
PipedConsoleInput in = new PipedConsoleInput(new PrintStream(pout))) { PipedConsoleInput in = new PipedConsoleInput(new PrintStream(pout))) {
final String res = "some content"; //$NON-NLS-1$ final String res = "some content"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() { final ATestRunnable target = new ATestRunnable() {
@Override @Override
public void run() { public void testRun() {
try { try {
assertEquals("Expected provided message to be returned", res, assertEquals("Expected provided message to be returned", res,
CLIPrompter.promptNonEmpty(in, "My message", //$NON-NLS-1$ CLIPrompter.promptNonEmpty(in, "My message", //$NON-NLS-1$
"my reprompt")); //$NON-NLS-1$ "my reprompt")); //$NON-NLS-1$
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
} }
} }
}); };
final Thread th = new Thread(target);
th.start(); th.start();
assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
in.type(""); //$NON-NLS-1$ in.type(""); //$NON-NLS-1$
assertTrue(buf.readLine().startsWith("my reprompt")); //$NON-NLS-1$ assertTrue(buf.readLine().startsWith("my reprompt")); //$NON-NLS-1$
in.type(res); in.type(res);
th.join(); ThreadTest.assertRuns(th, target);
} catch (IOException | InterruptedException e) { } catch (final IOException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$ fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
} }