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