Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 27c3cef

Browse files
committed
add some more tests
1 parent bfa8f13 commit 27c3cef

File tree

5 files changed

+117
-4
lines changed

5 files changed

+117
-4
lines changed

pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@
9999
<version>2.22.0</version>
100100
</plugin>
101101
<!-- end -->
102+
<plugin>
103+
<groupId>org.jacoco</groupId>
104+
<artifactId>jacoco-maven-plugin</artifactId>
105+
<version>0.8.3</version>
106+
<executions>
107+
<execution>
108+
<goals>
109+
<goal>prepare-agent</goal>
110+
</goals>
111+
</execution>
112+
<!-- attached to Maven test phase -->
113+
<execution>
114+
<id>report</id>
115+
<phase>test</phase>
116+
<goals>
117+
<goal>report</goal>
118+
</goals>
119+
</execution>
120+
</executions>
121+
</plugin>
102122
</plugins>
103123
</build>
104124

@@ -186,4 +206,21 @@
186206
<version>3.5.2</version>
187207
</dependency>
188208
</dependencies>
209+
210+
<reporting>
211+
<plugins>
212+
<plugin>
213+
<groupId>org.jacoco</groupId>
214+
<artifactId>jacoco-maven-plugin</artifactId>
215+
<reportSets>
216+
<reportSet>
217+
<reports>
218+
<!-- select non-aggregate reports -->
219+
<report>report</report>
220+
</reports>
221+
</reportSet>
222+
</reportSets>
223+
</plugin>
224+
</plugins>
225+
</reporting>
189226
</project>

src/main/java/weblogic/logging/exporter/config/Config.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@ public static Config loadConfig(File file) {
8585
try {
8686
return loadConfig(new FileInputStream(file));
8787
} catch (FileNotFoundException e) {
88-
System.out.println(file.toString() + "Not Found");
88+
System.out.println(file.toString() + " Not Found");
8989
} catch (YamlParserException ex) {
9090
System.out.println("Error parsing configuration file : " + file.toString());
91-
} catch (Exception ex) {
92-
System.out.println("Error detected in configuration file.");
9391
}
9492
System.out.println("Using default for all parameters");
9593
return new Config();

src/test/java/weblogic/logging/exporter/config/ConfigTest.java

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,42 @@
88

99
import static org.junit.jupiter.api.Assertions.*;
1010

11+
import java.io.ByteArrayOutputStream;
1112
import java.io.File;
13+
import java.io.PrintStream;
1214
import java.util.ArrayList;
15+
import org.junit.jupiter.api.AfterEach;
16+
import org.junit.jupiter.api.BeforeEach;
1317
import org.junit.jupiter.api.DisplayName;
1418
import org.junit.jupiter.api.Test;
1519

1620
@DisplayName("Test the Config class")
1721
public 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
}

src/test/resources/bad.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# I am not well formated
2+
publishHost: host1
3+
something else that should
4+
- not be here
5+
at all

src/test/resources/config2.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Test config file
2+
# This one has an upper case index name, which should be converted to lower case
3+
4+
publishHost: host1
5+
publishPort: 1234
6+
weblogicLoggingExporterFilters:
7+
- FilterExpression: MSGID != 'BEA-000449'
8+
weblogicLoggingExporterEnabled: false
9+
weblogicLoggingExporterSeverity: Warning
10+
weblogicLoggingExporterBulkSize: 2
11+
weblogicLoggingIndexName: INDEX2
12+
domainUID: domain1

0 commit comments

Comments
 (0)