-
-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathJsonIncludePropertiesTest.java
More file actions
122 lines (105 loc) · 4.45 KB
/
JsonIncludePropertiesTest.java
File metadata and controls
122 lines (105 loc) · 4.45 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.fasterxml.jackson.annotation;
import java.util.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* Tests to verify that it is possibly to merge {@link JsonIncludeProperties.Value}
* instances for overrides
*/
public class JsonIncludePropertiesTest
extends AnnotationTestUtil
{
@JsonIncludeProperties(value = {"foo", "bar"})
private final static class Bogus
{
}
@JsonIncludeProperties(order = OptBoolean.TRUE, value = {"id", "code", "name"})
private final static class Ordered
{
}
private final JsonIncludeProperties.Value ALL = JsonIncludeProperties.Value.all();
@Test
public void testAll()
{
assertSame(ALL, JsonIncludeProperties.Value.from(null));
assertNull(ALL.getIncluded());
assertNull(ALL.getOrdered());
assertEquals(ALL, ALL);
assertEquals("JsonIncludeProperties.Value(included=null,ordered=null)", ALL.toString());
assertEquals(0, ALL.hashCode());
}
@Test
public void testFromAnnotation()
{
JsonIncludeProperties.Value v = JsonIncludeProperties.Value.from(Bogus.class.getAnnotation(JsonIncludeProperties.class));
assertNotNull(v);
Set<String> included = v.getIncluded();
assertEquals(2, v.getIncluded().size());
assertEquals(_set("foo", "bar"), included);
assertNull(v.getOrdered());
String tmp = v.toString();
boolean test1 = tmp.equals("JsonIncludeProperties.Value(included=[foo, bar],ordered=null)");
boolean test2 = tmp.equals("JsonIncludeProperties.Value(included=[bar, foo],ordered=null)");
assertTrue(test1 || test2);
assertEquals(v, JsonIncludeProperties.Value.from(Bogus.class.getAnnotation(JsonIncludeProperties.class)));
// Let's also verify JDK serializability
byte[] b = jdkSerialize(v);
JsonIncludeProperties.Value deser = jdkDeserialize(b);
assertEquals(v, deser);
}
@Test
public void testWithOverridesAll() {
JsonIncludeProperties.Value v = JsonIncludeProperties.Value.from(Bogus.class.getAnnotation(JsonIncludeProperties.class));
v = v.withOverrides(ALL);
Set<String> included = v.getIncluded();
assertEquals(2, included.size());
assertEquals(_set("foo", "bar"), included);
}
@Test
public void testWithOverridesEmpty() {
JsonIncludeProperties.Value v = JsonIncludeProperties.Value.from(Bogus.class.getAnnotation(JsonIncludeProperties.class));
v = v.withOverrides(new JsonIncludeProperties.Value(Collections.<String>emptySet(), false));
Set<String> included = v.getIncluded();
assertEquals(0, included.size());
}
@Test
public void testWithOverridesMerge() {
JsonIncludeProperties.Value v = JsonIncludeProperties.Value.from(Bogus.class.getAnnotation(JsonIncludeProperties.class));
v = v.withOverrides(new JsonIncludeProperties.Value(_set("foo"), false));
Set<String> included = v.getIncluded();
assertEquals(1, included.size());
assertEquals(_set("foo"), included);
}
@Test
public void testFromAnnotationOrdered()
{
JsonIncludeProperties.Value v = JsonIncludeProperties.Value.from(
Ordered.class.getAnnotation(JsonIncludeProperties.class));
assertNotNull(v);
assertEquals(3, v.getIncluded().size());
assertEquals(Boolean.TRUE, v.getOrdered());
}
@Test
public void testHashCodeIncludesContents() {
JsonIncludeProperties.Value v1 = new JsonIncludeProperties.Value(_set("a", "b"), null);
JsonIncludeProperties.Value v2 = new JsonIncludeProperties.Value(_set("c", "d"), null);
assertNotEquals(v1, v2);
assertNotEquals(v1.hashCode(), v2.hashCode());
}
@Test
public void testOrderedEquality()
{
JsonIncludeProperties.Value v1 = new JsonIncludeProperties.Value(_set("a", "b"), Boolean.TRUE);
JsonIncludeProperties.Value v2 = new JsonIncludeProperties.Value(_set("a", "b"), Boolean.FALSE);
JsonIncludeProperties.Value v3 = new JsonIncludeProperties.Value(_set("a", "b"), Boolean.TRUE);
JsonIncludeProperties.Value v4 = new JsonIncludeProperties.Value(_set("a", "b"), null);
assertNotEquals(v1, v2);
assertNotEquals(v1, v4);
assertNotEquals(v2, v4);
assertEquals(v1, v3);
}
private Set<String> _set(String... args)
{
return new LinkedHashSet<String>(Arrays.asList(args));
}
}