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  	// Shouldn't be found in keySet()/entrySet() but should be found in containsKey()/get()
34  	//-----------------------------------------------------------------------------------------------------------------
35  	@Test void beanPropertyMethodNotInBeanProperties() {
36  		var bc = BeanContext.DEFAULT;
37  
38  		var bm = bc.newBeanMap(A1.class);
39  		assertTrue(bm.containsKey("f2"));
40  		assertEquals(-1, bm.get("f2"));
41  		bm.put("f2", -2);
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  		assertTrue(bm.containsKey("f2"));
61  		assertEquals(-1, bm.get("f2"));
62  		bm.put("f2", -2);
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  	//-----------------------------------------------------------------------------------------------------------------
85  	@Test void beanPropertyFieldNotInBeanProperties() {
86  		var bc = BeanContext.DEFAULT;
87  
88  		var bm = bc.newBeanMap(A2.class);
89  		assertTrue(bm.containsKey("f2"));
90  		assertEquals(-1, bm.get("f2"));
91  		bm.put("f2", -2);
92  		assertEquals(-2, bm.get("f2"));
93  		assertFalse(bm.keySet().contains("f2"));
94  		assertFalse(bm.entrySet().stream().map(Entry::getKey).toList().contains("f2"));
95  	}
96  
97  	@Bean(p="f1")
98  	public static class A2 {
99  		public int f1;
100 
101 		@Beanp("f2")
102 		public int f2 = -1;
103 	}
104 
105 	@Test void beanPropertyFieldNotInBeanProperties_usingBeanConfig() {
106 		var bc = BeanContext.create().applyAnnotations(B2Config.class).build();
107 
108 		var bm = bc.newBeanMap(B2.class);
109 		assertTrue(bm.containsKey("f2"));
110 		assertEquals(-1, bm.get("f2"));
111 		bm.put("f2", -2);
112 		assertEquals(-2, bm.get("f2"));
113 		assertFalse(bm.keySet().contains("f2"));
114 		assertFalse(bm.entrySet().stream().map(Entry::getKey).toList().contains("f2"));
115 	}
116 
117 	@Bean(on="Dummy",p="dummy")
118 	@Bean(on="B2", p="f1")
119 	@Beanp(on="Dummy", value="dummy")
120 	@Beanp(on="B2.f2", value="f2")
121 	private static class B2Config {}
122 
123 	public static class B2 {
124 		public int f1;
125 		public int f2 = -1;
126 	}
127 }