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

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