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.xml.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 XmlAnnotation_Test extends TestBase {
27  
28  	private static final String CNAME = XmlAnnotation_Test.class.getName();
29  
30  	//------------------------------------------------------------------------------------------------------------------
31  	// Basic tests
32  	//------------------------------------------------------------------------------------------------------------------
33  
34  	Xml a1 = XmlAnnotation.create()
35  		.childName("a")
36  		.description("b")
37  		.format(XmlFormat.ATTR)
38  		.namespace("c")
39  		.on("d")
40  		.prefix("e")
41  		.build();
42  
43  	Xml a2 = XmlAnnotation.create()
44  		.childName("a")
45  		.description("b")
46  		.format(XmlFormat.ATTR)
47  		.namespace("c")
48  		.on("d")
49  		.prefix("e")
50  		.build();
51  
52  	@Test void a01_basic() {
53  		assertBean(a1, "childName,description,format,namespace,on,onClass,prefix", "a,[b],ATTR,c,[d],[],e");
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() throws Exception {
86  		var c1 = XmlAnnotation.create(C1.class).on(C2.class).build();
87  		var c2 = XmlAnnotation.create("a").on("b").build();
88  		var c3 = XmlAnnotation.create().on(C1.class.getField("f1")).on(C2.class.getField("f2")).build();
89  		var c4 = XmlAnnotation.create().on(C1.class.getMethod("m1")).on(C2.class.getMethod("m2")).build();
90  
91  		assertBean(c1, "on", "["+CNAME+"$C1,"+CNAME+"$C2]");
92  		assertBean(c2, "on", "[a,b]");
93  		assertBean(c3, "on", "["+CNAME+"$C1.f1,"+CNAME+"$C2.f2]");
94  		assertBean(c4, "on", "["+CNAME+"$C1.m1(),"+CNAME+"$C2.m2()]");
95  	}
96  
97  	//------------------------------------------------------------------------------------------------------------------
98  	// Comparison with declared annotations.
99  	//------------------------------------------------------------------------------------------------------------------
100 
101 	@Xml(
102 		childName="a",
103 		description={ "b" },
104 		format=XmlFormat.ATTR,
105 		namespace="c",
106 		on="d",
107 		prefix="e"
108 	)
109 	public static class D1 {}
110 	Xml d1 = D1.class.getAnnotationsByType(Xml.class)[0];
111 
112 	@Xml(
113 		childName="a",
114 		description={ "b" },
115 		format=XmlFormat.ATTR,
116 		namespace="c",
117 		on="d",
118 		prefix="e"
119 	)
120 	public static class D2 {}
121 	Xml d2 = D2.class.getAnnotationsByType(Xml.class)[0];
122 
123 	@Test void d01_comparisonWithDeclarativeAnnotations() {
124 		assertEqualsAll(a1, d1, d2);
125 		assertNotEqualsAny(a1.hashCode(), 0, -1);
126 		assertEqualsAll(a1.hashCode(), d1.hashCode(), d2.hashCode());
127 	}
128 }