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