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.junit.jupiter.api.*;
24  
25  class BeanpAnnotation_Test extends TestBase {
26  
27  	private static final String CNAME = BeanpAnnotation_Test.class.getName();
28  
29  	public static class X1 {}
30  
31  	//------------------------------------------------------------------------------------------------------------------
32  	// Basic tests
33  	//------------------------------------------------------------------------------------------------------------------
34  
35  	Beanp a1 = BeanpAnnotation.create()
36  		.description("a")
37  		.dictionary(X1.class)
38  		.format("b")
39  		.name("c")
40  		.on("d")
41  		.params(X1.class)
42  		.properties("e")
43  		.ro("f")
44  		.type(X1.class)
45  		.value("g")
46  		.wo("h")
47  		.build();
48  
49  	Beanp a2 = BeanpAnnotation.create()
50  		.description("a")
51  		.dictionary(X1.class)
52  		.format("b")
53  		.name("c")
54  		.on("d")
55  		.params(X1.class)
56  		.properties("e")
57  		.ro("f")
58  		.type(X1.class)
59  		.value("g")
60  		.wo("h")
61  		.build();
62  
63  	@Test void a01_basic() {
64  		assertBean(a1, "description,dictionary,format,name,on,params,properties,ro,type,value,wo", "[a],[X1],b,c,[d],[X1],e,f,X1,g,h");
65  	}
66  
67  	@Test void a02_testEquivalency() {
68  		assertEquals(a2, a1);
69  		assertNotEqualsAny(a2.hashCode(), 0, -1);
70  		assertEquals(a2.hashCode(), a1.hashCode());
71  	}
72  
73  	//------------------------------------------------------------------------------------------------------------------
74  	// PropertyStore equivalency.
75  	//------------------------------------------------------------------------------------------------------------------
76  
77  	@Test void b01_testEquivalencyInPropertyStores() {
78  		var bc1 = BeanContext.create().annotations(a1).build();
79  		var bc2 = BeanContext.create().annotations(a2).build();
80  		assertSame(bc1, bc2);
81  	}
82  
83  	//------------------------------------------------------------------------------------------------------------------
84  	// Other methods.
85  	//------------------------------------------------------------------------------------------------------------------
86  
87  	public static class C1 {
88  		public int f1;
89  		public void m1() {}  // NOSONAR
90  	}
91  	public static class C2 {
92  		public int f2;
93  		public void m2() {}  // NOSONAR
94  	}
95  
96  	@Test void c01_otherMethods() throws Exception {
97  		var c1 = BeanpAnnotation.create("a").on("b").build();
98  		var c2 = BeanpAnnotation.create().on(C1.class.getField("f1")).on(C2.class.getField("f2")).build();
99  		var c3 = BeanpAnnotation.create().on(C1.class.getMethod("m1")).on(C2.class.getMethod("m2")).build();
100 
101 		assertBean(c1, "on", "[a,b]");
102 		assertBean(c2, "on", "["+CNAME+"$C1.f1,"+CNAME+"$C2.f2]");
103 		assertBean(c3, "on", "["+CNAME+"$C1.m1(),"+CNAME+"$C2.m2()]");
104 	}
105 
106 	//------------------------------------------------------------------------------------------------------------------
107 	// Comparison with declared annotations.
108 	//------------------------------------------------------------------------------------------------------------------
109 
110 	@Beanp(
111 		description={ "a" },
112 		dictionary=X1.class,
113 		format="b",
114 		name="c",
115 		on="d",
116 		params=X1.class,
117 		properties="e",
118 		ro="f",
119 		type=X1.class,
120 		value="g",
121 		wo="h"
122 	)
123 	public static class D1 {}
124 	Beanp d1 = D1.class.getAnnotationsByType(Beanp.class)[0];
125 
126 	@Beanp(
127 		description={ "a" },
128 		dictionary=X1.class,
129 		format="b",
130 		name="c",
131 		on="d",
132 		params=X1.class,
133 		properties="e",
134 		ro="f",
135 		type=X1.class,
136 		value="g",
137 		wo="h"
138 	)
139 	public static class D2 {}
140 	Beanp d2 = D2.class.getAnnotationsByType(Beanp.class)[0];
141 
142 	@Test void d01_comparisonWithDeclarativeAnnotations() {
143 		assertEqualsAll(a1, d1, d2);
144 		assertNotEqualsAny(a1.hashCode(), 0, -1);
145 		assertEqualsAll(a1.hashCode(), d1.hashCode(), d2.hashCode());
146 	}
147 }