source: josm/trunk/test/unit/org/openstreetmap/josm/tools/TextTagParserTest.java@ 17442

Last change on this file since 17442 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5
6import java.util.ArrayList;
7import java.util.Arrays;
8import java.util.Collections;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12
13import org.junit.jupiter.api.extension.RegisterExtension;
14import org.junit.jupiter.api.Test;
15import org.openstreetmap.josm.testutils.JOSMTestRules;
16
17import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18
19/**
20 * Unit tests of {@link TextTagParser} class.
21 */
22class TextTagParserTest {
23 /**
24 * Some of this depends on preferences.
25 */
26 @RegisterExtension
27 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
28 public JOSMTestRules test = new JOSMTestRules().preferences();
29
30 /**
31 * Test of {@link TextTagParser#unescape} method.
32 */
33 @Test
34 void testUnescape() {
35 String s, s1;
36 s = "\"2 3 4\"";
37 s1 = "2 3 4";
38 assertEquals(s1, TextTagParser.unescape(s));
39
40 s = "\"2 \\\"3\\\" 4\"";
41 s1 = "2 \"3\" 4";
42 assertEquals(s1, TextTagParser.unescape(s));
43
44 s = "\"2 3 ===4===\"";
45 s1 = "2 3 ===4===";
46 assertEquals(s1, TextTagParser.unescape(s));
47
48 s = "\"2 3 \\\\\\\\===4===\"";
49 s1 = "2 3 \\\\===4===";
50 assertEquals(s1, TextTagParser.unescape(s));
51 }
52
53 /**
54 * Test of {@link TextTagParser#readTagsFromText} method with tabs and new lines.
55 */
56 @Test
57 void testTNformat() {
58 String txt = " a \t 1 \n\n\n b\t2 \n c \t the value with \"quotes\"";
59 Map<String, String> correctTags = new HashMap<String, String>() { {
60 put("a", "1"); put("b", "2"); put("c", "the value with \"quotes\"");
61 } };
62 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
63 assertEquals(correctTags, tags);
64 }
65
66 /**
67 * Test of {@link TextTagParser#readTagsFromText} method with quotes.
68 */
69 @Test
70 void testEQformat() {
71 String txt = "key1=value key2=\"long value\" tag3=\"hotel \\\"Quote\\\"\"";
72 Map<String, String> correctTags = new HashMap<String, String>() { {
73 put("key1", "value"); put("key2", "long value");
74 put("tag3", "hotel \"Quote\"");
75 } };
76 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
77 assertEquals(correctTags, tags);
78 }
79
80 /**
81 * Test of {@link TextTagParser#readTagsFromText} method with JSON.
82 */
83 @Test
84 void testJSONformat() {
85 String txt;
86 Map<String, String> tags, correctTags;
87 txt = "{ \"a\":\"1\", \"b\":\"2 3 4\" }";
88 correctTags = new HashMap<String, String>() { { put("a", "1"); put("b", "2 3 4"); } };
89 tags = TextTagParser.readTagsFromText(txt);
90 assertEquals(correctTags, tags);
91
92 txt = "\"a\" : \"1 1 1\", \"b2\" :\"2 \\\"3 qwe\\\" 4\"";
93 correctTags = new HashMap<String, String>() { { put("a", "1 1 1"); put("b2", "2 \"3 qwe\" 4"); } };
94 tags = TextTagParser.readTagsFromText(txt);
95 assertEquals(correctTags, tags);
96
97 txt = " \"aыыы\" : \"val\\\"\\\"\\\"ue1\"";
98 correctTags = new HashMap<String, String>() { { put("aыыы", "val\"\"\"ue1"); } };
99 tags = TextTagParser.readTagsFromText(txt);
100 assertEquals(correctTags, tags);
101 }
102
103 /**
104 * Test of {@link TextTagParser#readTagsFromText} method with free format.
105 */
106 @Test
107 void testFreeformat() {
108 String txt = "a 1 b=2 c=\"hello === \\\"\\\"world\"";
109 Map<String, String> correctTags = new HashMap<String, String>() { {
110 put("a", "1"); put("b", "2"); put("c", "hello === \"\"world");
111 } };
112 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
113 assertEquals(correctTags, tags);
114 }
115
116 /**
117 * Test of {@link TextTagParser#readTagsFromText} method (error detection).
118 */
119 @Test
120 void testErrorDetect() {
121 String txt = "a=2 b=3 4";
122 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
123 assertEquals(Collections.EMPTY_MAP, tags);
124 }
125
126 /**
127 * Test of {@link TextTagParser#readTagsFromText} method with tabs.
128 */
129 @Test
130 void testTab() {
131 assertEquals(Collections.singletonMap("shop", "jewelry"), TextTagParser.readTagsFromText("shop\tjewelry"));
132 assertEquals(Collections.singletonMap("shop", "jewelry"), TextTagParser.readTagsFromText("!shop\tjewelry"));
133 assertEquals(Collections.singletonMap("shop", "jewelry"), TextTagParser.readTagsFromText("!!!shop\tjewelry"));
134 assertEquals(Collections.singletonMap("shop", "jewelry"), TextTagParser.readTagsFromText("shop\t\t\tjewelry"));
135 }
136
137 /**
138 * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/16104">#16104</a>
139 */
140 @Test
141 void testTicket16104() {
142 Map<String, String> expected = new HashMap<>();
143 expected.put("boundary", "national_park");
144 expected.put("name", "Raet nasjonalpark");
145 expected.put("naturbase:iid", "VV00003273");
146 expected.put("naturbase:verneform", "NP");
147 expected.put("operator", "Raet Nasjonalparkstyre");
148 expected.put("protect_class", "2");
149 expected.put("related_law", "https://lovdata.no/forskrift/2016-12-16-1632");
150 expected.put("short_name", "Raet");
151 expected.put("start_date", "2016-12-16");
152 expected.put("type", "boundary");
153 expected.put("url", "http://faktaark.naturbase.no/?id=VV00003273");
154 assertEquals(expected, TextTagParser.readTagsFromText(
155 "boundary=national_park\n" +
156 "name=Raet nasjonalpark\n" +
157 "naturbase:iid=VV00003273\n" +
158 "naturbase:verneform=NP\n" +
159 "operator=Raet Nasjonalparkstyre\n" +
160 "protect_class=2\n" +
161 "related_law=https://lovdata.no/forskrift/2016-12-16-1632\n" +
162 "short_name=Raet\n" +
163 "start_date=2016-12-16\n" +
164 "type=boundary\n" +
165 "url=http://faktaark.naturbase.no/?id=VV00003273"));
166 }
167
168 /**
169 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/8384#comment:58">ticket:8384#comment:58</a>
170 */
171 @Test
172 void testTicket8384Comment58() {
173 Map<String, String> expected = new HashMap<>();
174 expected.put("name", "Main street");
175 expected.put("url", "https://example.com/?id=1");
176 assertEquals(expected, TextTagParser.readTagsFromText("name=Main street\nurl=https://example.com/?id=1"));
177 }
178
179 /**
180 * Tests that the tags ordering is stable.
181 */
182 @Test
183 void testStableOrder() {
184 List<String> expected = Arrays.asList("foo4", "foo3", "foo2", "foo1");
185 ArrayList<String> actual = new ArrayList<>(TextTagParser.readTagsByRegexp(
186 "foo4=bar4 foo3=bar3 foo2=bar2 foo1=bar1", " ", "(.*?)=(.*?)", true).keySet());
187 assertEquals(expected, actual);
188 }
189}
Note: See TracBrowser for help on using the repository browser.