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  class HtmlElementContainer_Test extends TestBase {
27  
28  	@Test void a01_getChild() {
29  		Div x = new Div();
30  		assertNull(new Div().getChild(0));
31  		assertNull(x.getChild(String.class, 0));
32  
33  		x = div("child1");
34  		assertNull(x.getChild(5));
35  		assertNull(x.getChild(-1));
36  		assertNull(x.getChild(String.class, 5));
37  		assertNull(x.getChild(String.class, -1));
38  
39  		x = div("child1", "child2");
40  		assertString("child1", x.getChild(0));
41  		assertString("child2", x.getChild(1));
42  		assertString("child1", x.getChild(String.class, 0));
43  		assertString("child2", x.getChild(String.class, 1));
44  
45  		x = new Div();
46  		assertNull(x.getChildren());
47  		x.children();
48  		assertNull(x.getChildren());
49  
50  		x = div("child1", "child2");
51  		assertString("[child1,child2]", x.getChildren());
52  		x.children("child1", "child2");
53  		assertString("[child1,child2,child1,child2]", x.getChildren());
54  
55  		x = new Div();
56  		x.child("child1");
57  		assertString("[child1]", x.getChildren());
58  
59  		x = div("child1");
60  		assertNull(x.getChild(new int[]{}));
61  
62  		x = div("child1", "child2");
63  		assertString("child1", x.getChild(new int[]{0}));
64  
65  		x = div(
66  			div(
67  				div("text1", "text2"),
68  				div("text3")
69  			),
70  			div("text4")
71  		);
72  
73  		// Navigate to nested elements
74  		assertString("text1", x.getChild(0, 0, 0));
75  		assertString("text2", x.getChild(0, 0, 1));
76  		assertString("text3", x.getChild(0, 1, 0));
77  		assertString("text4", x.getChild(1, 0));
78  
79  		x = div("text");
80  		assertNull(x.getChild(0, 0));// Try to get child of a text node (which is not a container)
81  	}
82  
83  	@Test void a02_getChild_varargs_mixedElement() {
84  		// Test navigation through HtmlElementMixed
85  		P x = p(
86  			span("text1"),
87  			span("text2")
88  		);
89  
90  		assertString("text1", x.getChild(0, 0));
91  		assertString("text2", x.getChild(1, 0));
92  	}
93  
94  	@Test void a03_setChildren() {
95  		Div x = new Div();
96  		java.util.List<Object> children = java.util.Arrays.asList("child1", "child2");
97  		x.setChildren(children);
98  		assertString("[child1,child2]", x.getChildren());
99  	}
100 }