PatternTest.java

  1package de.gultsch.common;
  2
  3import com.google.common.collect.ImmutableList;
  4import java.util.Arrays;
  5import java.util.regex.MatchResult;
  6import java.util.stream.Collectors;
  7import org.junit.Assert;
  8import org.junit.Test;
  9
 10public class PatternTest {
 11
 12    @Test
 13    public void shortImMessage() {
 14        final var message =
 15                "Hi. I'm refactoring how URIs are linked in Conversations. We now support more URI"
 16                    + " schemes like mailto:user@example.com and tel:+1-269-555-0107 and obviously"
 17                    + " maintain support for things like"
 18                    + " xmpp:conversations@conference.siacs.eu?join and https://example.com however"
 19                    + " we no longer link domains that aren't actual URIs like example.com to avoid"
 20                    + " some false positives.";
 21
 22        final var matches =
 23                Patterns.URI_GENERIC
 24                        .matcher(message)
 25                        .results()
 26                        .map(MatchResult::group)
 27                        .collect(Collectors.toList());
 28
 29        Assert.assertEquals(
 30                Arrays.asList(
 31                        "mailto:user@example.com",
 32                        "tel:+1-269-555-0107",
 33                        "xmpp:conversations@conference.siacs.eu?join",
 34                        "https://example.com"),
 35                matches);
 36    }
 37
 38    @Test
 39    public void ambiguous() {
 40        final var message =
 41                "Please find more information in the corresponding page on Wikipedia"
 42                    + " (https://en.wikipedia.org/wiki/Ambiguity_(disambiguation)). Let me know if"
 43                    + " you have questions!";
 44        final var matches =
 45                Patterns.URI_GENERIC
 46                        .matcher(message)
 47                        .results()
 48                        .map(MatchResult::group)
 49                        .collect(Collectors.toList());
 50
 51        Assert.assertEquals(
 52                ImmutableList.of("https://en.wikipedia.org/wiki/Ambiguity_(disambiguation)"),
 53                matches);
 54    }
 55
 56    @Test
 57    public void parenthesis() {
 58        final var message = "Daniel is on Mastodon (https://gultsch.social/@daniel)";
 59        final var matches =
 60                Patterns.URI_GENERIC
 61                        .matcher(message)
 62                        .results()
 63                        .map(MatchResult::group)
 64                        .collect(Collectors.toList());
 65
 66        Assert.assertEquals(ImmutableList.of("https://gultsch.social/@daniel"), matches);
 67    }
 68
 69    @Test
 70    public void fullWidthSpace() {
 71        final var message = "\u3000https://conversations.im";
 72        final var matches =
 73                Patterns.URI_GENERIC
 74                        .matcher(message)
 75                        .results()
 76                        .map(MatchResult::group)
 77                        .collect(Collectors.toList());
 78
 79        Assert.assertEquals(ImmutableList.of("https://conversations.im"), matches);
 80    }
 81
 82    @Test
 83    public void fullWidthColon() {
 84        final var message = "\uFF1Ahttps://conversations.im";
 85        final var matches =
 86                Patterns.URI_GENERIC
 87                        .matcher(message)
 88                        .results()
 89                        .map(MatchResult::group)
 90                        .collect(Collectors.toList());
 91
 92        Assert.assertEquals(ImmutableList.of("https://conversations.im"), matches);
 93    }
 94
 95    @Test
 96    public void newLine() {
 97        final var message = "\nxmpp:example.com";
 98        final var matches =
 99                Patterns.URI_GENERIC
100                        .matcher(message)
101                        .results()
102                        .map(MatchResult::group)
103                        .collect(Collectors.toList());
104
105        Assert.assertEquals(ImmutableList.of("xmpp:example.com"), matches);
106    }
107
108    @Test
109    public void code() {
110        final var message = "`xmpp:example.com`";
111        final var matches =
112                Patterns.URI_GENERIC
113                        .matcher(message)
114                        .results()
115                        .map(MatchResult::group)
116                        .collect(Collectors.toList());
117
118        Assert.assertTrue(matches.isEmpty());
119    }
120}