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.transforms;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.collections.*;
23  import org.apache.juneau.swap.*;
24  import org.apache.juneau.swaps.*;
25  import org.junit.jupiter.api.*;
26  
27  class BeanMap_Test extends TestBase {
28  
29  	//====================================================================================================
30  	// testFilteredEntry
31  	//====================================================================================================
32  	@Test void a01_filteredEntry() {
33  		var session = BeanContext.create().swaps(ByteArraySwap.Base64.class).build().getSession();
34  		var m = session.toBeanMap(new A());
35  
36  		assertEquals("AQID", m.get("f1"));
37  		m.put("f1", "BAUG");
38  		assertEquals("BAUG", m.get("f1"));
39  		assertEquals(4, m.getBean().f1[0]);
40  
41  		assertNull(m.get("f3"));
42  	}
43  
44  	public static class A {
45  		public byte[] f1 = {1,2,3};
46  		public byte[] f3 = null;
47  	}
48  
49  	//====================================================================================================
50  	// testFilteredEntryWithMultipleMatchingFilters
51  	// When bean properties can have multiple filters applied to them, pick the first match.
52  	//====================================================================================================
53  	@Test void a02_filteredEntryWithMultipleMatchingFilters() {
54  		var session = BeanContext.create().swaps(B2Swap.class, B1Swap.class).build().getSession();
55  		var bm = session.toBeanMap(B.create());
56  		var m = (JsonMap)bm.get("b1");
57  		assertEquals("b2", m.getString("type"));
58  
59  		session = BeanContext.create().swaps(B1Swap.class, B2Swap.class).build().getSession();
60  		bm = session.toBeanMap(B.create());
61  		m = (JsonMap)bm.get("b1");
62  		assertEquals("b1", m.getString("type"));
63  	}
64  
65  
66  	public static class B {
67  		public B1 b1;
68  
69  		static B create() {
70  			var b = new B();
71  			var b2 = new B2();
72  			b2.f1 = "f1";
73  			b2.f2 = "f2";
74  			b.b1 = b2;
75  			return b;
76  		}
77  	}
78  
79  	public static class B1 {
80  		public String f1;
81  	}
82  
83  	public static class B2 extends B1 {
84  		public String f2;
85  	}
86  
87  	public static class B1Swap extends MapSwap<B1> {
88  		@Override /* ObjectSwap */
89  		public JsonMap swap(BeanSession session, B1 b1) {
90  			return JsonMap.of("type", "b1", "f1", b1.f1);
91  		}
92  	}
93  
94  	public static class B2Swap extends MapSwap<B2> {
95  		@Override /* ObjectSwap */
96  		public JsonMap swap(BeanSession session, B2 b2) {
97  			return JsonMap.of("type", "b2", "f1", b2.f1);
98  		}
99  	}
100 }