|
1 | 1 | package io.thekraken.grok.api;
|
2 | 2 |
|
3 |
| -import io.thekraken.grok.api.exception.GrokException; |
4 |
| -import org.junit.FixMethodOrder; |
5 |
| -import org.junit.Test; |
6 |
| -import org.junit.runners.MethodSorters; |
| 3 | +import static java.lang.String.format; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertNotEquals; |
| 6 | +import static org.junit.Assert.assertNotNull; |
| 7 | +import static org.junit.Assert.assertTrue; |
7 | 8 |
|
8 | 9 | import java.io.BufferedReader;
|
9 | 10 | import java.io.FileReader;
|
10 |
| -import java.util.*; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
11 | 16 |
|
12 |
| -import static org.junit.Assert.*; |
| 17 | +import org.junit.FixMethodOrder; |
| 18 | +import org.junit.Test; |
| 19 | +import org.junit.runners.MethodSorters; |
| 20 | + |
| 21 | +import io.thekraken.grok.api.exception.GrokException; |
13 | 22 |
|
14 | 23 |
|
15 | 24 | @FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
@@ -522,6 +531,55 @@ public void test017_nonMachingList() throws GrokException {
|
522 | 531 | assertEquals(i, 4);
|
523 | 532 | }
|
524 | 533 |
|
| 534 | + @Test |
| 535 | + public void test018_namedOnlySimpleCase() throws GrokException { |
| 536 | + Grok grok = Grok.create(ResourceManager.PATTERNS); |
| 537 | + |
| 538 | + grok.addPattern("WORD", "foo|bar"); |
| 539 | + grok.addPattern("TEXT", "<< %{WORD}+ >>"); |
| 540 | + |
| 541 | + grok.compile("%{TEXT:text}", true); |
| 542 | + |
| 543 | + String text = "<< barfoobarfoo >>"; |
| 544 | + Match match = grok.match(text); |
| 545 | + match.captures(); |
| 546 | + assertEquals("unable to parse: " + text, |
| 547 | + text, |
| 548 | + match.toMap().get("text")); |
| 549 | + } |
| 550 | + |
| 551 | + @Test |
| 552 | + public void test019_namedOnlyAllCases() throws GrokException { |
| 553 | + /* like previous test, but systematic all four possible options */ |
| 554 | + testPatternRepetitions(true, "(?:foo|bar)"); |
| 555 | + testPatternRepetitions(true, "foo|bar"); |
| 556 | + testPatternRepetitions(false, "(?:foo|bar)"); |
| 557 | + testPatternRepetitions(false, "foo|bar"); |
| 558 | + } |
| 559 | + |
| 560 | + private void testPatternRepetitions(boolean namedOnly, String pattern) throws GrokException { |
| 561 | + String description = format("[readonly:%s pattern:%s] ", namedOnly, pattern);; |
| 562 | + |
| 563 | + Grok grok = Grok.create(ResourceManager.PATTERNS); |
| 564 | + |
| 565 | + grok.addPattern("WORD", pattern); |
| 566 | + grok.addPattern("TEXT", "<< %{WORD}+ >>"); |
| 567 | + |
| 568 | + grok.compile("%{TEXT:text}", namedOnly); |
| 569 | + assertMatches(description, grok, "<< foo >>"); |
| 570 | + assertMatches(description, grok, "<< foobar >>"); |
| 571 | + assertMatches(description, grok, "<< foofoobarbar >>"); |
| 572 | + assertMatches(description, grok, "<< barfoobarfoo >>"); |
| 573 | + } |
| 574 | + |
| 575 | + private void assertMatches(String description, Grok grok, String text) { |
| 576 | + Match match = grok.match(text); |
| 577 | + match.captures(); |
| 578 | + assertEquals(format("%s: unable to parse '%s'", description, text), |
| 579 | + text, |
| 580 | + match.toMap().get("text")); |
| 581 | + } |
| 582 | + |
525 | 583 | /* see: https://github.com/thekrakken/java-grok/issues/64 */
|
526 | 584 | @Test
|
527 | 585 | public void testDisablingAutomaticConversion() throws GrokException {
|
|
0 commit comments