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.apache.juneau.annotation.*;
23  import org.junit.jupiter.params.*;
24  import org.junit.jupiter.params.provider.*;
25  
26  /**
27   * Tests designed to serialize and parse objects to make sure we end up
28   * with the same objects for all serializers and parsers.
29   */
30  class Generics_RoundTripTest extends RoundTripTest_Base {
31  
32  	//====================================================================================================
33  	// testBeansWithUnboundTypeVars
34  	//====================================================================================================
35  
36  	@ParameterizedTest
37  	@MethodSource("testers")
38  	void a01_beansWithUnboundTypeVars(RoundTrip_Tester t) throws Exception {
39  
40  		if (t.returnOriginalObject)
41  			return;
42  
43  		// Unbound type variables should be interpreted as Object.
44  		// During parsing, these become JsonMaps.
45  		var x = new Pair<Object,Object>(new Source().init(), new Target().init());
46  		x = t.roundTrip(x);
47  		assertBean(x, "s{s1},t{t1}", "{a1},{b1}");
48  		assertEquals("JsonMap", x.getS().getClass().getSimpleName());
49  		assertEquals("JsonMap", x.getT().getClass().getSimpleName());
50  
51  		// If you specify a concrete class, the type variables become bound and
52  		// the property types correctly resolve.
53  		x = t.roundTrip(x, RealPair.class);
54  		assertBean(x, "s{s1},t{t1}", "{a1},{b1}");
55  		assertEquals("Source", x.getS().getClass().getSimpleName());
56  		assertEquals("Target", x.getT().getClass().getSimpleName());
57  	}
58  
59  	// Class with unbound type variables.
60  	@Bean(p="s,t")
61  	public static class Pair<S,T> {
62  
63  		public Pair() {}
64  
65  		public Pair(S s, T t) {
66  			this.s = s;
67  			this.t = t;
68  		}
69  
70  		// Getters/setters
71  		private S s;
72  		public S getS() { return s; }
73  		public void setS(S v) { s = v; }
74  
75  		private T t;
76  		public T getT() { return t; }
77  		public void setT(T v) { t = v; }
78  	}
79  
80  	// Sublcass with bound type variables.
81  	public static class RealPair extends Pair<Source,Target> {}
82  
83  	public static class Source {
84  		public String s1;
85  		public Source init() {
86  			s1 = "a1";
87  			return this;
88  		}
89  	}
90  
91  	public static class Target {
92  		public String t1;
93  		public Target init() {
94  			t1 = "b1";
95  			return this;
96  		}
97  	}
98  }