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.CollectionUtils.*;
21  import static org.apache.juneau.junit.bct.BctAssertions.*;
22  import static org.junit.jupiter.api.Assertions.*;
23  
24  import java.util.*;
25  
26  import org.apache.juneau.json.*;
27  import org.junit.jupiter.params.*;
28  import org.junit.jupiter.params.provider.*;
29  
30  /**
31   * Tests designed to serialize and parse objects to make sure we end up
32   * with the same objects for all serializers and parsers.
33   */
34  class Enum_RoundTripTest extends RoundTripTest_Base {
35  
36  	//====================================================================================================
37  	// Enum object
38  	//====================================================================================================
39  
40  	@ParameterizedTest
41  	@MethodSource("testers")
42  	void a01_enumA(RoundTrip_Tester t) throws Exception {
43  		var x = AEnum.FOO;
44  		assertJson("'FOO'", x);
45  		x = t.roundTrip(x, AEnum.class);
46  		assertEquals(AEnum.FOO, x);
47  	}
48  
49  	@ParameterizedTest
50  	@MethodSource("testers")
51  	void a02_enumB(RoundTrip_Tester t) throws Exception {
52  		var s = JsonSerializer.create().json5().build();
53  		var x = BEnum.FOO;
54  		assertEquals("'xfoo'", s.serialize(x));
55  		x = t.roundTrip(x, BEnum.class);
56  		assertEquals(BEnum.FOO, x);
57  	}
58  
59  	//====================================================================================================
60  	// Enum[] object
61  	//====================================================================================================
62  
63  	@ParameterizedTest
64  	@MethodSource("testers")
65  	void a03_enumArrayA(RoundTrip_Tester t) throws Exception {
66  		var x = a(AEnum.FOO,AEnum.BAR,null);
67  		assertJson("['FOO','BAR',null]", x);
68  		x = t.roundTrip(x, AEnum[].class);
69  		assertEquals(AEnum.FOO, x[0]);
70  		assertEquals(AEnum.BAR, x[1]);
71  		assertNull(x[2]);
72  	}
73  
74  	@ParameterizedTest
75  	@MethodSource("testers")
76  	void a04_enumArrayB(RoundTrip_Tester t) throws Exception {
77  		var x = a(BEnum.FOO,BEnum.BAR,null);
78  		assertJson("['xfoo','xbar',null]", x);
79  		x = t.roundTrip(x, BEnum[].class);
80  		assertEquals(BEnum.FOO, x[0]);
81  		assertEquals(BEnum.BAR, x[1]);
82  		assertNull(x[2]);
83  	}
84  
85  	//====================================================================================================
86  	// Enum[][] object
87  	//====================================================================================================
88  
89  	@ParameterizedTest
90  	@MethodSource("testers")
91  	void a05_enum2dArrayA(RoundTrip_Tester t) throws Exception {
92  		var x = a(a(AEnum.FOO,AEnum.BAR,null),null);
93  		assertJson("[['FOO','BAR',null],null]", x);
94  		x = t.roundTrip(x, AEnum[][].class);
95  		assertEquals(AEnum.FOO, x[0][0]);
96  		assertEquals(AEnum.BAR, x[0][1]);
97  		assertNull(x[0][2]);
98  		assertNull(x[1]);
99  	}
100 
101 	@ParameterizedTest
102 	@MethodSource("testers")
103 	void a06_enum2dArrayB(RoundTrip_Tester t) throws Exception {
104 		var x = a(a(BEnum.FOO,BEnum.BAR,null),null);
105 		assertJson("[['xfoo','xbar',null],null]", x);
106 		x = t.roundTrip(x, BEnum[][].class);
107 		assertEquals(BEnum.FOO, x[0][0]);
108 		assertEquals(BEnum.BAR, x[0][1]);
109 		assertNull(x[0][2]);
110 		assertNull(x[1]);
111 	}
112 
113 	//====================================================================================================
114 	// Bean with Enum fields
115 	//====================================================================================================
116 
117 	@ParameterizedTest
118 	@MethodSource("testers")
119 	void a07_beansWithEnumA(RoundTrip_Tester t) throws Exception {
120 		var x1 = new A().init();
121 		var x2 = t.roundTrip(x1, A.class);
122 		assertEquals(json(x1), json(x2));
123 		assertBean(x2, "f3{0,1},f4{0{0,1},1}", "{FOO,<null>},{{FOO,<null>},<null>}");
124 	}
125 
126 	@ParameterizedTest
127 	@MethodSource("testers")
128 	void a08_beansWithEnumB(RoundTrip_Tester t) throws Exception {
129 		var x1 = new B().init();
130 		var x2 = t.roundTrip(x1, B.class);
131 		assertEquals(json(x1), json(x2));
132 		assertBean(x2, "f3{0,1},f4{0{0,1},1}", "{FOO,<null>},{{FOO,<null>},<null>}");
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 /* Overridden from 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 = map();
193 
194 		public A init() {
195 			f1 = AEnum.FOO;
196 			f2 = AEnum.BAR;
197 			f3 = a(AEnum.FOO,null);
198 			f4 = a(a(AEnum.FOO,null),null);
199 			f5 = l(AEnum.FOO);
200 			f6 = l(AEnum.FOO);
201 			f7 = set(AEnum.FOO);
202 			f8 = 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 = map();
235 
236 		public B init() {
237 			f1 = BEnum.FOO;
238 			f2 = BEnum.BAR;
239 			f3 = a(BEnum.FOO,null);
240 			f4 = a(a(BEnum.FOO,null),null);
241 			f5 = l(BEnum.FOO);
242 			f6 = l(BEnum.FOO);
243 			f7 = set(BEnum.FOO);
244 			f8 = set(BEnum.FOO);
245 
246 			return this;
247 		}
248 	}
249 }