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