blob: 793a907b5d1bddb6044ed413ea4f8d7e0e9db645 (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.properties.test;
import com.yahoo.processing.request.properties.PropertyMap;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author bratseth
*/
public class PropertyMapTestCase {
@Test
void testCloning() {
PropertyMap map = new PropertyMap();
map.set("clonable", new ClonableObject());
map.set("nonclonable", new NonClonableObject());
map.set("clonableArray", new ClonableObject[]{new ClonableObject()});
map.set("nonclonableArray", new NonClonableObject[]{new NonClonableObject()});
map.set("clonableList", List.of(new ClonableObject()));
map.set("nonclonableList", List.of(new NonClonableObject()));
assertNotNull(map.get("clonable"));
assertNotNull(map.get("nonclonable"));
PropertyMap mapClone = map.clone();
assertNotSame(map.get("clonable"), mapClone.get("clonable"));
assertEquals(map.get("nonclonable"), mapClone.get("nonclonable"));
assertNotSame(map.get("clonableArray"), mapClone.get("clonableArray"));
assertNotSame(first(map.get("clonableArray")), first(mapClone.get("clonableArray")));
assertEquals(first(map.get("nonclonableArray")), first(mapClone.get("nonclonableArray")));
}
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);
}
}
}
private static class NonClonableObject {
}
}
|