@ -24,6 +24,7 @@ import static org.mockito.Mockito.when;
import java.io.ByteArrayOutputStream ;
import java.io.File ;
import java.io.IOException ;
import java.io.PrintStream ;
import java.util.ArrayList ;
import java.util.List ;
@ -51,7 +52,7 @@ import picocli.CommandLine.RunLast;
@RunWith ( MockitoJUnitRunner . class )
public class ConfigOptionSearchAndRunHandlerTest {
private static final String CONFIG_FILE_OPTION_NAME = "--config" ;
private static final String CONFIG_FILE_OPTION_NAME = "--config-file " ;
@Rule public final TemporaryFolder temp = new TemporaryFolder ( ) ;
@Rule public ExpectedException exceptionRule = ExpectedException . none ( ) ;
@ -67,8 +68,7 @@ public class ConfigOptionSearchAndRunHandlerTest {
new DefaultExceptionHandler < List < Object > > ( ) . useErr ( errPrintStream ) . useAnsi ( Ansi . OFF ) ;
private final Map < String , String > environment = singletonMap ( "BESU_LOGGING" , "ERROR" ) ;
private final ConfigOptionSearchAndRunHandler configParsingHandler =
new ConfigOptionSearchAndRunHandler (
resultHandler , exceptionHandler , CONFIG_FILE_OPTION_NAME , environment ) ;
new ConfigOptionSearchAndRunHandler ( resultHandler , exceptionHandler , environment ) ;
@Mock ParseResult mockParseResult ;
@Mock CommandLine mockCommandLine ;
@ -89,7 +89,7 @@ public class ConfigOptionSearchAndRunHandlerTest {
}
@Test
public void handle ( ) throws Exception {
public void handleWithCommandLineOption ( ) throws Exception {
when ( mockConfigOptionGetter . get ( ) ) . thenReturn ( temp . newFile ( ) ) ;
final List < Object > result = configParsingHandler . handle ( mockParseResult ) ;
verify ( mockCommandLine ) . setDefaultValueProvider ( any ( IDefaultValueProvider . class ) ) ;
@ -98,7 +98,22 @@ public class ConfigOptionSearchAndRunHandlerTest {
}
@Test
public void handleShouldRaiseExceptionIfNoFileParam ( ) throws Exception {
public void handleWithEnvironmentVariable ( ) throws IOException {
when ( mockParseResult . hasMatchedOption ( CONFIG_FILE_OPTION_NAME ) ) . thenReturn ( false ) ;
final ConfigOptionSearchAndRunHandler environmentConfigFileParsingHandler =
new ConfigOptionSearchAndRunHandler (
resultHandler ,
exceptionHandler ,
singletonMap ( "BESU_CONFIG_FILE" , temp . newFile ( ) . getAbsolutePath ( ) ) ) ;
when ( mockParseResult . hasMatchedOption ( CONFIG_FILE_OPTION_NAME ) ) . thenReturn ( false ) ;
environmentConfigFileParsingHandler . handle ( mockParseResult ) ;
}
@Test
public void handleWithCommandLineOptionShouldRaiseExceptionIfNoFileParam ( ) throws Exception {
exceptionRule . expect ( Exception . class ) ;
final String error_message = "an error occurred during get" ;
exceptionRule . expectMessage ( error_message ) ;
@ -106,6 +121,18 @@ public class ConfigOptionSearchAndRunHandlerTest {
configParsingHandler . handle ( mockParseResult ) ;
}
@Test
public void handleWithEnvironmentVariableOptionShouldRaiseExceptionIfNoFileParam ( ) {
exceptionRule . expect ( CommandLine . ParameterException . class ) ;
final ConfigOptionSearchAndRunHandler environmentConfigFileParsingHandler =
new ConfigOptionSearchAndRunHandler (
resultHandler , exceptionHandler , singletonMap ( "BESU_CONFIG_FILE" , "not_found.toml" ) ) ;
when ( mockParseResult . hasMatchedOption ( CONFIG_FILE_OPTION_NAME ) ) . thenReturn ( false ) ;
environmentConfigFileParsingHandler . handle ( mockParseResult ) ;
}
@Test
public void selfMustReturnTheHandler ( ) {
assertThat ( configParsingHandler . self ( ) ) . isSameAs ( configParsingHandler ) ;
@ -127,4 +154,20 @@ public class ConfigOptionSearchAndRunHandlerTest {
final String value = defaultValueProvider . defaultValue ( OptionSpec . builder ( "--logging" ) . build ( ) ) ;
assertThat ( value ) . isEqualTo ( "ERROR" ) ;
}
@Test
public void handleThrowsErrorWithWithEnvironmentVariableAndCommandLineSpecified ( )
throws IOException {
exceptionRule . expect ( CommandLine . ParameterException . class ) ;
final ConfigOptionSearchAndRunHandler environmentConfigFileParsingHandler =
new ConfigOptionSearchAndRunHandler (
resultHandler ,
exceptionHandler ,
singletonMap ( "BESU_CONFIG_FILE" , temp . newFile ( ) . getAbsolutePath ( ) ) ) ;
when ( mockParseResult . hasMatchedOption ( CONFIG_FILE_OPTION_NAME ) ) . thenReturn ( true ) ;
environmentConfigFileParsingHandler . handle ( mockParseResult ) ;
}
}