aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/processing/request/test/PropertyMapTestCase.java
blob: 925b122429de63ff505aa5230635d7385ab5c8fb (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.processing.request.test;

import com.yahoo.lang.PublicCloneable;
import com.yahoo.processing.request.properties.PropertyMap;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author  bratseth
 */
public class PropertyMapTestCase {

    @Test
    void testObjectCloning() {
        PropertyMap map = new PropertyMap();
        map.set("clonable", new ClonableObject());
        map.set("publicClonable", new PublicClonableObject());
        map.set("nonclonable", new NonClonableObject());
        map.set("clonableArray", new ClonableObject[]{new ClonableObject()});
        map.set("publicClonableArray", new ClonableObject[]{new ClonableObject()});
        map.set("nonclonableArray", new NonClonableObject[]{new NonClonableObject()});
        map.set("clonableList", Collections.singletonList(new ClonableObject()));
        map.set("nonclonableList", Collections.singletonList(new NonClonableObject()));
        assertNotNull(map.get("clonable"));
        assertNotNull(map.get("nonclonable"));

        PropertyMap mapClone = map.clone();
        assertNotSame(map.get("clonable"), mapClone.get("clonable"));
        assertNotSame(map.get("publicClonable"), mapClone.get("publicClonable"));
        assertEquals(map.get("nonclonable"), mapClone.get("nonclonable"));

        assertNotSame(map.get("clonableArray"), mapClone.get("clonableArray"));
        assertNotSame(first(map.get("clonableArray")), first(mapClone.get("clonableArray")));
        assertNotSame(map.get("publicClonableArray"), mapClone.get("publicClonableArray"));
        assertNotSame(first(map.get("publicClonableArray")), first(mapClone.get("publicClonableArray")));
        assertEquals(first(map.get("nonclonableArray")), first(mapClone.get("nonclonableArray")));
    }

    @Test
    void testArrayCloning() {
        PropertyMap map = new PropertyMap();
        byte[] byteArray = new byte[]{2, 4, 7};
        map.set("byteArray", byteArray);

        PropertyMap mapClone = map.clone();
        assertArrayEquals(byteArray, (byte[]) mapClone.get("byteArray"));
        assertNotSame(mapClone.get("byteArray"), byteArray, "Array was cloned");
    }

    private Object first(Object object) {
        if (object instanceof Object[])
            return ((Object[])object)[0];
        if (object instanceof List)
            return ((List<?>)object).get(0);
        throw new IllegalArgumentException();
    }

    public static class ClonableObject implements Cloneable {

        @Override
        public ClonableObject clone() {
            try {
                return (ClonableObject)super.clone();
            }
            catch (CloneNotSupportedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    public static class PublicClonableObject implements PublicCloneable<PublicClonableObject> {

        @Override
        public PublicClonableObject clone() {
            try {
                return (PublicClonableObject)super.clone();
            }
            catch (CloneNotSupportedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    private static class NonClonableObject {

    }


}