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

Commit 49d49f7

Browse files
authored
Merge pull request #4 from oracle/mark-tests
Add some unit tests
2 parents aaef5bd + 93cca29 commit 49d49f7

File tree

12 files changed

+506
-11
lines changed

12 files changed

+506
-11
lines changed

pom.xml

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,54 @@
7171
</execution>
7272
</executions>
7373
</plugin>
74+
<!-- for junit 5 -->
75+
<plugin>
76+
<artifactId>maven-surefire-plugin</artifactId>
77+
<version>2.22.0</version>
78+
<configuration>
79+
<!-- for future use
80+
<groups>acceptance | !feature-a</groups>
81+
<excludedGroups>integration, regression</excludedGroups>
82+
-->
83+
<excludes>
84+
<exclude/>
85+
</excludes>
86+
<!-- for future use
87+
<properties>
88+
<configurationParameters>
89+
junit.jupiter.conditions.deactivate = *
90+
junit.jupiter.extensions.autodetection.enabled = true
91+
junit.jupiter.testinstance.lifecycle.default = per_class
92+
</configurationParameters>
93+
</properties>
94+
-->
95+
</configuration>
96+
</plugin>
97+
<plugin>
98+
<artifactId>maven-failsafe-plugin</artifactId>
99+
<version>2.22.0</version>
100+
</plugin>
101+
<!-- 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>
74122
</plugins>
75123
</build>
76124

@@ -93,6 +141,18 @@
93141
<version>12.2.1-3-0</version>
94142
<scope>provided</scope>
95143
</dependency>
144+
<dependency>
145+
<groupId>com.oracle.weblogic</groupId>
146+
<artifactId>com.bea.core.logging.debug</artifactId>
147+
<version>12.2.1-3-0</version>
148+
<scope>provided</scope>
149+
</dependency>
150+
<dependency>
151+
<groupId>com.oracle.weblogic</groupId>
152+
<artifactId>com.bea.core.diagnostics.core</artifactId>
153+
<version>12.2.1-3-0</version>
154+
<scope>provided</scope>
155+
</dependency>
96156
<dependency>
97157
<groupId>com.oracle.weblogic</groupId>
98158
<artifactId>com.bea.core.diagnostics.logging</artifactId>
@@ -105,16 +165,28 @@
105165
<version>12.2.1-3-0</version>
106166
<scope>provided</scope>
107167
</dependency>
168+
<dependency>
169+
<groupId>com.oracle.weblogic</groupId>
170+
<artifactId>com.bea.core.repackaged.asm</artifactId>
171+
<version>12.2.1-3-0</version>
172+
<scope>provided</scope>
173+
</dependency>
108174
<dependency>
109175
<groupId>com.oracle.weblogic</groupId>
110176
<artifactId>com.bea.core.diagnostics.query</artifactId>
111177
<version>12.2.1-3-0</version>
112178
<scope>provided</scope>
113179
</dependency>
114180
<dependency>
115-
<groupId>junit</groupId>
116-
<artifactId>junit</artifactId>
117-
<version>4.13-beta-2</version>
181+
<groupId>org.junit.jupiter</groupId>
182+
<artifactId>junit-jupiter-api</artifactId>
183+
<version>5.4.1</version>
184+
<scope>test</scope>
185+
</dependency>
186+
<dependency>
187+
<groupId>org.junit.jupiter</groupId>
188+
<artifactId>junit-jupiter-engine</artifactId>
189+
<version>5.4.1</version>
118190
<scope>test</scope>
119191
</dependency>
120192
<dependency>
@@ -128,5 +200,27 @@
128200
<artifactId>snakeyaml</artifactId>
129201
<version>1.23</version>
130202
</dependency>
203+
<dependency>
204+
<groupId>org.antlr</groupId>
205+
<artifactId>antlr-complete</artifactId>
206+
<version>3.5.2</version>
207+
</dependency>
131208
</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>
132226
</project>

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright 2018, 2019, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
33

