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.html;
14  
15  import static org.apache.juneau.commons.utils.CollectionUtils.*;
16  import static org.junit.jupiter.api.Assertions.*;
17  
18  import org.apache.juneau.*;
19  import org.junit.jupiter.api.*;
20  
21  /**
22   * Tests for SimpleHtmlWriter fluent setter overrides.
23   */
24  class SimpleHtmlWriter_Test extends TestBase {
25  
26  	@Test void a01_fluentChaining_tagMethods() {
27  		var w = new SimpleHtmlWriter();
28  
29  		// Test that fluent methods return SimpleHtmlWriter (not HtmlWriter)
30  		SimpleHtmlWriter result;
31  
32  		result = w.sTag("div");
33  		assertSame(w, result);
34  		assertInstanceOf(SimpleHtmlWriter.class, result);
35  
36  		result = w.attr("class", "test");
37  		assertSame(w, result);
38  
39  		result = w.cTag();
40  		assertSame(w, result);
41  
42  		result = w.text("content");
43  		assertSame(w, result);
44  
45  		result = w.eTag("div");
46  		assertSame(w, result);
47  	}
48  
49  	@Test void a02_fluentChaining_appendMethods() {
50  		var w = new SimpleHtmlWriter();
51  
52  		// Test append methods
53  		SimpleHtmlWriter result;
54  
55  		result = w.append("test");
56  		assertSame(w, result);
57  		assertInstanceOf(SimpleHtmlWriter.class, result);
58  
59  		result = w.append('x');
60  		assertSame(w, result);
61  
62  		result = w.append((Object)"obj");
63  		assertSame(w, result);
64  
65  		result = w.append(chars('a','b','c'));
66  		assertSame(w, result);
67  
68  		result = w.appendIf(true, "conditional");
69  		assertSame(w, result);
70  
71  		result = w.appendIf(true, 'y');
72  		assertSame(w, result);
73  	}
74  
75  	@Test void a03_fluentChaining_formattingMethods() {
76  		var w = new SimpleHtmlWriter();
77  
78  		// Test formatting methods
79  		SimpleHtmlWriter result;
80  
81  		result = w.cr(0);
82  		assertSame(w, result);
83  		assertInstanceOf(SimpleHtmlWriter.class, result);
84  
85  		result = w.cre(0);
86  		assertSame(w, result);
87  
88  		result = w.i(1);
89  		assertSame(w, result);
90  
91  		result = w.ie(1);
92  		assertSame(w, result);
93  
94  		result = w.nl(1);
95  		assertSame(w, result);
96  
97  		result = w.s();
98  		assertSame(w, result);
99  
100 		result = w.q();
101 		assertSame(w, result);
102 
103 		result = w.sIf(true);
104 		assertSame(w, result);
105 
106 		result = w.nlIf(true, 1);
107 		assertSame(w, result);
108 
109 		result = w.w('z');
110 		assertSame(w, result);
111 
112 		result = w.w("write");
113 		assertSame(w, result);
114 	}
115 
116 	@Test void a04_fluentChaining_complex() {
117 		var w = new SimpleHtmlWriter();
118 
119 		// Test chaining multiple fluent calls
120 		SimpleHtmlWriter result = w
121 			.sTag("table")
122 			.sTag("tr")
123 			.sTag("td")
124 			.append("hello")
125 			.eTag("td")
126 			.eTag("tr")
127 			.eTag("table");
128 
129 		assertSame(w, result);
130 		assertInstanceOf(SimpleHtmlWriter.class, result);
131 	}
132 
133 	@Test void a05_output_simpleTable() {
134 		var w = new SimpleHtmlWriter();
135 
136 		String result = w
137 			.sTag("table")
138 			.sTag("tr")
139 			.sTag("td")
140 			.append("hello")
141 			.eTag("td")
142 			.eTag("tr")
143 			.eTag("table")
144 			.toString();
145 
146 		assertTrue(result.contains("<table"));
147 		assertTrue(result.contains("<tr"));
148 		assertTrue(result.contains("<td"));
149 		assertTrue(result.contains("hello"));
150 		assertTrue(result.contains("</td>"));
151 		assertTrue(result.contains("</tr>"));
152 		assertTrue(result.contains("</table>"));
153 	}
154 
155 	@Test void a06_output_withAttributes() {
156 		var w = new SimpleHtmlWriter();
157 
158 		String result = w
159 			.sTag("div")
160 			.attr("class", "container")
161 			.attr("id", "main")
162 			.cTag()
163 			.text("content")
164 			.eTag("div")
165 			.toString();
166 
167 		assertTrue(result.contains("class"));
168 		assertTrue(result.contains("container"));
169 		assertTrue(result.contains("id"));
170 		assertTrue(result.contains("main"));
171 		assertTrue(result.contains("content"));
172 		assertTrue(result.contains("</div>"));
173 	}
174 
175 	@Test void a08_tagVariations() {
176 		var w = new SimpleHtmlWriter();
177 
178 		// Test different tag method variations
179 		SimpleHtmlWriter result;
180 
181 		result = w.oTag("div");
182 		assertSame(w, result);
183 
184 		result = w.tag("span");
185 		assertSame(w, result);
186 
187 		result = w.sTag(1, "p");
188 		assertSame(w, result);
189 
190 		result = w.eTag(1, "p");
191 		assertSame(w, result);
192 
193 		result = w.oTag(null, "div");
194 		assertSame(w, result);
195 
196 		result = w.sTag(null, "span");
197 		assertSame(w, result);
198 
199 		result = w.eTag(null, "span");
200 		assertSame(w, result);
201 	}
202 
203 	@Test void a09_attrVariations() {
204 		var w = new SimpleHtmlWriter();
205 
206 		// Test different attr method variations
207 		SimpleHtmlWriter result;
208 
209 		result = w.attr("id", "test", true);
210 		assertSame(w, result);
211 
212 		result = w.attr("class", "test");
213 		assertSame(w, result);
214 	}
215 
216 	@Test void a10_appendVariations() {
217 		var w = new SimpleHtmlWriter();
218 
219 		// Test different append method variations
220 		SimpleHtmlWriter result;
221 
222 		result = w.appendln("line");
223 		assertSame(w, result);
224 
225 		result = w.appendln(1, "indented");
226 		assertSame(w, result);
227 
228 		result = w.append(1, "indented text");
229 		assertSame(w, result);
230 
231 		result = w.append(2, 'c');
232 		assertSame(w, result);
233 	}
234 
235 	@Test void a11_textVariations() {
236 		var w = new SimpleHtmlWriter();
237 
238 		// Test different text method variations
239 		SimpleHtmlWriter result;
240 
241 		result = w.text("normal text");
242 		assertSame(w, result);
243 
244 		result = w.text("preserved", true);
245 		assertSame(w, result);
246 	}
247 
248 	@Test void a12_ceTag() {
249 		var w = new SimpleHtmlWriter();
250 
251 		String result = w
252 			.sTag("br")
253 			.ceTag()
254 			.toString();
255 
256 		assertTrue(result.contains("<br"));
257 		assertTrue(result.contains("/>"));
258 	}
259 }