|
19 | 19 | import static org.hamcrest.CoreMatchers.equalTo;
|
20 | 20 | import static org.hamcrest.MatcherAssert.assertThat;
|
21 | 21 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
22 | 24 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
23 | 25 | import static org.junit.jupiter.api.Assertions.assertNull;
|
24 | 26 | import static org.junit.jupiter.api.Assertions.assertSame;
|
| 27 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
25 | 28 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
26 | 29 |
|
27 | 30 | import org.apache.logging.log4j.Level;
|
@@ -89,38 +92,131 @@ public void testDotAllPattern() throws Exception {
|
89 | 92 | }
|
90 | 93 |
|
91 | 94 | @Test
|
92 |
| - public void testNoMsg() throws Exception { |
93 |
| - final RegexFilter filter = RegexFilter.createFilter(".* test .*", null, false, null, null); |
| 95 | + void testNoMsg() { |
| 96 | + |
| 97 | + final RegexFilter filter = |
| 98 | + RegexFilter.newBuilder() |
| 99 | + .setRegex(".* test .*") |
| 100 | + .setUseRawMsg(false) |
| 101 | + .build(); |
| 102 | + |
| 103 | + assertNotNull(filter); |
| 104 | + |
94 | 105 | filter.start();
|
| 106 | + |
95 | 107 | assertTrue(filter.isStarted());
|
96 | 108 | assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
|
97 | 109 | assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Message) null, (Throwable) null));
|
98 | 110 | assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, null, (Object[]) null));
|
99 | 111 | }
|
100 | 112 |
|
101 | 113 | @Test
|
102 |
| - public void testParameterizedMsg() throws Exception { |
| 114 | + void testParameterizedMsg() { |
103 | 115 | final String msg = "params {} {}";
|
104 | 116 | final Object[] params = {"foo", "bar"};
|
105 | 117 |
|
106 | 118 | // match against raw message
|
107 |
| - final RegexFilter rawFilter = RegexFilter.createFilter( |
108 |
| - "params \\{\\} \\{\\}", |
109 |
| - null, |
110 |
| - true, // useRawMsg |
111 |
| - Result.ACCEPT, |
112 |
| - Result.DENY); |
| 119 | + final RegexFilter rawFilter = |
| 120 | + RegexFilter.newBuilder() |
| 121 | + .setRegex("params \\{\\} \\{\\}") |
| 122 | + .setUseRawMsg(true) |
| 123 | + .setOnMatch(Result.ACCEPT) |
| 124 | + .setOnMismatch(Result.DENY) |
| 125 | + .build(); |
| 126 | + |
| 127 | + assertNotNull(rawFilter); |
| 128 | + |
113 | 129 | final Result rawResult = rawFilter.filter(null, null, null, msg, params);
|
114 | 130 | assertThat(rawResult, equalTo(Result.ACCEPT));
|
115 | 131 |
|
116 | 132 | // match against formatted message
|
117 |
| - final RegexFilter fmtFilter = RegexFilter.createFilter( |
118 |
| - "params foo bar", |
119 |
| - null, |
120 |
| - false, // useRawMsg |
121 |
| - Result.ACCEPT, |
122 |
| - Result.DENY); |
| 133 | + final RegexFilter fmtFilter = |
| 134 | + RegexFilter.newBuilder() |
| 135 | + .setRegex("params foo bar") |
| 136 | + .setUseRawMsg(false) |
| 137 | + .setOnMatch(Result.ACCEPT) |
| 138 | + .setOnMismatch(Result.DENY).build(); |
| 139 | + |
| 140 | + assertNotNull(fmtFilter); |
| 141 | + |
123 | 142 | final Result fmtResult = fmtFilter.filter(null, null, null, msg, params);
|
124 | 143 | assertThat(fmtResult, equalTo(Result.ACCEPT));
|
125 | 144 | }
|
| 145 | + |
| 146 | + /** |
| 147 | + * A builder with no 'regex' expression should both be invalid and return null on 'build()'. |
| 148 | + */ |
| 149 | + @Test |
| 150 | + void testWithValidRegex() { |
| 151 | + |
| 152 | + final String regex = "^[a-zA-Z0-9_]+$"; // matches alphanumeric with underscores |
| 153 | + |
| 154 | + final RegexFilter.Builder builder = |
| 155 | + RegexFilter.newBuilder().setRegex(regex).setUseRawMsg(false).setOnMatch(Result.ACCEPT).setOnMismatch(Result.DENY); |
| 156 | + |
| 157 | + assertTrue(builder.isValid()); |
| 158 | + |
| 159 | + final RegexFilter filter = builder.build(); |
| 160 | + |
| 161 | + assertNotNull(filter); |
| 162 | + |
| 163 | + assertEquals(Result.ACCEPT, filter.filter("Hello_123")); |
| 164 | + |
| 165 | + assertEquals(Result.DENY, filter.filter("Hello@123")); |
| 166 | + |
| 167 | + assertEquals(regex, filter.getRegex()); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void testRegexFilterGetters() { |
| 172 | + |
| 173 | + final String regex = "^[a-zA-Z0-9_]+$"; // matches alphanumeric with underscores |
| 174 | + |
| 175 | + final RegexFilter filter = |
| 176 | + RegexFilter.newBuilder() |
| 177 | + .setRegex(regex) |
| 178 | + .setUseRawMsg(false) |
| 179 | + .setOnMatch(Result.ACCEPT) |
| 180 | + .setOnMismatch(Result.DENY) |
| 181 | + .build(); |
| 182 | + |
| 183 | + assertNotNull(filter); |
| 184 | + |
| 185 | + assertEquals(regex, filter.getRegex()); |
| 186 | + assertFalse(filter.isUseRawMessage()); |
| 187 | + assertEquals(Result.ACCEPT, filter.getOnMatch()); |
| 188 | + assertEquals(Result.DENY, filter.getOnMismatch()); |
| 189 | + assertNotNull(filter.getPattern()); |
| 190 | + assertEquals(regex, filter.getPattern().pattern()); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * A builder with no 'regex' expression should both be invalid and return null on 'build()'. |
| 195 | + */ |
| 196 | + @Test |
| 197 | + void testBuilderWithoutRegexNotValid() { |
| 198 | + |
| 199 | + final RegexFilter.Builder builder = RegexFilter.newBuilder(); |
| 200 | + |
| 201 | + assertFalse(builder.isValid()); |
| 202 | + |
| 203 | + assertNull(builder.build()); |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * A builder with an invalid 'regex' expression should return null on 'build()'. |
| 209 | + */ |
| 210 | + @Test |
| 211 | + void testBuilderWithInvalidRegexNotValid() { |
| 212 | + |
| 213 | + final RegexFilter.Builder builder = RegexFilter.newBuilder(); |
| 214 | + |
| 215 | + builder.setRegex("[a-z"); |
| 216 | + |
| 217 | + assertFalse(builder.isValid()); |
| 218 | + |
| 219 | + assertNull(builder.build()); |
| 220 | + |
| 221 | + } |
126 | 222 | }
|
0 commit comments