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",
82          "<a href='http:&#x2F;&#x2F;example.org&#x2F;web?text=%C3%A4%C3%B6%C3%BC%C3%9F'\n></a>",
83          stringWriter.toString());
84    }
85  
86    @Test
87    public void testAttributeQuoting() throws IOException {
88      writer.startElement(HtmlElements.SELECT, null);
89      writer.writeAttribute(HtmlAttributes.VALUE, "-<->-ü-€-", null);
90      writer.endElement(HtmlElements.SELECT);
91      Assert.assertEquals("attr tag", "<select value='-&lt;-&gt;-ü-€-'\n></select>", stringWriter.toString());
92    }
93  
94    @Test
95    public void testTextQuoting() throws IOException {
96      writer.startElement(HtmlElements.TEXTAREA, null);
97      writer.writeText("-<->-ü-€-", null);
98      writer.endElement("textarea");
99      Assert.assertEquals("attr tag", "<textarea\n>-&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, null);
111     final StringBuffer buffer = new StringBuffer();
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, null);
119     writer.writeText(buffer, null);
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", "<select value='" + result + "'\n>" + 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, null);
137     writer1.writeAttribute(HtmlAttributes.VALUE, "Gutschein über 100 €.", null);
138     writer1.writeAttribute(HtmlAttributes.READONLY, true);
139     writer1.endElement(HtmlElements.INPUT);
140     writer1.close();
141     Assert.assertEquals("<input value='Gutschein &uuml;ber 100 &euro;.' readonly='readonly'\n>",
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 }