You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LinkExtractorTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util.text;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. import org.junit.Before;
  26. import org.junit.Ignore;
  27. import org.junit.Test;
  28. import static org.junit.Assert.assertEquals;
  29. public class LinkExtractorTest {
  30. private LinkExtractor extractor;
  31. @Before
  32. public void setup() {
  33. extractor = new LinkExtractor();
  34. }
  35. private void testLinks(final CharSequence input, final Link ... expected) {
  36. final List<Link> result = extractor.findLinks(input);
  37. assertEquals(Arrays.asList(expected), result);
  38. }
  39. @Test
  40. public void testEmptyString() {
  41. testLinks("");
  42. }
  43. @Test
  44. public void testNoLinks() {
  45. testLinks("no links here!");
  46. }
  47. @Test
  48. public void testPlainWwwLink() {
  49. testLinks("www.google.com", new Link(0, 14, "www.google.com"));
  50. }
  51. @Test
  52. @Ignore("Not implemented yet")
  53. public void testPlainWwwLinkInSentence() {
  54. testLinks("word www.google.com word", new Link(5, 19, "www.google.com"));
  55. testLinks("word www.google.com, word", new Link(5, 19, "www.google.com"));
  56. }
  57. @Test
  58. @Ignore("Not implemented yet")
  59. public void testPlainWwwLinkWithPunctuation() {
  60. testLinks("www.google.com.", new Link(0, 14, "www.google.com"));
  61. testLinks("www.google.com!", new Link(0, 14, "www.google.com"));
  62. testLinks("www.google.com?", new Link(0, 14, "www.google.com"));
  63. }
  64. @Test
  65. @Ignore("Not implemented yet")
  66. public void testPlainWwwLinkWithBrackets() {
  67. testLinks("(www.google.com)", new Link(1, 15, "www.google.com"));
  68. testLinks("[www.google.com]", new Link(1, 15, "www.google.com"));
  69. testLinks("<www.google.com>", new Link(1, 15, "www.google.com"));
  70. }
  71. @Test
  72. @Ignore("Not implemented yet")
  73. public void testPlainWwwLinkWithComplicatedBrackets() {
  74. testLinks("(foo: www.google.com)", new Link(6, 21, "www.google.com"));
  75. testLinks("[foo: www.google.com]", new Link(6, 21, "www.google.com"));
  76. testLinks("<foo: www.google.com>", new Link(6, 21, "www.google.com"));
  77. testLinks("(foo: 'www.google.com')", new Link(7, 22, "www.google.com"));
  78. testLinks("('www.google.com')", new Link(2, 17, "www.google.com"));
  79. testLinks("[\"www.google.com\"]", new Link(2, 17, "www.google.com"));
  80. testLinks("www.example.com/blah(foobar)", new Link(0, 28, "www.example.com/blah(foobar)"));
  81. }
  82. @Test
  83. @Ignore("Not implemented yet")
  84. public void testPlainWwwLinkWithQuotes() {
  85. testLinks("\"www.google.com\"", new Link(1, 15, "www.google.com"));
  86. testLinks("'www.google.com'", new Link(1, 15, "www.google.com"));
  87. }
  88. @Test
  89. public void testPrefixedLinks() {
  90. testLinks("http://www.google.com", new Link(0, 21, "http://www.google.com"));
  91. testLinks("http://www.google.com:80/test#flub",
  92. new Link(0, 34, "http://www.google.com:80/test#flub"));
  93. testLinks("svn+ssh://foo:bar", new Link(0, 17, "svn+ssh://foo:bar"));
  94. }
  95. @Test
  96. @Ignore("Not implemented yet")
  97. public void testMultipleLinks() {
  98. testLinks("www.foo.com www.bar.com",
  99. new Link(0, 11, "www.foo.com"),
  100. new Link(12, 23, "www.bar.com"));
  101. testLinks("(www.foo.com www.bar.com)",
  102. new Link(1, 12, "www.foo.com"),
  103. new Link(13, 24, "www.bar.com"));
  104. testLinks("('www.foo.com')->x(\"www.bar.com\")",
  105. new Link(2, 13, "www.foo.com"),
  106. new Link(20, 31, "www.bar.com"));
  107. testLinks("\"foo\" www.foo.com \"www.bar.com\"",
  108. new Link(6, 17, "www.foo.com"),
  109. new Link(19, 30, "www.bar.com"));
  110. }
  111. @Test
  112. public void testIncompleteLinks() {
  113. testLinks("www...");
  114. testLinks("http://...");
  115. testLinks("/var/web/www.google.com/blah");
  116. }
  117. }