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 org.apache.juneau.annotation.*;
22  import org.junit.jupiter.api.*;
23  
24  class Annotations_Test extends TestBase {
25  
26  	//====================================================================================================
27  	// Bean with explicitly specified properties.
28  	//====================================================================================================
29  	@Test void a01_beanWithExplicitProperties() throws Exception {
30  		var bc = BeanContext.DEFAULT;
31  
32  		// Basic test
33  		var bm = bc.newBeanMap(Person1.class).load("{age:21,name:'foobar'}");
34  		assertBean(bm.getBean(), "name,age", "foobar,21");
35  
36  		bm.put("age", 65);
37  		bm.put("name", "futbol");
38  		assertMap(bm, "name,age", "futbol,65");
39  		assertBean(bm.getBean(), "name,age", "futbol,65");
40  	}
41  
42  	/** Class with explicitly specified properties */
43  	@Bean(properties="age,name")
44  	public static class Person1 {
45  		public int age;
46  
47  		private String name;
48  		public String getName() { return name; }
49  		public void setName(String v) { name = v; }
50  	}
51  
52  	@Test void a02_beanWithExplicitProperties2() throws Exception {
53  		var bc = BeanContext.DEFAULT;
54  
55  		// Basic test
56  		var bm = bc.newBeanMap(Person2.class).load("{age:21,name:'foobar'}");
57  		assertBean(bm.getBean(), "name,age", "foobar,21");
58  
59  		bm.put("age", 65);
60  		bm.put("name", "futbol");
61  		assertMap(bm, "name,age", "futbol,65");
62  		assertBean(bm.getBean(), "name,age", "futbol,65");
63  	}
64  
65  	/** Class with explicitly specified properties */
66  	@Bean(p="age,name")
67  	public static class Person2 {
68  		public int age;
69  
70  		private String name;
71  		public String getName() { return name; }
72  		public void setName(String v) { name = v; }
73  	}
74  
75  	@Test void a03_beanWithExplicitProperties3() throws Exception {
76  		var bc = BeanContext.DEFAULT;
77  
78  		// Basic test
79  		var bm = bc.newBeanMap(Person3.class).load("{age:21,name:'foobar'}");
80  		assertBean(bm.getBean(), "name,age", "foobar,21");
81  
82  		bm.put("age", 65);
83  		bm.put("name", "futbol");
84  		assertMap(bm, "name,age", "futbol,65");
85  		assertBean(bm.getBean(), "name,age", "futbol,65");
86  	}
87  
88  	/** Class with explicitly specified properties */
89  	@Bean(properties="age",p="name")
90  	public static class Person3 {
91  		public int age;
92  
93  		private String name;
94  		public String getName() { return name; }
95  		public void setName(String v) { name = v; }
96  	}
97  
98  	@Test void a04_beanWithExplicitProperties_usingConfig() throws Exception {
99  		var bc = BeanContext.DEFAULT.copy().applyAnnotations(PersonConfig.class).build();
100 
101 		// Basic test
102 		var bm = bc.newBeanMap(Person4.class).load("{age:21,name:'foobar'}");
103 		assertBean(bm.getBean(), "name,age", "foobar,21");
104 
105 		bm.put("age", 65);
106 		bm.put("name", "futbol");
107 		assertMap(bm, "name,age", "futbol,65");
108 		assertBean(bm.getBean(), "name,age", "futbol,65");
109 	}
110 
111 	/** Class with explicitly specified properties */
112 	public static class Person4 {
113 		public int age;
114 
115 		private String name;
116 		public String getName() { return name; }
117 		public void setName(String v) { name = v; }
118 	}
119 
120 	@Bean(on="Person4",properties="age,name")
121 	public static class PersonConfig {}
122 
123 	//====================================================================================================
124 	// Private/protected/default fields should be ignored.
125 	//====================================================================================================
126 	@Test void a05_forOnlyPublicFields() throws Exception {
127 		var bc = BeanContext.DEFAULT;
128 
129 		// Make sure only public fields are detected
130 		var bm = bc.newBeanMap(A.class).load("{publicField:123}");
131 		assertMap(bm, "publicField", "123");
132 	}
133 
134 	public static class A {
135 		public int publicField;
136 		protected int protectedField;
137 		@SuppressWarnings("unused")
138 		private int privateField;
139 		int defaultField;
140 	}
141 }