44
# Required metadata

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();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
5+
*/
6+
7+
package weblogic.logging.exporter;
8+
9+
import static org.hamcrest.CoreMatchers.containsString;
10+
import static org.hamcrest.MatcherAssert.assertThat;
11+
import static org.hamcrest.core.AllOf.allOf;
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
import org.junit.jupiter.api.DisplayName;
15+
import org.junit.jupiter.api.Test;
16+
17+
@DisplayName("Test the Result object")
18+
public class ResultTest {
19+
20+
// using 200 for status field just to test it is set/got correctly
21+
// 200 is HTTP status for OK
22+
private static final String EXPECTED_STRING =
23+
"Result{response='good', status=200, successful=true}";
24+
25+
@DisplayName("After creating a Result, make sure I can retrieve the field values")
26+
@Test
27+
public void afterCreated_canRetrieveFieldValues() {
28+
Result result = new Result("good", 200, true);
29+
30+
assertEquals("good", result.getResponse());
31+
assertEquals(200, result.getStatus());
32+
assertEquals(true, result.isSuccessful());
33+
}
34+
35+
@DisplayName("Check toString() includes response, status and successful")
36+
@Test
37+
public void toStringIncludesResponseStatusAndSuccessful() {
38+
Result result = new Result("good", 1, true);
39+
assertThat(
40+
result.toString(),
41+
allOf(containsString("good"), containsString("1"), containsString("true")));
42+
}
43+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
5+
*/
6+
7+
package weblogic.logging.exporter.config;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
import java.io.ByteArrayOutputStream;
12+
import java.io.File;
13+
import java.io.PrintStream;
14+
import java.util.ArrayList;
15+
import org.junit.jupiter.api.AfterEach;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.DisplayName;
18+
import org.junit.jupiter.api.Test;
19+
20+
@DisplayName("Test the Config class")
21+
public class ConfigTest {
22+
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+
47+
@DisplayName("Create the default config from an empty file")
48+
@Test
49+
public void createDefaultConfigFromEmptyFile() {
50+
// create config by loading an empty file
51+
Config config = Config.loadConfig(new File("src/test/resources/emptyConfig.yaml"));
52+
53+
// now check that the config contains the expected default values
54+
assertAll(
55+
"config",
56+
() -> assertEquals("localhost", config.getHost()),
57+
() -> assertEquals(9200, config.getPort()),
58+
() -> assertEquals(true, config.getEnabled()),
59+
() -> assertEquals("wls", config.getIndexName()),
60+
() -> assertEquals(null, config.getSeverity()),
61+
() -> assertTrue(config.getFilterConfigs() instanceof ArrayList),
62+
() -> assertEquals(0, config.getFilterConfigs().size()),
63+
() -> assertEquals(1, config.getBulkSize()),
64+
() -> assertEquals("unknown", config.getDomainUID()));
65+
}
66+
67+
@DisplayName("Create config from file")
68+
@Test
69+
public void createConfigFromFile() {
70+
// create config by loading an empty file
71+
Config config = Config.loadConfig(new File("src/test/resources/config1.yaml"));
72+
73+
// now check that the config contains the expected values
74+
assertAll(
75+
"config",
76+
() -> assertEquals("host1", config.getHost()),
77+
() -> assertEquals(1234, config.getPort()),
78+
() -> assertEquals(false, config.getEnabled()),
79+
() -> assertEquals("index1", config.getIndexName()),
80+
() -> assertEquals("Warning", config.getSeverity()),
81+
() -> assertTrue(config.getFilterConfigs() instanceof ArrayList),
82+
() -> assertEquals(1, config.getFilterConfigs().size()),
83+
() ->
84+
assertEquals(
85+
"FilterConfig{expression='MSGID != 'BEA-000449'', servers=[]}",
86+
config.getFilterConfigs().get(0).toString()),
87+
() -> assertEquals(2, config.getBulkSize()),
88+
() -> assertEquals("domain1", config.getDomainUID()));
89+
}
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+
}
123+
}

0 commit comments

Comments
 (0)