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