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 static org.junit.jupiter.api.Assertions.*;
21  
22  import java.util.Locale;
23  import java.util.TimeZone;
24  
25  import org.apache.juneau.commons.reflect.*;
26  import org.apache.juneau.commons.settings.Settings;
27  import org.apache.juneau.json.*;
28  import org.apache.juneau.testutils.pojos.*;
29  import org.junit.jupiter.api.*;
30  
31  class BeanContext_Test extends TestBase {
32  
33  	BeanContext bc = BeanContext.DEFAULT;
34  	BeanSession bs = BeanContext.DEFAULT_SESSION;
35  
36  	@AfterEach
37  	void tearDown() {
38  		Settings.get().clearLocal();
39  	}
40  
41  	public interface A1 {
42  		int getF1();
43  		void setF1(int f1);
44  	}
45  
46  	@Test void a01_normalCachableBean() throws ExecutableException {
47  		var cm1 = bc.getClassMeta(A1.class);
48  		var cm2 = bc.getClassMeta(A1.class);
49  		assertSame(cm1, cm2);
50  	}
51  
52  	interface A2 {
53  		void foo(int x);
54  	}
55  
56  	@Test void a02_lambdaExpressionsNotCached() throws ExecutableException {
57  		var bc2 = BeanContext.DEFAULT;
58  		var fi = (A2)System.out::println;
59  		var cm1 = bc2.getClassMeta(fi.getClass());
60  		var cm2 = bc2.getClassMeta(fi.getClass());
61  		assertNotSame(cm1, cm2);
62  	}
63  
64  	@Test void a03_proxiesNotCached() throws ExecutableException {
65  		var a1 = bs.getBeanMeta(A1.class).newBean(null);
66  		var cm1 = bc.getClassMeta(a1.getClass());
67  		var cm2 = bc.getClassMeta(a1.getClass());
68  		assertNotSame(cm1, cm2);
69  	}
70  
71  	@Test void b01_ignoreUnknownEnumValues() {
72  		var p1 = JsonParser.DEFAULT;
73  		assertThrowsWithMessage(Exception.class, "Could not resolve enum value 'UNKNOWN' on class 'org.apache.juneau.testutils.pojos.TestEnum'", () -> p1.parse("'UNKNOWN'", TestEnum.class));
74  
75  		var p2 = JsonParser.create().ignoreUnknownEnumValues().build();
76  		assertNull(p2.parse("'UNKNOWN'", TestEnum.class));
77  	}
78  
79  	//====================================================================================================
80  	// BeanContext.Builder locale, mediaType, and timeZone from Settings
81  	//====================================================================================================
82  
83  	@Test void c01_locale_fromSettings() {
84  		Settings.get().setLocal("BeanContext.locale", "fr-CA");
85  		try {
86  			var bc = BeanContext.create().build();
87  			assertEquals(Locale.forLanguageTag("fr-CA"), bc.getLocale());
88  		} finally {
89  			Settings.get().clearLocal();
90  		}
91  	}
92  
93  	@Test void c02_locale_defaultWhenNotSet() {
94  		var bc = BeanContext.create().build();
95  		assertEquals(Locale.getDefault(), bc.getLocale());
96  	}
97  
98  	@Test void c03_mediaType_fromSettings() {
99  		Settings.get().setLocal("BeanContext.mediaType", "application/json");
100 		try {
101 			var bc = BeanContext.create().build();
102 			assertEquals(MediaType.of("application/json"), bc.getMediaType());
103 		} finally {
104 			Settings.get().clearLocal();
105 		}
106 	}
107 
108 	@Test void c04_mediaType_nullWhenNotSet() {
109 		var bc = BeanContext.create().build();
110 		assertNull(bc.getMediaType());
111 	}
112 
113 	@Test void c05_timeZone_fromSettings() {
114 		Settings.get().setLocal("BeanContext.timeZone", "America/New_York");
115 		try {
116 			var bc = BeanContext.create().build();
117 			assertEquals(TimeZone.getTimeZone("America/New_York"), bc.getTimeZone());
118 		} finally {
119 			Settings.get().clearLocal();
120 		}
121 	}
122 
123 	@Test void c06_timeZone_nullWhenNotSet() {
124 		var bc = BeanContext.create().build();
125 		assertNull(bc.getTimeZone());
126 	}
127 
128 	//====================================================================================================
129 	// BeanContext.copy() - Copy constructor coverage
130 	//====================================================================================================
131 
132 	@Test void d01_copy() {
133 		// Create a BeanContext with some properties set to exercise the copy constructor
134 		var original = BeanContext.create()
135 			.sortProperties()
136 			.locale(Locale.CANADA)
137 			.mediaType(MediaType.JSON)
138 			.timeZone(TimeZone.getTimeZone("America/New_York"))
139 			.build();
140 
141 		// Call copy() which uses the copy constructor Builder(BeanContext copyFrom)
142 		// This exercises lines 273-277 which convert boolean fields from BeanContext to Builder disable flags
143 		var builder = original.copy();
144 
145 		// Build a new context from the copied builder
146 		var copied = builder.build();
147 
148 		// Verify the copied context has the same values
149 		assertEquals(original.getBeanClassVisibility(), copied.getBeanClassVisibility());
150 		assertEquals(original.getBeanConstructorVisibility(), copied.getBeanConstructorVisibility());
151 		assertEquals(original.getBeanFieldVisibility(), copied.getBeanFieldVisibility());
152 		assertEquals(original.getBeanMethodVisibility(), copied.getBeanMethodVisibility());
153 		assertEquals(original.getBeanDictionary(), copied.getBeanDictionary());
154 		assertEquals(original.getLocale(), copied.getLocale());
155 		assertEquals(original.getMediaType(), copied.getMediaType());
156 		assertEquals(original.getTimeZone(), copied.getTimeZone());
157 	}
158 
159 	//====================================================================================================
160 	// BeanContext.Builder.impl() - Line 2372 coverage
161 	//====================================================================================================
162 
163 	@Test void e01_impl() {
164 		// Create a BeanContext to use as the implementation
165 		var impl = BeanContext.create()
166 			.sortProperties()
167 			.locale(Locale.CANADA)
168 			.build();
169 
170 		// Call impl() which exercises line 2372: super.impl(value);
171 		var builder = BeanContext.create()
172 			.impl(impl);
173 
174 		// Build should return the pre-instantiated context
175 		var result = builder.build();
176 		assertSame(impl, result);
177 	}
178 }