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