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