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