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

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

unit tests - simplify assertions

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertNull;
6import static org.junit.Assert.assertSame;
7import static org.junit.Assert.assertTrue;
8
9import org.junit.BeforeClass;
10import org.junit.Test;
11import org.openstreetmap.josm.JOSMFixture;
12import 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 */
19public class OsmPrimitiveKeyHandlingTest {
20
21 @BeforeClass
22 public static void init() {
23 JOSMFixture.createUnitTestFixture().init();
24 }
25
26 /**
27 * test query and get methods on a node withouth keys
28 */
29 @Test
30 public void emptyNode() {
31 Node n = new Node();
32 assertSame(n.getKeys().size(), 0);
33 assertFalse(n.hasKeys());
34 assertFalse(n.hasKey("nosuchkey"));
35 assertTrue(n.keySet().isEmpty());
36
37 n.remove("nosuchkey"); // should work
38 }
39
40 /**
41 * Adds a tag to an empty node and test the query and get methods.
42 */
43 @Test
44 public void put() {
45 Node n = new Node();
46 n.put("akey", "avalue");
47 assertTrue(n.get("akey").equals("avalue"));
48 assertSame(n.getKeys().size(), 1);
49
50 assertSame(n.keySet().size(), 1);
51 assertTrue(n.keySet().contains("akey"));
52 }
53
54 /**
55 * Adds two tags to an empty node and test the query and get methods.
56 */
57 @Test
58 public void put2() {
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 assertSame(n.getKeys().size(), 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 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 assertSame(n.getKeys().size(), 2); // still 2 tags ?
82
83 n.remove("key.1");
84 assertSame(n.getKeys().size(), 1);
85 assertFalse(n.hasKey("key.1"));
86 assertNull(n.get("key.1"));
87 assertTrue(n.hasKey("key.2"));
88 assertTrue(n.get("key.2").equals("value.2"));
89
90 n.remove("key.2");
91 assertSame(n.getKeys().size(), 0);
92 assertFalse(n.hasKey("key.1"));
93 assertNull(n.get("key.1"));
94 assertFalse(n.hasKey("key.2"));
95 assertNull(n.get("key.2"));
96 }
97
98 /**
99 * Removes all tags from a node.
100 */
101 @Test
102 public void removeAll() {
103 Node n = new Node();
104
105 n.put("key.1", "value.1");
106 n.put("key.2", "value.2");
107
108 n.removeAll();
109 assertSame(n.getKeys().size(), 0);
110 }
111
112 /**
113 * Test hasEqualSemanticAttributes on two nodes whose identical tags are added
114 * in different orders.
115 */
116 @Test
117 public void hasEqualSemanticAttributes() {
118 Node n1 = new Node(1);
119 n1.setCoor(new LatLon(0,0));
120 n1.put("key.1", "value.1");
121 n1.put("key.2", "value.2");
122
123 Node n2 = new Node(1);
124 n2.setCoor(new LatLon(0,0));
125 n2.put("key.2", "value.2");
126 n2.put("key.1", "value.1");
127
128 assertTrue(n1.hasEqualSemanticAttributes(n2));
129 }
130
131 /**
132 * Test hasEqualSemanticAttributes on two nodes with different tags.
133 */
134 @Test
135 public void hasEqualSemanticAttributes_2() {
136 Node n1 = new Node(1);
137 n1.setCoor(new LatLon(0,0));
138 n1.put("key.1", "value.1");
139 n1.put("key.2", "value.3");
140
141 Node n2 = new Node(1);
142 n2.setCoor(new LatLon(0,0));
143 n2.put("key.1", "value.1");
144 n2.put("key.2", "value.4");
145
146 assertFalse(n1.hasEqualSemanticAttributes(n2));
147 }
148}
Note: See TracBrowser for help on using the repository browser.