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 java.util.*;
23  
24  import org.apache.juneau.common.utils.*;
25  import org.apache.juneau.json.*;
26  import org.junit.jupiter.params.*;
27  import org.junit.jupiter.params.provider.*;
28  
29  /**
30   * Tests designed to serialize and parse objects to make sure we end up
31   * with the same objects for all serializers and parsers.
32   */
33  class Enum_RoundTripTest extends RoundTripTest_Base {
34  
35  	//====================================================================================================
36  	// Enum object
37  	//====================================================================================================
38  
39  	@ParameterizedTest
40  	@MethodSource("testers")
41  	void a01_enumA(RoundTrip_Tester t) throws Exception {
42  		var x = AEnum.FOO;
43  		assertJson("'FOO'", x);
44  		x = t.roundTrip(x, AEnum.class);
45  		assertEquals(AEnum.FOO, x);
46  	}
47  
48  	@ParameterizedTest
49  	@MethodSource("testers")
50  	void a02_enumB(RoundTrip_Tester t) throws Exception {
51  		var s = JsonSerializer.create().json5().build();
52  		var x = BEnum.FOO;
53  		assertEquals("'xfoo'", s.serialize(x));
54  		x = t.roundTrip(x, BEnum.class);
55  		assertEquals(BEnum.FOO, x);
56  	}
57  
58  	//====================================================================================================
59  	// Enum[] object
60  	//====================================================================================================
61  
62  	@ParameterizedTest
63  	@MethodSource("testers")
64  	void a03_enumArrayA(RoundTrip_Tester t) throws Exception {
65  		var x = new AEnum[]{AEnum.FOO,AEnum.BAR,null};
66  		assertJson("['FOO','BAR',null]", x);
67  		x = t.roundTrip(x, AEnum[].class);
68  		assertEquals(AEnum.FOO, x[0]);
69  		assertEquals(AEnum.BAR, x[1]);
70  		assertNull(x[2]);
71  	}
72  
73  	@ParameterizedTest
74  	@MethodSource("testers")
75  	void a04_enumArrayB(RoundTrip_Tester t) throws Exception {
76  		var x = new BEnum[]{BEnum.FOO,BEnum.BAR,null};
77  		assertJson("['xfoo','xbar',null]", x);
78  		x = t.roundTrip(x, BEnum[].class);
79  		assertEquals(BEnum.FOO, x[0]);
80  		assertEquals(BEnum.BAR, x[1]);
81  		assertNull(x[2]);
82  	}
83  
84  	//====================================================================================================
85  	// Enum[][] object
86  	//====================================================================================================
87  
88  	@ParameterizedTest
89  	@MethodSource("testers")
90  	void a05_enum2dArrayA(RoundTrip_Tester t) throws Exception {
91  		var x = new AEnum[][]{{AEnum.FOO,AEnum.BAR,null},null};
92  		assertJson("[['FOO','BAR',null],null]", x);
93  		x = t.roundTrip(x, AEnum[][].class);
94  		assertEquals(AEnum.FOO, x[0][0]);
95  		assertEquals(AEnum.BAR, x[0][1]);
96  		assertNull(x[0][2]);
97  		assertNull(x[1]);
98  	}
99  
100 	@ParameterizedTest
101 	@MethodSource("testers")
102 	void a06_enum2dArrayB(RoundTrip_Tester t) throws Exception {
103 		var x = new BEnum[][]{{BEnum.FOO,BEnum.BAR,null},null};
104 		assertJson("[['xfoo','xbar',null],null]", x);
105 		x = t.roundTrip(x, BEnum[][].class);
106 		assertEquals(BEnum.FOO, x[0][0]);
107 		assertEquals(BEnum.BAR, x[0][1]);
108 		assertNull(x[0][2]);
109 		assertNull(x[1]);
110 	}
111 
112 	//====================================================================================================
113 	// Bean with Enum fields
114 	//====================================================================================================
115 
116 	@ParameterizedTest
117 	@MethodSource("testers")
118 	void a07_beansWithEnumA(RoundTrip_Tester t) throws Exception {
119 		var x1 = new A().init();
120 		var x2 = t.roundTrip(x1, A.class);
121 		assertEquals(json(x1), json(x2));
122 		assertBean(x2, "f3{0,1},f4{0{0,1},1}", "{FOO,<null>},{{FOO,<null>},<null>}");
123 	}
124 
125 	@ParameterizedTest
126 	@MethodSource("testers")
127 	void a08_beansWithEnumB(RoundTrip_Tester t) throws Exception {
128 		var x1 = new B().init();
129 		var x2 = t.roundTrip(x1, B.class);
130 		assertEquals(json(x1), json(x2));
131 		assertBean(x2, "f3{0,1},f4{0{0,1},1}", "{FOO,<null>},{{FOO,<null>},<null>}");
132 	}
133 
134 
135 	/** Normal Enum */
136 	public enum AEnum {
137 		FOO,BAR,BAZ
138 	}
139 
140 	/** Enum with custom serialized values */
141 	public enum BEnum {
142 		FOO("xfoo"), BAR("xbar"), BAZ("xbaz");
143 
144 		private String val;
145 
146 		BEnum(String val) {
147 			this.val = val;
148 		}
149 
150 		@Override /* Object */
151 		public String toString() {
152 			return val;
153 		}
154 
155 		public static BEnum fromString(String val) {
156 			if (val.equals("xfoo"))
157 				return FOO;
158 			if (val.equals("xbar"))
159 				return BAR;
160 			if (val.equals("xbaz"))
161 				return BAZ;
162 			return null;
163 		}
164 	}
165 
166 	public static class A {
167 
168 		// Should have 'enum' attribute.
169 		public AEnum f1;
170 
171 		private AEnum f2;
172 		public AEnum getF2() { return f2; }
173 		public void setF2(AEnum v) { f2 = v; }
174 
175 		public AEnum[] f3;
176 		public AEnum[][] f4;
177 
178 		// Should not have 'uniqueSet' attribute.
179 		public List<AEnum> f5 = new LinkedList<>();
180 
181 		private List<AEnum> f6 = new LinkedList<>();
182 		public List<AEnum> getF6() { return f6; }
183 		public void setF6(List<AEnum> v) { f6 = v; }
184 
185 		// Should have 'uniqueSet' attribute.
186 		public Set<AEnum> f7 = new HashSet<>();
187 
188 		private Set<AEnum> f8 = new HashSet<>();
189 		public Set<AEnum> getF8() { return f8; }
190 		public void setF8(Set<AEnum> v) { f8 = v; }
191 
192 		public Map<AEnum,AEnum> f9 = new LinkedHashMap<>();
193 
194 		public A init() {
195 			f1 = AEnum.FOO;
196 			f2 = AEnum.BAR;
197 			f3 = new AEnum[]{AEnum.FOO,null};
198 			f4 = new AEnum[][]{{AEnum.FOO,null},null};
199 			f5 = alist(AEnum.FOO);
200 			f6 = alist(AEnum.FOO);
201 			f7 = Utils.set(AEnum.FOO);
202 			f8 = Utils.set(AEnum.FOO);
203 
204 			return this;
205 		}
206 	}
207 
208 	public static class B {
209 
210 		// Should have 'enum' attribute.
211 		public BEnum f1;
212 
213 		private BEnum f2;
214 		public BEnum getF2() { return f2; }
215 		public void setF2(BEnum v) { f2 = v; }
216 
217 		public BEnum[] f3;
218 		public BEnum[][] f4;
219 
220 		// Should not have 'uniqueSet' attribute.
221 		public List<BEnum> f5 = new LinkedList<>();
222 
223 		private List<BEnum> f6 = new LinkedList<>();
224 		public List<BEnum> getF6() { return f6; }
225 		public void setF6(List<BEnum> v) { f6 = v; }
226 
227 		// Should have 'uniqueSet' attribute.
228 		public Set<BEnum> f7 = new HashSet<>();
229 
230 		private Set<BEnum> f8 = new HashSet<>();
231 		public Set<BEnum> getF8() { return f8; }
232 		public void setF8(Set<BEnum> v) { f8 = v; }
233 
234 		public Map<BEnum,BEnum> f9 = new LinkedHashMap<>();
235 
236 		public B init() {
237 			f1 = BEnum.FOO;
238 			f2 = BEnum.BAR;
239 			f3 = new BEnum[]{BEnum.FOO,null};
240 			f4 = new BEnum[][]{{BEnum.FOO,null},null};
241 			f5 = alist(BEnum.FOO);
242 			f6 = alist(BEnum.FOO);
243 			f7 = Utils.set(BEnum.FOO);
244 			f8 = Utils.set(BEnum.FOO);
245 
246 			return this;
247 		}
248 	}
249 }