1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau;
18
19 import static org.apache.juneau.TestUtils.*;
20 import static org.junit.jupiter.api.Assertions.*;
21
22 import org.apache.juneau.annotation.*;
23 import org.apache.juneau.json.*;
24 import org.apache.juneau.marshaller.*;
25 import org.junit.jupiter.api.*;
26
27 class ReadWriteOnlyProperties_Test extends TestBase {
28
29
30
31
32
33 public static class A {
34 @Beanp(ro="true")
35 public int f1;
36
37 @Beanp(wo="true")
38 public int f2;
39
40 static A create() {
41 var x = new A();
42 x.f1 = 1;
43 x.f2 = 2;
44 return x;
45 }
46 }
47
48 @Test void a01_beanpOnPrimitiveFields_serializer() {
49 assertJson("{f1:1}", A.create());
50 }
51
52 @Test void a02_beanpOnPrimitiveFields_parser() {
53 var x = Json5.DEFAULT.read("{f1:1,f2:2}", A.class);
54 assertEquals(0, x.f1);
55 assertEquals(2, x.f2);
56 }
57
58
59
60
61
62 @Bean(readOnlyProperties="f1", writeOnlyProperties="f2")
63 public static class B {
64 @Beanp(ro="true") public int f1;
65 @Beanp(wo="true") public int f2;
66
67 static B create() {
68 var x = new B();
69 x.f1 = 1;
70 x.f2 = 2;
71 return x;
72 }
73 }
74
75 @Test void b01_beanAnnotation_serializer() {
76 assertJson("{f1:1}", B.create());
77 }
78
79 @Test void b02_beanAnnotationParser() {
80 var x = JsonParser.DEFAULT.copy().applyAnnotations(BcConfig.class).build().parse("{f1:1,f2:2}", Bc.class);
81 assertEquals(0, x.f1);
82 assertEquals(2, x.f2);
83 }
84
85 @Bean(on="Dummy1", readOnlyProperties="f1", writeOnlyProperties="f2")
86 @Bean(on="Bc", readOnlyProperties="f1", writeOnlyProperties="f2")
87 @Bean(on="Dummy2", readOnlyProperties="f1", writeOnlyProperties="f2")
88 @Beanp(on="Bc.f1", ro="true")
89 @Beanp(on="Bc.f2", wo="true")
90 private static class BcConfig {}
91
92 public static class Bc {
93 public int f1;
94 public int f2;
95
96 static Bc create() {
97 var x = new Bc();
98 x.f1 = 1;
99 x.f2 = 2;
100 return x;
101 }
102 }
103
104 @Test void b01_beanAnnotation_serializer_usingConfig() {
105 assertJson("{f1:1}", B.create());
106 }
107
108 @Test void b02_beanAnnotationParser_usingConfig() throws Exception {
109 var x = Json5.DEFAULT.read("{f1:1,f2:2}", B.class);
110 assertEquals(0, x.f1);
111 assertEquals(2, x.f2);
112 }
113
114
115
116
117
118 public static class C {
119 public int f1;
120 public int f2;
121
122 static C create() {
123 var x = new C();
124 x.f1 = 1;
125 x.f2 = 2;
126 return x;
127 }
128 }
129
130 @Test void c01_beanContext_serializer() {
131 var sw = Json5Serializer.DEFAULT.copy()
132 .beanPropertiesReadOnly(C.class.getName(), "f1")
133 .beanPropertiesWriteOnly(C.class.getName(), "f2")
134 .build();
135 assertEquals("{f1:1}", sw.toString(C.create()));
136
137 sw = Json5Serializer.DEFAULT.copy()
138 .beanPropertiesReadOnly("ReadWriteOnlyProperties_Test$C", "f1")
139 .beanPropertiesWriteOnly("ReadWriteOnlyProperties_Test$C", "f2")
140 .build();
141 assertEquals("{f1:1}", sw.toString(C.create()));
142
143 sw = Json5Serializer.DEFAULT.copy()
144 .beanPropertiesReadOnly(C.class, "f1")
145 .beanPropertiesWriteOnly(C.class, "f2")
146 .build();
147 assertEquals("{f1:1}", sw.toString(C.create()));
148
149 sw = Json5Serializer.DEFAULT.copy()
150 .beanPropertiesReadOnly(map(C.class.getName(), "f1"))
151 .beanPropertiesWriteOnly(map(C.class.getName(), "f2"))
152 .build();
153 assertEquals("{f1:1}", sw.toString(C.create()));
154
155 sw = Json5Serializer.DEFAULT.copy()
156 .beanPropertiesReadOnly(map("ReadWriteOnlyProperties_Test$C", "f1"))
157 .beanPropertiesWriteOnly(map("ReadWriteOnlyProperties_Test$C", "f2"))
158 .build();
159 assertEquals("{f1:1}", sw.toString(C.create()));
160 }
161
162 @Test void c02_beanAnnotationParser() throws Exception {
163 var rp = JsonParser.DEFAULT.copy()
164 .beanPropertiesReadOnly(C.class.getName(), "f1")
165 .beanPropertiesWriteOnly(C.class.getName(), "f2")
166 .build();
167 var x = rp.parse("{f1:1,f2:2}", C.class);
168 assertEquals(0, x.f1);
169 assertEquals(2, x.f2);
170
171 rp = JsonParser.DEFAULT.copy()
172 .beanPropertiesReadOnly("ReadWriteOnlyProperties_Test$C", "f1")
173 .beanPropertiesWriteOnly("ReadWriteOnlyProperties_Test$C", "f2")
174 .build();
175 x = rp.parse("{f1:1,f2:2}", C.class);
176 assertEquals(0, x.f1);
177 assertEquals(2, x.f2);
178
179 rp = JsonParser.DEFAULT.copy()
180 .beanPropertiesReadOnly(C.class, "f1")
181 .beanPropertiesWriteOnly(C.class, "f2")
182 .build();
183 x = rp.parse("{f1:1,f2:2}", C.class);
184 assertEquals(0, x.f1);
185 assertEquals(2, x.f2);
186
187 rp = JsonParser.DEFAULT.copy()
188 .beanPropertiesReadOnly(map(C.class.getName(), "f1"))
189 .beanPropertiesWriteOnly(map(C.class.getName(), "f2"))
190 .build();
191 x = rp.parse("{f1:1,f2:2}", C.class);
192 assertEquals(0, x.f1);
193 assertEquals(2, x.f2);
194
195 rp = JsonParser.DEFAULT.copy()
196 .beanPropertiesReadOnly(map("ReadWriteOnlyProperties_Test$C", "f1"))
197 .beanPropertiesWriteOnly(map("ReadWriteOnlyProperties_Test$C", "f2"))
198 .build();
199 x = rp.parse("{f1:1,f2:2}", C.class);
200 assertEquals(0, x.f1);
201 assertEquals(2, x.f2);
202 }
203
204
205
206
207
208 @Bean(readOnlyProperties="*")
209 public static class D {
210 public int f1;
211 public int f2;
212
213 static D create() {
214 var x = new D();
215 x.f1 = 1;
216 x.f2 = 2;
217 return x;
218 }
219 }
220
221 @Test void d01_beanAnnotation_bproAll_serializer() {
222 assertJson("{f1:1,f2:2}", D.create());
223 }
224
225 @Test void d02_beanAnnotation_bproAll_Parser() {
226 var x = Json5.DEFAULT.read("{f1:1,f2:2}", D.class);
227 assertEquals(0, x.f1);
228 assertEquals(0, x.f2);
229 }
230
231 @Bean(on="Dc",readOnlyProperties="*")
232 private static class DcConfig {}
233
234 public static class Dc {
235 public int f1;
236 public int f2;
237
238 static Dc create() {
239 var x = new Dc();
240 x.f1 = 1;
241 x.f2 = 2;
242 return x;
243 }
244 }
245
246 @Test void d03_beanAnnotation_bproAll_serializer_usingConfig() {
247 assertSerialized(Dc.create(), Json5Serializer.DEFAULT.copy().applyAnnotations(DcConfig.class).build(), "{f1:1,f2:2}");
248 }
249
250 @Test void d04_beanAnnotation_bproAll_Parser_usingConfig() throws Exception {
251 var x = JsonParser.DEFAULT.copy().applyAnnotations(DcConfig.class).build().parse("{f1:1,f2:2}", Dc.class);
252 assertEquals(0, x.f1);
253 assertEquals(0, x.f2);
254 }
255
256
257
258
259
260 @Bean(writeOnlyProperties="*")
261 public static class E {
262 public int f1;
263 public int f2;
264
265 static E create() {
266 var x = new E();
267 x.f1 = 1;
268 x.f2 = 2;
269 return x;
270 }
271 }
272
273 @Test void e01_beanAnnotation_bpwoAll_serializer() {
274 assertJson("{}", E.create());
275 }
276
277 @Test void e02_beanAnnotation_bpwoAll_Parser() throws Exception {
278 var x = Json5.DEFAULT.read("{f1:1,f2:2}", E.class);
279 assertEquals(1, x.f1);
280 assertEquals(2, x.f2);
281 }
282
283 @Bean(on="Ec", writeOnlyProperties="*")
284 private static class EcConfig {}
285
286 public static class Ec {
287 public int f1;
288 public int f2;
289
290 static Ec create() {
291 var x = new Ec();
292 x.f1 = 1;
293 x.f2 = 2;
294 return x;
295 }
296 }
297
298 @Test void e03_beanAnnotation_bpwoAll_serializer_usingConfig() {
299 assertSerialized(E.create(), Json5Serializer.DEFAULT.copy().applyAnnotations(EcConfig.class).build(), "{}");
300 }
301
302 @Test void e04_beanAnnotation_bpwoAll_Parser_usingConfig() throws Exception {
303 var x = JsonParser.DEFAULT.copy().applyAnnotations(EcConfig.class).build().parse("{f1:1,f2:2}", Ec.class);
304 assertEquals(1, x.f1);
305 assertEquals(2, x.f2);
306 }
307 }