1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Arrays;
|
---|
6 | import java.util.List;
|
---|
7 |
|
---|
8 | import org.junit.Assert;
|
---|
9 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
10 | import org.junit.jupiter.api.Test;
|
---|
11 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
12 |
|
---|
13 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Test {@link Tag2Link}
|
---|
17 | */
|
---|
18 | class Tag2LinkTest {
|
---|
19 |
|
---|
20 | List<String> links = new ArrayList<>();
|
---|
21 |
|
---|
22 | void addLink(String name, String url, ImageResource icon) {
|
---|
23 | links.add(name + " // " + url);
|
---|
24 | }
|
---|
25 |
|
---|
26 | void checkLinks(String... expected) {
|
---|
27 | Assert.assertEquals(Arrays.asList(expected), links);
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Setup test.
|
---|
32 | */
|
---|
33 | @RegisterExtension
|
---|
34 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
35 | public JOSMTestRules test = new JOSMTestRules().preferences();
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Unit test of function {@link Tag2Link#initialize()}.
|
---|
39 | */
|
---|
40 | @Test
|
---|
41 | void testInitialize() {
|
---|
42 | Tag2Link.initialize();
|
---|
43 | Assert.assertTrue("obtains at least 40 rules", Tag2Link.wikidataRules.size() > 40);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Unit test of function {@link Tag2Link#getLinksForTag}.
|
---|
48 | */
|
---|
49 | @Test
|
---|
50 | void testName() {
|
---|
51 | Tag2Link.getLinksForTag("name", "foobar", this::addLink);
|
---|
52 | checkLinks("Search on duckduckgo.com // https://duckduckgo.com/?q=foobar",
|
---|
53 | "Search on google.com // https://www.google.com/search?q=foobar");
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Unit test of function {@link Tag2Link#getLinksForTag}.
|
---|
58 | */
|
---|
59 | @Test
|
---|
60 | void testWebsite() {
|
---|
61 | Tag2Link.getLinksForTag("website", "http://www.openstreetmap.org/", this::addLink);
|
---|
62 | checkLinks("Open openstreetmap.org // http://www.openstreetmap.org/");
|
---|
63 | links.clear();
|
---|
64 | Tag2Link.getLinksForTag("website", "https://www.openstreetmap.org/", this::addLink);
|
---|
65 | checkLinks("Open openstreetmap.org // https://www.openstreetmap.org/");
|
---|
66 | links.clear();
|
---|
67 | Tag2Link.getLinksForTag("website", "www.openstreetmap.org", this::addLink);
|
---|
68 | checkLinks("Open openstreetmap.org // http://www.openstreetmap.org");
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Unit test of function {@link Tag2Link#getLinksForTag}.
|
---|
73 | */
|
---|
74 | @Test
|
---|
75 | void testWikipedia() {
|
---|
76 | Tag2Link.getLinksForTag("wikipedia", "de:Wohnhausgruppe Herderstraße", this::addLink);
|
---|
77 | checkLinks("View Wikipedia article // https://de.wikipedia.org/wiki/Wohnhausgruppe_Herderstraße");
|
---|
78 | links.clear();
|
---|
79 | Tag2Link.getLinksForTag("wikipedia", "de:Stadtbahn Köln#Innenstadttunnel", this::addLink);
|
---|
80 | checkLinks("View Wikipedia article // https://de.wikipedia.org/wiki/Stadtbahn_Köln#Innenstadttunnel");
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Unit test of function {@link Tag2Link#getLinksForTag}.
|
---|
85 | */
|
---|
86 | @Test
|
---|
87 | void testImageCommonsImage() {
|
---|
88 | Tag2Link.getLinksForTag("image", "File:Witten Brücke Gasstraße.jpg", this::addLink);
|
---|
89 | checkLinks("View image on Wikimedia Commons // https://commons.wikimedia.org/wiki/File:Witten Brücke Gasstraße.jpg");
|
---|
90 | links.clear();
|
---|
91 | // non-regression test for #19754
|
---|
92 | Tag2Link.getLinksForTag("image", "File:Foo.jpg;File:Bar.jpg", this::addLink);
|
---|
93 | checkLinks("View image on Wikimedia Commons // https://commons.wikimedia.org/wiki/File:Foo.jpg",
|
---|
94 | "View image on Wikimedia Commons // https://commons.wikimedia.org/wiki/File:Bar.jpg");
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Unit test of function {@link Tag2Link#getLinksForTag}.
|
---|
99 | */
|
---|
100 | @Test
|
---|
101 | void testImageCommonsCategory() {
|
---|
102 | Tag2Link.getLinksForTag("image", "category:JOSM", this::addLink);
|
---|
103 | checkLinks("View category on Wikimedia Commons // https://commons.wikimedia.org/wiki/category:JOSM");
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Unit test of function {@link Tag2Link#getLinksForTag}.
|
---|
108 | */
|
---|
109 | @Test
|
---|
110 | void testBrandWikidata() {
|
---|
111 | Tag2Link.getLinksForTag("brand:wikidata", "Q259340", this::addLink);
|
---|
112 | checkLinks("View Wikidata item // https://www.wikidata.org/wiki/Q259340");
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Unit test of function {@link Tag2Link#getLinksForTag}.
|
---|
117 | */
|
---|
118 | @Test
|
---|
119 | void testArchipelagoWikidata() {
|
---|
120 | Tag2Link.getLinksForTag("archipelago:wikidata", "Q756987;Q756988", this::addLink);
|
---|
121 | checkLinks("View Wikidata item // https://www.wikidata.org/wiki/Q756987",
|
---|
122 | "View Wikidata item // https://www.wikidata.org/wiki/Q756988");
|
---|
123 | }
|
---|
124 |
|
---|
125 | }
|
---|