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.a.rttests;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.util.*;
23  
24  import org.junit.jupiter.params.*;
25  import org.junit.jupiter.params.provider.*;
26  
27  /**
28   * Tests designed to serialize and parse objects to make sure we end up
29   * with the same objects for all serializers and parsers.
30   */
31  class OptionalObjects_RoundTripTest extends RoundTripTest_Base {
32  
33  	//------------------------------------------------------------------------------------------------------------------
34  	// Standalone Optional objects
35  	//------------------------------------------------------------------------------------------------------------------
36  
37  	@ParameterizedTest
38  	@MethodSource("testers")
39  	void a01_emptyOptional(RoundTrip_Tester t) throws Exception {
40  		var x = empty();
41  		x = t.roundTrip(x);
42  		assertFalse(x.isPresent());
43  	}
44  
45  	@ParameterizedTest
46  	@MethodSource("testers")
47  	void a02_optionalContainingString(RoundTrip_Tester t) throws Exception {
48  		var x = opt("foobar");
49  		x = t.roundTrip(x);
50  		assertEquals("foobar", x.get());
51  		x = opt("");
52  		x = t.roundTrip(x);
53  		assertEquals("", x.get());
54  	}
55  
56  	//------------------------------------------------------------------------------------------------------------------
57  	// Bean properties of Optional types.
58  	//------------------------------------------------------------------------------------------------------------------
59  
60  	//-----------------------------------------------------
61  	// Optional<String>
62  	//-----------------------------------------------------
63  
64  	public static class B01 {
65  		public Optional<String> f1;
66  	}
67  
68  	@ParameterizedTest
69  	@MethodSource("testers")
70  	void b01a_stringField(RoundTrip_Tester t) throws Exception {
71  		var x = new B01();
72  		x.f1 = opt("foo");
73  		x = t.roundTrip(x);
74  		assertEquals("foo", x.f1.get());
75  	}
76  
77  	@ParameterizedTest
78  	@MethodSource("testers")
79  	void b01b_stringField_emptyValue(RoundTrip_Tester t) throws Exception {
80  		var x = new B01();
81  		x.f1 = empty();
82  		x = t.roundTrip(x);
83  		assertFalse(x.f1.isPresent());
84  	}
85  
86  	@ParameterizedTest
87  	@MethodSource("testers")
88  	void b01c_stringField_nullField(RoundTrip_Tester t) throws Exception {
89  		var x = new B01();
90  		x.f1 = null;
91  		x = t.roundTrip(x);
92  		if (t.isValidationOnly())
93  			return;
94  		assertFalse(x.f1.isPresent());
95  	}
96  
97  	//-----------------------------------------------------
98  	// Optional<Integer>
99  	//-----------------------------------------------------
100 
101 	public static class B02 {
102 		public Optional<Integer> f1;
103 	}
104 
105 	@ParameterizedTest
106 	@MethodSource("testers")
107 	void b02a_integerField(RoundTrip_Tester t) throws Exception {
108 		var x = new B02();
109 		x.f1 = opt(123);
110 		x = t.roundTrip(x);
111 		assertEquals(123, x.f1.get().intValue());
112 	}
113 
114 	@ParameterizedTest
115 	@MethodSource("testers")
116 	void b02b_integerField_emptyValue(RoundTrip_Tester t) throws Exception {
117 		var x = new B02();
118 		x.f1 = empty();
119 		x = t.roundTrip(x);
120 		assertFalse(x.f1.isPresent());
121 	}
122 
123 	@ParameterizedTest
124 	@MethodSource("testers")
125 	void b02c_integerField_nullField(RoundTrip_Tester t) throws Exception {
126 		var x = new B02();
127 		x.f1 = null;
128 		x = t.roundTrip(x);
129 		if (t.isValidationOnly())
130 			return;
131 		assertFalse(x.f1.isPresent());
132 	}
133 
134 	//-----------------------------------------------------
135 	// Optional<List<Integer>>
136 	//-----------------------------------------------------
137 
138 	public static class B03 {
139 		public Optional<List<Integer>> f1;
140 	}
141 
142 	@ParameterizedTest
143 	@MethodSource("testers")
144 	void b03a_integerListField(RoundTrip_Tester t) throws Exception {
145 		var x = new B03();
146 		x.f1 = opt(alist(123));
147 		x = t.roundTrip(x);
148 		assertEquals(123, x.f1.get().get(0).intValue());
149 	}
150 
151 	@ParameterizedTest
152 	@MethodSource("testers")
153 	void b03b_integerListField_listWithNull(RoundTrip_Tester t) throws Exception {
154 		var x = new B03();
155 		x.f1 = opt(alist((Integer)null));
156 		x = t.roundTrip(x);
157 		assertTrue(x.f1.isPresent());
158 		assertEquals(1, x.f1.get().size());
159 		assertNull(x.f1.get().get(0));
160 	}
161 
162 	@ParameterizedTest
163 	@MethodSource("testers")
164 	void b03c_integerListField_emptyList(RoundTrip_Tester t) throws Exception {
165 		var x = new B03();
166 		x.f1 = opt(alist());
167 		x = t.roundTrip(x);
168 		assertTrue(x.f1.isPresent());
169 		assertEquals(0, x.f1.get().size());
170 	}
171 
172 	@ParameterizedTest
173 	@MethodSource("testers")
174 	void b03d_integerListField_emptyValue(RoundTrip_Tester t) throws Exception {
175 		var x = new B03();
176 		x.f1 = empty();
177 		x = t.roundTrip(x);
178 		assertFalse(x.f1.isPresent());
179 	}
180 
181 	@ParameterizedTest
182 	@MethodSource("testers")
183 	void b03e_integerListField_nullField(RoundTrip_Tester t) throws Exception {
184 		var x = new B03();
185 		x.f1 = null;
186 		x = t.roundTrip(x);
187 		if (t.isValidationOnly())
188 			return;
189 		assertFalse(x.f1.isPresent());
190 	}
191 
192 	//-----------------------------------------------------
193 	// Optional<Optional<Integer>>
194 	//-----------------------------------------------------
195 
196 	public static class B04 {
197 		public Optional<Optional<Integer>> f1;
198 	}
199 
200 	@ParameterizedTest
201 	@MethodSource("testers")
202 	void b04a_optionalOptionalInteger(RoundTrip_Tester t) throws Exception {
203 		var x = new B04();
204 		x.f1 = opt(opt(123));
205 		x = t.roundTrip(x);
206 		assertEquals(123, x.f1.get().get().intValue());
207 	}
208 
209 	@ParameterizedTest
210 	@MethodSource("testers")
211 	void b04b_optionalOptionalInteger_emptyInnerValue(RoundTrip_Tester t) throws Exception {
212 		var x = new B04();
213 		x.f1 = opt(empty());
214 		x = t.roundTrip(x);
215 		assertTrue(x.f1.isPresent());
216 		assertFalse(x.f1.get().isPresent());
217 	}
218 
219 	@ParameterizedTest
220 	@MethodSource("testers")
221 	void b04c_optionalOptionalInteger_emptyOuterValue(RoundTrip_Tester t) throws Exception {
222 		var x = new B04();
223 		x.f1 = empty();
224 		x = t.roundTrip(x);
225 		if (t.isValidationOnly())
226 			return;
227 		assertTrue(x.f1.isPresent());
228 		assertFalse(x.f1.get().isPresent());
229 	}
230 
231 	@ParameterizedTest
232 	@MethodSource("testers")
233 	void b04d_optionalOptionalInteger_nullField(RoundTrip_Tester t) throws Exception {
234 		var x = new B04();
235 		x.f1 = null;
236 		x = t.roundTrip(x);
237 		if (t.isValidationOnly())
238 			return;
239 		assertTrue(x.f1.isPresent());
240 		assertFalse(x.f1.get().isPresent());
241 	}
242 
243 	//-----------------------------------------------------
244 	// Optional<Optional<Bean>>
245 	//-----------------------------------------------------
246 
247 	public static class B05 {
248 		public Optional<Optional<B05B>> f1;
249 	}
250 
251 	public static class B05B {
252 		public int f2;
253 		public static B05B create() {
254 			var b = new B05B();
255 			b.f2 = 123;
256 			return b;
257 		}
258 	}
259 
260 	@ParameterizedTest
261 	@MethodSource("testers")
262 	void b05a_optionalOptionalBean(RoundTrip_Tester t) throws Exception {
263 		var x = new B05();
264 		x.f1 = opt(opt(B05B.create()));
265 		x = t.roundTrip(x);
266 		assertBean(x, "f1{f2}", "{123}");
267 	}
268 
269 	@ParameterizedTest
270 	@MethodSource("testers")
271 	void b05b_optionalOptionalBean_emptyInnerValue(RoundTrip_Tester t) throws Exception {
272 		var x = new B05();
273 		x.f1 = opt(empty());
274 		x = t.roundTrip(x);
275 		assertTrue(x.f1.isPresent());
276 		assertFalse(x.f1.get().isPresent());
277 	}
278 
279 	@ParameterizedTest
280 	@MethodSource("testers")
281 	void b05c_optionalOptionalBean_emptyOuterValue(RoundTrip_Tester t) throws Exception {
282 		var x = new B05();
283 		x.f1 = empty();
284 		x = t.roundTrip(x);
285 		if (t.isValidationOnly())
286 			return;
287 		assertTrue(x.f1.isPresent());
288 		assertFalse(x.f1.get().isPresent());
289 	}
290 
291 	@ParameterizedTest
292 	@MethodSource("testers")
293 	void b05d_optionalOptionalBean_nullField(RoundTrip_Tester t) throws Exception {
294 		var x = new B05();
295 		x.f1 = null;
296 		x = t.roundTrip(x);
297 		if (t.isValidationOnly())
298 			return;
299 		assertTrue(x.f1.isPresent());
300 		assertFalse(x.f1.get().isPresent());
301 	}
302 
303 	//-----------------------------------------------------
304 	// List<Optional<Integer>>
305 	//-----------------------------------------------------
306 
307 	public static class B06 {
308 		public List<Optional<Integer>> f1;
309 	}
310 
311 	@ParameterizedTest
312 	@MethodSource("testers")
313 	void b06a_listOfOptionalIntegers(RoundTrip_Tester t) throws Exception {
314 		var x = new B06();
315 		x.f1 = alist(opt(123));
316 		x = t.roundTrip(x);
317 		assertEquals(123, x.f1.get(0).get().intValue());
318 	}
319 
320 	@ParameterizedTest
321 	@MethodSource("testers")
322 	void b06b_listOfOptionalIntegers_listWithEmpty(RoundTrip_Tester t) throws Exception {
323 		var x = new B06();
324 		x.f1 = alist(empty());
325 		x = t.roundTrip(x);
326 		assertEquals(1, x.f1.size());
327 		assertFalse(x.f1.get(0).isPresent());
328 	}
329 
330 	@ParameterizedTest
331 	@MethodSource("testers")
332 	void b06c_listOfOptionalIntegers_listWithNull(RoundTrip_Tester t) throws Exception {
333 		var x = new B06();
334 		x.f1 = alist((Optional<Integer>)null);
335 		x = t.roundTrip(x);
336 		if (t.isValidationOnly())
337 			return;
338 		assertEquals(1, x.f1.size());
339 		assertFalse(x.f1.get(0).isPresent());
340 	}
341 
342 	@ParameterizedTest
343 	@MethodSource("testers")
344 	void b06d_listOfOptionalIntegers_nullField(RoundTrip_Tester t) throws Exception {
345 		var x = new B06();
346 		x.f1 = null;
347 		x = t.roundTrip(x);
348 		assertNull(x.f1);
349 	}
350 
351 	//-----------------------------------------------------
352 	// Optional<Integer>[]
353 	//-----------------------------------------------------
354 
355 	public static class B07 {
356 		public Optional<Integer>[] f1;
357 		public List<Integer>[] f2;
358 	}
359 
360 	@ParameterizedTest
361 	@MethodSource("testers")
362 	void b07a_arrayOfOptionalIntegers(RoundTrip_Tester t) throws Exception {
363 		var x = new B07();
364 		x.f1 = a(opt(123));
365 		x.f2 = a(alist(123));
366 		x = t.roundTrip(x);
367 		assertEquals(123, x.f1[0].get().intValue());
368 	}
369 
370 	@ParameterizedTest
371 	@MethodSource("testers")
372 	void b07b_arrayOfOptionalIntegers_listWithEmpty(RoundTrip_Tester t) throws Exception {
373 		var x = new B07();
374 		x.f1 = a(empty());
375 		x = t.roundTrip(x);
376 		assertEquals(1, x.f1.length);
377 		assertFalse(x.f1[0].isPresent());
378 	}
379 
380 	@SuppressWarnings("unchecked")
381 	@ParameterizedTest
382 	@MethodSource("testers")
383 	void b07c_arrayOfOptionalIntegers_listWithNull(RoundTrip_Tester t) throws Exception {
384 		var x = new B07();
385 		x.f1 = a(n(Optional.class));
386 		x = t.roundTrip(x);
387 		if (t.isValidationOnly())
388 			return;
389 		assertEquals(1, x.f1.length);
390 		assertFalse(x.f1[0].isPresent());
391 	}
392 
393 	@ParameterizedTest
394 	@MethodSource("testers")
395 	void b07d_arrayOfOptionalIntegers_nullField(RoundTrip_Tester t) throws Exception {
396 		var x = new B07();
397 		x.f1 = null;
398 		x = t.roundTrip(x);
399 		assertNull(x.f1);
400 	}
401 }