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.commons.utils.CollectionUtils.*;
21  import static org.apache.juneau.junit.bct.BctAssertions.*;
22  import static org.junit.jupiter.api.Assertions.*;
23  
24  import org.apache.juneau.*;
25  import org.junit.jupiter.api.*;
26  
27  class HtmlElementMixed_Test extends TestBase {
28  
29  	@Test void a01_getChild_nullChildren() {
30  		P x = new P();
31  		assertNull(x.getChild(0));
32  	}
33  
34  	@Test void a02_getChild_outOfBounds() {
35  		P x = p("child1");
36  		assertNull(x.getChild(5));
37  		assertNull(x.getChild(-1));
38  	}
39  
40  	@Test void a03_getChild_valid() {
41  		P x = p("child1", "child2");
42  		assertString("child1", x.getChild(0));
43  		assertString("child2", x.getChild(1));
44  	}
45  
46  	@Test void a04_getChild_typed_nullChildren() {
47  		P x = new P();
48  		assertNull(x.getChild(String.class, 0));
49  	}
50  
51  	@Test void a05_getChild_typed_outOfBounds() {
52  		P x = p("child1");
53  		assertNull(x.getChild(String.class, 5));
54  		assertNull(x.getChild(String.class, -1));
55  	}
56  
57  	@Test void a06_getChild_typed_valid() {
58  		P x = p("child1", "child2");
59  		assertString("child1", x.getChild(String.class, 0));
60  		assertString("child2", x.getChild(String.class, 1));
61  	}
62  
63  	@Test void a07_child_withCollection() {
64  		P x = new P();
65  		x.child(l("text1", "text2"));
66  		assertString("<p>text1text2</p>", x);
67  	}
68  
69  	@Test void a08_child_withSingleValue() {
70  		P x = new P();
71  		x.child("text1");
72  		assertString("<p>text1</p>", x);
73  	}
74  
75  	@Test void a09_getChildren() {
76  		P x1 = new P();
77  		assertNull(x1.getChildren());
78  
79  		P x2 = p("child1", "child2");
80  		assertString("[child1,child2]", x2.getChildren());
81  	}
82  
83  	@Test void a10_children_emptyArray() {
84  		P x = new P();
85  		x.children();
86  		assertNull(x.getChildren());
87  	}
88  
89  	@Test void a11_children_withValues() {
90  		P x = new P();
91  		x.children("child1", "child2");
92  		assertString("[child1,child2]", x.getChildren());
93  	}
94  
95  	@Test void a12_getChild_varargs_emptyArray() {
96  		P x = p("child1");
97  		assertNull(x.getChild(ints()));
98  	}
99  
100 	@Test void a13_getChild_varargs_singleIndex() {
101 		P x = p("child1", "child2");
102 		assertString("child1", x.getChild(ints(0)));
103 	}
104 
105 	@Test void a14_getChild_varargs_multipleIndices() {
106 		// Create nested structure: p -> span -> strong -> "text"
107 		P x = p(
108 			span(
109 				strong("text1"),
110 				strong("text2")
111 			),
112 			span("text3")
113 		);
114 
115 		// Navigate to nested elements
116 		assertString("text1", x.getChild(0, 0, 0));
117 		assertString("text2", x.getChild(0, 1, 0));
118 		assertString("text3", x.getChild(1, 0));
119 	}
120 
121 	@Test void a15_getChild_varargs_invalidPath() {
122 		P x = p("text");
123 		// Try to get child of a text node (which is not a container or mixed element)
124 		assertNull(x.getChild(0, 0));
125 	}
126 
127 	@Test void a16_getChild_varargs_throughContainer() {
128 		// Test navigation from Mixed through Container element
129 		P x = p(
130 			div(
131 				span("nested")
132 			)
133 		);
134 
135 		assertString("nested", x.getChild(0, 0, 0));
136 	}
137 
138 	@Test void a17_setChildren() {
139 		P x = new P();
140 		java.util.List<Object> children = l("child1", "child2");
141 		x.setChildren(children);
142 		assertString("[child1,child2]", x.getChildren());
143 	}
144 
145 	@Test void a18_child_withCollection_existingChildren() {
146 		// Test adding a collection when children list already exists
147 		P x = new P();
148 		x.child("existing");  // Initialize children list
149 		x.child(l("new1", "new2"));  // Add collection to existing list
150 		assertString("<p>existingnew1new2</p>", x);
151 	}
152 }