aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java
blob: fcafb4c615288357f4cc44d35957095f0e09c885 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.stream;

import com.yahoo.stream.CustomCollectors.DuplicateKeyException;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static com.yahoo.stream.CustomCollectors.toCustomMap;
import static com.yahoo.stream.CustomCollectors.toLinkedMap;
import static java.util.function.Function.identity;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * @author gjoranv
 */
public class CustomCollectorsTest {

    @Test
    public void linked_map_collector_returns_map_with_insertion_order() {
        List<String> stringList = numberList();
        Map<String, String> orderedMap = stringList.stream().collect(toLinkedMap(identity(), identity()));
        int i = 0;
        for (String val : orderedMap.keySet()) {
            assertEquals(stringList.get(i), val);
            i++;
        }
    }

    @Test
    public void custom_map_collector_returns_map_from_given_supplier() {
        List<String> stringList = numberList();
        Map<String, String> customMap = stringList.stream().collect(toCustomMap(identity(), identity(), CustomHashMap::new));

        assertEquals(CustomHashMap.class, customMap.getClass());
    }

    @Test
    public void custom_map_collector_throws_exception_upon_duplicate_keys() {
        List<String> duplicates = List.of("same", "same");

        try {
            duplicates.stream().collect(toCustomMap(Function.identity(), Function.identity(), HashMap::new));
            fail();
        } catch (DuplicateKeyException e) {

        }
    }

    private static List<String> numberList() {
        return List.of("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");
    }

    private static class CustomHashMap<K,V> extends HashMap<K,V> {
        private static final long serialVersionUID = 1L;  // To avoid compiler warning
    }

}