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 testAttributeQuoting() throws IOException {
78      writer.startElement(HtmlElements.SELECT, null);
79      writer.writeAttribute(HtmlAttributes.VALUE, "-<->-ü-€-", null);
80      writer.endElement(HtmlElements.SELECT);
81      Assert.assertEquals("attr tag", "<select value=\"-&lt;-&gt;-ü-€-\"\n></select>", stringWriter.toString());
82    }
83  
84    @Test
85    public void testTextQuoting() throws IOException {
86      writer.startElement(HtmlElements.TEXTAREA, null);
87      writer.writeText("-<->-ü-€-", null);
88      writer.endElement("textarea");
89      Assert.assertEquals("attr tag", "<textarea\n>-&lt;-&gt;-ü-€-</textarea>", stringWriter.toString());
90    }
91  
92    @Test
93    public void testStringWriter() throws IOException {
94      stringWriter.write("-ü-€-");
95      Assert.assertEquals("-ü-€-", stringWriter.toString());
96    }
97  
98    @Test
99    public void testManyChars() throws IOException {
100     writer.startElement(HtmlElements.SELECT, null);
101     StringBuffer buffer = new StringBuffer();
102     for (char c = 0x20; c < 0x7F; c++) {
103       buffer.append(c);
104     }
105     for (char c = 0xA0; c < 0x1ff; c++) {
106       buffer.append(c);
107     }
108     writer.writeAttribute(HtmlAttributes.VALUE, buffer, null);
109     writer.writeText(buffer, null);
110     writer.endElement(HtmlElements.SELECT);
111 
112     String result = buffer.toString(); // all the same but this 4 items
113     result = result.replace("&", "&amp;");
114     result = result.replace("\"", "&quot;");
115     result = result.replace("<", "&lt;");
116     result = result.replace(">", "&gt;");
117     Assert.assertEquals("all chars", "<select value=\"" + result + "\"\n>" + result + "</select>",
118         stringWriter.toString());
119   }
120 
121   @Test
122   public void testNonUtf8() throws IOException {
123     TobagoResponseWriter writer1 = new HtmlResponseWriter(stringWriter, "", "ISO-8859-1");
124     writer1.startElement(HtmlElements.INPUT, null);
125     writer1.writeAttribute(HtmlAttributes.VALUE, "Gutschein über 100 €.", null);
126     writer1.writeAttribute(HtmlAttributes.READONLY, true);
127     writer1.endElement(HtmlElements.INPUT);
128     writer1.close();
129     Assert.assertEquals("<input value=\"Gutschein &uuml;ber 100 &euro;.\" readonly=\"readonly\"\n>",
130         stringWriter.toString());
131   }
132 
133   @Test
134   public void testCharArray() throws IOException {
135     TobagoResponseWriter writer = new XmlResponseWriter(stringWriter, "text/xml", "ISO-8859-1");
136     writer.writeText("123".toCharArray(), 0, 3);
137     Assert.assertEquals("123", stringWriter.toString());
138   }
139 }