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 org.apache.juneau.json.*;
23  import org.apache.juneau.testutils.pojos.*;
24  import org.junit.jupiter.api.*;
25  
26  class BeanContext_Test extends TestBase {
27  
28  	BeanContext bc = BeanContext.DEFAULT;
29  	BeanSession bs = BeanContext.DEFAULT_SESSION;
30  
31  	public interface A1 {
32  		int getF1();
33  		void setF1(int f1);
34  	}
35  
36  	@Test void a01_normalCachableBean() throws ExecutableException {
37  		var cm1 = bc.getClassMeta(A1.class);
38  		var cm2 = bc.getClassMeta(A1.class);
39  		assertSame(cm1, cm2);
40  	}
41  
42  	interface A2 {
43  		void foo(int x);
44  	}
45  
46  	@Test void a02_lambdaExpressionsNotCached() throws ExecutableException {
47  		var bc2 = BeanContext.DEFAULT;
48  		var fi = (A2)System.out::println;
49  		var cm1 = bc2.getClassMeta(fi.getClass());
50  		var cm2 = bc2.getClassMeta(fi.getClass());
51  		assertNotSame(cm1, cm2);
52  	}
53  
54  	@Test void a03_proxiesNotCached() throws ExecutableException {
55  		var a1 = bs.getBeanMeta(A1.class).newBean(null);
56  		var cm1 = bc.getClassMeta(a1.getClass());
57  		var cm2 = bc.getClassMeta(a1.getClass());
58  		assertNotSame(cm1, cm2);
59  	}
60  
61  	@Test void b01_ignoreUnknownEnumValues() {
62  		var p1 = JsonParser.DEFAULT;
63  		assertThrowsWithMessage(Exception.class, "Could not resolve enum value 'UNKNOWN' on class 'org.apache.juneau.testutils.pojos.TestEnum'", () -> p1.parse("'UNKNOWN'", TestEnum.class));
64  
65  		var p2 = JsonParser.create().ignoreUnknownEnumValues().build();
66  		assertNull(p2.parse("'UNKNOWN'", TestEnum.class));
67  	}
68  }