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.annotation;
18  
19  import static org.apache.juneau.TestUtils.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.json.*;
23  import org.junit.jupiter.api.*;
24  
25  class BeanIgnore_Test extends TestBase {
26  
27  	//------------------------------------------------------------------------------------------------------------------
28  	// Test @BeanIgnore on properties
29  	//------------------------------------------------------------------------------------------------------------------
30  
31  	public static class A {
32  		public String getA() {
33  			return "a";
34  		}
35  
36  		@BeanIgnore
37  		public String getB() {
38  			return "b";
39  		}
40  
41  		public String c = "c";
42  
43  		@BeanIgnore public String d = "d";
44  	}
45  
46  	@Test void a01_beanIgnoreOnProperties() {
47  		assertJson("{a:'a',c:'c'}", new A());
48  	}
49  
50  	@BeanIgnore(on="Ac.getB")
51  	@BeanIgnore(on="Ac.d")
52  	private static class AcConfig {}
53  
54  	public static class Ac {
55  		public String getA() {
56  			return "a";
57  		}
58  
59  		public String getB() {
60  			return "b";
61  		}
62  
63  		public String c = "c";
64  
65  		public String d = "d";
66  	}
67  
68  	@Test void a02_beanIgnoreOnProperties_usingConfig() {
69  		assertSerialized(new Ac(), Json5Serializer.DEFAULT.copy().applyAnnotations(AcConfig.class).build(), "{c:'c',a:'a'}");
70  	}
71  
72  	//------------------------------------------------------------------------------------------------------------------
73  	// Test @BeanIgnore on classes
74  	//------------------------------------------------------------------------------------------------------------------
75  
76  	@BeanIgnore
77  	public static class B1 {
78  		public int f = 1;
79  
80  		@Override
81  		public String toString() {
82  			return "xxx";
83  		}
84  	}
85  
86  	public static class B {
87  		public int f2 = 2;
88  		public B1 f3 = new B1();
89  
90  		public B1 getF4() {
91  			return new B1();
92  		}
93  	}
94  
95  	@Test void a03_beanIgnoreOnBean() {
96  		assertJson("{f2:2,f3:'xxx',f4:'xxx'}", new B());
97  	}
98  
99  	@BeanIgnore(on="B1c")
100 	private static class B1cConfig {}
101 
102 	public static class B1c {
103 		public int f = 1;
104 
105 		@Override
106 		public String toString() {
107 			return "xxx";
108 		}
109 	}
110 
111 	public static class Bc {
112 		public int f2 = 2;
113 		public B1c f3 = new B1c();
114 
115 		public B1c getF4() {
116 			return new B1c();
117 		}
118 	}
119 
120 	@Test void a04_beanIgnoreOnBean_usingConfig() {
121 		assertSerialized(new Bc(), Json5Serializer.DEFAULT.copy().applyAnnotations(B1cConfig.class).build(), "{f2:2,f3:'xxx',f4:'xxx'}");
122 	}
123 }