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.bean.html5;
18  
19  import static org.apache.juneau.bean.html5.HtmlBuilder.*;
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  /**
27   * Tests for attr() and attrUri() fluent setter overrides in HTML5 element classes.
28   */
29  class HtmlElement_Attr_Test extends TestBase {
30  
31  	@Test void a01_div_attr_fluent_chaining() {
32  		Div x = div();
33  		// Verify fluent chaining returns same instance
34  		assertSame(x, x.attr("data-test", "value1"));
35  		assertSame(x, x.attr("data-foo", "value2"));
36  		assertString("<div data-test='value1' data-foo='value2'></div>", x);
37  	}
38  
39  	@Test void a02_div_attrUri_fluent_chaining() {
40  		Div x = div();
41  		assertSame(x, x.attrUri("data-url", "http://localhost/test"));
42  		assertString("<div data-url='http://localhost/test'></div>", x);
43  	}
44  
45  	@Test void a03_figure_attr_chaining() {
46  		Figure x = figure()
47  			.attr("data-id", "123")
48  			.attr("role", "img");
49  		assertString("<figure data-id='123' role='img'></figure>", x);
50  	}
51  
52  	@Test void a04_br_attr() {
53  		Br x = br();
54  		assertSame(x, x.attr("data-break", "soft"));
55  		assertString("<br data-break='soft'/>", x);
56  	}
57  
58  	@Test void a05_img_attr() {
59  		Img x = img()
60  			.attr("alt", "Test")
61  			.attrUri("src", "http://example.com/img.png");
62  		assertTrue(x.toString().contains("alt='Test'"));
63  		assertTrue(x.toString().contains("src='http://example.com/img.png'"));
64  	}
65  
66  	@Test void a06_b_attr() {
67  		B x = b();
68  		assertSame(x, x.attr("data-weight", "bold"));
69  		assertString("<b data-weight='bold'></b>", x);
70  	}
71  
72  	@Test void a07_script_attr() {
73  		Script x = script();
74  		assertSame(x, x.attr("type", "text/javascript"));
75  		assertSame(x, x.attrUri("src", "http://example.com/script.js"));
76  		assertTrue(x.toString().contains("type='text/javascript'"));
77  		assertTrue(x.toString().contains("src='http://example.com/script.js'"));
78  	}
79  
80  	@Test void a08_style_attr() {
81  		Style x = style();
82  		assertSame(x, x.attr("type", "text/css"));
83  		assertTrue(x.toString().contains("type='text/css'"));
84  	}
85  
86  	@Test void a09_attr_null_removes_attribute() {
87  		Div x = div()
88  			.attr("data-test", "value")
89  			.attr("data-test", null);
90  		assertString("<div></div>", x);
91  	}
92  
93  	@Test void a10_attrUri_with_standard_attrs() {
94  		A x = a()
95  			.href("http://example.com")
96  			.attrUri("data-fallback", "http://fallback.com");
97  		assertTrue(x.toString().contains("href='http://example.com'"));
98  		assertTrue(x.toString().contains("data-fallback='http://fallback.com'"));
99  	}
100 
101 	@Test void a11_no_serialization_errors() {
102 		// Verifies @Beanp annotation inheritance doesn't cause serialization errors
103 		Div x = div()
104 			.attr("data-test", "value")
105 			.attrUri("data-url", "http://test.com")
106 			._class("test");
107 		var result = x.toString();
108 		assertNotNull(result);
109 		assertTrue(result.contains("data-test='value'"));
110 		assertTrue(result.contains("data-url='http://test.com'"));
111 		assertTrue(result.contains("class='test'"));
112 	}
113 }