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.junit.jupiter.api.Assertions.*;
20  
21  import org.apache.juneau.annotation.*;
22  import org.junit.jupiter.params.*;
23  import org.junit.jupiter.params.provider.*;
24  
25  /**
26   * Tests designed to serialize and parse objects to make sure we end up
27   * with the same objects for all serializers and parsers.
28   */
29  class ParentProperty_RoundTripTest extends RoundTripTest_Base {
30  
31  	//====================================================================================================
32  	// @ParentProperty method.
33  	//====================================================================================================
34  
35  	@ParameterizedTest
36  	@MethodSource("testers")
37  	void a01_parentProperty(RoundTrip_Tester t) throws Exception {
38  		var x = new ParentPropertyMethodContainer().init();
39  		x = t.roundTrip(x);
40  		if (t.isValidationOnly())
41  			return;
42  		assertEquals(x.f1, x.bean.parent.f1);
43  	}
44  
45  	public static class ParentPropertyMethodContainer {
46  		public int f1;
47  		public ParentPropertyMethodBean bean;
48  
49  		ParentPropertyMethodContainer init() {
50  			f1 = 1;
51  			bean = new ParentPropertyMethodBean().init();
52  			return this;
53  		}
54  
55  	}
56  	public static class ParentPropertyMethodBean {
57  		ParentPropertyMethodContainer parent;
58  		public int f2;
59  
60  		@ParentProperty
61  		protected void setParent(ParentPropertyMethodContainer v) {
62  			parent = v;
63  		}
64  
65  		ParentPropertyMethodBean init() {
66  			f2 = 2;
67  			return this;
68  		}
69  	}
70  
71  	//====================================================================================================
72  	// @ParentProperty field.
73  	//====================================================================================================
74  
75  	@ParameterizedTest
76  	@MethodSource("testers")
77  	void a02_parentPropertyField(RoundTrip_Tester t) throws Exception {
78  		var x = new ParentPropertyFieldContainer().init();
79  		x = t.roundTrip(x);
80  		if (t.isValidationOnly())
81  			return;
82  		assertEquals(x.f1, x.bean.parent.f1);
83  	}
84  
85  	public static class ParentPropertyFieldContainer {
86  		public int f1;
87  		public ParentPropertyFieldBean bean;
88  
89  		ParentPropertyFieldContainer init() {
90  			f1 = 1;
91  			bean = new ParentPropertyFieldBean().init();
92  			return this;
93  		}
94  	}
95  	public static class ParentPropertyFieldBean {
96  		@ParentProperty
97  		public ParentPropertyFieldContainer parent;
98  		public int f2;
99  
100 		ParentPropertyFieldBean init() {
101 			f2 = 2;
102 			return this;
103 		}
104 	}
105 
106 	//====================================================================================================
107 	// @ParentProperty read-only (getter only, no setter).
108 	//====================================================================================================
109 
110 	@ParameterizedTest
111 	@MethodSource("testers")
112 	void a03_readOnlyParentProperty(RoundTrip_Tester t) throws Exception {
113 		var x = new ReadOnlyParentPropertyContainer().init();
114 		// Initially, parent should be null (read-only property, can't be set)
115 		assertNull(x.bean.getParent(), "Read-only parent property should initially be null");
116 		x = t.roundTrip(x);
117 		if (t.isValidationOnly())
118 			return;
119 		// After round-trip, parent should still be null because parser won't set read-only properties
120 		assertNull(x.bean.getParent(), "Read-only parent property should not be set by parser");
121 		// Verify the object structure is still correct
122 		assertEquals(1, x.f1);
123 		assertEquals(2, x.bean.f2);
124 	}
125 
126 	public static class ReadOnlyParentPropertyContainer {
127 		public int f1;
128 		public ReadOnlyParentPropertyBean bean;
129 
130 		ReadOnlyParentPropertyContainer init() {
131 			f1 = 1;
132 			bean = new ReadOnlyParentPropertyBean().init();
133 			return this;
134 		}
135 	}
136 	public static class ReadOnlyParentPropertyBean {
137 		private ReadOnlyParentPropertyContainer parent;
138 		public int f2;
139 
140 		@ParentProperty
141 		public ReadOnlyParentPropertyContainer getParent() {
142 			return parent;
143 		}
144 
145 		ReadOnlyParentPropertyBean init() {
146 			f2 = 2;
147 			return this;
148 		}
149 	}
150 }
151