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