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