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.apache.juneau.annotation.*;
25  import org.junit.jupiter.params.*;
26  import org.junit.jupiter.params.provider.*;
27  
28  /**
29   * Tests designed to serialize and parse objects to make sure we end up
30   * with the same objects for all serializers and parsers.
31   */
32  class ObjectsWithSpecialMethods_RoundTripTest extends RoundTripTest_Base {
33  
34  	//====================================================================================================
35  	// @NameProperty method.
36  	//====================================================================================================
37  
38  	@ParameterizedTest
39  	@MethodSource("testers")
40  	void a01_nameProperty(RoundTrip_Tester t) throws Exception {
41  		var x = new A().init();
42  		x = t.roundTrip(x);
43  		assertBean(x, "a2{f2},m{k1{f2}}", "{2},{{2}}");
44  		if (t.isValidationOnly())
45  			return;
46  		assertBean(x, "a2{name}", "{a2}");
47  		assertBean(x, "m{k1{name}}", "{{k1}}");
48  	}
49  
50  	public static class A {
51  		public A2 a2;
52  		public Map<String,A2> m;
53  
54  		A init() {
55  			a2 = new A2().init();
56  			m = new LinkedHashMap<>();
57  			m.put("k1", new A2().init());
58  			return this;
59  		}
60  
61  	}
62  	public static class A2 {
63  		String name;
64  		public int f2;
65  
66  		@NameProperty
67  		protected void setName(String name) {
68  			this.name = name;
69  		}
70  
71  		A2 init() {
72  			f2 = 2;
73  			return this;
74  		}
75  	}
76  
77  	//====================================================================================================
78  	// @ParentProperty method.
79  	//====================================================================================================
80  
81  	@ParameterizedTest
82  	@MethodSource("testers")
83  	void a02_parentProperty(RoundTrip_Tester t) throws Exception {
84  		var x = new B().init();
85  		x = t.roundTrip(x);
86  		if (t.isValidationOnly())
87  			return;
88  		assertEquals(x.f1, x.b2.parent.f1);
89  	}
90  
91  	public static class B {
92  		public int f1;
93  		public B2 b2;
94  
95  		B init() {
96  			f1 = 1;
97  			b2 = new B2().init();
98  			return this;
99  		}
100 
101 	}
102 	public static class B2 {
103 		B parent;
104 		public int f2;
105 
106 		@ParentProperty
107 		protected void setParent(B v) {
108 			parent = v;
109 		}
110 
111 		B2 init() {
112 			f2 = 2;
113 			return this;
114 		}
115 	}
116 }