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.junit.jupiter.api.Assertions.*;
20  
21  import java.util.Map.*;
22  
23  import org.apache.juneau.annotation.*;
24  import org.junit.jupiter.api.*;
25  
26  /**
27   * Tests various error conditions when defining beans.
28   */
29  class BeanMapErrors_Test extends TestBase {
30  
31  	//-----------------------------------------------------------------------------------------------------------------
32  	// @Beanp(name) on method not in @Bean(properties)
33  	// JUNEAU-248: Shouldn't be found in keySet()/entrySet()/containsKey() but should be accessible via get()/put()
34  	//-----------------------------------------------------------------------------------------------------------------
35  	@Test void beanPropertyMethodNotInBeanProperties() {
36  		var bc = BeanContext.DEFAULT;
37  
38  		var bm = bc.newBeanMap(A1.class);
39  		assertFalse(bm.containsKey("f2"));  // JUNEAU-248: Now consistent with keySet()
40  		assertEquals(-1, bm.get("f2"));      // But get() still works
41  		bm.put("f2", -2);                    // And put() still works
42  		assertEquals(-2, bm.get("f2"));
43  		assertFalse(bm.keySet().contains("f2"));
44  		assertFalse(bm.entrySet().stream().map(Entry::getKey).toList().contains("f2"));
45  	}
46  
47  	@Bean(p="f1")
48  	public static class A1 {
49  		public int f1;
50  
51  		private int f2 = -1;
52  		@Beanp("f2") public int f2() { return f2; }
53  		public void setF2(int v) { f2 = v; }
54  	}
55  
56  	@Test void beanPropertyMethodNotInBeanProperties_usingConfig() {
57  		var bc = BeanContext.create().applyAnnotations(B1Config.class).build();
58  
59  		var bm = bc.newBeanMap(B1.class);
60  		assertFalse(bm.containsKey("f2"));  // JUNEAU-248: Now consistent with keySet()
61  		assertEquals(-1, bm.get("f2"));      // But get() still works
62  		bm.put("f2", -2);                    // And put() still works
63  		assertEquals(-2, bm.get("f2"));
64  		assertFalse(bm.keySet().contains("f2"));
65  		assertFalse(bm.entrySet().stream().map(Entry::getKey).toList().contains("f2"));
66  	}
67  
68  	@Bean(on="Dummy",p="dummy")
69  	@Bean(on="B1", p="f1")
70  	@Beanp(on="Dummy", value="dummy")
71  	@Beanp(on="B1.f2", value="f2")
72  	private static class B1Config {}
73  
74  	public static class B1 {
75  		public int f1;
76  
77  		private int f2 = -1;
78  		@Beanp("f2") public int f2() { return f2; }
79  		public void setF2(int v) { f2 = v; }
80  	}
81  
82  	//-----------------------------------------------------------------------------------------------------------------
83  	// @Beanp(name) on field not in @Bean(properties)
84  	// JUNEAU-248: Shouldn't be found in keySet()/entrySet()/containsKey() but should be accessible via get()/put()
85  	//-----------------------------------------------------------------------------------------------------------------
86  	@Test void beanPropertyFieldNotInBeanProperties() {
87  		var bc = BeanContext.DEFAULT;
88  
89  		var bm = bc.newBeanMap(A2.class);
90  		assertFalse(bm.containsKey("f2"));  // JUNEAU-248: Now consistent with keySet()
91  		assertEquals(-1, bm.get("f2"));      // But get() still works
92  		bm.put("f2", -2);                    // And put() still works
93  		assertEquals(-2, bm.get("f2"));
94  		assertFalse(bm.keySet().contains("f2"));
95  		assertFalse(bm.entrySet().stream().map(Entry::getKey).toList().contains("f2"));
96  	}
97  
98  	@Bean(p="f1")
99  	public static class A2 {
100 		public int f1;
101 
102 		@Beanp("f2")
103 		public int f2 = -1;
104 	}
105 
106 	@Test void beanPropertyFieldNotInBeanConfig() {
107 		var bc = BeanContext.create().applyAnnotations(B2Config.class).build();
108 
109 		var bm = bc.newBeanMap(B2.class);
110 		assertFalse(bm.containsKey("f2"));  // JUNEAU-248: Now consistent with keySet()
111 		assertEquals(-1, bm.get("f2"));      // But get() still works
112 		bm.put("f2", -2);                    // And put() still works
113 		assertEquals(-2, bm.get("f2"));
114 		assertFalse(bm.keySet().contains("f2"));
115 		assertFalse(bm.entrySet().stream().map(Entry::getKey).toList().contains("f2"));
116 	}
117 
118 	@Bean(on="Dummy",p="dummy")
119 	@Bean(on="B2", p="f1")
120 	@Beanp(on="Dummy", value="dummy")
121 	@Beanp(on="B2.f2", value="f2")
122 	private static class B2Config {}
123 
124 	public static class B2 {
125 		public int f1;
126 		public int f2 = -1;
127 	}
128 }