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