Fix test and wait

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
2018-10-14 18:14:51 -04:00
parent 2a05366e31
commit 4301f2a15e
12 changed files with 213 additions and 154 deletions

View File

@@ -154,7 +154,8 @@ public class CommandParametersTest {
}
/** Test method for
* {@link net.bigeon.gclc.command.CommandParameters#getAdditionals()}. */
* {@link net.bigeon.gclc.command.CommandParameters#getAdditionals()}.
* @throws CommandParsingException if the parameter parsing failed*/
@Test
public final void testGetAdditionals() throws CommandParsingException {
final Set<String> strings = new HashSet<>();
@@ -187,7 +188,7 @@ public class CommandParametersTest {
}
/** Test method for
* {@link net.bigeon.gclc.command.CommandParameters#getBool(java.lang.String)}.
* {@link net.bigeon.gclc.command.CommandParameters#isActive(String)}.
*
* @throws CommandParsingException if a command parsing failed */
@Test
@@ -200,12 +201,12 @@ public class CommandParametersTest {
CommandParameters parameters = new CommandParameters(bools, strings, true);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
assertFalse(parameters.isActive("boolFlag"));
parameters.parseArgs("-boolFlag");
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.isActive("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
try {
parameters.parseArgs("-ungiven");
@@ -213,29 +214,29 @@ public class CommandParametersTest {
} catch (final CommandParsingException e) {
// ok
}
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
assertTrue(parameters.isActive("boolFlag"));
parameters = new CommandParameters(bools, strings, false);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
assertFalse(parameters.isActive("boolFlag"));
try {
parameters.parseArgs("-boolFlag");
} catch (final CommandParsingException e) {
// ok
}
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.isActive("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
try {
parameters.parseArgs("-ungiven");
} catch (final CommandParsingException e) {
// ok
}
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
assertTrue(parameters.isActive("boolFlag"));
}
/** Test method for
@@ -281,29 +282,29 @@ public class CommandParametersTest {
CommandParameters parameters = new CommandParameters(bools, strings, true);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
assertFalse(parameters.isActive("boolFlag"));
parameters.set("boolFlag", true);
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.isActive("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
parameters.set("ungiven", true);
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
assertTrue(parameters.isActive("boolFlag"));
parameters = new CommandParameters(bools, strings, false);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
assertFalse(parameters.isActive("boolFlag"));
parameters.set("boolFlag", true);
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.isActive("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
parameters.set("ungiven", true);
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.isActive("ungiven"));
assertTrue(parameters.isActive("boolFlag"));
}
/** Test method for

View File

@@ -0,0 +1,29 @@
package net.bigeon.gclc.command;
import static org.junit.Assert.*;
import org.junit.Test;
import net.bigeon.gclc.exception.InvalidParameterException;
public class ParametrizedCommandDataTest {
@Test
public void testAddParameter() throws InvalidParameterException {
ParametrizedCommandData data = new ParametrizedCommandData();
data.addBooleanParameter("flag");
data.addStringParameter("arg", false);
try {
data.addBooleanParameter("arg");
fail("String parameter cannot be converted to flag");
} catch (InvalidParameterException e) {
// ok
}
try {
data.addStringParameter("flag", false);
fail("Flag parameter cannot be converted to string");
} catch (InvalidParameterException e) {
// ok
}
}
}

View File

@@ -241,22 +241,22 @@ public class ParametrizedCommandTest {
case 3:
assertNull(parameters.get(str1));
assertNull(parameters.get(str2));
assertFalse(parameters.getBool(bool1));
assertFalse(parameters.getBool(bool2));
assertFalse(parameters.isActive(bool1));
assertFalse(parameters.isActive(bool2));
call++;
break;
case 4:
assertEquals(str2, parameters.get(str1));
assertNull(parameters.get(str2));
assertFalse(parameters.getBool(bool1));
assertFalse(parameters.getBool(bool2));
assertFalse(parameters.isActive(bool1));
assertFalse(parameters.isActive(bool2));
call++;
break;
case 5:
assertEquals(str2, parameters.get(str1));
assertNull(parameters.get(str2));
assertTrue(parameters.getBool(bool1));
assertFalse(parameters.getBool(bool2));
assertTrue(parameters.isActive(bool1));
assertFalse(parameters.isActive(bool2));
call = 0;
break;
default:
@@ -304,22 +304,22 @@ public class ParametrizedCommandTest {
case 0:
assertNull(parameters.get(str1));
assertNull(parameters.get(str2));
assertFalse(parameters.getBool(bool1));
assertFalse(parameters.getBool(bool2));
assertFalse(parameters.isActive(bool1));
assertFalse(parameters.isActive(bool2));
call++;
break;
case 1:
assertEquals(str2, parameters.get(str1));
assertNull(parameters.get(str2));
assertFalse(parameters.getBool(bool1));
assertFalse(parameters.getBool(bool2));
assertFalse(parameters.isActive(bool1));
assertFalse(parameters.isActive(bool2));
call++;
break;
case 2:
assertEquals(str2, parameters.get(str1));
assertNull(parameters.get(str2));
assertTrue(parameters.getBool(bool1));
assertFalse(parameters.getBool(bool2));
assertTrue(parameters.isActive(bool1));
assertFalse(parameters.isActive(bool2));
call = 0;
break;
default:

View File

@@ -118,7 +118,7 @@ public class ReadingRunnableTest {
}
/** Test method for
* {@link net.bigeon.gclc.utils.ReadingRunnable#getWaitForDelivery(java.lang.String)}.
* {@link net.bigeon.gclc.utils.ReadingRunnable#waitForDelivery(String)}.
*
* @throws InterruptedException if the test failed
* @throws IOException if the test failed */
@@ -131,7 +131,19 @@ public class ReadingRunnableTest {
final ReadingRunnable runnable = new ReadingRunnable(reader);
final Thread th0 = new Thread(runnable, "read");
th0.start();
final Thread th = runnable.getWaitForDelivery("msg");
Thread th = new Thread(new Runnable() {
@Override
public void run() {
try {
runnable.waitForDelivery("msg");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
th.start();
out.write(Charset.forName("UTF-8").encode("msg" + System.lineSeparator())
.array());

View File

@@ -0,0 +1,67 @@
package net.bigeon.gclc.utils;
import static org.junit.Assert.*;
import java.io.IOException;
import org.junit.Test;
import net.bigeon.gclc.manager.ConsoleInput;
import net.bigeon.gclc.tools.ConstantString;
import net.bigeon.gclc.utils.EmptyInput;
public class EmptyInputTest {
@Test
public void testClose() throws IOException {
ConsoleInput input = EmptyInput.INSTANCE;
// several close operation shold create no error
input.close();
input.close();
assertFalse("This source input should never be closed", input.isClosed());
}
@Test
public void testGetPrompt() {
ConsoleInput input = EmptyInput.INSTANCE;
String init = input.getPrompt().apply();
input.setPrompt("some prompt different from "+init);
assertEquals("Prompts should not be changed", init, input.getPrompt().apply());
input.setPrompt(new ConstantString("some other prompt different from "+init));
assertEquals("Prompts should not be changed", init, input.getPrompt().apply());
}
@Test
public void testInterruptPrompt() {
// Nothing to test, really...
ConsoleInput input = EmptyInput.INSTANCE;
// several close operation shold create no error
input.interruptPrompt();
assertFalse("This source input should never be closed", input.isClosed());
}
@Test
public void testIsClosed() throws IOException {
ConsoleInput input = EmptyInput.INSTANCE;
// several close operation shold create no error
assertFalse("This source input should never be closed", input.isClosed());
input.close();
assertFalse("This source input should never be closed", input.isClosed());
input.close();
assertFalse("This source input should never be closed", input.isClosed());
}
@Test
public void testPrompt() throws IOException {
ConsoleInput input = EmptyInput.INSTANCE;
// several close operation shold create no error
assertEquals("Any prompt should return the empty value", "", input.prompt());
assertEquals("Any prompt should return the empty value", "", input.prompt(0));
assertEquals("Any prompt should return the empty value", "", input.prompt(12));
assertEquals("Any prompt should return the empty value", "", input.prompt("Test"));
assertEquals("Any prompt should return the empty value", "", input.prompt("Test", 0));
assertEquals("Any prompt should return the empty value", "", input.prompt("Test", 12));
}
}

View File

@@ -0,0 +1,31 @@
package net.bigeon.gclc.utils;
import static org.junit.Assert.assertFalse;
import java.io.IOException;
import org.junit.Test;
import net.bigeon.gclc.manager.ConsoleOutput;
public class SinkOutputTest {
@Test
public void testClose() throws Exception {
ConsoleOutput output = SinkOutput.INSTANCE;
// several close operation shold create no error
output.close();
output.close();
assertFalse("This source input should never be closed", output.isClosed());
}
@Test
public void testPrint() throws IOException {
ConsoleOutput output = SinkOutput.INSTANCE;
// several close operation shold create no error
output.print("Anything");
output.println("Anything");
output.println();
assertFalse("This source input should never be closed", output.isClosed());
}
}