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.annotation;
18  
19  import static org.apache.juneau.common.utils.Utils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.util.*;
23  import java.util.function.*;
24  import java.util.stream.*;
25  
26  import org.apache.juneau.*;
27  import org.apache.juneau.collections.*;
28  import org.apache.juneau.internal.*;
29  import org.apache.juneau.json.*;
30  import org.apache.juneau.marshaller.*;
31  import org.apache.juneau.reflect.*;
32  import org.apache.juneau.svl.*;
33  import org.apache.juneau.swap.*;
34  import org.junit.jupiter.api.*;
35  
36  /**
37   * Tests the @BeanConfig annotation.
38   */
39  class BeanConfigAnnotation_Test extends TestBase {
40  
41  	private static void check(String expected, Object o) {
42  		assertEquals(expected, TO_STRING.apply(o));
43  	}
44  
45  	private static final Function<Object,String> TO_STRING = new Function<>() {
46  		@SuppressWarnings({ "rawtypes" })
47  		@Override
48  		public String apply(Object t) {
49  			if (t == null)
50  				return null;
51  			if (t instanceof List)
52  				return ((List<?>)t)
53  					.stream()
54  					.map(TO_STRING)
55  					.collect(Collectors.joining(","));
56  			if (isArray(t))
57  				return apply(ArrayUtils.toList(t, Object.class));
58  			if (t instanceof JsonMap)
59  				return ((JsonMap)t).toString();
60  			if (t instanceof Map)
61  				return ((Map<?,?>)t)
62  					.entrySet()
63  					.stream()
64  					.map(TO_STRING)
65  					.collect(Collectors.joining(","));
66  			if (t instanceof Map.Entry) {
67  				Map.Entry e = (Map.Entry)t;
68  				return apply(e.getKey()) + "=" + apply(e.getValue());
69  			}
70  			if (t instanceof BeanFilter)
71  				return ((BeanFilter)t).getBeanClass().getSimpleName();
72  			if (t instanceof Class)
73  				return ((Class<?>)t).getSimpleName();
74  			if (t instanceof ClassInfo)
75  				return ((ClassInfo)t).getSimpleName();
76  			if (t instanceof PropertyNamer)
77  				return t.getClass().getSimpleName();
78  			if (t instanceof TimeZone)
79  				return ((TimeZone)t).getID();
80  			return t.toString();
81  		}
82  	};
83  
84  	static VarResolverSession sr = VarResolver.create().vars(XVar.class).build().createSession();
85  
86  	//-----------------------------------------------------------------------------------------------------------------
87  	// Basic tests
88  	//-----------------------------------------------------------------------------------------------------------------
89  
90  	@Bean(typeName="A1")
91  	public static class A1 {
92  		public int foo;
93  		@Override
94  		public String toString() {return Json5.of(this);}
95  	}
96  	@Bean(typeName="A2")
97  	public static class A2 {
98  		public int foo;
99  	}
100 	@Bean(typeName="A3")
101 	public static class A3 {
102 		public int foo;
103 	}
104 	public static class AB1 extends ObjectSwap<String,Integer> {
105 	}
106 	public static class AB2 extends ObjectSwap<String,Integer> {
107 	}
108 	public static class AB3 extends ObjectSwap<String,Integer> {
109 	}
110 
111 	@BeanConfig(
112 		beanClassVisibility="$X{PRIVATE}",
113 		beanConstructorVisibility="$X{PRIVATE}",
114 		dictionary={A1.class,A2.class},
115 		dictionary_replace={A1.class,A2.class,A3.class},
116 		beanFieldVisibility="$X{PRIVATE}",
117 		beanMapPutReturnsOldValue="$X{true}",
118 		beanMethodVisibility="$X{PRIVATE}",
119 		beansRequireDefaultConstructor="$X{true}",
120 		beansRequireSerializable="$X{true}",
121 		beansRequireSettersForGetters="$X{true}",
122 		disableBeansRequireSomeProperties="$X{true}",
123 		typePropertyName="$X{foo}",
124 		debug="$X{true}",
125 		disableIgnoreUnknownNullBeanProperties="$X{true}",
126 		disableIgnoreMissingSetters="$X{true}",
127 		disableInterfaceProxies="$X{true}",
128 		findFluentSetters="$X{true}",
129 		ignoreInvocationExceptionsOnGetters="$X{true}",
130 		ignoreInvocationExceptionsOnSetters="$X{true}",
131 		ignoreUnknownBeanProperties="$X{true}",
132 		locale="$X{en-US}",
133 		mediaType="$X{text/foo}",
134 		notBeanClasses={A1.class,A2.class},
135 		notBeanClasses_replace={A1.class,A2.class,A3.class},
136 		notBeanPackages={"$X{foo1}","$X{foo2}"},
137 		notBeanPackages_replace={"$X{foo1}","$X{foo2}","$X{foo3}"},
138 		swaps={AB1.class,AB2.class},
139 		swaps_replace={AB1.class,AB2.class,AB3.class},
140 		propertyNamer=PropertyNamerULC.class,
141 		sortProperties="$X{true}",
142 		timeZone="$X{z}",
143 		useEnumNames="$X{true}",
144 		useJavaBeanIntrospector="$X{true}"
145 	)
146 	static class A {}
147 	static ClassInfo a = ClassInfo.of(A.class);
148 
149 	@Test void a01_basic() {
150 		var al = AnnotationWorkList.of(sr, a.getAnnotationList());
151 		var bs = JsonSerializer.create().apply(al).build().getSession();
152 
153 		check("PRIVATE", bs.getBeanClassVisibility());
154 		check("PRIVATE", bs.getBeanConstructorVisibility());
155 		check("A1,A2,A3", bs.getBeanDictionary());
156 		check("PRIVATE", bs.getBeanFieldVisibility());
157 		check("true", bs.isBeanMapPutReturnsOldValue());
158 		check("PRIVATE", bs.getBeanMethodVisibility());
159 		check("true", bs.isBeansRequireDefaultConstructor());
160 		check("true", bs.isBeansRequireSerializable());
161 		check("true", bs.isBeansRequireSettersForGetters());
162 		check("false", bs.isBeansRequireSomeProperties());
163 		check("foo", bs.getBeanTypePropertyName());
164 		check("true", bs.isDebug());
165 		check("true", bs.isFindFluentSetters());
166 		check("true", bs.isIgnoreInvocationExceptionsOnGetters());
167 		check("true", bs.isIgnoreInvocationExceptionsOnSetters());
168 		check("false", bs.isIgnoreMissingSetters());
169 		check("true", bs.isIgnoreUnknownBeanProperties());
170 		check("false", bs.isIgnoreUnknownNullBeanProperties());
171 		check("en_US", bs.getLocale());
172 		check("text/foo", bs.getMediaType());
173 		check("A1,A2,A3,Map,Collection,Reader,Writer,InputStream,OutputStream,Throwable", bs.getNotBeanClasses());
174 		check("foo1,foo2,foo3,java.lang,java.lang.annotation,java.lang.ref,java.lang.reflect,java.io,java.net", bs.getNotBeanPackagesNames());
175 		check("AB1<String,Integer>,AB2<String,Integer>,AB3<String,Integer>", bs.getSwaps());
176 		check("PropertyNamerULC", bs.getPropertyNamer());
177 		check("true", bs.isSortProperties());
178 		check("GMT", bs.getTimeZone());
179 		check("true", bs.isUseEnumNames());
180 		check("false", bs.isUseInterfaceProxies());
181 		check("true", bs.isUseJavaBeanIntrospector());
182 	}
183 
184 	//-----------------------------------------------------------------------------------------------------------------
185 	// Annotation with no values.
186 	//-----------------------------------------------------------------------------------------------------------------
187 
188 	@BeanConfig()
189 	static class B {}
190 	static ClassInfo b = ClassInfo.of(B.class);
191 
192 	@Test void b01_noValues() {
193 		var al = AnnotationWorkList.of(sr, b.getAnnotationList());
194 		var js = JsonSerializer.create().apply(al).build();
195 		var bc = js.getBeanContext();
196 		check("PUBLIC", bc.getBeanClassVisibility());
197 		check("PUBLIC", bc.getBeanConstructorVisibility());
198 		check("", bc.getBeanDictionary());
199 		check("PUBLIC", bc.getBeanFieldVisibility());
200 		check("false", bc.isBeanMapPutReturnsOldValue());
201 		check("PUBLIC", bc.getBeanMethodVisibility());
202 		check("false", bc.isBeansRequireDefaultConstructor());
203 		check("false", bc.isBeansRequireSerializable());
204 		check("false", bc.isBeansRequireSettersForGetters());
205 		check("true", bc.isBeansRequireSomeProperties());
206 		check("_type", bc.getBeanTypePropertyName());
207 		check("false", js.isDebug());
208 		check("false", js.isDetectRecursions());
209 		check("false", bc.isFindFluentSetters());
210 		check("false", bc.isIgnoreInvocationExceptionsOnGetters());
211 		check("false", bc.isIgnoreInvocationExceptionsOnSetters());
212 		check("true", bc.isIgnoreMissingSetters());
213 		check("false", js.isIgnoreRecursions());
214 		check("false", bc.isIgnoreUnknownBeanProperties());
215 		check("true", bc.isIgnoreUnknownNullBeanProperties());
216 		check("0", js.getInitialDepth());
217 		check(Locale.getDefault().toString(), bc.getDefaultLocale());
218 		check("100", js.getMaxDepth());
219 		check(null, bc.getDefaultMediaType());
220 		check("java.lang,java.lang.annotation,java.lang.ref,java.lang.reflect,java.io,java.net", bc.getNotBeanPackagesNames());
221 		check("", bc.getSwaps());
222 		check("BasicPropertyNamer", bc.getPropertyNamer());
223 		check("false", bc.isSortProperties());
224 		check(null, bc.getDefaultTimeZone());
225 		check("false", bc.isUseEnumNames());
226 		check("true", bc.isUseInterfaceProxies());
227 		check("false", bc.isUseJavaBeanIntrospector());
228 	}
229 
230 	//-----------------------------------------------------------------------------------------------------------------
231 	// No annotation.
232 	//-----------------------------------------------------------------------------------------------------------------
233 
234 	static class C {}
235 	static ClassInfo c = ClassInfo.of(C.class);
236 
237 	@Test void c01_noAnnotation() {
238 		var al = AnnotationWorkList.of(sr, c.getAnnotationList());
239 		var js = JsonSerializer.create().apply(al).build();
240 		var bc = js.getBeanContext();
241 		check("PUBLIC", bc.getBeanClassVisibility());
242 		check("PUBLIC", bc.getBeanConstructorVisibility());
243 		check("", bc.getBeanDictionary());
244 		check("PUBLIC", bc.getBeanFieldVisibility());
245 		check("false", bc.isBeanMapPutReturnsOldValue());
246 		check("PUBLIC", bc.getBeanMethodVisibility());
247 		check("false", bc.isBeansRequireDefaultConstructor());
248 		check("false", bc.isBeansRequireSerializable());
249 		check("false", bc.isBeansRequireSettersForGetters());
250 		check("true", bc.isBeansRequireSomeProperties());
251 		check("_type", bc.getBeanTypePropertyName());
252 		check("false", js.isDebug());
253 		check("false", js.isDetectRecursions());
254 		check("false", bc.isFindFluentSetters());
255 		check("false", bc.isIgnoreInvocationExceptionsOnGetters());
256 		check("false", bc.isIgnoreInvocationExceptionsOnSetters());
257 		check("true", bc.isIgnoreMissingSetters());
258 		check("false", js.isIgnoreRecursions());
259 		check("false", bc.isIgnoreUnknownBeanProperties());
260 		check("true", bc.isIgnoreUnknownNullBeanProperties());
261 		check("0", js.getInitialDepth());
262 		check(Locale.getDefault().toString(), bc.getDefaultLocale());
263 		check("100", js.getMaxDepth());
264 		check(null, bc.getDefaultMediaType());
265 		check("java.lang,java.lang.annotation,java.lang.ref,java.lang.reflect,java.io,java.net", bc.getNotBeanPackagesNames());
266 		check("", bc.getSwaps());
267 		check("BasicPropertyNamer", bc.getPropertyNamer());
268 		check("false", bc.isSortProperties());
269 		check(null, bc.getDefaultTimeZone());
270 		check("false", bc.isUseEnumNames());
271 		check("true", bc.isUseInterfaceProxies());
272 		check("false", bc.isUseJavaBeanIntrospector());
273 	}
274 
275 }