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