PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/org/openstreetmap/josm/tools/Tag2LinkTest.java

https://github.com/openstreetmap/josm
Java | 135 lines | 84 code | 16 blank | 35 comment | 0 complexity | 6100a753412799af66f756d63ba4cc58 MD5 | raw file
  1. // License: GPL. For details, see LICENSE file.
  2. package org.openstreetmap.josm.tools;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import org.junit.Assert;
  7. import org.junit.jupiter.api.Test;
  8. import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
  9. /**
  10. * Test {@link Tag2Link}
  11. */
  12. @BasicPreferences
  13. class Tag2LinkTest {
  14. List<String> links = new ArrayList<>();
  15. void addLink(String name, String url, ImageResource icon) {
  16. links.add(name + " // " + url);
  17. }
  18. void checkLinks(String... expected) {
  19. Assert.assertEquals(Arrays.asList(expected), links);
  20. }
  21. /**
  22. * Unit test of function {@link Tag2Link#initialize()}.
  23. */
  24. @Test
  25. void testInitialize() {
  26. Tag2Link.initialize();
  27. Assert.assertTrue("obtains at least 40 rules", Tag2Link.wikidataRules.size() > 40);
  28. }
  29. /**
  30. * Unit test of function {@link Tag2Link#getLinksForTag}.
  31. */
  32. @Test
  33. void testName() {
  34. Tag2Link.getLinksForTag("name", "foobar", this::addLink);
  35. checkLinks("Search on duckduckgo.com // https://duckduckgo.com/?q=foobar",
  36. "Search on google.com // https://www.google.com/search?q=foobar");
  37. }
  38. /**
  39. * Unit test of function {@link Tag2Link#getLinksForTag}.
  40. */
  41. @Test
  42. void testWebsite() {
  43. Tag2Link.getLinksForTag("website", "http://www.openstreetmap.org/", this::addLink);
  44. checkLinks("Open openstreetmap.org // http://www.openstreetmap.org/");
  45. links.clear();
  46. Tag2Link.getLinksForTag("website", "https://www.openstreetmap.org/", this::addLink);
  47. checkLinks("Open openstreetmap.org // https://www.openstreetmap.org/");
  48. links.clear();
  49. Tag2Link.getLinksForTag("website", "www.openstreetmap.org", this::addLink);
  50. checkLinks("Open openstreetmap.org // http://www.openstreetmap.org");
  51. }
  52. /**
  53. * Unit test of function {@link Tag2Link#getLinksForTag}.
  54. */
  55. @Test
  56. void testWikipedia() {
  57. Tag2Link.getLinksForTag("wikipedia", "de:Wohnhausgruppe Herderstraße", this::addLink);
  58. checkLinks("View Wikipedia article // https://de.wikipedia.org/wiki/Wohnhausgruppe_Herderstraße");
  59. links.clear();
  60. Tag2Link.getLinksForTag("wikipedia", "de:Stadtbahn Köln#Innenstadttunnel", this::addLink);
  61. checkLinks("View Wikipedia article // https://de.wikipedia.org/wiki/Stadtbahn_Köln#Innenstadttunnel");
  62. }
  63. /**
  64. * Unit test of function {@link Tag2Link#getLinksForTag}.
  65. */
  66. @Test
  67. void testImageCommonsImage() {
  68. Tag2Link.getLinksForTag("image", "File:Witten Brücke Gasstraße.jpg", this::addLink);
  69. checkLinks("View image on Wikimedia Commons // https://commons.wikimedia.org/wiki/File%3AWitten_Br%C3%BCcke_Gasstra%C3%9Fe.jpg");
  70. links.clear();
  71. // non-regression test for #19754
  72. Tag2Link.getLinksForTag("image", "File:Foo.jpg;File:Bar.jpg", this::addLink);
  73. checkLinks("View image on Wikimedia Commons // https://commons.wikimedia.org/wiki/File%3AFoo.jpg",
  74. "View image on Wikimedia Commons // https://commons.wikimedia.org/wiki/File%3ABar.jpg");
  75. links.clear();
  76. // non-regression test for #19771
  77. Tag2Link.getLinksForTag("image", "File:Côte de granite rose - Trégastel à Ploumanac'h - 20190723 - 025.jpg", this::addLink);
  78. checkLinks("View image on Wikimedia Commons // " +
  79. "https://commons.wikimedia.org/wiki/" +
  80. "File%3AC%C3%B4te_de_granite_rose_-_Tr%C3%A9gastel_%C3%A0_Ploumanac%27h_-_20190723_-_025.jpg");
  81. }
  82. /**
  83. * Unit test of function {@link Tag2Link#getLinksForTag}.
  84. */
  85. @Test
  86. void testImageCommonsCategory() {
  87. Tag2Link.getLinksForTag("image", "category:JOSM", this::addLink);
  88. checkLinks("View category on Wikimedia Commons // https://commons.wikimedia.org/wiki/category%3AJOSM");
  89. }
  90. /**
  91. * Unit test of function {@link Tag2Link#getLinksForTag}.
  92. */
  93. @Test
  94. void testBrandWikidata() {
  95. Tag2Link.getLinksForTag("brand:wikidata", "Q259340", this::addLink);
  96. checkLinks("View Wikidata item // https://www.wikidata.org/wiki/Q259340");
  97. }
  98. /**
  99. * Unit test of function {@link Tag2Link#getLinksForTag}.
  100. */
  101. @Test
  102. void testArchipelagoWikidata() {
  103. Tag2Link.getLinksForTag("archipelago:wikidata", "Q756987;Q756988", this::addLink);
  104. checkLinks("View Wikidata item // https://www.wikidata.org/wiki/Q756987",
  105. "View Wikidata item // https://www.wikidata.org/wiki/Q756988");
  106. }
  107. /**
  108. * Unit test of function {@link Tag2Link#getLinksForTag}.
  109. *
  110. * Non-regression test for https://josm.openstreetmap.de/ticket/19754#comment:9
  111. */
  112. @Test
  113. void testMultipleSources() {
  114. Tag2Link.getLinksForTag("source", "https://foo.com/; https://bar.com/; https://baz.com/", this::addLink);
  115. checkLinks("Open foo.com // https://foo.com/",
  116. "Open bar.com // https://bar.com/",
  117. "Open baz.com // https://baz.com/");
  118. }
  119. }