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