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 PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n" 
50          + "<html\n"
51          + "></html>", stringWriter.toString());
52    }
53  
54    @Test
55    public void testEmptyTag() throws IOException {
56      writer.startElement(HtmlElements.INPUT, null);
57      writer.endElement(HtmlElements.INPUT);
58      Assert.assertEquals("empty tag", "<input\n>", stringWriter.toString());
59    }
60  
61    @Test
62    public void testNormalTag() throws IOException {
63      writer.startElement(HtmlElements.SELECT, null);
64      writer.endElement(HtmlElements.SELECT);
65      Assert.assertEquals("normal tag", "<select\n></select>", stringWriter.toString());
66    }
67  
68    @Test
69    public void testAttribute() throws IOException {
70      writer.startElement(HtmlElements.SELECT, null);
71      writer.writeAttribute(HtmlAttributes.VALUE, "0", null);
72      writer.endElement(HtmlElements.SELECT);
73      Assert.assertEquals("attr tag", "<select value='0'\n></select>", stringWriter.toString());
74    }
75    
76    @Test
77    public void testURIAttribute() throws IOException {
78      writer.startElement(HtmlElements.A, null);
79      writer.writeURIAttribute(HtmlAttributes.HREF, "http://example.org/web?text=äöüß", null);
80      writer.endElement(HtmlElements.A);
81      Assert.assertEquals("uri attr tag", "<a href='http://example.org/web?text=%C3%A4%C3%B6%C3%BC%C3%9F'\n></a>",
82              stringWriter.toString());
83    }
84  
85    @Test
86    public void testAttributeQuoting() throws IOException {
87      writer.startElement(HtmlElements.SELECT, null);
88      writer.writeAttribute(HtmlAttributes.VALUE, "-<->-ü-€-", null);
89      writer.endElement(HtmlElements.SELECT);
90      Assert.assertEquals("attr tag", "<select value='-&lt;-&gt;-ü-€-'\n></select>", stringWriter.toString());
91    }
92  
93    @Test
94    public void testTextQuoting() throws IOException {
95      writer.startElement(HtmlElements.TEXTAREA, null);
96      writer.writeText("-<->-ü-€-", null);
97      writer.endElement("textarea");
98      Assert.assertEquals("attr tag", "<textarea\n>-&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, null);
110     final StringBuffer buffer = new StringBuffer();
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, null);
118     writer.writeText(buffer, null);
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("'", "&#39;");
124     result = result.replace("<", "&lt;");
125     result = result.replace(">", "&gt;");
126     Assert.assertEquals("all chars", "<select value='" + result + "'\n>" + result + "</select>",
127         stringWriter.toString());
128   }
129 
130   @Test
131   public void testNonUtf8() throws IOException {
132     final TobagoResponseWriter writer1 = new HtmlResponseWriter(stringWriter, "", "ISO-8859-1");
133     writer1.startElement(HtmlElements.INPUT, null);
134     writer1.writeAttribute(HtmlAttributes.VALUE, "Gutschein über 100 €.", null);
135     writer1.writeAttribute(HtmlAttributes.READONLY, true);
136     writer1.endElement(HtmlElements.INPUT);
137     writer1.close();
138     Assert.assertEquals("<input value='Gutschein &uuml;ber 100 &euro;.' readonly='readonly'\n>",
139         stringWriter.toString());
140   }
141 
142   @Test
143   public void testCharArray() throws IOException {
144     final TobagoResponseWriter writer = new XmlResponseWriter(stringWriter, "text/xml", "ISO-8859-1");
145     writer.writeText("123".toCharArray(), 0, 3);
146     Assert.assertEquals("123", stringWriter.toString());
147   }
148 }