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.apache.juneau.commons.utils.IoUtils.*;
21  import static org.apache.juneau.junit.bct.BctAssertions.*;
22  import static org.junit.jupiter.api.Assertions.*;
23  
24  import org.junit.jupiter.params.*;
25  import org.junit.jupiter.params.provider.*;
26  
27  /**
28   * Tests to ensure the valueOf(String), fromString(String), parse(String), and parseString(String) methods
29   * are used correctly by parsers.
30   */
31  class ObjectsAsStrings_RoundTripTest extends RoundTripTest_Base {
32  
33  	//====================================================================================================
34  	// testBasic
35  	//====================================================================================================
36  
37  	@ParameterizedTest
38  	@MethodSource("testers")
39  	void a01_basic(RoundTrip_Tester t) throws Exception {
40  		var x = new A().init();
41  		x = t.roundTrip(x);
42  		assertBean(x, "a1{f},a2{f},a3{f},a4{f}", "{1},{2},{3},{4}");
43  	}
44  
45  	public static class A {
46  		public A1 a1;
47  		public A2 a2;
48  		public A3 a3;
49  		public A4 a4;
50  
51  		public A init() {
52  			a1 = new A1();
53  			a1.f = "1";
54  			a2 = new A2();
55  			a2.f = "2";
56  			a3 = new A3();
57  			a3.f = "3";
58  			a4 = new A4();
59  			a4.f = "4";
60  			return this;
61  		}
62  	}
63  
64  	public static class A1 {
65  		public String f;
66  		public static A1 fromString(String s) {
67  			var x = new A1();
68  			x.f = s.substring(3);
69  			return x;
70  		}
71  		@Override /* Overridden from Object */
72  		public String toString() {
73  			return "A1-" + f;
74  		}
75  	}
76  
77  	public static class A2 {
78  		public String f;
79  		public static A2 valueOf(String s) {
80  			var x = new A2();
81  			x.f = s.substring(3);
82  			return x;
83  		}
84  		@Override /* Overridden from Object */
85  		public String toString() {
86  			return "A2-" + f;
87  		}
88  	}
89  
90  	public static class A3 {
91  		public String f;
92  		public static A3 parse(String s) {
93  			var x = new A3();
94  			x.f = s.substring(3);
95  			return x;
96  		}
97  		@Override /* Overridden from Object */
98  		public String toString() {
99  			return "A3-" + f;
100 		}
101 	}
102 
103 	public static class A4 {
104 		public String f;
105 		public static A4 parseString(String s) {
106 			var x = new A4();
107 			x.f = s.substring(3);
108 			return x;
109 		}
110 		@Override /* Overridden from Object */
111 		public String toString() {
112 			return "A4-" + f;
113 		}
114 	}
115 
116 	//====================================================================================================
117 	// testEnumWithOverriddenStringValue
118 	// The B1 enum should serialize as "X1" but the B2 enum should serialize as "X-1".
119 	//====================================================================================================
120 
121 	@ParameterizedTest
122 	@MethodSource("testers")
123 	void a02_enumWithOverriddenStringValue(RoundTrip_Tester t) throws Exception {
124 		var x = new B().init();
125 		if (! t.returnOriginalObject) {
126 			var r = t.getSerializer().serialize(x);
127 			assertTrue(toString(r).contains("X-2"));
128 		}
129 		x = t.roundTrip(x);
130 		assertBean(x, "b1,b2", "X1,X2");
131 	}
132 
133 	public static class B {
134 		public B1 b1;
135 		public B2 b2;
136 
137 		public B init() {
138 			b1 = B1.X1;
139 			b2 = B2.X2;
140 			return this;
141 		}
142 
143 	}
144 
145 	public enum B1 {
146 		X1(1),
147 		X2(2),
148 		X3(3);
149 
150 		@SuppressWarnings("unused")
151 		private int i;
152 		B1(int i) {
153 			this.i = i;
154 		}
155 	}
156 
157 	public enum B2 {
158 		X1(1),
159 		X2(2),
160 		X3(3);
161 
162 		private int i;
163 		B2(int i) {
164 			this.i = i;
165 		}
166 
167 		@Override /* Overridden from Object */
168 		public String toString() {
169 			return "X-" + i;
170 		}
171 
172 		public static B2 fromString(String s) {
173 			return valueOf("X" + s.substring(2));
174 		}
175 	}
176 
177 	//====================================================================================================
178 	// testMethodOrdering
179 	//====================================================================================================
180 
181 	@ParameterizedTest
182 	@MethodSource("testers")
183 	void a03_ordering(RoundTrip_Tester t) throws Exception {
184 		var x = new C().init();
185 		x = t.roundTrip(x);
186 		assertJson("{c1:{f:'1'},c2:{f:'2'},c3:{f:'3'},c4:{f:'4'}}", x);
187 	}
188 
189 	public static class C {
190 		public C1 c1;
191 		public C2 c2;
192 		public C3 c3;
193 		public C4 c4;
194 
195 		public C init() {
196 			c1 = new C1();
197 			c1.f = "1";
198 			c2 = new C2();
199 			c2.f = "2";
200 			c3 = new C3();
201 			c3.f = "3";
202 			c4 = new C4();
203 			c4.f = "4";
204 			return this;
205 		}
206 	}
207 
208 	public static class C1 {
209 		public String f;
210 		public static C2 valueOf(String s) {
211 			throw new IllegalCallerException("Shouldn't be called!");
212 		}
213 		public static C2 parse(String s) {
214 			throw new IllegalCallerException("Shouldn't be called!");
215 		}
216 		public static C2 parseString(String s) {
217 			throw new IllegalCallerException("Shouldn't be called!");
218 		}
219 		public static C1 fromString(String s) {
220 			var x = new C1();
221 			x.f = s.substring(3);
222 			return x;
223 		}
224 
225 		@Override /* Overridden from Object */
226 		public String toString() {
227 			return "C1-" + f;
228 		}
229 	}
230 
231 	public static class C2 {
232 		public String f;
233 		public static C2 parse(String s) {
234 			throw new IllegalCallerException("Shouldn't be called!");
235 		}
236 		public static C2 parseString(String s) {
237 			throw new IllegalCallerException("Shouldn't be called!");
238 		}
239 		public static C2 valueOf(String s) {
240 			var x = new C2();
241 			x.f = s.substring(3);
242 			return x;
243 		}
244 		@Override /* Overridden from Object */
245 		public String toString() {
246 			return "C2-" + f;
247 		}
248 	}
249 
250 	public static class C3 {
251 		public String f;
252 		public static C2 parseString(String s) {
253 			throw new IllegalCallerException("Shouldn't be called!");
254 		}
255 		public static C3 parse(String s) {
256 			var x = new C3();
257 			x.f = s.substring(3);
258 			return x;
259 		}
260 		@Override /* Overridden from Object */
261 		public String toString() {
262 			return "C3-" + f;
263 		}
264 	}
265 
266 	public static class C4 {
267 		public String f;
268 		public static C4 parseString(String s) {
269 			var x = new C4();
270 			x.f = s.substring(3);
271 			return x;
272 		}
273 		@Override /* Overridden from Object */
274 		public String toString() {
275 			return "C4" + f;
276 		}
277 	}
278 
279 	//------------------------------------------------------------------------------------------------------------------
280 	// Utility methods.
281 	//------------------------------------------------------------------------------------------------------------------
282 
283 	private static final String toString(Object o) {
284 		if (o == null)
285 			return null;
286 		if (o instanceof String)
287 			return (String)o;
288 		if (o instanceof byte[])
289 			return new String((byte[])o, UTF8);
290 		return o.toString();
291 	}
292 }