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