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  import static org.apache.juneau.junit.bct.BctAssertions.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import org.apache.juneau.*;
24  import org.apache.juneau.swap.*;
25  import org.junit.jupiter.api.*;
26  
27  class BeanAnnotation_Test extends TestBase {
28  
29  	private static final String CNAME = BeanAnnotation_Test.class.getName();
30  
31  	private static class X1 {}
32  	private  static class X2 extends BeanInterceptor<BeanAnnotation_Test> {}
33  
34  	//------------------------------------------------------------------------------------------------------------------
35  	// Basic tests
36  	//------------------------------------------------------------------------------------------------------------------
37  
38  	Bean a1 = BeanAnnotation.create()
39  		.description("a")
40  		.dictionary(X1.class)
41  		.example("b")
42  		.excludeProperties("c")
43  		.findFluentSetters(true)
44  		.implClass(X1.class)
45  		.interceptor(X2.class)
46  		.interfaceClass(X1.class)
47  		.on("d")
48  		.onClass(X1.class)
49  		.p("e")
50  		.properties("f")
51  		.propertyNamer(BasicPropertyNamer.class)
52  		.readOnlyProperties("g")
53  		.ro("h")
54  		.sort(true)
55  		.stopClass(X1.class)
56  		.typeName("i")
57  		.typePropertyName("j")
58  		.wo("k")
59  		.writeOnlyProperties("l")
60  		.xp("m")
61  		.build();
62  
63  	Bean a2 = BeanAnnotation.create()
64  		.description("a")
65  		.dictionary(X1.class)
66  		.example("b")
67  		.excludeProperties("c")
68  		.findFluentSetters(true)
69  		.implClass(X1.class)
70  		.interceptor(X2.class)
71  		.interfaceClass(X1.class)
72  		.on("d")
73  		.onClass(X1.class)
74  		.p("e")
75  		.properties("f")
76  		.propertyNamer(BasicPropertyNamer.class)
77  		.readOnlyProperties("g")
78  		.ro("h")
79  		.sort(true)
80  		.stopClass(X1.class)
81  		.typeName("i")
82  		.typePropertyName("j")
83  		.wo("k")
84  		.writeOnlyProperties("l")
85  		.xp("m")
86  		.build();
87  
88  	@Test void a01_basic() {
89  		assertBean(a1,
90  			"description,dictionary,example,excludeProperties,findFluentSetters,implClass,interceptor,interfaceClass,on,onClass,p,properties,propertyNamer,readOnlyProperties,ro,sort,stopClass,typeName,typePropertyName,wo,writeOnlyProperties,xp",
91  			"[a],[X1],b,c,true,X1,X2,X1,[d],[X1],e,f,BasicPropertyNamer,g,h,true,X1,i,j,k,l,m");
92  	}
93  
94  	@Test void a02_testEquivalency() {
95  		assertEquals(a2, a1);
96  		assertNotEquals(0, a1.hashCode());
97  		assertNotEquals(-1, a1.hashCode());
98  		assertEquals(a1.hashCode(), a2.hashCode());
99  	}
100 
101 	//------------------------------------------------------------------------------------------------------------------
102 	// PropertyStore equivalency.
103 	//------------------------------------------------------------------------------------------------------------------
104 
105 	@Test void b01_testEquivalencyInPropertyStores() {
106 		var b1 = BeanContext.create().annotations(a1).build();
107 		var b2 = BeanContext.create().annotations(a2).build();
108 		assertSame(b1, b2);
109 	}
110 
111 	//------------------------------------------------------------------------------------------------------------------
112 	// Other methods.
113 	//------------------------------------------------------------------------------------------------------------------
114 
115 	public static class C1 {}
116 	public static class C2 {}
117 
118 	@Test void c01_otherMethods() {
119 		var c1 = BeanAnnotation.create(C1.class).on(C2.class).build();
120 		var c2 = BeanAnnotation.create("a").on("b").build();
121 
122 		assertBean(c1, "on", "["+CNAME+"$C1,"+CNAME+"$C2]");
123 		assertBean(c2, "on", "[a,b]");
124 	}
125 
126 	//------------------------------------------------------------------------------------------------------------------
127 	// Comparison with declared annotations.
128 	//------------------------------------------------------------------------------------------------------------------
129 
130 	@Bean(
131 		description={ "a" },
132 		dictionary=X1.class,
133 		example="b",
134 		excludeProperties="c",
135 		findFluentSetters=true,
136 		implClass=X1.class,
137 		interceptor=X2.class,
138 		interfaceClass=X1.class,
139 		on="d",
140 		onClass=X1.class,
141 		p="e",
142 		properties="f",
143 		propertyNamer=BasicPropertyNamer.class,
144 		readOnlyProperties="g",
145 		ro="h",
146 		sort=true,
147 		stopClass=X1.class,
148 		typeName="i",
149 		typePropertyName="j",
150 		wo="k",
151 		writeOnlyProperties="l",
152 		xp="m"
153 	)
154 	public static class D1 {}
155 	Bean d1 = D1.class.getAnnotationsByType(Bean.class)[0];
156 
157 	@Bean(
158 		description={ "a" },
159 		dictionary=X1.class,
160 		example="b",
161 		excludeProperties="c",
162 		findFluentSetters=true,
163 		implClass=X1.class,
164 		interceptor=X2.class,
165 		interfaceClass=X1.class,
166 		on="d",
167 		onClass=X1.class,
168 		p="e",
169 		properties="f",
170 		propertyNamer=BasicPropertyNamer.class,
171 		readOnlyProperties="g",
172 		ro="h",
173 		sort=true,
174 		stopClass=X1.class,
175 		typeName="i",
176 		typePropertyName="j",
177 		wo="k",
178 		writeOnlyProperties="l",
179 		xp="m"
180 	)
181 	public static class D2 {}
182 	Bean d2 = D2.class.getAnnotationsByType(Bean.class)[0];
183 
184 	@Test void d01_comparisonWithDeclarativeAnnotations() {
185 		assertEqualsAll(a1, d1, d2);
186 		assertNotEqualsAny(a1.hashCode(), 0, -1);
187 		assertEqualsAll(a1.hashCode(), d1.hashCode(), d2.hashCode());
188 	}
189 }