1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import org.junit.Rule;
|
---|
9 | import org.junit.Test;
|
---|
10 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
11 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
12 |
|
---|
13 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Some unit test cases for basic tag management on {@link OsmPrimitive}. Uses
|
---|
17 | * {@link Node} for the tests, {@link OsmPrimitive} is abstract.
|
---|
18 | */
|
---|
19 | public class OsmPrimitiveKeyHandlingTest {
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Setup test.
|
---|
23 | */
|
---|
24 | @Rule
|
---|
25 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
26 | public JOSMTestRules test = new JOSMTestRules();
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * test query and get methods on a node withouth keys
|
---|
30 | */
|
---|
31 | @Test
|
---|
32 | public void testEmptyNode() {
|
---|
33 | Node n = new Node();
|
---|
34 | testKeysSize(n, 0);
|
---|
35 | testGetKey(n, "nosuchkey", null);
|
---|
36 |
|
---|
37 | n.remove("nosuchkey"); // should work
|
---|
38 | testKeysSize(n, 0);
|
---|
39 | testGetKey(n, "nosuchkey", null);
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Adds a tag to an empty node and test the query and get methods.
|
---|
44 | */
|
---|
45 | @Test
|
---|
46 | public void testPut() {
|
---|
47 | Node n = new Node();
|
---|
48 | n.put("akey", "avalue");
|
---|
49 | testKeysSize(n, 1);
|
---|
50 |
|
---|
51 | testGetKey(n, "akey", "avalue");
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Adds two tags to an empty node and test the query and get methods.
|
---|
56 | */
|
---|
57 | @Test
|
---|
58 | public void testPut2() {
|
---|
59 | Node n = new Node();
|
---|
60 | n.put("key.1", "value.1");
|
---|
61 | n.put("key.2", "value.2");
|
---|
62 | assertTrue(n.get("key.1").equals("value.1"));
|
---|
63 | assertTrue(n.get("key.2").equals("value.2"));
|
---|
64 | testKeysSize(n, 2);
|
---|
65 | assertTrue(n.hasKeys());
|
---|
66 | assertTrue(n.hasKey("key.1"));
|
---|
67 | assertTrue(n.hasKey("key.2"));
|
---|
68 | assertFalse(n.hasKey("nosuchkey"));
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Removes tags from a node with two tags and test the state of the node.
|
---|
73 | */
|
---|
74 | @Test
|
---|
75 | @SuppressFBWarnings(value = "DM_STRING_CTOR", justification = "test that equals is used and not ==")
|
---|
76 | public void testRemove() {
|
---|
77 | Node n = new Node();
|
---|
78 | n.put("key.1", "value.1");
|
---|
79 | n.put(new String("key.2"), new String("value.2")); // Test that equals is used and not ==
|
---|
80 |
|
---|
81 | testGetKey(n, "key.1", "value.1");
|
---|
82 | testGetKey(n, "key.2", "value.2");
|
---|
83 |
|
---|
84 | n.remove("nosuchkey"); // should work
|
---|
85 | testKeysSize(n, 2); // still 2 tags ?
|
---|
86 |
|
---|
87 | testGetKey(n, "key.1", "value.1");
|
---|
88 | testGetKey(n, "key.2", "value.2");
|
---|
89 |
|
---|
90 | n.remove("key.1");
|
---|
91 | testKeysSize(n, 1);
|
---|
92 | assertTrue(n.hasKeys());
|
---|
93 |
|
---|
94 | testGetKey(n, "key.1", null);
|
---|
95 | testGetKey(n, "key.2", "value.2");
|
---|
96 |
|
---|
97 | n.remove("key.2");
|
---|
98 | testKeysSize(n, 0);
|
---|
99 | assertFalse(n.hasKeys());
|
---|
100 | testGetKey(n, "key.1", null);
|
---|
101 | testGetKey(n, "key.2", null);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Removes all tags from a node.
|
---|
106 | */
|
---|
107 | @Test
|
---|
108 | public void testRemoveAll() {
|
---|
109 | Node n = new Node();
|
---|
110 |
|
---|
111 | n.put("key.1", "value.1");
|
---|
112 | n.put("key.2", "value.2");
|
---|
113 |
|
---|
114 | n.removeAll();
|
---|
115 | testKeysSize(n, 0);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Test hasEqualSemanticAttributes on two nodes whose identical tags are added
|
---|
120 | * in different orders.
|
---|
121 | */
|
---|
122 | @Test
|
---|
123 | public void testHasEqualSemanticAttributes() {
|
---|
124 | Node n1 = new Node(1);
|
---|
125 | n1.setCoor(LatLon.ZERO);
|
---|
126 | n1.put("key.1", "value.1");
|
---|
127 | n1.put("key.2", "value.2");
|
---|
128 |
|
---|
129 | Node n2 = new Node(1);
|
---|
130 | n2.setCoor(LatLon.ZERO);
|
---|
131 | n2.put("key.2", "value.2");
|
---|
132 | n2.put("key.1", "value.1");
|
---|
133 |
|
---|
134 | assertTrue(n1.hasEqualSemanticAttributes(n2));
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Test hasEqualSemanticAttributes on two nodes with different tags.
|
---|
139 | */
|
---|
140 | @Test
|
---|
141 | public void testHasEqualSemanticAttributes2() {
|
---|
142 | Node n1 = new Node(1);
|
---|
143 | n1.setCoor(LatLon.ZERO);
|
---|
144 | n1.put("key.1", "value.1");
|
---|
145 | n1.put("key.2", "value.3");
|
---|
146 |
|
---|
147 | Node n2 = new Node(1);
|
---|
148 | n2.setCoor(LatLon.ZERO);
|
---|
149 | n2.put("key.1", "value.1");
|
---|
150 | n2.put("key.2", "value.4");
|
---|
151 |
|
---|
152 | assertFalse(n1.hasEqualSemanticAttributes(n2));
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Tests if the size of the keys map is right.
|
---|
157 | * @author Michael Zangl
|
---|
158 | * @param p The primitive (node) to test
|
---|
159 | * @param expectedSize The expected size.
|
---|
160 | * @throws AssertionError on failure.
|
---|
161 | */
|
---|
162 | private void testKeysSize(OsmPrimitive p, int expectedSize) {
|
---|
163 | assertEquals(expectedSize, p.getKeys().size());
|
---|
164 | assertEquals(expectedSize, p.keySet().size());
|
---|
165 | assertEquals(expectedSize, p.getKeys().entrySet().size());
|
---|
166 | assertEquals(expectedSize, p.getKeys().keySet().size());
|
---|
167 | assertEquals(expectedSize, p.getNumKeys());
|
---|
168 | boolean empty = expectedSize == 0;
|
---|
169 | assertEquals(empty, p.getKeys().isEmpty());
|
---|
170 | assertEquals(empty, p.keySet().isEmpty());
|
---|
171 | assertEquals(empty, p.getKeys().entrySet().isEmpty());
|
---|
172 | assertEquals(empty, p.getKeys().keySet().isEmpty());
|
---|
173 | assertEquals(!empty, p.hasKeys());
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Tests all key get methods for that node.
|
---|
178 | * @author Michael Zangl
|
---|
179 | * @param p The primitive (node)
|
---|
180 | * @param key The key to test
|
---|
181 | * @param value The value the key should have.
|
---|
182 | * @throws AssertionError on failure.
|
---|
183 | */
|
---|
184 | private void testGetKey(OsmPrimitive p, String key, String value) {
|
---|
185 | assertEquals(value != null, p.hasKey(key));
|
---|
186 | assertEquals(value != null, p.getKeys().containsKey(key));
|
---|
187 | assertEquals(value != null, p.getKeys().keySet().contains(key));
|
---|
188 | assertEquals(value, p.get(key));
|
---|
189 | assertEquals(value, p.getKeys().get(key));
|
---|
190 | }
|
---|
191 |
|
---|
192 | }
|
---|