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