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