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 java.beans.*;
22  import java.util.*;
23  
24  import org.apache.juneau.annotation.*;
25  import org.apache.juneau.json.*;
26  import org.junit.jupiter.api.*;
27  
28  class BasicBeans_Test extends TestBase {
29  
30  	//------------------------------------------------------------------------------------------------------------------
31  	// Beans with transient fields and methods.
32  	//------------------------------------------------------------------------------------------------------------------
33  
34  	public static class A1 {
35  		public int f1;
36  		public transient int f2;
37  
38  		public static A1 create() {
39  			var x = new A1();
40  			x.f1 = 1;
41  			x.f2 = 2;
42  			return x;
43  		}
44  	}
45  
46  	@BeanConfig(disableIgnoreTransientFields="true")
47  	public static class A {}
48  
49  	@Test void a01_testTransientFieldsIgnored() {
50  		assertJson("{f1:1}", A1.create());
51  	}
52  
53  	@Test void a02_testTransientFieldsIgnored_overrideSetting() {
54  		assertSerialized(A1.create(), Json5Serializer.DEFAULT.copy().disableIgnoreTransientFields().build(), "{f1:1,f2:2}");
55  	}
56  
57  	@Test void a03_testTransientFieldsIgnored_overrideAnnotation() {
58  		assertSerialized(A1.create(), Json5Serializer.DEFAULT.copy().applyAnnotations(A.class).build(), "{f1:1,f2:2}");
59  	}
60  
61  	public static class A2 {
62  		public int f1;
63  
64  		private int f2;
65  		public void setF2(int v) { f2 = v; }
66  		@Transient public int getF2() { return f2; }
67  
68  		public static A2 create() {
69  			var x = new A2();
70  			x.f1 = 1;
71  			x.f2 = 2;
72  			return x;
73  		}
74  	}
75  
76  	@Test void a04_testTransientMethodsIgnored() {
77  		assertJson("{f1:1}", A2.create());
78  	}
79  
80  	//------------------------------------------------------------------------------------------------------------------
81  	// Bean with dyna property
82  	//------------------------------------------------------------------------------------------------------------------
83  
84  	public static class B {
85  		@Beanp(name="*")
86  		public Map<String,Integer> f1 = new TreeMap<>();
87  
88  		public static B create() {
89  			var x = new B();
90  			x.f1.put("a", 1);
91  			return x;
92  		}
93  	}
94  
95  	@Test void b01_beanWithDynaProperty() throws Exception {
96  		assertJson("{a:1}", B.create());
97  
98  		var b = JsonParser.DEFAULT.parse("{a:1}", B.class);
99  		assertJson("{a:1}", b);
100 	}
101 }