55
66package io .opentelemetry .contrib .jmxscraper ;
77
8+ import static org .assertj .core .api .Assertions .assertThat ;
89import static org .assertj .core .api .Assertions .assertThatThrownBy ;
910import static org .mockito .Mockito .mock ;
1011
12+ import io .opentelemetry .contrib .jmxscraper .config .ConfigurationException ;
13+ import io .opentelemetry .contrib .jmxscraper .config .JmxScraperConfig ;
1114import io .opentelemetry .contrib .jmxscraper .config .JmxScraperConfigFactory ;
15+ import java .io .IOException ;
16+ import java .io .InputStream ;
1217import java .util .Arrays ;
1318import java .util .Collections ;
1419import java .util .List ;
@@ -18,7 +23,7 @@ class JmxScraperTest {
1823 @ Test
1924 void shouldThrowExceptionWhenInvalidCommandLineArgsProvided () {
2025 // Given
21- List <String > emptyArgs = Collections .singletonList ("-inexistingOption " );
26+ List <String > emptyArgs = Collections .singletonList ("-nonExistentOption " );
2227 JmxScraperConfigFactory configFactoryMock = mock (JmxScraperConfigFactory .class );
2328
2429 // When and Then
@@ -29,11 +34,46 @@ void shouldThrowExceptionWhenInvalidCommandLineArgsProvided() {
2934 @ Test
3035 void shouldThrowExceptionWhenTooManyCommandLineArgsProvided () {
3136 // Given
32- List <String > emptyArgs = Arrays .asList ("-config" , "path" , "-inexistingOption " );
37+ List <String > args = Arrays .asList ("-config" , "path" , "-nonExistentOption " );
3338 JmxScraperConfigFactory configFactoryMock = mock (JmxScraperConfigFactory .class );
3439
3540 // When and Then
36- assertThatThrownBy (() -> JmxScraper .createConfigFromArgs (emptyArgs , configFactoryMock ))
41+ assertThatThrownBy (() -> JmxScraper .createConfigFromArgs (args , configFactoryMock ))
3742 .isInstanceOf (ArgumentsParsingException .class );
3843 }
44+
45+ @ Test
46+ void shouldCreateConfig_propertiesLoadedFromFile ()
47+ throws ConfigurationException , ArgumentsParsingException {
48+ // Given
49+ String filePath = ClassLoader .getSystemClassLoader ().getResource ("validConfig.properties" ).getPath ();
50+ List <String > args = Arrays .asList ("-config" , filePath );
51+ JmxScraperConfigFactory configFactory = new JmxScraperConfigFactory ();
52+
53+ // When
54+ JmxScraperConfig config = JmxScraper .createConfigFromArgs (args , configFactory );
55+
56+ // Then
57+ assertThat (config ).isNotNull ();
58+ }
59+
60+ @ Test
61+ void shouldCreateConfig_propertiesLoadedFromStdIn ()
62+ throws ConfigurationException , ArgumentsParsingException , IOException {
63+ InputStream originalIn = System .in ;
64+ try (InputStream stream = ClassLoader .getSystemClassLoader ().getResourceAsStream ("validConfig.properties" )) {
65+ // Given
66+ System .setIn (stream );
67+ List <String > args = Arrays .asList ("-config" , "-" );
68+ JmxScraperConfigFactory configFactory = new JmxScraperConfigFactory ();
69+
70+ // When
71+ JmxScraperConfig config = JmxScraper .createConfigFromArgs (args , configFactory );
72+
73+ // Then
74+ assertThat (config ).isNotNull ();
75+ } finally {
76+ System .setIn (originalIn );
77+ }
78+ }
3979}
0 commit comments