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

Last change on this file since 11241 was 10758, checked in by Don-vip, 8 years ago

sonar - squid:S3578 - Test methods should comply with a naming convention

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.Map;
7
8import org.junit.Assert;
9import org.junit.Rule;
10import org.junit.Test;
11import org.openstreetmap.josm.testutils.JOSMTestRules;
12
13import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14
15/**
16 * Unit tests of {@link TextTagParser} class.
17 */
18public class TextTagParserTest {
19 /**
20 * Some of this depends on preferences.
21 */
22 @Rule
23 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
24 public JOSMTestRules test = new JOSMTestRules().preferences();
25
26 /**
27 * Test of {@link TextTagParser#unescape} method.
28 */
29 @Test
30 public void testUnescape() {
31 String s, s1;
32 s = "\"2 3 4\"";
33 s1 = "2 3 4";
34 Assert.assertEquals(s1, TextTagParser.unescape(s));
35
36 s = "\"2 \\\"3\\\" 4\"";
37 s1 = "2 \"3\" 4";
38 Assert.assertEquals(s1, TextTagParser.unescape(s));
39
40 s = "\"2 3 ===4===\"";
41 s1 = "2 3 ===4===";
42 Assert.assertEquals(s1, TextTagParser.unescape(s));
43
44 s = "\"2 3 \\\\\\\\===4===\"";
45 s1 = "2 3 \\\\===4===";
46 Assert.assertEquals(s1, TextTagParser.unescape(s));
47 }
48
49 /**
50 * Test of {@link TextTagParser#readTagsFromText} method with tabs and new lines.
51 */
52 @Test
53 public void testTNformat() {
54 String txt = " a \t 1 \n\n\n b\t2 \n c \t the value with \"quotes\"";
55 Map<String, String> correctTags = new HashMap<String, String>() { {
56 put("a", "1"); put("b", "2"); put("c", "the value with \"quotes\"");
57 } };
58 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
59 Assert.assertEquals(correctTags, tags);
60 }
61
62 /**
63 * Test of {@link TextTagParser#readTagsFromText} method with quotes.
64 */
65 @Test
66 public void testEQformat() {
67 String txt = "key1=value key2=\"long value\" tag3=\"hotel \\\"Quote\\\"\"";
68 Map<String, String> correctTags = new HashMap<String, String>() { {
69 put("key1", "value"); put("key2", "long value");
70 put("tag3", "hotel \"Quote\"");
71 } };
72 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
73 Assert.assertEquals(correctTags, tags);
74 }
75
76 /**
77 * Test of {@link TextTagParser#readTagsFromText} method with JSON.
78 */
79 @Test
80 public void testJSONformat() {
81 String txt;
82 Map<String, String> tags, correctTags;
83 txt = "{ \"a\":\"1\", \"b\":\"2 3 4\" }";
84 correctTags = new HashMap<String, String>() { { put("a", "1"); put("b", "2 3 4"); } };
85 tags = TextTagParser.readTagsFromText(txt);
86 Assert.assertEquals(correctTags, tags);
87
88 txt = "\"a\" : \"1 1 1\", \"b2\" :\"2 \\\"3 qwe\\\" 4\"";
89 correctTags = new HashMap<String, String>() { { put("a", "1 1 1"); put("b2", "2 \"3 qwe\" 4"); } };
90 tags = TextTagParser.readTagsFromText(txt);
91 Assert.assertEquals(correctTags, tags);
92
93 txt = " \"aыыы\" : \"val\\\"\\\"\\\"ue1\"";
94 correctTags = new HashMap<String, String>() { { put("aыыы", "val\"\"\"ue1"); } };
95 tags = TextTagParser.readTagsFromText(txt);
96 Assert.assertEquals(correctTags, tags);
97 }
98
99 /**
100 * Test of {@link TextTagParser#readTagsFromText} method with free format.
101 */
102 @Test
103 public void testFreeformat() {
104 String txt = "a 1 b=2 c=\"hello === \\\"\\\"world\"";
105 Map<String, String> correctTags = new HashMap<String, String>() { {
106 put("a", "1"); put("b", "2"); put("c", "hello === \"\"world");
107 } };
108 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
109 Assert.assertEquals(correctTags, tags);
110 }
111
112 /**
113 * Test of {@link TextTagParser#readTagsFromText} method (error detection).
114 */
115 @Test
116 public void testErrorDetect() {
117 String txt = "a=2 b=3 4";
118 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
119 Assert.assertEquals(Collections.EMPTY_MAP, tags);
120 }
121
122 /**
123 * Test of {@link TextTagParser#readTagsFromText} method with tabs.
124 */
125 @Test
126 public void testTab() {
127 Assert.assertEquals(TextTagParser.readTagsFromText("shop\tjewelry"), Collections.singletonMap("shop", "jewelry"));
128 Assert.assertEquals(TextTagParser.readTagsFromText("!shop\tjewelry"), Collections.singletonMap("shop", "jewelry"));
129 Assert.assertEquals(TextTagParser.readTagsFromText("!!!shop\tjewelry"), Collections.singletonMap("shop", "jewelry"));
130 Assert.assertEquals(TextTagParser.readTagsFromText("shop\t\t\tjewelry"), Collections.singletonMap("shop", "jewelry"));
131 }
132}
Note: See TracBrowser for help on using the repository browser.