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  import static org.junit.jupiter.api.Assertions.*;
21  
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 BeanFilter_Test extends TestBase {
29  
30  	//====================================================================================================
31  	// Test sub types
32  	//====================================================================================================
33  	@Test void a01_subTypes() throws Exception {
34  		var s = Json5Serializer.DEFAULT.copy().addBeanTypes().addRootType().build();
35  		var p = JsonParser.DEFAULT;
36  
37  		var a1 = new A1();
38  		a1.f1 = "f1";
39  		a1.fb = new B2();
40  		((B2)a1.fb).f2 = "f2";
41  		var r = s.serialize(a1);
42  		assertEquals("{_type:'A1',f0:'f0',fb:{_type:'B2',f0b:'f0b',f2:'f2'},f1:'f1'}", r);
43  
44  		assertBean(
45  			p.parse(r, A.class),
46  			"class{simpleName},fb{class{simpleName}},f1,fb{f2}",
47  			"{A1},{{B2}},f1,{f2}"
48  		);
49  
50  		// Try out-of-order creation.
51  		assertBean(
52  			p.parse("{f0:'f0',f1:'f1',_type:'A1',fb:{f0b:'f0b',f2:'f2',_type:'B2'}}", A.class),
53  			"class{simpleName},fb{class{simpleName}},f1,fb{f2}",
54  			"{A1},{{B2}},f1,{f2}"
55  		);
56  	}
57  
58  	@Bean(dictionary={A1.class, A2.class})
59  	public abstract static class A {
60  		public String f0 = "f0";
61  		public B fb;
62  	}
63  
64  	@Bean(typeName="A1")
65  	public static class A1 extends A {
66  		public String f1;
67  	}
68  
69  	@Bean(typeName="A2")
70  	public static class A2 extends A {
71  		public String f2;
72  	}
73  
74  	@Bean(dictionary={B1.class,B2.class})
75  	public abstract static class B {
76  		public String f0b = "f0b";
77  	}
78  
79  	@Bean(typeName="B1")
80  	public static class B1 extends B {
81  		public String f1;
82  	}
83  
84  	@Bean(typeName="B2")
85  	public static class B2 extends B {
86  		public String f2;
87  	}
88  
89  	@Test void a02_subTypes_usingConfig() throws Exception {
90  		var s = Json5Serializer.DEFAULT.copy().addBeanTypes().addRootType().applyAnnotations(EConfig.class).build();
91  		var p = JsonParser.create().applyAnnotations(EConfig2.class).build();
92  
93  		var e1 = new E1();
94  		e1.f1 = "f1";
95  		e1.fb = new F2();
96  		((F2)e1.fb).f2 = "f2";
97  		var r = s.serialize(e1);
98  		assertEquals("{_type:'E1',f0:'f0',fb:{_type:'F2',f0b:'f0b',f2:'f2'},f1:'f1'}", r);
99  
100 		assertBean(
101 			p.parse(r, E.class),
102 			"class{simpleName},fb{class{simpleName}},f1,fb{f2}",
103 			"{E1},{{F2}},f1,{f2}"
104 		);
105 
106 		// Try out-of-order creation.
107 		r = "{f0:'f0',f1:'f1',_type:'E1',fb:{f0b:'f0b',f2:'f2',_type:'F2'}}";
108 		assertBean(
109 			p.parse(r, E.class),
110 			"class{simpleName},fb{class{simpleName}},f1,fb{f2}",
111 			"{E1},{{F2}},f1,{f2}"
112 		);
113 	}
114 
115 	@Bean(on="E", dictionary={E1.class, E2.class})
116 	@Bean(on="E1", typeName="E1")
117 	@Bean(on="E2", typeName="E2")
118 	@Bean(on="F", dictionary={F1.class,F2.class})
119 	@Bean(on="F1", typeName="F1")
120 	@Bean(on="F2", typeName="F2")
121 	private static class EConfig {}
122 
123 	private static class EConfig2 extends EConfig {}
124 
125 	public abstract static class E {
126 		public String f0 = "f0";
127 		public F fb;
128 	}
129 
130 	public static class E1 extends E {
131 		public String f1;
132 	}
133 
134 	public static class E2 extends E {
135 		public String f2;
136 	}
137 
138 	public abstract static class F {
139 		public String f0b = "f0b";
140 	}
141 
142 	public static class F1 extends F {
143 		public String f1;
144 	}
145 
146 	public static class F2 extends F {
147 		public String f2;
148 	}
149 
150 	//====================================================================================================
151 	// Test parent class used as filter
152 	//====================================================================================================
153 	@Test void a03_parentClassFilter() throws Exception {
154 		var s = JsonSerializer.create().json5().interfaces(C1.class).build();
155 
156 		var c1 = new C2();
157 		var r = s.serialize(c1);
158 		assertEquals("{f0:'f0'}", r);
159 
160 		var l = new LinkedList<>();
161 		l.add(new C2());
162 		r = s.serialize(l);
163 		assertEquals("[{f0:'f0'}]", r);
164 	}
165 
166 	public static class C1 {
167 		public String f0 = "f0";
168 	}
169 
170 	public static class C2 extends C1 {
171 		public String f1 = "f1";
172 	}
173 
174 	//====================================================================================================
175 	// Test non-static parent class used as filter
176 	//====================================================================================================
177 	@Test void a04_parentClassFilter2() throws Exception {
178 		var s = JsonSerializer.create().json5().interfaces(D1.class).build();
179 
180 		var d1 = new D2();
181 		var r = s.serialize(d1);
182 		assertEquals("{f0:'f0'}", r);
183 
184 		var l = new LinkedList<>();
185 		l.add(new D2());
186 		r = s.serialize(l);
187 		assertEquals("[{f0:'f0'}]", r);
188 	}
189 
190 	public class D1 {
191 		public String f0 = "f0";
192 	}
193 
194 	public class D2 extends D1 {
195 		public String f1 = "f1";
196 	}
197 }