@ -16,14 +16,17 @@ package org.hyperledger.besu.cli;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.hyperledger.besu.cli.util.CommandLineUtils.DEPENDENCY_WARNING_MSG ;
import static org.hyperledger.besu.cli.util.CommandLineUtils.DEPENDENCY_WARNING_MSG ;
import static org.hyperledger.besu.cli.util.CommandLineUtils.MULTI_DEPENDENCY_WARNING_MSG ;
import static org.mockito.Mockito.verify ;
import static org.mockito.Mockito.verify ;
import static org.mockito.Mockito.verifyNoMoreInteractions ;
import static org.mockito.Mockito.verifyNoMoreInteractions ;
import static picocli.CommandLine.defaultExceptionHandler ;
import static picocli.CommandLine.defaultExceptionHandler ;
import org.hyperledger.besu.cli.util.CommandLineUtils ;
import org.hyperledger.besu.cli.util.CommandLineUtils ;
import org.hyperledger.besu.util.StringUtils ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.List ;
import org.apache.logging.log4j.Logger ;
import org.apache.logging.log4j.Logger ;
import org.junit.Test ;
import org.junit.Test ;
@ -62,6 +65,11 @@ public class CommandLineUtilsTest {
arity = "1" )
arity = "1" )
final Boolean optionEnabled = true ;
final Boolean optionEnabled = true ;
@Option (
names = { "--other-option-enabled" } ,
arity = "1" )
final Boolean otherOptionEnabled = true ;
@Option ( names = { "--option2" } )
@Option ( names = { "--option2" } )
final Integer option2 = 2 ;
final Integer option2 = 2 ;
@ -104,6 +112,22 @@ public class CommandLineUtilsTest {
}
}
}
}
private static class TestMultiCommandWithDeps extends AbstractTestCommand {
TestMultiCommandWithDeps ( final Logger logger ) {
super ( logger ) ;
}
@Override
public void run ( ) {
CommandLineUtils . checkMultiOptionDependencies (
logger ,
commandLine ,
List . of ( "--option-enabled" , "--other-option-enabled" ) ,
List . of ( ! optionEnabled , ! otherOptionEnabled ) ,
Arrays . asList ( "--option2" , "--option3" ) ) ;
}
}
@Test
@Test
public void optionsAreNotExpected ( ) {
public void optionsAreNotExpected ( ) {
final AbstractTestCommand testCommand = new TestCommandWithDeps ( mockLogger ) ;
final AbstractTestCommand testCommand = new TestCommandWithDeps ( mockLogger ) ;
@ -186,6 +210,26 @@ public class CommandLineUtilsTest {
assertThat ( testCommand . option4 ) . isEqualTo ( 40 ) ;
assertThat ( testCommand . option4 ) . isEqualTo ( 40 ) ;
}
}
@Test
public void multipleMainOptions ( ) {
final AbstractTestCommand testCommand = new TestMultiCommandWithDeps ( mockLogger ) ;
testCommand . commandLine . parseWithHandlers (
new RunLast ( ) ,
defaultExceptionHandler ( ) ,
"--option-enabled" ,
"false" ,
"--other-option-enabled" ,
"false" ,
"--option2" ,
"20" ) ;
verifyMultiOptionsConstraintLoggerCall (
mockLogger , "--option2" , "--option-enabled" , "--other-option-enabled" ) ;
assertThat ( testCommand . optionEnabled ) . isFalse ( ) ;
assertThat ( testCommand . otherOptionEnabled ) . isFalse ( ) ;
assertThat ( testCommand . option2 ) . isEqualTo ( 20 ) ;
}
/ * *
/ * *
* Check logger calls
* Check logger calls
*
*
@ -193,10 +237,32 @@ public class CommandLineUtilsTest {
* logger itself but the fact that we call it .
* logger itself but the fact that we call it .
*
*
* @param dependentOptions the string representing the list of dependent options names
* @param dependentOptions the string representing the list of dependent options names
* @param mainOption the main option name
* @param mainOptions the main option name
* /
* /
private void verifyOptionsConstraintLoggerCall (
private void verifyOptionsConstraintLoggerCall (
final Logger logger , final String dependentOptions , final String mainOption ) {
final Logger logger , final String dependentOptions , final String . . . mainOptions ) {
verifyCall ( logger , dependentOptions , DEPENDENCY_WARNING_MSG , mainOptions ) ;
}
/ * *
* Check logger calls , where multiple main options have been specified
*
* < p > Here we check the calls to logger and not the result of the log line as we don ' t test the
* logger itself but the fact that we call it .
*
* @param dependentOptions the string representing the list of dependent options names
* @param mainOptions the main option name
* /
private void verifyMultiOptionsConstraintLoggerCall (
final Logger logger , final String dependentOptions , final String . . . mainOptions ) {
verifyCall ( logger , dependentOptions , MULTI_DEPENDENCY_WARNING_MSG , mainOptions ) ;
}
private void verifyCall (
final Logger logger ,
final String dependentOptions ,
final String dependencyWarningMsg ,
final String . . . mainOptions ) {
final ArgumentCaptor < String > stringArgumentCaptor = ArgumentCaptor . forClass ( String . class ) ;
final ArgumentCaptor < String > stringArgumentCaptor = ArgumentCaptor . forClass ( String . class ) ;
@ -205,8 +271,11 @@ public class CommandLineUtilsTest {
stringArgumentCaptor . capture ( ) ,
stringArgumentCaptor . capture ( ) ,
stringArgumentCaptor . capture ( ) ,
stringArgumentCaptor . capture ( ) ,
stringArgumentCaptor . capture ( ) ) ;
stringArgumentCaptor . capture ( ) ) ;
assertThat ( stringArgumentCaptor . getAllValues ( ) . get ( 0 ) ) . isEqualTo ( DEPENDENCY_WARNING_MSG ) ;
assertThat ( stringArgumentCaptor . getAllValues ( ) . get ( 0 ) ) . isEqualTo ( dependencyWarningMsg ) ;
assertThat ( stringArgumentCaptor . getAllValues ( ) . get ( 1 ) ) . isEqualTo ( dependentOptions ) ;
assertThat ( stringArgumentCaptor . getAllValues ( ) . get ( 1 ) ) . isEqualTo ( dependentOptions ) ;
assertThat ( stringArgumentCaptor . getAllValues ( ) . get ( 2 ) ) . isEqualTo ( mainOption ) ;
final String joinedMainOptions =
StringUtils . joiningWithLastDelimiter ( ", " , " or " ) . apply ( Arrays . asList ( mainOptions ) ) ;
assertThat ( stringArgumentCaptor . getAllValues ( ) . get ( 2 ) ) . isEqualTo ( joinedMainOptions ) ;
}
}
}
}