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