source: josm/trunk/test/unit/org/openstreetmap/josm/data/osm/OsmPrimitiveKeyHandlingTest.java@ 7081

Last change on this file since 7081 was 7081, checked in by Don-vip, 10 years ago

fixes for unit tests

File size: 4.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import static org.junit.Assert.assertTrue;
5
6import org.junit.BeforeClass;
7import org.junit.Test;
8import org.openstreetmap.josm.JOSMFixture;
9import org.openstreetmap.josm.data.coor.LatLon;
10
11/**
12 * Some unit test cases for basic tag management on {@link OsmPrimitive}. Uses
13 * {@link Node} for the tests, {@link OsmPrimitive} is abstract.
14 *
15 */
16public class OsmPrimitiveKeyHandlingTest {
17
18 @BeforeClass
19 public static void init() {
20 JOSMFixture.createUnitTestFixture().init();
21 }
22
23 /**
24 * test query and get methods on a node withouth keys
25 */
26 @Test
27 public void emptyNode() {
28 Node n = new Node();
29 assertTrue(n.getKeys().size() == 0);
30 assertTrue(!n.hasKeys());
31 assertTrue(!n.hasKey("nosuchkey"));
32 assertTrue(n.keySet().isEmpty());
33
34 n.remove("nosuchkey"); // should work
35 }
36
37 /**
38 * Adds a tag to an empty node and test the query and get methods.
39 */
40 @Test
41 public void put() {
42 Node n = new Node();
43 n.put("akey", "avalue");
44 assertTrue(n.get("akey").equals("avalue"));
45 assertTrue(n.getKeys().size() == 1);
46
47 assertTrue(n.keySet().size() == 1);
48 assertTrue(n.keySet().contains("akey"));
49 }
50
51 /**
52 * Adds two tags to an empty node and test the query and get methods.
53 */
54 @Test
55 public void put2() {
56 Node n = new Node();
57 n.put("key.1", "value.1");
58 n.put("key.2", "value.2");
59 assertTrue(n.get("key.1").equals("value.1"));
60 assertTrue(n.get("key.2").equals("value.2"));
61 assertTrue(n.getKeys().size() == 2);
62 assertTrue(n.hasKeys());
63 assertTrue(n.hasKey("key.1"));
64 assertTrue(n.hasKey("key.2"));
65 assertTrue(!n.hasKey("nosuchkey"));
66 }
67
68 /**
69 * Removes tags from a node with two tags and test the state of the node.
70 */
71 @Test
72 public void remove() {
73 Node n = new Node();
74 n.put("key.1", "value.1");
75 n.put("key.2", "value.2");
76
77 n.remove("nosuchkey"); // should work
78 assertTrue(n.getKeys().size() == 2); // still 2 tags ?
79
80 n.remove("key.1");
81 assertTrue(n.getKeys().size() == 1);
82 assertTrue(!n.hasKey("key.1"));
83 assertTrue(n.get("key.1") == null);
84 assertTrue(n.hasKey("key.2"));
85 assertTrue(n.get("key.2").equals("value.2"));
86
87 n.remove("key.2");
88 assertTrue(n.getKeys().size() == 0);
89 assertTrue(!n.hasKey("key.1"));
90 assertTrue(n.get("key.1") == null);
91 assertTrue(!n.hasKey("key.2"));
92 assertTrue(n.get("key.2") == null);
93 }
94
95 /**
96 * Removes all tags from a node.
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 @Test
132 public void hasEqualSemanticAttributes_2() {
133 Node n1 = new Node(1);
134 n1.setCoor(new LatLon(0,0));
135 n1.put("key.1", "value.1");
136 n1.put("key.2", "value.3");
137
138 Node n2 = new Node(1);
139 n2.setCoor(new LatLon(0,0));
140 n2.put("key.1", "value.1");
141 n2.put("key.2", "value.4");
142
143 assertTrue(!n1.hasEqualSemanticAttributes(n2));
144 }
145}
Note: See TracBrowser for help on using the repository browser.