-
-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathJsonIgnorePropertiesTest.java
More file actions
177 lines (146 loc) · 6.33 KB
/
JsonIgnorePropertiesTest.java
File metadata and controls
177 lines (146 loc) · 6.33 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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 JsonIgnoreProperties.Value}
* instances for overrides
*/
public class JsonIgnorePropertiesTest
extends AnnotationTestUtil
{
@JsonIgnoreProperties(value={ "foo", "bar" }, ignoreUnknown=true)
private final static class Bogus {
}
private final JsonIgnoreProperties.Value EMPTY = JsonIgnoreProperties.Value.empty();
@Test
public void testEmpty() {
// ok to try to create from null; gives empty
assertSame(EMPTY, JsonIgnoreProperties.Value.from(null));
assertEquals(0, EMPTY.getIgnored().size());
assertFalse(EMPTY.getAllowGetters());
assertFalse(EMPTY.getAllowSetters());
}
@Test
public void testEquality() {
assertEquals(EMPTY, EMPTY);
// empty has "merge" set to 'true' so:
assertSame(EMPTY, EMPTY.withMerge());
JsonIgnoreProperties.Value v = EMPTY.withoutMerge();
assertEquals(v, v);
assertFalse(EMPTY.equals(v));
assertFalse(v.equals(EMPTY));
}
@Test
public void testFromAnnotation() throws Exception
{
JsonIgnoreProperties.Value v = JsonIgnoreProperties.Value.from(
Bogus.class.getAnnotation(JsonIgnoreProperties.class));
assertNotNull(v);
assertFalse(v.getMerge());
assertFalse(v.getAllowGetters());
assertFalse(v.getAllowSetters());
Set<String> ign = v.getIgnored();
assertEquals(2, v.getIgnored().size());
assertEquals(_set("foo", "bar"), ign);
// Let's also verify JDK serializability
byte[] b = jdkSerialize(v);
JsonIgnoreProperties.Value deser = jdkDeserialize(b);
assertEquals(v, deser);
}
@Test
public void testFactories() {
assertSame(EMPTY, JsonIgnoreProperties.Value.forIgnoreUnknown(false));
assertSame(EMPTY, JsonIgnoreProperties.Value.forIgnoredProperties());
assertSame(EMPTY, JsonIgnoreProperties.Value.forIgnoredProperties(Collections.<String>emptySet()));
JsonIgnoreProperties.Value v = JsonIgnoreProperties.Value.forIgnoredProperties("a", "b");
assertEquals(_set("a", "b"), v.getIgnored());
JsonIgnoreProperties.Value vser = v.withAllowGetters();
assertTrue(vser.getAllowGetters());
assertFalse(vser.getAllowSetters());
assertEquals(_set("a", "b"), vser.getIgnored());
assertEquals(_set("a", "b"), vser.findIgnoredForDeserialization());
assertEquals(_set(), vser.findIgnoredForSerialization());
JsonIgnoreProperties.Value vdeser = v.withAllowSetters();
assertFalse(vdeser.getAllowGetters());
assertTrue(vdeser.getAllowSetters());
assertEquals(_set("a", "b"), vdeser.getIgnored());
assertEquals(_set(), vdeser.findIgnoredForDeserialization());
assertEquals(_set("a", "b"), vdeser.findIgnoredForSerialization());
}
@Test
public void testMutantFactories()
{
assertEquals(2, EMPTY.withIgnored("a", "b").getIgnored().size());
assertEquals(1, EMPTY.withIgnored(Collections.singleton("x")).getIgnored().size());
assertEquals(0, EMPTY.withIgnored((Set<String>) null).getIgnored().size());
assertTrue(EMPTY.withIgnoreUnknown().getIgnoreUnknown());
assertFalse(EMPTY.withoutIgnoreUnknown().getIgnoreUnknown());
assertTrue(EMPTY.withAllowGetters().getAllowGetters());
assertFalse(EMPTY.withoutAllowGetters().getAllowGetters());
assertTrue(EMPTY.withAllowSetters().getAllowSetters());
assertFalse(EMPTY.withoutAllowSetters().getAllowSetters());
assertTrue(EMPTY.withMerge().getMerge());
assertFalse(EMPTY.withoutMerge().getMerge());
}
@Test
public void testSimpleMerge()
{
JsonIgnoreProperties.Value v1 = EMPTY.withIgnoreUnknown().withAllowGetters();
JsonIgnoreProperties.Value v2a = EMPTY.withMerge()
.withIgnored("a");
JsonIgnoreProperties.Value v2b = EMPTY.withoutMerge();
// when merging, should just have union of things
JsonIgnoreProperties.Value v3a = v1.withOverrides(v2a);
assertEquals(Collections.singleton("a"), v3a.getIgnored());
assertTrue(v3a.getIgnoreUnknown());
assertTrue(v3a.getAllowGetters());
assertFalse(v3a.getAllowSetters());
// when NOT merging, simply replacing values
JsonIgnoreProperties.Value v3b = JsonIgnoreProperties.Value.merge(v1, v2b);
assertEquals(Collections.emptySet(), v3b.getIgnored());
assertFalse(v3b.getIgnoreUnknown());
assertFalse(v3b.getAllowGetters());
assertFalse(v3b.getAllowSetters());
// and effectively really just uses overrides as is
assertEquals(v2b, v3b);
assertSame(v2b, v2b.withOverrides(null));
assertSame(v2b, v2b.withOverrides(EMPTY));
}
@Test
public void testMergeIgnoreProperties()
{
JsonIgnoreProperties.Value v1 = EMPTY.withIgnored("a");
JsonIgnoreProperties.Value v2 = EMPTY.withIgnored("b");
JsonIgnoreProperties.Value v3 = EMPTY.withIgnored("c");
JsonIgnoreProperties.Value merged = JsonIgnoreProperties.Value.mergeAll(v1, v2, v3);
Set<String> all = merged.getIgnored();
assertEquals(3, all.size());
assertTrue(all.contains("a"));
assertTrue(all.contains("b"));
assertTrue(all.contains("c"));
}
@Test
public void testHashCodeIncludesIgnoredContents() {
JsonIgnoreProperties.Value v1 = EMPTY.withIgnored("a", "b");
JsonIgnoreProperties.Value v2 = EMPTY.withIgnored("c", "d");
assertNotEquals(v1, v2);
assertNotEquals(v1.hashCode(), v2.hashCode());
}
@Test
public void testToString() {
assertEquals(
"JsonIgnoreProperties.Value(ignored=[],ignoreUnknown=false,allowGetters=false,allowSetters=true,merge=true)",
EMPTY.withAllowSetters()
.withMerge()
.toString());
int hash = EMPTY.hashCode();
// no real good way to test but...
if (hash == 0) {
fail("Should not get 0 for hash");
}
}
private Set<String> _set(String... args) {
return new LinkedHashSet<String>(Arrays.asList(args));
}
}