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.shared.renderkit;
20  
21  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  public class ContentTypeUtilsTest extends AbstractJsfTestCase
26  {
27  
28      @Test
29      public void testContainsContentType() throws Exception
30      {
31          Assert.assertTrue(ContentTypeUtils.containsContentType(
32                  ContentTypeUtils.HTML_CONTENT_TYPE, ContentTypeUtils.HTML_ALLOWED_CONTENT_TYPES));
33          Assert.assertTrue(ContentTypeUtils.containsContentType(
34                  ContentTypeUtils.ANY_CONTENT_TYPE, ContentTypeUtils.HTML_ALLOWED_CONTENT_TYPES));
35          Assert.assertTrue(ContentTypeUtils.containsContentType(
36                  ContentTypeUtils.TEXT_ANY_CONTENT_TYPE, ContentTypeUtils.HTML_ALLOWED_CONTENT_TYPES));
37          
38          Assert.assertTrue(ContentTypeUtils.containsContentType(
39                  ContentTypeUtils.XHTML_CONTENT_TYPE, ContentTypeUtils.XHTML_ALLOWED_CONTENT_TYPES));
40          Assert.assertTrue(ContentTypeUtils.containsContentType(
41                  ContentTypeUtils.APPLICATION_XML_CONTENT_TYPE, ContentTypeUtils.XHTML_ALLOWED_CONTENT_TYPES));
42          Assert.assertTrue(ContentTypeUtils.containsContentType(
43                  ContentTypeUtils.TEXT_XML_CONTENT_TYPE, ContentTypeUtils.XHTML_ALLOWED_CONTENT_TYPES));
44      }
45      
46      @Test
47      public void testSplitContentTypeListString() throws Exception
48      {
49          String [] splittedArray = ContentTypeUtils.splitContentTypeListString("text/*, text/html ");
50          
51          Assert.assertTrue(ContentTypeUtils.containsContentType(
52                  ContentTypeUtils.HTML_CONTENT_TYPE, splittedArray));
53          Assert.assertTrue(ContentTypeUtils.containsContentType(
54                  ContentTypeUtils.TEXT_ANY_CONTENT_TYPE, splittedArray));
55          
56          splittedArray = ContentTypeUtils.splitContentTypeListString(" text/x-dvi; q=.8; mxb=100000; mxt=5.0, text/x-c");
57          Assert.assertTrue(ContentTypeUtils.containsContentType(
58                  "text/x-dvi", splittedArray));
59          Assert.assertTrue(ContentTypeUtils.containsContentType(
60                  "text/x-c", splittedArray));
61      }
62      
63      public void testChooseWriterContentType() throws Exception
64      {
65          Assert.assertEquals(ContentTypeUtils.HTML_CONTENT_TYPE, 
66                  ContentTypeUtils.chooseWriterContentType(
67                          "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
68                          ContentTypeUtils.HTML_ALLOWED_CONTENT_TYPES,
69                          ContentTypeUtils.XHTML_ALLOWED_CONTENT_TYPES));
70  
71          Assert.assertEquals(ContentTypeUtils.XHTML_CONTENT_TYPE, 
72                  ContentTypeUtils.chooseWriterContentType(
73                          "application/xml, text/xml , */*; q=0.01", 
74                          ContentTypeUtils.HTML_ALLOWED_CONTENT_TYPES,
75                          ContentTypeUtils.XHTML_ALLOWED_CONTENT_TYPES));
76  
77          //In ajax request application/xml and text/xml does not match.
78          Assert.assertNull(ContentTypeUtils.chooseWriterContentType(
79                          "application/xml, text/xml, */*; q=0.01", 
80                          ContentTypeUtils.HTML_ALLOWED_CONTENT_TYPES,
81                          ContentTypeUtils.AJAX_XHTML_ALLOWED_CONTENT_TYPES));
82      }
83  }