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;
18  
19  import static org.apache.juneau.TestUtils.*;
20  
21  import org.apache.juneau.annotation.*;
22  import org.apache.juneau.common.utils.*;
23  import org.apache.juneau.json.*;
24  import org.junit.jupiter.api.*;
25  
26  /*
27   * Tests the BEAN_examples property and @Example annotation.
28   */
29  class PojoExamplesTest extends TestBase {
30  
31  	private final JsonParserSession session = JsonParser.DEFAULT.getSession();
32  
33  	//====================================================================================================
34  	// test BEAN_examples
35  	//====================================================================================================
36  	@Test void a01_property() {
37  		var bs = BeanContext.create().example(A.class, new A().init()).build().getSession();
38  		assertJson("{f1:'f1a'}", bs.getClassMeta(A.class).getExample(bs,session));
39  	}
40  
41  	public static class A {
42  		public String f1;
43  
44  		public A init() {
45  			this.f1 = "f1a";
46  			return this;
47  		}
48  	}
49  
50  	//====================================================================================================
51  	// test @Example on public field
52  	//====================================================================================================
53  	@Test void a02_exampleField() {
54  		var bs = BeanContext.DEFAULT_SESSION;
55  		assertJson("{f1:'f1b'}", bs.getClassMeta(B1.class).getExample(bs,session));
56  	}
57  
58  	public static class B1 {
59  		public String f1;
60  
61  		@Example
62  		public static B1 EXAMPLE = new B1().init();
63  
64  		public B1 init() {
65  			this.f1 = "f1b";
66  			return this;
67  		}
68  	}
69  
70  	@Test void a03_exampleField_usingConfig() {
71  		var bs = BeanContext.create().applyAnnotations(B1cConfig.class).build().getSession();
72  		assertJson("{f1:'f1b'}", bs.getClassMeta(B1c.class).getExample(bs,session));
73  	}
74  
75  	@Example(on="Dummy1.EXAMPLE")
76  	@Example(on="B1c.EXAMPLE")
77  	@Example(on="Dummy2.EXAMPLE")
78  	private static class B1cConfig {}
79  
80  	public static class B1c {
81  		public String f1;
82  
83  		public static B1c EXAMPLE = new B1c().init();
84  
85  		public B1c init() {
86  			this.f1 = "f1b";
87  			return this;
88  		}
89  	}
90  
91  	//====================================================================================================
92  	// test @Example on private field
93  	//====================================================================================================
94  	@Test void a04_exampleFieldPrivate() {
95  		var bs = BeanContext.DEFAULT_SESSION;
96  		assertJson("{f1:'f1b'}", bs.getClassMeta(B2.class).getExample(bs,session));
97  	}
98  
99  	public static class B2 {
100 		public String f1;
101 
102 		@Example
103 		private static B2 EXAMPLE = new B2().init();
104 
105 		public B2 init() {
106 			this.f1 = "f1b";
107 			return this;
108 		}
109 	}
110 
111 	@Test void a05_exampleFieldPrivate_usingConfig() {
112 		var bs = BeanContext.create().applyAnnotations(B2cConfig.class).build().getSession();
113 		assertJson("{f1:'f1b'}", bs.getClassMeta(B2c.class).getExample(bs,session));
114 	}
115 
116 	@Example(on="Dummy1.EXAMPLE")
117 	@Example(on="B2c.EXAMPLE")
118 	@Example(on="Dummy2.EXAMPLE")
119 	private static class B2cConfig {}
120 
121 	public static class B2c {
122 		public String f1;
123 
124 		@SuppressWarnings("unused")
125 		private static B2c EXAMPLE = new B2c().init();
126 
127 		public B2c init() {
128 			this.f1 = "f1b";
129 			return this;
130 		}
131 	}
132 
133 	//====================================================================================================
134 	// test @Example on public no-arg method.
135 	//====================================================================================================
136 	@Test void a06_exampleOnPublicNoArgMethod() {
137 		var bs = BeanContext.DEFAULT_SESSION;
138 		assertJson("{f1:'f1c'}", bs.getClassMeta(C1.class).getExample(bs,session));
139 	}
140 
141 	public static class C1 {
142 		public String f1;
143 
144 		public C1 init() {
145 			this.f1 = "f1c";
146 			return this;
147 		}
148 
149 		@Example
150 		public static C1 x() {
151 			return new C1().init();
152 		}
153 	}
154 
155 	@Test void a07_exampleOnPublicNoArgMethod_usingConfig() {
156 		var bs = BeanContext.create().applyAnnotations(C1cConfig.class).build().getSession();
157 		assertJson("{f1:'f1c'}", bs.getClassMeta(C1c.class).getExample(bs,session));
158 	}
159 
160 	@Example(on="Dummy1.x")
161 	@Example(on="C1c.x")
162 	@Example(on="Dummy2.x")
163 	private static class C1cConfig {}
164 
165 	public static class C1c {
166 		public String f1;
167 
168 		public C1c init() {
169 			this.f1 = "f1c";
170 			return this;
171 		}
172 
173 		public static C1c x() {
174 			return new C1c().init();
175 		}
176 	}
177 
178 	//====================================================================================================
179 	// test @Example on private no-arg method.
180 	//====================================================================================================
181 	@Test void a08_exampleOnPrivateNoArgMethod() {
182 		var bs = BeanContext.DEFAULT_SESSION;
183 		assertJson("{f1:'f1c'}", bs.getClassMeta(C2.class).getExample(bs,session));
184 	}
185 
186 	public static class C2 {
187 		public String f1;
188 
189 		public C2 init() {
190 			this.f1 = "f1c";
191 			return this;
192 		}
193 
194 		@Example
195 		private static C2 x() {
196 			return new C2().init();
197 		}
198 	}
199 
200 	@Test void a09_exampleOnPrivateNoArgMethod_usingConfig() {
201 		var bs = BeanContext.create().applyAnnotations(C2cConfig.class).build().getSession();
202 		assertJson("{f1:'f1c'}", bs.getClassMeta(C2c.class).getExample(bs,session));
203 	}
204 
205 	@Example(on="Dummy1.x")
206 	@Example(on="C2c.x")
207 	@Example(on="Dummy2.x")
208 	private static class C2cConfig {}
209 
210 	public static class C2c {
211 		public String f1;
212 
213 		public C2c init() {
214 			this.f1 = "f1c";
215 			return this;
216 		}
217 
218 		@SuppressWarnings("unused")
219 		private static C2c x() {
220 			return new C2c().init();
221 		}
222 	}
223 
224 	//====================================================================================================
225 	// test @Example on public 1-arg method
226 	//====================================================================================================
227 	@Test void a10_exampleOnPublicOneArgMethod() {
228 		var bs = BeanContext.DEFAULT_SESSION;
229 		assertJson("{f1:'f1d'}", bs.getClassMeta(D1.class).getExample(bs,session));
230 	}
231 
232 	public static class D1 {
233 		public String f1;
234 
235 		public D1 init() {
236 			this.f1 = "f1d";
237 			return this;
238 		}
239 
240 		@Example
241 		public static D1 x(BeanSession bs) {
242 			return new D1().init();
243 		}
244 	}
245 
246 	@Test void a11_exampleOnPublicOneArgMethod_usingConfig() {
247 		var bs = BeanContext.create().applyAnnotations(D1cConfig.class).build().getSession();
248 		assertJson("{f1:'f1d'}", bs.getClassMeta(D1c.class).getExample(bs,session));
249 	}
250 
251 	@Example(on="Dummy1.x(BeanSession)")
252 	@Example(on="D1c.x(BeanSession)")
253 	@Example(on="Dummy2.x(BeanSession)")
254 	private static class D1cConfig {}
255 
256 	public static class D1c {
257 		public String f1;
258 
259 		public D1c init() {
260 			this.f1 = "f1d";
261 			return this;
262 		}
263 
264 		public static D1c x(BeanSession bs) {
265 			return new D1c().init();
266 		}
267 	}
268 
269 	//====================================================================================================
270 	// test example() method, no annotation.
271 	//====================================================================================================
272 	@Test void a12_exampleMethod() {
273 		var bs = BeanContext.DEFAULT_SESSION;
274 		assertJson("{f1:'f1e'}", bs.getClassMeta(E1.class).getExample(bs,session));
275 	}
276 
277 	public static class E1 {
278 		public String f1;
279 
280 		public E1 init() {
281 			this.f1 = "f1e";
282 			return this;
283 		}
284 
285 		public static E1 example() {
286 			return new E1().init();
287 		}
288 	}
289 
290 	//====================================================================================================
291 	// test example(BeanSession) method, no annotation.
292 	//====================================================================================================
293 	@Test void a13_exampleBeanSessionMethod() {
294 		var bs = BeanContext.DEFAULT_SESSION;
295 		assertJson("{f1:'f1e'}", bs.getClassMeta(E2.class).getExample(bs,session));
296 	}
297 
298 	public static class E2 {
299 		public String f1;
300 
301 		public E2 init() {
302 			this.f1 = "f1e";
303 			return this;
304 		}
305 
306 		public static E2 example(BeanSession bs) {
307 			return new E2().init();
308 		}
309 	}
310 
311 	//====================================================================================================
312 	// test invalid uses of @Example
313 	//====================================================================================================
314 	@Test void a14_invalidUsesOfExample() {
315 		var bs = BeanContext.DEFAULT_SESSION;
316 		assertThrowsWithMessage(Exception.class, "invalid method 'example(String)'", ()->bs.getClassMeta(F1.class));
317 		assertThrowsWithMessage(Exception.class, "invalid method 'example()'", ()->bs.getClassMeta(F2.class));
318 		assertThrowsWithMessage(Exception.class, Utils.list("invalid field","$F3.F3"), ()->bs.getClassMeta(F3.class));
319 		assertThrowsWithMessage(Exception.class, Utils.list("invalid field ","$F4.f4"), ()->bs.getClassMeta(F4.class));
320 	}
321 
322 	public static class F1 {
323 		@Example
324 		public static F1 example(String s) {
325 			return null;
326 		}
327 	}
328 	public static class F2 {
329 		@Example
330 		public F2 example() {
331 			return null;
332 		}
333 	}
334 	public static class F3 {
335 		@Example
336 		public static String F3 = "foo";
337 	}
338 	public static class F4 {
339 		@Example
340 		public F4 f4 = new F4();
341 	}
342 }