View Javadoc
1   // ***************************************************************************************************************************
2   // * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
3   // * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
4   // * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
5   // * with the License.  You may obtain a copy of the License at                                                              *
6   // *                                                                                                                         *
7   // *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
8   // *                                                                                                                         *
9   // * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
10  // * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
11  // * specific language governing permissions and limitations under the License.                                              *
12  // ***************************************************************************************************************************
13  package org.apache.juneau.rest.beans;
14  
15  import static org.junit.jupiter.api.Assertions.*;
16  
17  import org.apache.juneau.*;
18  import org.apache.juneau.html.*;
19  import org.junit.jupiter.api.*;
20  
21  /**
22   * Tests for Hyperlink fluent setter overrides.
23   */
24  class Hyperlink_Test extends TestBase {
25  
26  	@Test void a01_fluentChaining_anchorAttributes() {
27  		var h = new Hyperlink();
28  
29  		// Test that fluent methods return Hyperlink (not A)
30  		Hyperlink result;
31  
32  		result = h.href("/foo");
33  		assertSame(h, result);
34  		assertInstanceOf(Hyperlink.class, result);
35  
36  		result = h.download("file.pdf");
37  		assertSame(h, result);
38  
39  		result = h.hreflang("en");
40  		assertSame(h, result);
41  
42  		result = h.rel("nofollow");
43  		assertSame(h, result);
44  
45  		result = h.target("_blank");
46  		assertSame(h, result);
47  
48  		result = h.type("text/html");
49  		assertSame(h, result);
50  	}
51  
52  	@Test void a02_fluentChaining_globalAttributes() {
53  		var h = new Hyperlink();
54  
55  		// Test global HTML attributes
56  		Hyperlink result;
57  
58  		result = h._class("link-class");
59  		assertSame(h, result);
60  		assertInstanceOf(Hyperlink.class, result);
61  
62  		result = h.id("link-id");
63  		assertSame(h, result);
64  
65  		result = h.style("color:blue");
66  		assertSame(h, result);
67  
68  		result = h.title("Link title");
69  		assertSame(h, result);
70  
71  		result = h.lang("en");
72  		assertSame(h, result);
73  
74  		result = h.accesskey("l");
75  		assertSame(h, result);
76  
77  		result = h.contenteditable(true);
78  		assertSame(h, result);
79  
80  		result = h.dir("ltr");
81  		assertSame(h, result);
82  
83  		result = h.hidden(false);
84  		assertSame(h, result);
85  
86  		result = h.spellcheck(true);
87  		assertSame(h, result);
88  
89  		result = h.tabindex(1);
90  		assertSame(h, result);
91  
92  		result = h.translate(false);
93  		assertSame(h, result);
94  	}
95  
96  	@Test void a03_fluentChaining_eventHandlers() {
97  		var h = new Hyperlink();
98  
99  		// Test event handler attributes
100 		Hyperlink result;
101 
102 		result = h.onclick("alert('clicked')");
103 		assertSame(h, result);
104 		assertInstanceOf(Hyperlink.class, result);
105 
106 		result = h.onmouseover("console.log('hover')");
107 		assertSame(h, result);
108 
109 		result = h.onfocus("console.log('focus')");
110 		assertSame(h, result);
111 
112 		result = h.onblur("console.log('blur')");
113 		assertSame(h, result);
114 
115 		result = h.onload("console.log('load')");
116 		assertSame(h, result);
117 	}
118 
119 	@Test void a04_fluentChaining_childMethods() {
120 		var h = new Hyperlink();
121 
122 		// Test child/attribute methods
123 		Hyperlink result;
124 
125 		result = h.child("Link text");
126 		assertSame(h, result);
127 		assertInstanceOf(Hyperlink.class, result);
128 
129 		result = h.children("More", " text");
130 		assertSame(h, result);
131 
132 		result = h.attr("data-test", "value");
133 		assertSame(h, result);
134 
135 		result = h.attrUri("data-uri", "http://test.com");
136 		assertSame(h, result);
137 	}
138 
139 	@Test void a05_fluentChaining_complex() {
140 		// Test chaining multiple fluent calls
141 		var result = new Hyperlink()
142 			.href("/path/to/page")
143 			.target("_blank")
144 			._class("nav-link")
145 			.id("main-link")
146 			.onclick("track()")
147 			.children("Click here");
148 
149 		assertInstanceOf(Hyperlink.class, result);
150 	}
151 
152 	@Test void a06_output_basic() throws Exception {
153 		var h = new Hyperlink("/foo", "bar");
154 
155 		String html = HtmlSerializer.DEFAULT.serialize(h);
156 
157 		assertTrue(html.contains("href"));
158 		assertTrue(html.contains("/foo"));
159 		assertTrue(html.contains("bar"));
160 	}
161 
162 	@Test void a07_output_withAttributes() throws Exception {
163 		var h = new Hyperlink()
164 			.href("/path")
165 			.target("_blank")
166 			._class("link")
167 			.id("my-link")
168 			.children("Link text");
169 
170 		String html = HtmlSerializer.DEFAULT.serialize(h);
171 
172 		assertTrue(html.contains("href"));
173 		assertTrue(html.contains("/path"));
174 		assertTrue(html.contains("target"));
175 		assertTrue(html.contains("_blank"));
176 		assertTrue(html.contains("class"));
177 		assertTrue(html.contains("link"));
178 		assertTrue(html.contains("id"));
179 		assertTrue(html.contains("my-link"));
180 		assertTrue(html.contains("Link text"));
181 	}
182 
183 	@Test void a08_output_eventHandlers() throws Exception {
184 		var h = new Hyperlink()
185 			.href("#")
186 			.onclick("doSomething()")
187 			.onmouseover("highlight(this)")
188 			.children("Interactive");
189 
190 		String html = HtmlSerializer.DEFAULT.serialize(h);
191 
192 		assertTrue(html.contains("onclick"));
193 		assertTrue(html.contains("doSomething"));
194 		assertTrue(html.contains("onmouseover"));
195 		assertTrue(html.contains("highlight"));
196 	}
197 
198 	@Test void a09_staticCreator() {
199 		var h = Hyperlink.create("/test", "Test link");
200 
201 		assertInstanceOf(Hyperlink.class, h);
202 	}
203 
204 	@Test void a10_staticCreator_output() throws Exception {
205 		var h = Hyperlink.create("/static", "Static link");
206 
207 		String html = HtmlSerializer.DEFAULT.serialize(h);
208 
209 		assertTrue(html.contains("/static"));
210 		assertTrue(html.contains("Static link"));
211 	}
212 
213 	@Test void a11_multipleEventHandlers() {
214 		var h = new Hyperlink();
215 
216 		// Test multiple event handlers
217 		Hyperlink result = h
218 			.onabort("handle1()")
219 			.oncancel("handle2()")
220 			.oncanplay("handle3()")
221 			.onchange("handle4()")
222 			.ondblclick("handle5()")
223 			.ondurationchange("handle6()")
224 			.onemptied("handle7()")
225 			.onended("handle8()")
226 			.onerror("handle9()")
227 			.oninput("handle10()")
228 			.oninvalid("handle11()")
229 			.onkeydown("handle12()")
230 			.onkeypress("handle13()")
231 			.onkeyup("handle14()")
232 			.onloadeddata("handle15()")
233 			.onloadedmetadata("handle16()")
234 			.onloadstart("handle17()")
235 			.onmousedown("handle18()")
236 			.onmouseenter("handle19()")
237 			.onmouseleave("handle20()")
238 			.onmousemove("handle21()")
239 			.onmouseout("handle22()")
240 			.onmouseup("handle23()")
241 			.onmousewheel("handle24()")
242 			.onpause("handle25()")
243 			.onplay("handle26()")
244 			.onplaying("handle27()")
245 			.onprogress("handle28()")
246 			.onratechange("handle29()")
247 			.onreset("handle30()")
248 			.onresize("handle31()")
249 			.onscroll("handle32()")
250 			.onseeked("handle33()")
251 			.onseeking("handle34()")
252 			.onselect("handle35()")
253 			.onshow("handle36()")
254 			.onstalled("handle37()")
255 			.onsubmit("handle38()")
256 			.onsuspend("handle39()")
257 			.ontimeupdate("handle40()")
258 			.ontoggle("handle41()")
259 			.onvolumechange("handle42()")
260 			.onwaiting("handle43()")
261 			.oncuechange("handle44()");
262 
263 		assertSame(h, result);
264 		assertInstanceOf(Hyperlink.class, result);
265 	}
266 }