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