|
18 | 18 | import org.junit.Test;
|
19 | 19 | import org.junit.runners.MethodSorters;
|
20 | 20 |
|
| 21 | +import java.io.BufferedReader; |
| 22 | +import java.io.FileReader; |
| 23 | +import java.util.*; |
| 24 | + |
| 25 | +import static org.hamcrest.CoreMatchers.containsString; |
| 26 | +import static org.junit.Assert.*; |
21 | 27 | import io.thekraken.grok.api.exception.GrokException;
|
22 | 28 |
|
23 | 29 |
|
@@ -597,16 +603,43 @@ public void testDisablingAutomaticConversion() throws GrokException {
|
597 | 603 | @Test
|
598 | 604 | public void test020_postfix_patterns() throws Throwable {
|
599 | 605 | final Grok grok = Grok.create("patterns/postfix");
|
| 606 | + grok.addPatternFromFile("patterns/patterns"); |
600 | 607 | grok.compile("%{POSTFIX_SMTPD}", false);
|
601 | 608 |
|
602 | 609 | assertTrue(grok.getPatterns().containsKey("POSTFIX_SMTPD"));
|
603 | 610 | }
|
| 611 | + |
604 | 612 | @Test
|
605 | 613 | public void test021_postfix_patterns_with_named_captures_only() throws Throwable {
|
606 | 614 | final Grok grok = Grok.create("patterns/postfix");
|
| 615 | + grok.addPatternFromFile("patterns/patterns"); |
607 | 616 | grok.compile("%{POSTFIX_SMTPD}", true);
|
608 | 617 |
|
609 | 618 | assertTrue(grok.getPatterns().containsKey("POSTFIX_SMTPD"));
|
610 | 619 | }
|
611 | 620 |
|
| 621 | + @Test |
| 622 | + public void test022_named_captures_with_missing_definition() throws Throwable { |
| 623 | + ensureAbortsWithDefinitionMissing("FOO %{BAR}", "%{FOO}", true); |
| 624 | + } |
| 625 | + |
| 626 | + @Test |
| 627 | + public void test023_captures_with_missing_definition() throws Throwable { |
| 628 | + ensureAbortsWithDefinitionMissing("FOO %{BAR}", "%{FOO:name}", false); |
| 629 | + } |
| 630 | + |
| 631 | + @Test |
| 632 | + public void test024_captures_with_missing_definition() throws Throwable { |
| 633 | + ensureAbortsWithDefinitionMissing("FOO %{BAR}", "%{FOO}", false); |
| 634 | + } |
| 635 | + |
| 636 | + private void ensureAbortsWithDefinitionMissing(String pattern, String compilePattern, boolean namedOnly) { |
| 637 | + try { |
| 638 | + final Grok grok = Grok.create(ResourceManager.PATTERNS, pattern); |
| 639 | + grok.compile(compilePattern, namedOnly); |
| 640 | + fail("should abort due to missing definition"); |
| 641 | + } catch (GrokException e) { |
| 642 | + assertThat(e.getMessage(), containsString("No definition for key")); |
| 643 | + } |
| 644 | + } |
612 | 645 | }
|
0 commit comments