source: josm/trunk/test/unit/org/openstreetmap/josm/data/osm/TagCollectionTest.java@ 18037

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

fix #21064 - Add JUnit 5 extension for preferences (patch by taylor.smock)

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