88
99import static org .junit .jupiter .api .Assertions .*;
1010
11+ import java .io .ByteArrayOutputStream ;
1112import java .io .File ;
13+ import java .io .PrintStream ;
1214import java .util .ArrayList ;
15+ import org .junit .jupiter .api .AfterEach ;
16+ import org .junit .jupiter .api .BeforeEach ;
1317import org .junit .jupiter .api .DisplayName ;
1418import org .junit .jupiter .api .Test ;
1519
1620@ DisplayName ("Test the Config class" )
1721public class ConfigTest {
1822
23+ private final ByteArrayOutputStream outContent = new ByteArrayOutputStream ();
24+ private final ByteArrayOutputStream errContent = new ByteArrayOutputStream ();
25+ private final PrintStream originalOut = System .out ;
26+ private final PrintStream originalErr = System .err ;
27+
28+ private static final String EXPECTED_STRING =
29+ "Config{weblogicLoggingIndexName='index1', publishHost='host1', "
30+ + "publishPort=1234, weblogicLoggingExporterSeverity='Warning', "
31+ + "weblogicLoggingExporterBulkSize='2', enabled=false, "
32+ + "weblogicLoggingExporterFilters=[FilterConfig{expression='MSGID != 'BEA-000449'', "
33+ + "servers=[]}], domainUID='domain1'}" ;
34+
35+ @ BeforeEach
36+ public void setUpStreams () {
37+ System .setOut (new PrintStream (outContent ));
38+ System .setErr (new PrintStream (errContent ));
39+ }
40+
41+ @ AfterEach
42+ public void restoreStreams () {
43+ System .setOut (originalOut );
44+ System .setErr (originalErr );
45+ }
46+
1947 @ DisplayName ("Create the default config from an empty file" )
2048 @ Test
2149 public void createDefaultConfigFromEmptyFile () {
@@ -42,7 +70,7 @@ public void createConfigFromFile() {
4270 // create config by loading an empty file
4371 Config config = Config .loadConfig (new File ("src/test/resources/config1.yaml" ));
4472
45- // now check that the config contains the expected default values
73+ // now check that the config contains the expected values
4674 assertAll (
4775 "config" ,
4876 () -> assertEquals ("host1" , config .getHost ()),
@@ -59,4 +87,37 @@ public void createConfigFromFile() {
5987 () -> assertEquals (2 , config .getBulkSize ()),
6088 () -> assertEquals ("domain1" , config .getDomainUID ()));
6189 }
90+
91+ @ DisplayName ("Config file does not exist" )
92+ @ Test
93+ public void configFileDoesNotExist () {
94+ Config config = Config .loadConfig (new File ("src/test/resources/no-such-file.yaml" ));
95+ assertTrue (outContent .toString ().contains (("Not Found" )));
96+ assertTrue (outContent .toString ().contains ("Using default for all parameters" ));
97+ }
98+
99+ @ DisplayName ("Config file cannot be parsed" )
100+ @ Test
101+ public void configFileCannotBeParsed () {
102+ Config config = Config .loadConfig (new File ("src/test/resources/bad.yaml" ));
103+ assertTrue (outContent .toString ().contains (("Error parsing configuration file" )));
104+ assertTrue (outContent .toString ().contains ("Using default for all parameters" ));
105+ }
106+
107+ @ DisplayName ("Should convert index name to lower case" )
108+ @ Test
109+ public void shouldConvertIndexNameToLowerCase () {
110+ // create config by loading an empty file
111+ Config config = Config .loadConfig (new File ("src/test/resources/config2.yaml" ));
112+
113+ // now check that the config contains the expected values
114+ assertEquals ("index2" , config .getIndexName ());
115+ }
116+
117+ @ DisplayName ("Check the toString() method works as expected" )
118+ @ Test
119+ public void checkToStringWorksAsExpected () {
120+ Config config = Config .loadConfig (new File ("src/test/resources/config1.yaml" ));
121+ assertEquals (EXPECTED_STRING , config .toString ());
122+ }
62123}
0 commit comments