| | 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.assertTrue; |
| | 7 | import static org.junit.Assert.fail; |
| | 8 | |
| | 9 | import java.util.Arrays; |
| | 10 | import java.util.Collection; |
| | 11 | import java.util.Collections; |
| | 12 | import java.util.HashMap; |
| | 13 | import java.util.Iterator; |
| | 14 | import java.util.List; |
| | 15 | import java.util.Map; |
| | 16 | import java.util.Set; |
| | 17 | import java.util.stream.Collectors; |
| | 18 | import java.util.stream.Stream; |
| | 19 | |
| | 20 | import org.junit.Ignore; |
| | 21 | import org.junit.Rule; |
| | 22 | import org.junit.Test; |
| | 23 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| | 24 | |
| | 25 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| | 26 | |
| | 27 | /** |
| | 28 | * Tests of {@link TagCollection}. |
| | 29 | * @author Michael Zangl |
| | 30 | * @since xxx |
| | 31 | */ |
| | 32 | public class TagCollectionTest { |
| | 33 | private final Tag tagA = new Tag("k", "v"); |
| | 34 | private final Tag tagB = new Tag("k", "b"); |
| | 35 | private final Tag tagC = new Tag("k2", "b"); |
| | 36 | private final Tag tagD = new Tag("k3", "c"); |
| | 37 | private final Tag tagEmpty = new Tag("k", ""); |
| | 38 | private final Tag tagNullKey = new Tag(null, "b"); |
| | 39 | private final Tag tagNullValue = new Tag("k2", null); |
| | 40 | |
| | 41 | /** |
| | 42 | * We need prefs for using primitives |
| | 43 | */ |
| | 44 | @Rule |
| | 45 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| | 46 | public JOSMTestRules test = new JOSMTestRules().preferences(); |
| | 47 | |
| | 48 | private void assertTagCounts(TagCollection collection, int a, int b, int c, int d) { |
| | 49 | assertEquals(a, collection.getTagOccurence(tagA)); |
| | 50 | assertEquals(b, collection.getTagOccurence(tagB)); |
| | 51 | assertEquals(c, collection.getTagOccurence(tagC)); |
| | 52 | assertEquals(d, collection.getTagOccurence(tagD)); |
| | 53 | } |
| | 54 | |
| | 55 | /** |
| | 56 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#from(org.openstreetmap.josm.data.osm.Tagged)}. |
| | 57 | */ |
| | 58 | @Test |
| | 59 | public void testFromTagged() { |
| | 60 | TagCollection c = TagCollection.from(tagA); |
| | 61 | assertTagCounts(c, 1, 0, 0, 0); |
| | 62 | |
| | 63 | NodeData p1 = new NodeData(); |
| | 64 | p1.put(tagA); |
| | 65 | p1.put(tagC); |
| | 66 | TagCollection d = TagCollection.from(p1); |
| | 67 | assertTagCounts(d, 1, 0, 1, 0); |
| | 68 | |
| | 69 | TagCollection e = TagCollection.from((Tagged) null); |
| | 70 | assertTagCounts(e, 0, 0, 0, 0); |
| | 71 | } |
| | 72 | |
| | 73 | /** |
| | 74 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#from(java.util.Map)}. |
| | 75 | */ |
| | 76 | @Test |
| | 77 | public void testFromMapOfStringString() { |
| | 78 | TagCollection c = TagCollection.from(tagA.getKeys()); |
| | 79 | assertTagCounts(c, 1, 0, 0, 0); |
| | 80 | |
| | 81 | HashMap<String, String> map = new HashMap<>(); |
| | 82 | map.putAll(tagA.getKeys()); |
| | 83 | map.putAll(tagC.getKeys()); |
| | 84 | TagCollection d = TagCollection.from(map); |
| | 85 | assertTagCounts(d, 1, 0, 1, 0); |
| | 86 | |
| | 87 | TagCollection e = TagCollection.from((Map<String, String>) null); |
| | 88 | assertTagCounts(e, 0, 0, 0, 0); |
| | 89 | } |
| | 90 | |
| | 91 | /** |
| | 92 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#unionOfAllPrimitives(java.util.Collection)}. |
| | 93 | */ |
| | 94 | @Test |
| | 95 | public void testUnionOfAllPrimitivesCollectionOfQextendsTagged() { |
| | 96 | TagCollection c = TagCollection.unionOfAllPrimitives(Arrays.asList(tagA)); |
| | 97 | assertEquals(1, c.getTagOccurence(tagA)); |
| | 98 | |
| | 99 | TagCollection d = TagCollection.unionOfAllPrimitives(Arrays.asList(tagA, tagC)); |
| | 100 | assertTagCounts(d, 1, 0, 1, 0); |
| | 101 | |
| | 102 | TagCollection e = TagCollection.unionOfAllPrimitives((Collection<? extends Tagged>) null); |
| | 103 | assertTagCounts(e, 0, 0, 0, 0); |
| | 104 | |
| | 105 | TagCollection f = TagCollection.unionOfAllPrimitives(Arrays.<Tagged>asList()); |
| | 106 | assertTagCounts(f, 0, 0, 0, 0); |
| | 107 | |
| | 108 | TagCollection g = TagCollection.unionOfAllPrimitives(Arrays.asList(tagA, tagC, tagC, null)); |
| | 109 | assertTagCounts(g, 1, 0, 2, 0); |
| | 110 | } |
| | 111 | |
| | 112 | /** |
| | 113 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#commonToAllPrimitives(java.util.Collection)}. |
| | 114 | */ |
| | 115 | @Test |
| | 116 | @Ignore("TODO") |
| | 117 | public void testCommonToAllPrimitives() { |
| | 118 | fail("Not yet implemented"); |
| | 119 | } |
| | 120 | |
| | 121 | /** |
| | 122 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#unionOfAllPrimitives(org.openstreetmap.josm.data.osm.DataSet)}. |
| | 123 | */ |
| | 124 | @Test |
| | 125 | @Ignore("TODO") |
| | 126 | public void testUnionOfAllPrimitivesDataSet() { |
| | 127 | fail("Not yet implemented"); |
| | 128 | } |
| | 129 | |
| | 130 | /** |
| | 131 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#TagCollection()}. |
| | 132 | */ |
| | 133 | @Test |
| | 134 | public void testTagCollection() { |
| | 135 | TagCollection c = new TagCollection(); |
| | 136 | assertTagCounts(c, 0, 0, 0, 0); |
| | 137 | } |
| | 138 | |
| | 139 | /** |
| | 140 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#TagCollection(org.openstreetmap.josm.data.osm.TagCollection)}. |
| | 141 | */ |
| | 142 | @Test |
| | 143 | public void testTagCollectionTagCollection() { |
| | 144 | TagCollection blueprint = TagCollection.unionOfAllPrimitives(Arrays.asList(tagA, tagC, tagC)); |
| | 145 | TagCollection c = new TagCollection(blueprint); |
| | 146 | assertTagCounts(c, 1, 0, 2, 0); |
| | 147 | |
| | 148 | TagCollection d = new TagCollection((TagCollection) null); |
| | 149 | assertTagCounts(d, 0, 0, 0, 0); |
| | 150 | } |
| | 151 | |
| | 152 | /** |
| | 153 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#TagCollection(java.util.Collection)}. |
| | 154 | */ |
| | 155 | @Test |
| | 156 | public void testTagCollectionCollectionOfTag() { |
| | 157 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC, tagC)); |
| | 158 | assertTagCounts(c, 1, 0, 2, 0); |
| | 159 | |
| | 160 | TagCollection d = new TagCollection((Collection<Tag>) null); |
| | 161 | assertTagCounts(d, 0, 0, 0, 0); |
| | 162 | } |
| | 163 | |
| | 164 | /** |
| | 165 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#size()}. |
| | 166 | */ |
| | 167 | @Test |
| | 168 | public void testSize() { |
| | 169 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC, tagC)); |
| | 170 | assertEquals(2, c.size()); |
| | 171 | |
| | 172 | TagCollection d = new TagCollection(); |
| | 173 | assertEquals(0, d.size()); |
| | 174 | } |
| | 175 | |
| | 176 | /** |
| | 177 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#isEmpty()}. |
| | 178 | */ |
| | 179 | @Test |
| | 180 | public void testIsEmpty() { |
| | 181 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC, tagC)); |
| | 182 | assertFalse(c.isEmpty()); |
| | 183 | |
| | 184 | TagCollection d = new TagCollection(); |
| | 185 | assertTrue(d.isEmpty()); |
| | 186 | } |
| | 187 | |
| | 188 | /** |
| | 189 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#add(org.openstreetmap.josm.data.osm.Tag)}. |
| | 190 | */ |
| | 191 | @Test |
| | 192 | public void testAddTag() { |
| | 193 | TagCollection c = new TagCollection(); |
| | 194 | assertTagCounts(c, 0, 0, 0, 0); |
| | 195 | c.add(tagC); |
| | 196 | assertTagCounts(c, 0, 0, 1, 0); |
| | 197 | c.add(tagA); |
| | 198 | c.add(tagC); |
| | 199 | assertTagCounts(c, 1, 0, 2, 0); |
| | 200 | c.add((Tag) null); |
| | 201 | assertTagCounts(c, 1, 0, 2, 0); |
| | 202 | } |
| | 203 | |
| | 204 | /** |
| | 205 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#getTagOccurence(org.openstreetmap.josm.data.osm.Tag)}. |
| | 206 | */ |
| | 207 | @Test |
| | 208 | public void testGetTagCount() { |
| | 209 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC, tagC)); |
| | 210 | assertEquals(2, c.getTagOccurence(tagC)); |
| | 211 | assertEquals(0, c.getTagOccurence(tagB)); |
| | 212 | assertEquals(0, c.getTagOccurence(tagNullKey)); |
| | 213 | assertEquals(0, c.getTagOccurence(tagNullValue)); |
| | 214 | } |
| | 215 | |
| | 216 | /** |
| | 217 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#add(java.util.Collection)}. |
| | 218 | */ |
| | 219 | @Test |
| | 220 | public void testAddCollectionOfTag() { |
| | 221 | TagCollection c = new TagCollection(); |
| | 222 | assertTagCounts(c, 0, 0, 0, 0); |
| | 223 | c.add(Arrays.asList(tagC)); |
| | 224 | assertTagCounts(c, 0, 0, 1, 0); |
| | 225 | c.add(Arrays.asList(tagA, tagC)); |
| | 226 | assertTagCounts(c, 1, 0, 2, 0); |
| | 227 | c.add(Collections.emptyList()); |
| | 228 | assertTagCounts(c, 1, 0, 2, 0); |
| | 229 | c.add((Collection<Tag>) null); |
| | 230 | assertTagCounts(c, 1, 0, 2, 0); |
| | 231 | } |
| | 232 | |
| | 233 | /** |
| | 234 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#add(org.openstreetmap.josm.data.osm.TagCollection)}. |
| | 235 | */ |
| | 236 | @Test |
| | 237 | public void testAddTagCollection() { |
| | 238 | TagCollection c = new TagCollection(); |
| | 239 | assertTagCounts(c, 0, 0, 0, 0); |
| | 240 | c.add(new TagCollection(Arrays.asList(tagC))); |
| | 241 | assertTagCounts(c, 0, 0, 1, 0); |
| | 242 | c.add(new TagCollection(Arrays.asList(tagA, tagC))); |
| | 243 | assertTagCounts(c, 1, 0, 2, 0); |
| | 244 | c.add(new TagCollection()); |
| | 245 | assertTagCounts(c, 1, 0, 2, 0); |
| | 246 | c.add((TagCollection) null); |
| | 247 | assertTagCounts(c, 1, 0, 2, 0); |
| | 248 | } |
| | 249 | |
| | 250 | /** |
| | 251 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#remove(org.openstreetmap.josm.data.osm.Tag)}. |
| | 252 | */ |
| | 253 | @Test |
| | 254 | public void testRemoveTag() { |
| | 255 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC, tagC)); |
| | 256 | assertTagCounts(c, 1, 0, 2, 0); |
| | 257 | c.remove(tagC); |
| | 258 | assertTagCounts(c, 1, 0, 0, 0); |
| | 259 | c.remove(tagB); |
| | 260 | assertTagCounts(c, 1, 0, 0, 0); |
| | 261 | c.remove((Tag) null); |
| | 262 | assertTagCounts(c, 1, 0, 0, 0); |
| | 263 | } |
| | 264 | |
| | 265 | /** |
| | 266 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#remove(java.util.Collection)}. |
| | 267 | */ |
| | 268 | @Test |
| | 269 | public void testRemoveCollectionOfTag() { |
| | 270 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC, tagC)); |
| | 271 | assertTagCounts(c, 1, 0, 2, 0); |
| | 272 | c.remove(Arrays.asList(tagC, tagB)); |
| | 273 | assertTagCounts(c, 1, 0, 0, 0); |
| | 274 | c.remove((Collection<Tag>) null); |
| | 275 | assertTagCounts(c, 1, 0, 0, 0); |
| | 276 | } |
| | 277 | |
| | 278 | /** |
| | 279 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#remove(org.openstreetmap.josm.data.osm.TagCollection)}. |
| | 280 | */ |
| | 281 | @Test |
| | 282 | public void testRemoveTagCollection() { |
| | 283 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC, tagC)); |
| | 284 | assertTagCounts(c, 1, 0, 2, 0); |
| | 285 | c.remove(new TagCollection(Arrays.asList(tagC, tagB))); |
| | 286 | assertTagCounts(c, 1, 0, 0, 0); |
| | 287 | c.remove(new TagCollection()); |
| | 288 | assertTagCounts(c, 1, 0, 0, 0); |
| | 289 | c.remove((TagCollection) null); |
| | 290 | assertTagCounts(c, 1, 0, 0, 0); |
| | 291 | } |
| | 292 | |
| | 293 | /** |
| | 294 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#removeByKey(java.lang.String)}. |
| | 295 | */ |
| | 296 | @Test |
| | 297 | public void testRemoveByKeyString() { |
| | 298 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagB, tagB, tagC)); |
| | 299 | assertTagCounts(c, 1, 2, 1, 0); |
| | 300 | c.removeByKey("k"); |
| | 301 | assertTagCounts(c, 0, 0, 1, 0); |
| | 302 | c.removeByKey((String) null); |
| | 303 | assertTagCounts(c, 0, 0, 1, 0); |
| | 304 | } |
| | 305 | |
| | 306 | /** |
| | 307 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#removeByKey(java.util.Collection)}. |
| | 308 | */ |
| | 309 | @Test |
| | 310 | public void testRemoveByKeyCollectionOfString() { |
| | 311 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagB, tagB, tagC, tagD)); |
| | 312 | assertTagCounts(c, 1, 2, 1, 1); |
| | 313 | c.removeByKey(Arrays.asList("k", "k2", null)); |
| | 314 | assertTagCounts(c, 0, 0, 0, 1); |
| | 315 | c.removeByKey((Collection<String>) null); |
| | 316 | assertTagCounts(c, 0, 0, 0, 1); |
| | 317 | } |
| | 318 | |
| | 319 | /** |
| | 320 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#contains(org.openstreetmap.josm.data.osm.Tag)}. |
| | 321 | */ |
| | 322 | @Test |
| | 323 | public void testContains() { |
| | 324 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagB, tagB)); |
| | 325 | assertTrue(c.contains(tagA)); |
| | 326 | assertTrue(c.contains(tagB)); |
| | 327 | assertFalse(c.contains(tagC)); |
| | 328 | } |
| | 329 | |
| | 330 | /** |
| | 331 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#containsAll(java.util.Collection)}. |
| | 332 | */ |
| | 333 | @Test |
| | 334 | public void testContainsAll() { |
| | 335 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagB, tagB)); |
| | 336 | assertTrue(c.containsAll(Arrays.asList(tagA, tagB))); |
| | 337 | assertFalse(c.containsAll(Arrays.asList(tagA, tagC))); |
| | 338 | assertTrue(c.containsAll(Arrays.asList())); |
| | 339 | assertFalse(c.containsAll(null)); |
| | 340 | } |
| | 341 | |
| | 342 | /** |
| | 343 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#containsAllKeys(java.util.Collection)}. |
| | 344 | */ |
| | 345 | @Test |
| | 346 | public void testContainsAllKeys() { |
| | 347 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagB, tagC)); |
| | 348 | assertTrue(c.containsAllKeys(Arrays.asList("k", "k2"))); |
| | 349 | assertFalse(c.containsAllKeys(Arrays.asList("k", "k3"))); |
| | 350 | assertTrue(c.containsAllKeys(Arrays.asList())); |
| | 351 | assertFalse(c.containsAllKeys(null)); |
| | 352 | } |
| | 353 | |
| | 354 | /** |
| | 355 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#getNumTagsFor(java.lang.String)}. |
| | 356 | */ |
| | 357 | @Test |
| | 358 | public void testGetNumTagsFor() { |
| | 359 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagB, tagC)); |
| | 360 | assertEquals(2, c.getNumTagsFor("k")); |
| | 361 | assertEquals(1, c.getNumTagsFor("k2")); |
| | 362 | assertEquals(0, c.getNumTagsFor("k3")); |
| | 363 | } |
| | 364 | |
| | 365 | /** |
| | 366 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#hasTagsFor(java.lang.String)}. |
| | 367 | */ |
| | 368 | @Test |
| | 369 | public void testHasTagsFor() { |
| | 370 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagB, tagC)); |
| | 371 | assertTrue(c.hasTagsFor("k")); |
| | 372 | assertTrue(c.hasTagsFor("k2")); |
| | 373 | assertFalse(c.hasTagsFor("k3")); |
| | 374 | } |
| | 375 | |
| | 376 | /** |
| | 377 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#hasValuesFor(java.lang.String)}. |
| | 378 | */ |
| | 379 | @Test |
| | 380 | public void testHasValuesFor() { |
| | 381 | TagCollection c = new TagCollection(Arrays.asList(tagC, tagEmpty)); |
| | 382 | assertFalse(c.hasValuesFor("k")); |
| | 383 | assertTrue(c.hasValuesFor("k2")); |
| | 384 | assertFalse(c.hasValuesFor("k3")); |
| | 385 | } |
| | 386 | |
| | 387 | /** |
| | 388 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#hasUniqueNonEmptyValue(java.lang.String)}. |
| | 389 | */ |
| | 390 | @Test |
| | 391 | public void testHasUniqueNonEmptyValue() { |
| | 392 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC, tagEmpty)); |
| | 393 | assertTrue(c.hasUniqueNonEmptyValue("k")); |
| | 394 | assertTrue(c.hasUniqueNonEmptyValue("k2")); |
| | 395 | assertFalse(c.hasUniqueNonEmptyValue("k3")); |
| | 396 | |
| | 397 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagB, tagC, tagEmpty)); |
| | 398 | assertFalse(d.hasUniqueNonEmptyValue("k")); |
| | 399 | assertTrue(d.hasUniqueNonEmptyValue("k2")); |
| | 400 | assertFalse(d.hasUniqueNonEmptyValue("k3")); |
| | 401 | } |
| | 402 | |
| | 403 | /** |
| | 404 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#hasEmptyValue(java.lang.String)}. |
| | 405 | */ |
| | 406 | @Test |
| | 407 | public void testHasEmptyValue() { |
| | 408 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC, tagEmpty)); |
| | 409 | assertTrue(c.hasEmptyValue("k")); |
| | 410 | assertFalse(c.hasEmptyValue("k2")); |
| | 411 | assertFalse(c.hasEmptyValue("k3")); |
| | 412 | } |
| | 413 | |
| | 414 | /** |
| | 415 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#hasUniqueEmptyValue(java.lang.String)}. |
| | 416 | */ |
| | 417 | @Test |
| | 418 | public void testHasUniqueEmptyValue() { |
| | 419 | TagCollection c = new TagCollection(Arrays.asList(tagC, tagEmpty)); |
| | 420 | assertTrue(c.hasUniqueEmptyValue("k")); |
| | 421 | assertFalse(c.hasUniqueEmptyValue("k2")); |
| | 422 | assertFalse(c.hasUniqueEmptyValue("k3")); |
| | 423 | |
| | 424 | TagCollection d = new TagCollection(Arrays.asList()); |
| | 425 | assertFalse(d.hasUniqueEmptyValue("k")); |
| | 426 | assertFalse(d.hasUniqueEmptyValue("k2")); |
| | 427 | assertFalse(d.hasUniqueEmptyValue("k3")); |
| | 428 | } |
| | 429 | |
| | 430 | /** |
| | 431 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#getTagsFor(java.lang.String)}. |
| | 432 | */ |
| | 433 | @Test |
| | 434 | public void testGetTagsForString() { |
| | 435 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagB, tagC, tagEmpty)); |
| | 436 | TagCollection collection = d.getTagsFor("k"); |
| | 437 | assertTagCounts(collection, 1, 1, 0, 0); |
| | 438 | assertEquals(1, collection.getTagOccurence(tagEmpty)); |
| | 439 | } |
| | 440 | |
| | 441 | /** |
| | 442 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#getTagsFor(java.util.Collection)}. |
| | 443 | */ |
| | 444 | @Test |
| | 445 | public void testGetTagsForCollectionOfString() { |
| | 446 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagB, tagC, tagEmpty)); |
| | 447 | TagCollection collection = d.getTagsFor(Arrays.asList("k", "k2")); |
| | 448 | assertTagCounts(collection, 1, 1, 1, 0); |
| | 449 | assertEquals(1, collection.getTagOccurence(tagEmpty)); |
| | 450 | } |
| | 451 | |
| | 452 | /** |
| | 453 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#asSet()}. |
| | 454 | */ |
| | 455 | @Test |
| | 456 | public void testAsSet() { |
| | 457 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagB, tagC, tagC)); |
| | 458 | Set<Tag> set = d.asSet(); |
| | 459 | assertEquals(3, set.size()); |
| | 460 | assertTrue(set.contains(tagA)); |
| | 461 | assertTrue(set.contains(tagB)); |
| | 462 | assertTrue(set.contains(tagC)); |
| | 463 | } |
| | 464 | |
| | 465 | /** |
| | 466 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#asList()}. |
| | 467 | */ |
| | 468 | @Test |
| | 469 | public void testAsList() { |
| | 470 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagB, tagC, tagC)); |
| | 471 | List<Tag> set = d.asList(); |
| | 472 | assertEquals(3, set.size()); |
| | 473 | assertTrue(set.contains(tagA)); |
| | 474 | assertTrue(set.contains(tagB)); |
| | 475 | assertTrue(set.contains(tagC)); |
| | 476 | } |
| | 477 | |
| | 478 | /** |
| | 479 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#iterator()}. |
| | 480 | */ |
| | 481 | @Test |
| | 482 | public void testIterator() { |
| | 483 | TagCollection d = new TagCollection(Arrays.asList(tagA)); |
| | 484 | Iterator<Tag> it = d.iterator(); |
| | 485 | assertTrue(it.hasNext()); |
| | 486 | assertEquals(tagA, it.next()); |
| | 487 | assertFalse(it.hasNext()); |
| | 488 | } |
| | 489 | |
| | 490 | /** |
| | 491 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#getKeys()}. |
| | 492 | */ |
| | 493 | @Test |
| | 494 | public void testGetKeys() { |
| | 495 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagB, tagC, tagC)); |
| | 496 | Set<String> set = d.getKeys(); |
| | 497 | assertEquals(2, set.size()); |
| | 498 | assertTrue(set.contains("k")); |
| | 499 | assertTrue(set.contains("k2")); |
| | 500 | } |
| | 501 | |
| | 502 | /** |
| | 503 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#getKeysWithMultipleValues()}. |
| | 504 | */ |
| | 505 | @Test |
| | 506 | public void testGetKeysWithMultipleValues() { |
| | 507 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagB, tagC, tagC)); |
| | 508 | Set<String> set = d.getKeysWithMultipleValues(); |
| | 509 | assertEquals(1, set.size()); |
| | 510 | assertTrue(set.contains("k")); |
| | 511 | } |
| | 512 | |
| | 513 | /** |
| | 514 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#setUniqueForKey(org.openstreetmap.josm.data.osm.Tag)}. |
| | 515 | */ |
| | 516 | @Test |
| | 517 | public void testSetUniqueForKeyTag() { |
| | 518 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagA, tagB, tagC, tagC)); |
| | 519 | assertTagCounts(d, 2, 1, 2, 0); |
| | 520 | d.setUniqueForKey(tagA); |
| | 521 | assertTagCounts(d, 1, 0, 2, 0); |
| | 522 | } |
| | 523 | |
| | 524 | /** |
| | 525 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#setUniqueForKey(java.lang.String, java.lang.String)}. |
| | 526 | */ |
| | 527 | @Test |
| | 528 | public void testSetUniqueForKeyStringString() { |
| | 529 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagA, tagB, tagC, tagC)); |
| | 530 | assertTagCounts(d, 2, 1, 2, 0); |
| | 531 | d.setUniqueForKey(tagA.getKey(), tagA.getValue()); |
| | 532 | assertTagCounts(d, 1, 0, 2, 0); |
| | 533 | } |
| | 534 | |
| | 535 | /** |
| | 536 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#getValues()}. |
| | 537 | */ |
| | 538 | @Test |
| | 539 | public void testGetValues() { |
| | 540 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagA, tagB, tagC, tagEmpty)); |
| | 541 | Set<String> set = d.getValues(); |
| | 542 | assertEquals(3, set.size()); |
| | 543 | assertTrue(set.contains("v")); |
| | 544 | assertTrue(set.contains("b")); |
| | 545 | assertTrue(set.contains("")); |
| | 546 | } |
| | 547 | |
| | 548 | /** |
| | 549 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#getValues(java.lang.String)}. |
| | 550 | */ |
| | 551 | @Test |
| | 552 | public void testGetValuesString() { |
| | 553 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagA, tagC, tagEmpty)); |
| | 554 | Set<String> set = d.getValues("k"); |
| | 555 | assertEquals(2, set.size()); |
| | 556 | assertTrue(set.contains("v")); |
| | 557 | assertTrue(set.contains("")); |
| | 558 | } |
| | 559 | |
| | 560 | /** |
| | 561 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#isApplicableToPrimitive()}. |
| | 562 | */ |
| | 563 | @Test |
| | 564 | public void testIsApplicableToPrimitive() { |
| | 565 | TagCollection c = new TagCollection(); |
| | 566 | assertTrue(c.isApplicableToPrimitive()); |
| | 567 | TagCollection d = new TagCollection(Arrays.asList(tagA, tagA, tagC, tagEmpty)); |
| | 568 | assertFalse(d.isApplicableToPrimitive()); |
| | 569 | TagCollection e = new TagCollection(Arrays.asList(tagA, tagC)); |
| | 570 | assertTrue(e.isApplicableToPrimitive()); |
| | 571 | } |
| | 572 | |
| | 573 | /** |
| | 574 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#applyTo(org.openstreetmap.josm.data.osm.Tagged)}. |
| | 575 | */ |
| | 576 | @Test |
| | 577 | public void testApplyToTagged() { |
| | 578 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC)); |
| | 579 | NodeData tagged = new NodeData(); |
| | 580 | tagged.put("k", "x"); |
| | 581 | tagged.put("k3", "x"); |
| | 582 | c.applyTo(tagged); |
| | 583 | assertEquals("v", tagged.get("k")); |
| | 584 | assertEquals("b", tagged.get("k2")); |
| | 585 | assertEquals("x", tagged.get("k3")); |
| | 586 | TagCollection d = new TagCollection(Arrays.asList(tagEmpty)); |
| | 587 | d.applyTo(tagged); |
| | 588 | assertEquals(null, tagged.get("k")); |
| | 589 | } |
| | 590 | |
| | 591 | /** |
| | 592 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#applyTo(java.util.Collection)}. |
| | 593 | */ |
| | 594 | @Test |
| | 595 | public void testApplyToCollectionOfQextendsTagged() { |
| | 596 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC)); |
| | 597 | NodeData tagged = new NodeData(); |
| | 598 | NodeData tagged2 = new NodeData(); |
| | 599 | tagged2.put("k", "x"); |
| | 600 | tagged2.put("k3", "x"); |
| | 601 | c.applyTo(Arrays.asList(tagged, tagged2)); |
| | 602 | assertEquals("v", tagged.get("k")); |
| | 603 | assertEquals("b", tagged.get("k2")); |
| | 604 | assertEquals("v", tagged2.get("k")); |
| | 605 | assertEquals("b", tagged2.get("k2")); |
| | 606 | assertEquals("x", tagged2.get("k3")); |
| | 607 | } |
| | 608 | |
| | 609 | /** |
| | 610 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#replaceTagsOf(org.openstreetmap.josm.data.osm.Tagged)}. |
| | 611 | */ |
| | 612 | @Test |
| | 613 | public void testReplaceTagsOfTagged() { |
| | 614 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC)); |
| | 615 | NodeData tagged = new NodeData(); |
| | 616 | tagged.put("k", "x"); |
| | 617 | tagged.put("k3", "x"); |
| | 618 | c.replaceTagsOf(tagged); |
| | 619 | assertEquals("v", tagged.get("k")); |
| | 620 | assertEquals("b", tagged.get("k2")); |
| | 621 | assertEquals(null, tagged.get("k3")); |
| | 622 | } |
| | 623 | |
| | 624 | /** |
| | 625 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#replaceTagsOf(java.util.Collection)}. |
| | 626 | */ |
| | 627 | @Test |
| | 628 | public void testReplaceTagsOfCollectionOfQextendsTagged() { |
| | 629 | TagCollection c = new TagCollection(Arrays.asList(tagA, tagC)); |
| | 630 | NodeData tagged = new NodeData(); |
| | 631 | NodeData tagged2 = new NodeData(); |
| | 632 | tagged2.put("k", "x"); |
| | 633 | tagged2.put("k3", "x"); |
| | 634 | c.replaceTagsOf(Arrays.asList(tagged, tagged2)); |
| | 635 | assertEquals("v", tagged.get("k")); |
| | 636 | assertEquals("b", tagged.get("k2")); |
| | 637 | assertEquals("v", tagged2.get("k")); |
| | 638 | assertEquals("b", tagged2.get("k2")); |
| | 639 | assertEquals(null, tagged2.get("k3")); |
| | 640 | } |
| | 641 | |
| | 642 | /** |
| | 643 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#intersect(org.openstreetmap.josm.data.osm.TagCollection)}. |
| | 644 | */ |
| | 645 | @Test |
| | 646 | public void testIntersect() { |
| | 647 | TagCollection c1 = new TagCollection(Arrays.asList(tagA, tagC, tagD, tagEmpty)); |
| | 648 | TagCollection c2 = new TagCollection(Arrays.asList(tagA, tagB, tagD)); |
| | 649 | TagCollection c = c1.intersect(c2); |
| | 650 | assertEquals(2, c.getKeys().size()); |
| | 651 | assertEquals(1, c.getTagOccurence(tagA)); |
| | 652 | assertEquals(1, c.getTagOccurence(tagD)); |
| | 653 | } |
| | 654 | |
| | 655 | /** |
| | 656 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#minus(org.openstreetmap.josm.data.osm.TagCollection)}. |
| | 657 | */ |
| | 658 | @Test |
| | 659 | public void testMinus() { |
| | 660 | TagCollection c1 = new TagCollection(Arrays.asList(tagA, tagC, tagD, tagEmpty)); |
| | 661 | TagCollection c2 = new TagCollection(Arrays.asList(tagA, tagB, tagD)); |
| | 662 | TagCollection c = c1.minus(c2); |
| | 663 | assertEquals(2, c.getKeys().size()); |
| | 664 | assertEquals(1, c.getTagOccurence(tagC)); |
| | 665 | assertEquals(1, c.getTagOccurence(tagEmpty)); |
| | 666 | } |
| | 667 | |
| | 668 | /** |
| | 669 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#union(org.openstreetmap.josm.data.osm.TagCollection)}. |
| | 670 | */ |
| | 671 | @Test |
| | 672 | public void testUnion() { |
| | 673 | TagCollection c1 = new TagCollection(Arrays.asList(tagA, tagC, tagD, tagEmpty)); |
| | 674 | TagCollection c2 = new TagCollection(Arrays.asList(tagA, tagB, tagD)); |
| | 675 | TagCollection c = c1.union(c2); |
| | 676 | assertEquals(2, c.getTagOccurence(tagA)); |
| | 677 | assertEquals(1, c.getTagOccurence(tagB)); |
| | 678 | assertEquals(1, c.getTagOccurence(tagC)); |
| | 679 | assertEquals(2, c.getTagOccurence(tagD)); |
| | 680 | assertEquals(1, c.getTagOccurence(tagEmpty)); |
| | 681 | } |
| | 682 | |
| | 683 | /** |
| | 684 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#emptyTagsForKeysMissingIn(org.openstreetmap.josm.data.osm.TagCollection)}. |
| | 685 | */ |
| | 686 | @Test |
| | 687 | public void testEmptyTagsForKeysMissingIn() { |
| | 688 | TagCollection c1 = new TagCollection(Arrays.asList(tagA, tagC, tagD, tagEmpty)); |
| | 689 | TagCollection c2 = new TagCollection(Arrays.asList(tagA, tagB, tagD)); |
| | 690 | TagCollection c = c1.emptyTagsForKeysMissingIn(c2); |
| | 691 | assertEquals(2, c.getKeys().size()); |
| | 692 | assertEquals(1, c.getTagOccurence(new Tag(tagC.getKey(), ""))); |
| | 693 | assertEquals(1, c.getTagOccurence(tagEmpty)); |
| | 694 | } |
| | 695 | |
| | 696 | /** |
| | 697 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#getJoinedValues(java.lang.String)}. |
| | 698 | */ |
| | 699 | @Test |
| | 700 | public void testGetJoinedValues() { |
| | 701 | TagCollection c = new TagCollection(Arrays.asList(new Tag("k", "a"))); |
| | 702 | assertEquals("a", c.getJoinedValues("k")); |
| | 703 | TagCollection d = new TagCollection(Arrays.asList(new Tag("k", "a"), new Tag("k", "b"))); |
| | 704 | assertEquals("a;b", d.getJoinedValues("k")); |
| | 705 | TagCollection e = new TagCollection(Arrays.asList(new Tag("k", "b"), new Tag("k", "a"), new Tag("k", "b;a"))); |
| | 706 | assertEquals("b;a", e.getJoinedValues("k")); |
| | 707 | TagCollection f = new TagCollection(Arrays.asList(new Tag("k", "b"), new Tag("k", "a"), new Tag("k", "b"), new Tag("k", "c"), new Tag("k", "d"), new Tag("k", "a;b;c;d"))); |
| | 708 | assertEquals("a;b;c;d", f.getJoinedValues("k")); |
| | 709 | TagCollection g = new TagCollection(Arrays.asList(new Tag("k", "b"), new Tag("k", "a"), new Tag("k", "b"), new Tag("k", "c"), new Tag("k", "d"))); |
| | 710 | assertEquals("a;b;c;d", Stream.of(g.getJoinedValues("k").split(";")).sorted().collect(Collectors.joining(";"))); |
| | 711 | } |
| | 712 | |
| | 713 | /** |
| | 714 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#getSummedValues(java.lang.String)}. |
| | 715 | */ |
| | 716 | @Test |
| | 717 | public void testGetSummedValues() { |
| | 718 | TagCollection c = new TagCollection(Arrays.asList(new Tag("k", "10"), new Tag("k", "20"))); |
| | 719 | assertEquals("30", c.getSummedValues("k")); |
| | 720 | TagCollection d = new TagCollection(Arrays.asList(new Tag("k", "10"), new Tag("k", "10"))); |
| | 721 | assertEquals("10", d.getSummedValues("k")); |
| | 722 | TagCollection e = new TagCollection(Arrays.asList(new Tag("k", "10"), new Tag("k", "x"))); |
| | 723 | assertEquals("10", e.getSummedValues("k")); |
| | 724 | TagCollection f = new TagCollection(); |
| | 725 | assertEquals("0", f.getSummedValues("k")); |
| | 726 | } |
| | 727 | |
| | 728 | /** |
| | 729 | * Test method for {@link org.openstreetmap.josm.data.osm.TagCollection#toString()}. |
| | 730 | */ |
| | 731 | @Test |
| | 732 | @Ignore("TODO") |
| | 733 | public void testToString() { |
| | 734 | fail("Not yet implemented"); |
| | 735 | } |
| | 736 | } |