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 MarshalledAnnotation_Test extends TestBase {
26  
27  	private static final String CNAME = MarshalledAnnotation_Test.class.getName();
28  
29  	private static class X1 {}
30  
31  	//------------------------------------------------------------------------------------------------------------------
32  	// Basic tests
33  	//------------------------------------------------------------------------------------------------------------------
34  
35  	Marshalled a1 = MarshalledAnnotation.create()
36  		.description("a")
37  		.example("b")
38  		.implClass(X1.class)
39  		.on("c")
40  		.onClass(X1.class)
41  		.build();
42  
43  	Marshalled a2 = MarshalledAnnotation.create()
44  		.description("a")
45  		.example("b")
46  		.implClass(X1.class)
47  		.on("c")
48  		.onClass(X1.class)
49  		.build();
50  
51  	@Test void a01_basic() {
52  		assertBean(a1, "description,example,implClass,on,onClass", "[a],b,X1,[c],[X1]");
53  	}
54  
55  	@Test void a02_testEquivalency() {
56  		assertEquals(a2, a1);
57  		assertNotEqualsAny(a1.hashCode(), 0, -1);
58  		assertEquals(a1.hashCode(), a2.hashCode());
59  	}
60  
61  	//------------------------------------------------------------------------------------------------------------------
62  	// PropertyStore equivalency.
63  	//------------------------------------------------------------------------------------------------------------------
64  
65  	@Test void b01_testEquivalencyInPropertyStores() {
66  		var bc1 = BeanContext.create().annotations(a1).build();
67  		var bc2 = BeanContext.create().annotations(a2).build();
68  		assertSame(bc1, bc2);
69  	}
70  
71  	//------------------------------------------------------------------------------------------------------------------
72  	// Other methods.
73  	//------------------------------------------------------------------------------------------------------------------
74  
75  	public static class C1 {
76  		public int f1;
77  		public void m1() {}  // NOSONAR
78  	}
79  	public static class C2 {
80  		public int f2;
81  		public void m2() {}  // NOSONAR
82  	}
83  
84  	@Test void c01_otherMethods() {
85  		var c1 = MarshalledAnnotation.create(C1.class).on(C2.class).build();
86  		var c2 = MarshalledAnnotation.create("a").on("b").build();
87  
88  		assertBean(c1, "on", "["+CNAME+"$C1,"+CNAME+"$C2]");
89  		assertBean(c2, "on", "[a,b]");
90  	}
91  
92  	//------------------------------------------------------------------------------------------------------------------
93  	// Comparison with declared annotations.
94  	//------------------------------------------------------------------------------------------------------------------
95  
96  	@Marshalled(
97  		description={ "a" },
98  		example="b",
99  		implClass=X1.class,
100 		on="c",
101 		onClass=X1.class
102 	)
103 	public static class D1 {}
104 	Marshalled d1 = D1.class.getAnnotationsByType(Marshalled.class)[0];
105 
106 	@Marshalled(
107 		description={ "a" },
108 		example="b",
109 		implClass=X1.class,
110 		on="c",
111 		onClass=X1.class
112 	)
113 	public static class D2 {}
114 	Marshalled d2 = D2.class.getAnnotationsByType(Marshalled.class)[0];
115 
116 	@Test void d01_comparisonWithDeclarativeAnnotations() {
117 		assertEqualsAll(a1, d1, d2);
118 		assertNotEqualsAny(a1.hashCode(), 0, -1);
119 		assertEqualsAll(a1.hashCode(), d1.hashCode(), d2.hashCode());
120 	}
121 }