|
6 | 6 |
|
7 | 7 | package weblogic.logging.exporter.config; |
8 | 8 |
|
9 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.*; |
10 | 10 |
|
| 11 | +import java.io.ByteArrayOutputStream; |
| 12 | +import java.io.PrintStream; |
11 | 13 | import java.util.HashMap; |
12 | 14 | import java.util.Map; |
| 15 | +import org.junit.jupiter.api.AfterEach; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
13 | 17 | import org.junit.jupiter.api.DisplayName; |
14 | 18 | import org.junit.jupiter.api.Test; |
15 | 19 |
|
16 | 20 | @DisplayName("Test FilterConfig class") |
17 | 21 | public class FilterConfigTest { |
18 | 22 |
|
19 | | - private static final String EXPECTED_STRING = |
| 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 | + @BeforeEach |
| 29 | + public void setUpStreams() { |
| 30 | + System.setOut(new PrintStream(outContent)); |
| 31 | + System.setErr(new PrintStream(errContent)); |
| 32 | + } |
| 33 | + |
| 34 | + @AfterEach |
| 35 | + public void restoreStreams() { |
| 36 | + System.setOut(originalOut); |
| 37 | + System.setErr(originalErr); |
| 38 | + } |
| 39 | + |
| 40 | + private static final String EXPECTED_FILTERS_STRING = |
20 | 41 | "FilterConfig{expression='MSGID != 'BEA-000449'', servers=[]}"; |
21 | 42 |
|
22 | | - @DisplayName("Check toString() works as expected") |
| 43 | + private static final String EXPECTED_SERVERS_STRING = |
| 44 | + "FilterConfig{expression='null', servers=[managed-server-1]}"; |
| 45 | + |
| 46 | + @DisplayName("Check toString() works as expected for filter expressions") |
| 47 | + @Test |
| 48 | + public void checkToStringWorksAsExpectedForFilterExpressions() { |
| 49 | + Map<String, Object> map = new HashMap<>(); |
| 50 | + map.put("FilterExpression", "MSGID != 'BEA-000449'"); |
| 51 | + FilterConfig filterConfig = FilterConfig.create(map); |
| 52 | + |
| 53 | + assertEquals(EXPECTED_FILTERS_STRING, filterConfig.toString()); |
| 54 | + } |
| 55 | + |
| 56 | + @DisplayName("Check toString() works as expected for servers") |
| 57 | + @Test |
| 58 | + public void checkToStringWorksAsExpectedForServers() { |
| 59 | + Map<String, Object> map = new HashMap<>(); |
| 60 | + map.put("FilterServers", "managed-server-1"); |
| 61 | + FilterConfig filterConfig = FilterConfig.create(map); |
| 62 | + |
| 63 | + assertEquals(EXPECTED_SERVERS_STRING, filterConfig.toString()); |
| 64 | + } |
| 65 | + |
| 66 | + @DisplayName("Check the query can be retrieved") |
23 | 67 | @Test |
24 | | - public void checkToStringWorksAsExpected() { |
| 68 | + public void checkTheQuery() { |
25 | 69 | Map<String, Object> map = new HashMap<>(); |
26 | 70 | map.put("FilterExpression", "MSGID != 'BEA-000449'"); |
27 | 71 | FilterConfig filterConfig = FilterConfig.create(map); |
28 | 72 |
|
29 | | - assertEquals(EXPECTED_STRING, filterConfig.toString()); |
| 73 | + assertTrue( |
| 74 | + filterConfig.getQuery().toString().contains("weblogic.diagnostics.query.CompiledQuery")); |
| 75 | + } |
| 76 | + |
| 77 | + @DisplayName("Bad filter expression") |
| 78 | + @Test |
| 79 | + public void badFilterExpression() { |
| 80 | + Map<String, Object> map = new HashMap<>(); |
| 81 | + map.put("FilterExpression", "nonsense-text"); |
| 82 | + FilterConfig filterConfig = FilterConfig.create(map); |
| 83 | + |
| 84 | + assertTrue(outContent.toString().contains(("Error Parsing expression:"))); |
| 85 | + } |
| 86 | + |
| 87 | + @DisplayName("Check duplicate values are rejected") |
| 88 | + @Test |
| 89 | + public void checkDuplicateValuesAreRejected() { |
| 90 | + Map<String, Object> map = new HashMap<>(); |
| 91 | + map.put( |
| 92 | + "FilterServers", new String[] {"managed-server-1", "managed-server-2", "managed-server-1"}); |
| 93 | + |
| 94 | + assertThrows(ConfigurationException.class, () -> FilterConfig.create(map)); |
30 | 95 | } |
31 | 96 | } |
0 commit comments