Skip to content

Commit

Permalink
Added m5_04_testValidateCommandArgumentsExistence, m5_05_testValidate…
Browse files Browse the repository at this point in the history
…CommandArgumentsCorrectness
  • Loading branch information
hhimanshu committed May 23, 2020
1 parent ce44c9b commit baa05be
Showing 1 changed file with 68 additions and 2 deletions.
70 changes: 68 additions & 2 deletions src/test/java/com/h2/Module05_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -127,7 +128,72 @@ public void m5_03_testCommandsToUsage() throws IllegalAccessException {
}

@Test
public void m5_04_testExecuteCommandExistence() {
public void m5_04_testValidateCommandArgumentsExistence() {
final Optional<Class<?>> maybeClass = getFinanceClass();
assertTrue(maybeClass.isPresent(), classToFind + " should be present");
assertEquals(classToFind, maybeClass.get().getCanonicalName());

final Class<?> aClass = maybeClass.get();
final String methodName = "validateCommandArguments";

try {
Method method = aClass.getDeclaredMethod(methodName, String[].class);
assertTrue(isPrivate(method), methodName + " must be declared 'private'");
assertTrue(isStatic(method), methodName + " must be declared 'static'");
assertEquals(boolean.class, method.getReturnType(), methodName + " must have a 'boolean' return type");

} catch (NoSuchMethodException e) {
fail("Can't find a method with name " + methodName + " in class " + classToFind + " with 1 parameter of type 'String[]'");
}
}

@Test
public void m5_05_testValidateCommandArgumentsCorrectness() throws InvocationTargetException, IllegalAccessException {
final Optional<Class<?>> maybeClass = getFinanceClass();
assertTrue(maybeClass.isPresent(), classToFind + " should be present");
assertEquals(classToFind, maybeClass.get().getCanonicalName());

final Class<?> aClass = maybeClass.get();
final String methodName = "validateCommandArguments";

try {
Method aMethod = aClass.getDeclaredMethod(methodName, String[].class);
aMethod.setAccessible(true);
{
boolean actual = (boolean) aMethod.invoke(null, (Object) new String[]{"bestLoanRates"});
assertTrue(actual, methodName + " should have returned 'true' for bestLoanRates with no additional arguments");
}
{
boolean actual = (boolean) aMethod.invoke(null, (Object) new String[]{"bestLoanRates", "1"});
assertFalse(actual, methodName + " should have returned 'false' for bestLoanRates with 1 additional argument");
}
{
boolean actual = (boolean) aMethod.invoke(null, (Object) new String[]{"savingsCalculator", "1"});
assertFalse(actual, methodName + " should have returned 'false' for savingsCalculator with 1 additional argument");
}
{
boolean actual = (boolean) aMethod.invoke(null, (Object) new String[]{"savingsCalculator", "1", "2"});
assertTrue(actual, methodName + " should have returned 'true' for savingsCalculator with 2 additional arguments");
}
{
boolean actual = (boolean) aMethod.invoke(null, (Object) new String[]{"mortgageCalculator", "1", "2"});
assertFalse(actual, methodName + " should have returned 'false' for mortgageCalculator with 2 additional arguments");
}
{
boolean actual = (boolean) aMethod.invoke(null, (Object) new String[]{"mortgageCalculator", "1", "2", "3"});
assertTrue(actual, methodName + " should have returned 'true' for mortgageCalculator with 3 additional arguments");
}
{
boolean actual = (boolean) aMethod.invoke(null, (Object) new String[]{"heightCalculator"});
assertFalse(actual, methodName + " should have returned 'false' for unknown app name 'heightCalculator'");
}
} catch (NoSuchMethodException e) {
fail("Can't find a method with name " + methodName + " in class " + classToFind + " with 1 parameter of type 'String[]'");
}
}

@Test
public void m5_06_testExecuteCommandExistence() {
final Optional<Class<?>> maybeClass = getFinanceClass();
assertTrue(maybeClass.isPresent(), classToFind + " should be present");
assertEquals(classToFind, maybeClass.get().getCanonicalName());
Expand All @@ -147,7 +213,7 @@ public void m5_04_testExecuteCommandExistence() {
}

@Test
public void m5_05_testExecuteCommandExistenceForCorrectness() {
public void m5_07_testExecuteCommandExistenceForCorrectness() {
final String methodName = "executeCommand";

final Optional<Class<?>> maybeClass = getFinanceClass();
Expand Down

0 comments on commit baa05be

Please sign in to comment.