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