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

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

fix #13271 - Make TagCollection count the occurence of tags (patch by michael2402) - gsoc-core

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