View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.webapp;
21  
22  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
23  import org.apache.myfaces.tobago.internal.webapp.HtmlResponseWriter;
24  import org.apache.myfaces.tobago.internal.webapp.XmlResponseWriter;
25  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
26  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  import java.io.IOException;
32  import java.io.StringWriter;
33  import java.nio.charset.StandardCharsets;
34  
35  public class TobagoResponseWriterUnitTest extends AbstractJsfTestCase {
36  
37    private StringWriter stringWriter;
38    private TobagoResponseWriter writer;
39  
40    @Override
41    @BeforeEach
42    public void setUp() throws Exception {
43      super.setUp();
44      stringWriter = new StringWriter();
45      writer = new HtmlResponseWriter(stringWriter, "", StandardCharsets.UTF_8);
46    }
47  
48    @Test
49    public void testDocument() throws IOException {
50      writer.startDocument();
51      writer.endDocument();
52      Assertions.assertEquals("<!DOCTYPE html>\n", stringWriter.toString(), "content expected");
53    }
54  
55    @Test
56    public void testEmptyTag() throws IOException {
57      writer.startElement(HtmlElements.INPUT);
58      writer.endElement(HtmlElements.INPUT);
59      Assertions.assertEquals("\n<input>", stringWriter.toString(), "empty tag");
60    }
61  
62    @Test
63    public void testNormalTag() throws IOException {
64      writer.startElement(HtmlElements.SELECT);
65      writer.endElement(HtmlElements.SELECT);
66      Assertions.assertEquals("\n<select></select>", stringWriter.toString(), "normal tag");
67    }
68  
69    @Test
70    public void testAttribute() throws IOException {
71      writer.startElement(HtmlElements.SELECT);
72      writer.writeAttribute(HtmlAttributes.VALUE, 0);
73      writer.endElement(HtmlElements.SELECT);
74      Assertions.assertEquals("\n<select value='0'></select>", stringWriter.toString(), "attr tag");
75    }
76  
77    @Test
78    public void testURIAttribute() throws IOException {
79      writer.startElement(HtmlElements.A);
80      writer.writeURIAttribute(HtmlAttributes.HREF, "http://example.org/web?text=äöüß");
81      writer.endElement(HtmlElements.A);
82      Assertions.assertEquals(
83          "\n<a href='http://example.org/web?text=%C3%A4%C3%B6%C3%BC%C3%9F'></a>",
84          stringWriter.toString(),
85          "uri attr tag");
86    }
87  
88    @Test
89    public void testAttributeQuoting() throws IOException {
90      writer.startElement(HtmlElements.SELECT);
91      writer.writeAttribute(HtmlAttributes.VALUE, "-<->-ü-€-", true);
92      writer.endElement(HtmlElements.SELECT);
93      Assertions.assertEquals("\n<select value='-&lt;-&gt;-ü-€-'></select>", stringWriter.toString(), "attr tag");
94    }
95  
96    @Test
97    public void testTextQuoting() throws IOException {
98      writer.startElement(HtmlElements.TEXTAREA);
99      writer.writeText("-<->-ü-€-");
100     writer.endElement(HtmlElements.TEXTAREA);
101     Assertions.assertEquals("\n<textarea>-&lt;-&gt;-ü-€-</textarea>", stringWriter.toString(), "attr tag");
102   }
103 
104   @Test
105   public void testStringWriter() throws IOException {
106     stringWriter.write("-ü-€-");
107     Assertions.assertEquals("-ü-€-", stringWriter.toString());
108   }
109 
110   @Test
111   public void testManyChars() throws IOException {
112     writer.startElement(HtmlElements.SELECT);
113     final StringBuilder buffer = new StringBuilder();
114     for (char c = 0x20; c < 0x7F; c++) {
115       buffer.append(c);
116     }
117     for (char c = 0xA0; c < 0x1ff; c++) {
118       buffer.append(c);
119     }
120     writer.writeAttribute(HtmlAttributes.VALUE, buffer.toString(), true);
121     writer.writeText(buffer.toString());
122     writer.endElement(HtmlElements.SELECT);
123 
124     String result = buffer.toString(); // all the same but this 4 items
125     result = result.replace("&", "&amp;");
126     result = result.replace("'", "&#x27;");
127 //    result = result.replace("\"", "&quot;");
128 //    result = result.replace("/", "&#x2F;");
129     result = result.replace("<", "&lt;");
130     result = result.replace(">", "&gt;");
131     Assertions.assertEquals(
132         "\n<select value='" + result + "'>" + result + "</select>", stringWriter.toString(), "all chars");
133   }
134 
135   @Test
136   public void testNonUtf8() throws IOException {
137     try (final TobagoResponseWriter writer1
138              = new HtmlResponseWriter(stringWriter, "", StandardCharsets.ISO_8859_1)) {
139       writer1.startElement(HtmlElements.INPUT);
140       writer1.writeAttribute(HtmlAttributes.VALUE, "Gutschein über 100 €.", true);
141       writer1.writeAttribute(HtmlAttributes.READONLY, true);
142       writer1.endElement(HtmlElements.INPUT);
143     }
144     Assertions.assertEquals("\n<input value='Gutschein &uuml;ber 100 &euro;.' readonly='readonly'>",
145         stringWriter.toString());
146   }
147 
148   @Test
149   public void testCharArray() throws IOException {
150     final TobagoResponseWriter xmlResponseWriter
151         = new XmlResponseWriter(stringWriter, "text/xml", StandardCharsets.ISO_8859_1);
152     xmlResponseWriter.writeText("123".toCharArray(), 0, 3);
153     Assertions.assertEquals("123", stringWriter.toString());
154   }
155 }