source: josm/trunk/test/unit/org/openstreetmap/josm/data/osm/ChangesetTest.java@ 12620

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

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

File size: 9.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNull;
7import static org.junit.Assert.assertTrue;
8import static org.openstreetmap.josm.data.osm.Changeset.MAX_CHANGESET_TAG_LENGTH;
9
10import java.util.Calendar;
11import java.util.Collection;
12import java.util.Date;
13import java.util.HashMap;
14import java.util.Map;
15
16import org.junit.Assert;
17import org.junit.Rule;
18import org.junit.Test;
19import org.openstreetmap.josm.data.Bounds;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.data.osm.visitor.Visitor;
22import org.openstreetmap.josm.gui.DefaultNameFormatter;
23import org.openstreetmap.josm.testutils.JOSMTestRules;
24import org.openstreetmap.josm.tools.Logging;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27
28/**
29 * Unit tests for class {@link Changeset}.
30 */
31public class ChangesetTest {
32
33 /**
34 * Setup test.
35 */
36 @Rule
37 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
38 public JOSMTestRules test = new JOSMTestRules();
39
40 /**
41 * Unit test of method {@link Changeset#setKeys}.
42 */
43 @Test
44 @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF_ALL_TARGETS_DANGEROUS")
45 public void testSetKeys() {
46 final Changeset cs = new Changeset();
47 // Cannot add null map => IllegalArgumentException
48 try {
49 cs.setKeys(null);
50 Assert.fail("Should have thrown an IllegalArgumentException as we gave a null argument.");
51 } catch (IllegalArgumentException e) {
52 Logging.trace(e);
53 // Was expected
54 }
55
56 // Add a map with no values
57 // => the key list is empty
58 Map<String, String> keys = new HashMap<>();
59
60 // Add a map with valid values : null and short texts
61 // => all the items are in the keys
62 keys.put("empty", null);
63 keys.put("test", "test");
64 cs.setKeys(keys);
65 Assert.assertEquals("Both valid keys should have been put in the ChangeSet.", 2, cs.getKeys().size());
66
67 // Add a map with too long values => IllegalArgumentException
68 keys = new HashMap<>();
69 StringBuilder b = new StringBuilder(MAX_CHANGESET_TAG_LENGTH + 1);
70 for (int i = 0; i < MAX_CHANGESET_TAG_LENGTH + 1; i++) {
71 b.append("x");
72 }
73 keys.put("test", b.toString());
74 try {
75 cs.setKeys(keys);
76 Assert.fail("Should have thrown an IllegalArgumentException as we gave a too long value.");
77 } catch (IllegalArgumentException e) {
78 Logging.trace(e);
79 // Was expected
80 }
81 }
82
83 /**
84 * Unit test of method {@link Changeset#compareTo}.
85 */
86 @Test
87 public void testCompareTo() {
88 Changeset cs1 = new Changeset(1);
89 Changeset cs2 = new Changeset(2);
90 assertEquals(0, cs1.compareTo(cs1));
91 assertEquals(-1, cs1.compareTo(cs2));
92 assertEquals(+1, cs2.compareTo(cs1));
93 }
94
95 /**
96 * Unit test of method {@link Changeset#getBounds}.
97 */
98 @Test
99 public void testGetBounds() {
100 Changeset cs = new Changeset();
101 assertNull(cs.getBounds());
102 cs.setMin(LatLon.NORTH_POLE);
103 cs.setMax(null);
104 assertNull(cs.getBounds());
105 cs.setMin(null);
106 cs.setMax(LatLon.SOUTH_POLE);
107 assertNull(cs.getBounds());
108 cs.setMin(LatLon.NORTH_POLE);
109 cs.setMax(LatLon.SOUTH_POLE);
110 assertEquals(new Bounds(90, 0, -90, 0), cs.getBounds());
111 }
112
113 /**
114 * Unit test of methods {@link Changeset#getContent} / {@link Changeset#setContent} / {@link Changeset#hasContent}.
115 */
116 @Test
117 public void testGetSetHasContent() {
118 Changeset cs = new Changeset();
119 assertNull(cs.getContent());
120 assertFalse(cs.hasContent());
121 ChangesetDataSet cds = new ChangesetDataSet();
122 cs.setContent(cds);
123 assertEquals(cds, cs.getContent());
124 assertTrue(cs.hasContent());
125 }
126
127 /**
128 * Unit test of method {@link Changeset#getDisplayName}.
129 */
130 @Test
131 public void testGetDisplayName() {
132 assertEquals("Changeset 0", new Changeset().getDisplayName(DefaultNameFormatter.getInstance()));
133 }
134
135 /**
136 * Unit test of method {@link Changeset#getName}.
137 */
138 @Test
139 public void testGetName() {
140 assertEquals("changeset 0", new Changeset().getName());
141 }
142
143 private static Date yesterday() {
144 final Calendar cal = Calendar.getInstance();
145 cal.add(Calendar.DATE, -1);
146 return cal.getTime();
147 }
148
149 /**
150 * Unit test of method {@link Changeset#hasEqualSemanticAttributes}.
151 */
152 @Test
153 public void testHasEqualSemanticAttributes() {
154 Changeset cs1 = new Changeset();
155 Changeset cs2 = new Changeset();
156 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
157 assertFalse(cs1.hasEqualSemanticAttributes(null));
158 // Closed At
159 cs1.setClosedAt(null);
160 cs2.setClosedAt(new Date());
161 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
162 cs1.setClosedAt(yesterday());
163 cs2.setClosedAt(new Date());
164 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
165 cs1.setClosedAt(new Date());
166 cs2.setClosedAt(new Date());
167 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
168 // Created At
169 cs1.setCreatedAt(null);
170 cs2.setCreatedAt(new Date());
171 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
172 cs1.setCreatedAt(yesterday());
173 cs2.setCreatedAt(new Date());
174 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
175 cs1.setCreatedAt(new Date());
176 cs2.setCreatedAt(new Date());
177 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
178 // Id
179 cs1.setId(1);
180 cs2.setId(2);
181 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
182 cs1.setId(1);
183 cs2.setId(1);
184 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
185 // Max
186 cs1.setMax(null);
187 cs2.setMax(null);
188 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
189 cs1.setMax(null);
190 cs2.setMax(LatLon.NORTH_POLE);
191 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
192 cs1.setMax(LatLon.SOUTH_POLE);
193 cs2.setMax(LatLon.NORTH_POLE);
194 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
195 cs1.setMax(LatLon.SOUTH_POLE);
196 cs2.setMax(LatLon.SOUTH_POLE);
197 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
198 // Min
199 cs1.setMin(null);
200 cs2.setMin(null);
201 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
202 cs1.setMin(null);
203 cs2.setMin(LatLon.SOUTH_POLE);
204 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
205 cs1.setMin(LatLon.NORTH_POLE);
206 cs2.setMin(LatLon.SOUTH_POLE);
207 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
208 cs1.setMin(LatLon.NORTH_POLE);
209 cs2.setMin(LatLon.NORTH_POLE);
210 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
211 // Open
212 cs1.setOpen(false);
213 cs2.setOpen(true);
214 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
215 cs1.setOpen(false);
216 cs2.setOpen(false);
217 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
218 // Tags
219 Map<String, String> tags = new HashMap<>();
220 tags.put("foo", "bar");
221 cs2.setKeys(tags);
222 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
223 cs1.setKeys(new HashMap<>(tags));
224 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
225 // User
226 cs1.setUser(null);
227 cs2.setUser(User.createLocalUser("foo"));
228 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
229 cs1.setUser(null);
230 cs2.setUser(null);
231 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
232 cs1.setUser(User.createLocalUser("foo"));
233 cs2.setUser(User.createLocalUser("foo"));
234 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
235 // Comment count
236 cs1.setCommentsCount(1);
237 cs2.setCommentsCount(2);
238 assertFalse(cs1.hasEqualSemanticAttributes(cs2));
239 cs1.setCommentsCount(1);
240 cs2.setCommentsCount(1);
241 assertTrue(cs1.hasEqualSemanticAttributes(cs2));
242 }
243
244 /**
245 * Unit test of methods {@link Changeset#keySet} / {@link Changeset#put} / {@link Changeset#remove} / {@link Changeset#removeAll}.
246 */
247 @Test
248 public void testKeySet() {
249 Changeset cs = new Changeset();
250 assertTrue(cs.keySet().isEmpty());
251 Map<String, String> tags = new HashMap<>();
252 tags.put("foo", "bar");
253 cs.setKeys(tags);
254 Collection<String> set = cs.keySet();
255 assertEquals(1, set.size());
256 assertEquals("foo", set.iterator().next());
257 cs.remove("foo");
258 assertTrue(cs.keySet().isEmpty());
259 cs.put("foo", "bar");
260 cs.put("bar", "foo");
261 assertEquals(2, cs.keySet().size());
262 cs.removeAll();
263 assertTrue(cs.keySet().isEmpty());
264 }
265
266 private static class CsVisitor implements Visitor {
267
268 boolean visited;
269
270 @Override
271 public void visit(Node n) {
272 // Do nothing
273 }
274
275 @Override
276 public void visit(Way w) {
277 // Do nothing
278 }
279
280 @Override
281 public void visit(Relation r) {
282 // Do nothing
283 }
284
285 @Override
286 public void visit(Changeset cs) {
287 visited = true;
288 }
289 }
290
291 /**
292 * Unit test of method {@link Changeset#visit}.
293 */
294 @Test
295 public void testVisit() {
296 CsVisitor v = new CsVisitor();
297 new Changeset().visit(v);
298 assertTrue(v.visited);
299 }
300}
Note: See TracBrowser for help on using the repository browser.