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

Last change on this file since 8624 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 3.9 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.BeforeClass;
10import org.junit.Test;
11import org.openstreetmap.josm.JOSMFixture;
12
13public class TextTagParserTest {
14
15 /**
16 * Setup test.
17 */
18 @BeforeClass
19 public static void setUp() {
20 JOSMFixture.createUnitTestFixture().init();
21 }
22
23 @Test
24 public void testUnescape() {
25 String s, s1;
26 s = "\"2 3 4\""; s1 = "2 3 4";
27 Assert.assertEquals(s1, TextTagParser.unescape(s));
28
29 s = "\"2 \\\"3\\\" 4\""; s1 = "2 \"3\" 4";
30 Assert.assertEquals(s1, TextTagParser.unescape(s));
31
32 s = "\"2 3 ===4===\""; s1 = "2 3 ===4===";
33 Assert.assertEquals(s1, TextTagParser.unescape(s));
34
35 s = "\"2 3 \\\\\\\\===4===\""; s1 = "2 3 \\\\===4===";
36 Assert.assertEquals(s1, TextTagParser.unescape(s));
37 }
38
39 @Test
40 public void testTNformat() {
41 String txt = " a \t 1 \n\n\n b\t2 \n c \t the value with \"quotes\"";
42 Map<String, String> correctTags = new HashMap<String, String>() { {
43 put("a", "1"); put("b", "2"); put("c", "the value with \"quotes\"");
44 } };
45 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
46 Assert.assertEquals(correctTags, tags);
47 }
48
49 @Test
50 public void testEQformat() {
51 String txt = "key1=value key2=\"long value\" tag3=\"hotel \\\"Quote\\\"\"";
52 Map<String, String> correctTags = new HashMap<String, String>() { {
53 put("key1", "value"); put("key2", "long value");
54 put("tag3", "hotel \"Quote\"");
55 } };
56 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
57 Assert.assertEquals(correctTags, tags);
58 }
59
60 @Test
61 public void testJSONformat() {
62 String txt;
63 Map<String, String> tags, correctTags;
64 txt = "{ \"a\":\"1\", \"b\":\"2 3 4\" }";
65 correctTags = new HashMap<String, String>() { { put("a", "1"); put("b", "2 3 4"); } };
66 tags = TextTagParser.readTagsFromText(txt);
67 Assert.assertEquals(correctTags, tags);
68
69 txt = "\"a\" : \"1 1 1\", \"b2\" :\"2 \\\"3 qwe\\\" 4\"";
70 correctTags = new HashMap<String, String>() { { put("a", "1 1 1"); put("b2", "2 \"3 qwe\" 4"); } };
71 tags = TextTagParser.readTagsFromText(txt);
72 Assert.assertEquals(correctTags, tags);
73
74 txt = " \"aыыы\" : \"val\\\"\\\"\\\"ue1\"";
75 correctTags = new HashMap<String, String>() { { put("aыыы", "val\"\"\"ue1"); } };
76 tags = TextTagParser.readTagsFromText(txt);
77 Assert.assertEquals(correctTags, tags);
78 }
79
80 @Test
81 public void testFreeformat() {
82 String txt = "a 1 b=2 c=\"hello === \\\"\\\"world\"";
83 Map<String, String> correctTags = new HashMap<String, String>() { {
84 put("a", "1"); put("b", "2"); put("c", "hello === \"\"world");
85 } };
86 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
87 Assert.assertEquals(correctTags, tags);
88 }
89
90 @Test
91 public void errorDetect() {
92 String txt = "a=2 b=3 4";
93 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
94 Assert.assertEquals(Collections.EMPTY_MAP, tags);
95
96 }
97
98 @Test
99 public void testTab() throws Exception {
100 Assert.assertEquals(TextTagParser.readTagsFromText("shop\tjewelry"), Collections.singletonMap("shop", "jewelry"));
101 Assert.assertEquals(TextTagParser.readTagsFromText("!shop\tjewelry"), Collections.singletonMap("shop", "jewelry"));
102 Assert.assertEquals(TextTagParser.readTagsFromText("!!!shop\tjewelry"), Collections.singletonMap("shop", "jewelry"));
103 Assert.assertEquals(TextTagParser.readTagsFromText("shop\t\t\tjewelry"), Collections.singletonMap("shop", "jewelry"));
104 }
105}
Note: See TracBrowser for help on using the repository browser.