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.tobago.internal.webapp.HtmlResponseWriter;
23  import org.apache.myfaces.tobago.internal.webapp.XmlResponseWriter;
24  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
25  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
26  import org.junit.Assert;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import java.io.IOException;
31  import java.io.StringWriter;
32  
33  public class TobagoResponseWriterUnitTest {
34  
35    private StringWriter stringWriter;
36    private TobagoResponseWriter writer;
37  
38    @Before
39    public void setUp() throws Exception {
40      stringWriter = new StringWriter();
41      writer = new HtmlResponseWriter(stringWriter, "", "UTF-8");
42    }
43  
44    @Test
45    public void testDocument() throws IOException {
46      writer.startDocument();
47      writer.endDocument();
48      Assert.assertEquals("content expected",
49          "<!DOCTYPE html>\n\n<html>\n</html>", stringWriter.toString());
50    }
51  
52    @Test
53    public void testEmptyTag() throws IOException {
54      writer.startElement(HtmlElements.INPUT);
55      writer.endElement(HtmlElements.INPUT);
56      Assert.assertEquals("empty tag", "\n<input>", stringWriter.toString());
57    }
58  
59    @Test
60    public void testNormalTag() throws IOException {
61      writer.startElement(HtmlElements.SELECT);
62      writer.endElement(HtmlElements.SELECT);
63      Assert.assertEquals("normal tag", "\n<select></select>", stringWriter.toString());
64    }
65  
66    @Test
67    public void testAttribute() throws IOException {
68      writer.startElement(HtmlElements.SELECT);
69      writer.writeAttribute(HtmlAttributes.VALUE, 0);
70      writer.endElement(HtmlElements.SELECT);
71      Assert.assertEquals("attr tag", "\n<select value='0'></select>", stringWriter.toString());
72    }
73    
74    @Test
75    public void testURIAttribute() throws IOException {
76      writer.startElement(HtmlElements.A);
77      writer.writeURIAttribute(HtmlAttributes.HREF, "http://example.org/web?text=äöüß");
78      writer.endElement(HtmlElements.A);
79      Assert.assertEquals(
80          "uri attr tag",
81          "\n<a href='http:&#x2F;&#x2F;example.org&#x2F;web?text=%C3%A4%C3%B6%C3%BC%C3%9F'></a>",
82          stringWriter.toString());
83    }
84  
85    @Test
86    public void testAttributeQuoting() throws IOException {
87      writer.startElement(HtmlElements.SELECT);
88      writer.writeAttribute(HtmlAttributes.VALUE, "-<->-ü-€-", true);
89      writer.endElement(HtmlElements.SELECT);
90      Assert.assertEquals("attr tag", "\n<select value='-&lt;-&gt;-ü-€-'></select>", stringWriter.toString());
91    }
92  
93    @Test
94    public void testTextQuoting() throws IOException {
95      writer.startElement(HtmlElements.TEXTAREA);
96      writer.writeText("-<->-ü-€-");
97      writer.endElement(HtmlElements.TEXTAREA);
98      Assert.assertEquals("attr tag", "\n<textarea>-&lt;-&gt;-ü-€-</textarea>", stringWriter.toString());
99    }
100 
101   @Test
102   public void testStringWriter() throws IOException {
103     stringWriter.write("-ü-€-");
104     Assert.assertEquals("-ü-€-", stringWriter.toString());
105   }
106 
107   @Test
108   public void testManyChars() throws IOException {
109     writer.startElement(HtmlElements.SELECT);
110     final StringBuilder buffer = new StringBuilder();
111     for (char c = 0x20; c < 0x7F; c++) {
112       buffer.append(c);
113     }
114     for (char c = 0xA0; c < 0x1ff; c++) {
115       buffer.append(c);
116     }
117     writer.writeAttribute(HtmlAttributes.VALUE, buffer.toString(), true);
118     writer.writeText(buffer.toString());
119     writer.endElement(HtmlElements.SELECT);
120 
121     String result = buffer.toString(); // all the same but this 4 items
122     result = result.replace("&", "&amp;");
123     result = result.replace("'", "&#x27;");
124     result = result.replace("\"", "&quot;");
125     result = result.replace("/", "&#x2F;");
126     result = result.replace("<", "&lt;");
127     result = result.replace(">", "&gt;");
128     Assert.assertEquals("all chars", "\n<select value='" + result + "'>" + result + "</select>",
129         stringWriter.toString());
130   }
131 
132   @Test
133   public void testNonUtf8() throws IOException {
134     final TobagoResponseWriter writer1 = new HtmlResponseWriter(stringWriter, "", "ISO-8859-1");
135     writer1.startElement(HtmlElements.INPUT);
136     writer1.writeAttribute(HtmlAttributes.VALUE, "Gutschein über 100 €.", true);
137     writer1.writeAttribute(HtmlAttributes.READONLY, true);
138     writer1.endElement(HtmlElements.INPUT);
139     writer1.close();
140     Assert.assertEquals("\n<input value='Gutschein &uuml;ber 100 &euro;.' readonly='readonly'>",
141         stringWriter.toString());
142   }
143 
144   @Test
145   public void testCharArray() throws IOException {
146     final TobagoResponseWriter writer = new XmlResponseWriter(stringWriter, "text/xml", "ISO-8859-1");
147     writer.writeText("123".toCharArray(), 0, 3);
148     Assert.assertEquals("123", stringWriter.toString());
149   }
150 }