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 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 BeanInheritance_RoundTripTest extends RoundTripTest_Base {
30  
31  	//====================================================================================================
32  	// testBeanInheritance
33  	//====================================================================================================
34  
35  	@ParameterizedTest
36  	@MethodSource("testers")
37  	void a01_beanInheritance(RoundTrip_Tester t) throws Exception {
38  
39  		// Skip tests that just return the same object.
40  		if (t.returnOriginalObject)
41  			return;
42  
43  		var t1 = new A2().init();
44  		var t2 = t.roundTrip(t1, A2.class);
45  		assertEquals(json(t2), json(t1));
46  
47  		var t3 = new A3().init();
48  		t.roundTrip(t3, A3.class);
49  	}
50  
51  
52  	public abstract static class A1 {
53  		protected String x;
54  		protected String y;
55  		protected String z;
56  
57  		public A1() {
58  			this.x = null;
59  			this.y = null;
60  			this.z = null;
61  		}
62  
63  		public A1(String x, String y, String z) {
64  			this.x = x;
65  			this.y = y;
66  			this.z = z;
67  		}
68  
69  		public void setX(String v) { x = v; }
70  
71  		public void setY(String v) { y = v; }
72  
73  		public void setZ(String v) { z = v; }
74  
75  		@Override /* Object */
76  		public String toString() {
77  			return ("A1(x: " + x + ", y: " + y + ", z: " + z + ")");
78  		}
79  
80  		public A1 init() {
81  			x = null;
82  			y = "";
83  			z = "z";
84  			return this;
85  		}
86  	}
87  
88  	public static class A2 extends A1 {
89  		public A2() {}
90  
91  		public A2(String x, String y, String z) {
92  			super(x, y, z);
93  		}
94  
95  		public String getX() { return x; }
96  
97  		public String getY() { return y; }
98  
99  		public String getZ() { return z; }
100 	}
101 
102 	// This is not supposed to be a valid bean since it has no getters defined.
103 	public static class A3 extends A1 {
104 		public A3() {
105 		}
106 
107 		public A3(String x, String y, String z) {
108 			super(x, y, z);
109 		}
110 
111 		public String isX() {
112 			throw new IllegalCallerException("Should not be called!");
113 		}
114 
115 		public String isY() {
116 			throw new IllegalCallerException("Should not be called!");
117 		}
118 
119 		public String isZ() {
120 			throw new IllegalCallerException("Should not be called!");
121 		}
122 	}
123 
124 	//====================================================================================================
125 	// Test bean inheritance
126 	//====================================================================================================
127 
128 	@ParameterizedTest
129 	@MethodSource("testers")
130 	void a02_beanInheritance(RoundTrip_Tester t) throws Exception {
131 		var t1 = new B1().init();
132 		var t2 = t. roundTrip(t1, B1.class);
133 		assertEquals(json(t2), json(t1));
134 	}
135 
136 	public static class B1 extends B2 {
137 		private A2 f4;
138 
139 		public A2 getF4() { return f4; }
140 		public void setF4(A2 v) { f4 = v; }
141 
142 		@Override /* Object */
143 		public String toString() {
144 			return super.toString() + " / " + f4;
145 		}
146 
147 		public B1 init() {
148 			setF1("A1");
149 			setF2(101);
150 			setF3(false);
151 			setF4((A2)new A2().init());
152 			return this;
153 		}
154 	}
155 
156 	public static class B2 {
157 		private String f1;
158 		private int f2 = -1;
159 		private boolean f3;
160 
161 		public String getF1() { return f1; }
162 		public void setF1(String v) { f1 = v; }
163 
164 		public int getF2() { return f2; }
165 		public void setF2(int v) { f2 = v; }
166 
167 		public boolean isF3() { return f3; }
168 		public void setF3(boolean v) { f3 = v; }
169 
170 		@Override /* Object */
171 		public String toString() {
172 			return ("B2(f1: " + getF1() + ", f2: " + getF2() + ")");
173 		}
174 	}
175 }