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