1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 |
|
---|
6 | import org.junit.BeforeClass;
|
---|
7 | import org.junit.Test;
|
---|
8 | import org.openstreetmap.josm.Main;
|
---|
9 | import org.openstreetmap.josm.data.Preferences;
|
---|
10 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Some unit test cases for basic tag management on {@see OsmPrimitive}. Uses
|
---|
14 | * {@see Node} for the tests, {@see OsmPrimitive} is abstract.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | public class OsmPrimitiveKeyHandling {
|
---|
18 |
|
---|
19 | @BeforeClass
|
---|
20 | public static void init() {
|
---|
21 | Main.initApplicationPreferences();
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * test query and get methods on a node withouth keys
|
---|
26 | */
|
---|
27 | @Test
|
---|
28 | public void emptyNode() {
|
---|
29 | Node n = new Node();
|
---|
30 | assertTrue(n.getKeys().size() == 0);
|
---|
31 | assertTrue(!n.hasKeys());
|
---|
32 | assertTrue(!n.hasKey("nosuchkey"));
|
---|
33 | assertTrue(n.keySet().isEmpty());
|
---|
34 |
|
---|
35 | n.remove("nosuchkey"); // should work
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Add a tag to an empty node and test the query and get methods.
|
---|
40 | *
|
---|
41 | */
|
---|
42 | @Test
|
---|
43 | public void put() {
|
---|
44 | Node n = new Node();
|
---|
45 | n.put("akey", "avalue");
|
---|
46 | assertTrue(n.get("akey").equals("avalue"));
|
---|
47 | assertTrue(n.getKeys().size() == 1);
|
---|
48 |
|
---|
49 | assertTrue(n.keySet().size() == 1);
|
---|
50 | assertTrue(n.keySet().contains("akey"));
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Add two tags to an empty node and test the query and get methods.
|
---|
55 | */
|
---|
56 | @Test
|
---|
57 | public void put2() {
|
---|
58 | Node n = new Node();
|
---|
59 | n.put("key.1", "value.1");
|
---|
60 | n.put("key.2", "value.2");
|
---|
61 | assertTrue(n.get("key.1").equals("value.1"));
|
---|
62 | assertTrue(n.get("key.2").equals("value.2"));
|
---|
63 | assertTrue(n.getKeys().size() == 2);
|
---|
64 | assertTrue(n.hasKeys());
|
---|
65 | assertTrue(n.hasKey("key.1"));
|
---|
66 | assertTrue(n.hasKey("key.2"));
|
---|
67 | assertTrue(!n.hasKey("nosuchkey"));
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Remove tags from a node with two tags and test the state of the node.
|
---|
72 | *
|
---|
73 | */
|
---|
74 | @Test
|
---|
75 | public void remove() {
|
---|
76 | Node n = new Node();
|
---|
77 | n.put("key.1", "value.1");
|
---|
78 | n.put("key.2", "value.2");
|
---|
79 |
|
---|
80 | n.remove("nosuchkey"); // should work
|
---|
81 | assertTrue(n.getKeys().size() == 2); // still 2 tags ?
|
---|
82 |
|
---|
83 | n.remove("key.1");
|
---|
84 | assertTrue(n.getKeys().size() == 1);
|
---|
85 | assertTrue(!n.hasKey("key.1"));
|
---|
86 | assertTrue(n.get("key.1") == null);
|
---|
87 | assertTrue(n.hasKey("key.2"));
|
---|
88 | assertTrue(n.get("key.2").equals("value.2"));
|
---|
89 |
|
---|
90 | n.remove("key.2");
|
---|
91 | assertTrue(n.getKeys().size() == 0);
|
---|
92 | assertTrue(!n.hasKey("key.1"));
|
---|
93 | assertTrue(n.get("key.1") == null);
|
---|
94 | assertTrue(!n.hasKey("key.2"));
|
---|
95 | assertTrue(n.get("key.2") == null);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Remove all tags from a node
|
---|
100 | *
|
---|
101 | */
|
---|
102 | @Test
|
---|
103 | public void removeAll() {
|
---|
104 | Node n = new Node();
|
---|
105 |
|
---|
106 | n.put("key.1", "value.1");
|
---|
107 | n.put("key.2", "value.2");
|
---|
108 |
|
---|
109 | n.removeAll();
|
---|
110 | assertTrue(n.getKeys().size() == 0);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Test hasEqualSemanticAttributes on two nodes whose identical tags are added
|
---|
115 | * in different orders.
|
---|
116 | */
|
---|
117 | @Test
|
---|
118 | public void hasEqualSemanticAttributes() {
|
---|
119 | Node n1 = new Node(1);
|
---|
120 | n1.setCoor(new LatLon(0,0));
|
---|
121 | n1.put("key.1", "value.1");
|
---|
122 | n1.put("key.2", "value.2");
|
---|
123 |
|
---|
124 | Node n2 = new Node(1);
|
---|
125 | n2.setCoor(new LatLon(0,0));
|
---|
126 | n2.put("key.2", "value.2");
|
---|
127 | n2.put("key.1", "value.1");
|
---|
128 |
|
---|
129 | assertTrue(n1.hasEqualSemanticAttributes(n2));
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Test hasEqualSemanticAttributes on two nodes with different tags.
|
---|
134 | */
|
---|
135 |
|
---|
136 | @Test
|
---|
137 | public void hasEqualSemanticAttributes_2() {
|
---|
138 | Node n1 = new Node(1);
|
---|
139 | n1.setCoor(new LatLon(0,0));
|
---|
140 | n1.put("key.1", "value.1");
|
---|
141 | n1.put("key.2", "value.3");
|
---|
142 |
|
---|
143 | Node n2 = new Node(1);
|
---|
144 | n2.setCoor(new LatLon(0,0));
|
---|
145 | n2.put("key.1", "value.1");
|
---|
146 | n2.put("key.2", "value.4");
|
---|
147 |
|
---|
148 | assertTrue(!n1.hasEqualSemanticAttributes(n2));
|
---|
149 | }
|
---|
150 |
|
---|
151 | }
|
---|