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  package org.apache.myfaces.renderkit;
20  
21  import java.io.StringWriter;
22  
23  import junit.framework.Assert;
24  
25  import org.apache.myfaces.renderkit.html.HtmlRenderKitImpl;
26  import org.apache.myfaces.shared.config.MyfacesConfig;
27  import org.apache.myfaces.shared.renderkit.ContentTypeUtils;
28  import org.apache.myfaces.shared.renderkit.html.HtmlResponseWriterImpl;
29  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
30  import org.junit.Test;
31  
32  public class HtmlRenderKitImplTest extends AbstractJsfTestCase
33  {
34  
35      @Test
36      public void testCreateResponseWriterContentType1()
37      {
38          MyfacesConfig config = new MyfacesConfig();
39          facesContext.getExternalContext().getApplicationMap().put(MyfacesConfig.class.getName(), config);
40          HtmlRenderKitImpl renderKit = new HtmlRenderKitImpl();
41          StringWriter writer = new StringWriter();
42          HtmlResponseWriterImpl responseWriter = (HtmlResponseWriterImpl) renderKit.createResponseWriter(writer, null, null);
43          Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getContentType());
44          Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getWriterContentTypeMode());
45      }
46      
47      @Test
48      public void testCreateResponseWriterContentType2()
49      {
50          MyfacesConfig config = new MyfacesConfig();
51          config.setDefaultResponseWriterContentTypeMode(ContentTypeUtils.XHTML_CONTENT_TYPE);
52          facesContext.getExternalContext().getApplicationMap().put(MyfacesConfig.class.getName(), config);
53          HtmlRenderKitImpl renderKit = new HtmlRenderKitImpl();
54          StringWriter writer = new StringWriter();
55          HtmlResponseWriterImpl responseWriter = (HtmlResponseWriterImpl) renderKit.createResponseWriter(writer, null, null);
56          Assert.assertEquals(ContentTypeUtils.XHTML_CONTENT_TYPE, responseWriter.getContentType());
57          Assert.assertEquals(ContentTypeUtils.XHTML_CONTENT_TYPE, responseWriter.getWriterContentTypeMode());
58      }
59      
60      @Test
61      public void testCreateResponseWriterContentType3()
62      {
63          MyfacesConfig config = new MyfacesConfig();
64          facesContext.getExternalContext().getApplicationMap().put(MyfacesConfig.class.getName(), config);
65          request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); //Firefox
66          request.addHeader("Faces-Request", "partial/ajax");
67          HtmlRenderKitImpl renderKit = new HtmlRenderKitImpl();
68          StringWriter writer = new StringWriter();
69          HtmlResponseWriterImpl responseWriter = (HtmlResponseWriterImpl) renderKit.createResponseWriter(writer, null, null);
70          Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getContentType());
71          Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getWriterContentTypeMode());
72      }
73      
74      @Test
75      public void testCreateResponseWriterContentType4()
76      {
77          MyfacesConfig config = new MyfacesConfig();
78          facesContext.getExternalContext().getApplicationMap().put(MyfacesConfig.class.getName(), config);
79          request.addHeader("Accept", "application/xml, text/xml, */*; q=0.01"); //Firefox
80          request.addHeader("Faces-Request", "partial/ajax");
81          HtmlRenderKitImpl renderKit = new HtmlRenderKitImpl();
82          StringWriter writer = new StringWriter();
83          HtmlResponseWriterImpl responseWriter = (HtmlResponseWriterImpl) renderKit.createResponseWriter(writer, null, null);
84          Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getContentType());
85          Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getWriterContentTypeMode());
86      }
87      
88      @Test
89      public void testCreateResponseWriterContentType5()
90      {
91          MyfacesConfig config = new MyfacesConfig();
92          facesContext.getExternalContext().getApplicationMap().put(MyfacesConfig.class.getName(), config);
93          request.addHeader("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); //Webkit
94          HtmlRenderKitImpl renderKit = new HtmlRenderKitImpl();
95          StringWriter writer = new StringWriter();
96          HtmlResponseWriterImpl responseWriter = (HtmlResponseWriterImpl) renderKit.createResponseWriter(writer, null, null);
97          Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getContentType());
98          Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getWriterContentTypeMode());
99      }
100     
101     @Test(expected=IllegalArgumentException.class)
102     public void testCreateResponseWriterContentType6()
103     {
104         MyfacesConfig config = new MyfacesConfig();
105         facesContext.getExternalContext().getApplicationMap().put(MyfacesConfig.class.getName(), config);
106         request.addHeader("Accept", "image/png"); //Webkit
107         HtmlRenderKitImpl renderKit = new HtmlRenderKitImpl();
108         StringWriter writer = new StringWriter();
109         HtmlResponseWriterImpl responseWriter = (HtmlResponseWriterImpl) renderKit.createResponseWriter(writer, null, null);
110     }
111     
112     @Test
113     public void testCreateResponseWriterContentType7()
114     {
115         MyfacesConfig config = new MyfacesConfig();
116         facesContext.getExternalContext().getApplicationMap().put(MyfacesConfig.class.getName(), config);
117         request.addHeader("Accept", "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, " +
118         		"image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, */*"); //IE8
119         HtmlRenderKitImpl renderKit = new HtmlRenderKitImpl();
120         StringWriter writer = new StringWriter();
121         HtmlResponseWriterImpl responseWriter = (HtmlResponseWriterImpl) renderKit.createResponseWriter(writer, null, null);
122         Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getContentType());
123         Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getWriterContentTypeMode());
124     }
125     
126     @Test
127     public void testCreateResponseWriterContentType8()
128     {
129         MyfacesConfig config = new MyfacesConfig();
130         facesContext.getExternalContext().getApplicationMap().put(MyfacesConfig.class.getName(), config);
131         request.addHeader("Accept", "text/html, application/xml;q=0.9, application/xhtml+xml;q=0.9, image/png, " +
132         		"image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1"); //Opera
133         HtmlRenderKitImpl renderKit = new HtmlRenderKitImpl();
134         StringWriter writer = new StringWriter();
135         HtmlResponseWriterImpl responseWriter = (HtmlResponseWriterImpl) renderKit.createResponseWriter(writer, null, null);
136         Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getContentType());
137         Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getWriterContentTypeMode());
138     }
139 
140     @Test
141     public void testCreateResponseWriterContentType9()
142     {
143         MyfacesConfig config = new MyfacesConfig();
144         facesContext.getExternalContext().getApplicationMap().put(MyfacesConfig.class.getName(), config);
145         request.addHeader("Accept", "image/gif, image/jpeg, image/pjpeg, application/x-ms-application,"+
146                 "application/vnd.ms-xpsdocument, application/xaml+xml,"+
147                 "application/x-ms-xbap, application/x-shockwave-flash,"+
148                 "application/x-silverlight-2-b2, application/x-silverlight,"+
149                 "application/vnd.ms-excel, application/vnd.ms-powerpoint,"+
150                 "application/msword, */*"); //IE
151         HtmlRenderKitImpl renderKit = new HtmlRenderKitImpl();
152         StringWriter writer = new StringWriter();
153         HtmlResponseWriterImpl responseWriter = (HtmlResponseWriterImpl) renderKit.createResponseWriter(writer, null, null);
154         Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getContentType());
155         Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, responseWriter.getWriterContentTypeMode());
156     }
157 }