View Javadoc
1   /*
2    * $Source$
3    * $Revision$
4    *
5    * Copyright (C) 2003 Jim Wright
6    *
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     Jim Wright <jimw@paneris.org>
42   *     Bohemian Enterprise
43   *     Predmerice nad Jizerou 77
44   *     294 74
45   *     Mlada Boleslav
46   *     Czech Republic
47   */
48  
49  package org.melati.util.test;
50  
51  import java.util.Arrays;
52  
53  import junit.framework.TestCase;
54  
55  import org.melati.util.AcceptCharset;
56  
57  /**
58   * Tests the corresponding class in the superpackage.
59   * 
60   * @see AcceptCharset
61   * @author jimw At paneris.org
62   */
63  public class AcceptCharsetTest extends TestCase {
64  
65      /**
66       * Constructor.
67       */
68      public AcceptCharsetTest(String testCaseName) {
69          super(testCaseName);
70      }
71  
72      // FIXME this runs differently on a MAC
73      public void testXmacRoman() throws Exception {
74  
75          String headerValue = "x-mac-roman,utf-8;q=0.87,*;q=0.7";
76          String supportedPreference[] = new String[] { "UTF-16", "UTF-8",
77                  "ISO-8859-1", };
78          AcceptCharset ac = new AcceptCharset(headerValue, Arrays
79                  .asList(supportedPreference));
80          //assertEquals("MacRoman", ac.clientChoice());
81          //assertEquals("UTF-8", ac.clientChoice());
82          assertEquals("UTF-16", ac.serverChoice());
83      }
84  
85      /**
86       * Test choosing charsets.
87       */
88      public void testChoices() throws Exception {
89  
90          String headerValue = "ISO-8859-2, utf-8;q=0.66, *;q=0.66";
91          String supportedPreference[] = new String[] { "UTF-16", "UTF-8",
92                  "ISO-8859-1", };
93          AcceptCharset ac = new AcceptCharset(headerValue, Arrays
94                  .asList(supportedPreference));
95          assertEquals("ISO-8859-2", ac.clientChoice());
96          assertEquals("UTF-16", ac.serverChoice());
97  
98          headerValue = "utf-8;q=0.66,ISO-8859-3,ISO-8859-2";
99          supportedPreference = new String[] { "ISO-8859-1", "UTF-16", "UTF-8",
100                 "BOLLOX", };
101 
102         ac = new AcceptCharset(headerValue, Arrays.asList(supportedPreference));
103         assertEquals("ISO-8859-3", ac.clientChoice());
104         assertEquals("ISO-8859-1", ac.serverChoice());
105 
106         headerValue = "*;q=0.0";
107         supportedPreference = new String[] { "UTF-16", "UTF-8", "BOLLOX",
108                 "ISO-8859-1", };
109         ac = new AcceptCharset(headerValue, Arrays.asList(supportedPreference));
110         assertEquals(null, ac.clientChoice());
111         assertEquals(null, ac.serverChoice());
112 
113         ac = new AcceptCharset(null, Arrays.asList(supportedPreference));
114         assertEquals("ISO-8859-1", ac.clientChoice());
115         assertEquals("ISO-8859-1", ac.serverChoice());
116 
117     }
118 
119     public void testSensibleDefault() throws Exception {
120         String supportedPreference[] = new String[] { "UTF-16", "UTF-8",
121                 "ISO-8859-1", };
122         AcceptCharset ac = new AcceptCharset("", Arrays
123                 .asList(supportedPreference));
124         assertEquals("ISO-8859-1", ac.clientChoice());
125         assertEquals("ISO-8859-1", ac.serverChoice());
126 
127     }
128 
129     public void testSensibleDefaultForRubbish() throws Exception {
130         String supportedPreference[] = new String[] { "UTF-16", "UTF-8",
131                 "ISO-8859-1", };
132         AcceptCharset ac = new AcceptCharset("BOLLOX", Arrays
133                 .asList(supportedPreference));
134         assertEquals("ISO-8859-1", ac.clientChoice());
135         assertEquals("ISO-8859-1", ac.serverChoice());
136 
137     }
138 }