1   /* ====================================================================
2    * Copyright (c) 2006 J.T. Beetstra
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining 
5    * a copy of this software and associated documentation files (the 
6    * "Software"), to deal in the Software without restriction, including 
7    * without limitation the rights to use, copy, modify, merge, publish, 
8    * distribute, sublicense, and/or sell copies of the Software, and to 
9    * permit persons to whom the Software is furnished to do so, subject to 
10   * the following conditions:
11   *
12   * The above copyright notice and this permission notice shall be 
13   * included in all copies or substantial portions of the Software.
14   *
15   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
16   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
17   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
18   * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
19   * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
20   * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
21   * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22   * ====================================================================
23   */
24  package com.beetstra.jutf7;
25  
26  import java.nio.charset.Charset;
27  import java.util.HashSet;
28  import java.util.Iterator;
29  import java.util.Locale;
30  
31  import junit.framework.TestCase;
32  
33  public class CharsetProviderTest extends TestCase {
34  	private CharsetProvider tested = new CharsetProvider();
35  
36  	public void testOK() throws Exception {
37  		assertTrue(true);
38  	}
39  
40  	public void testModifiedUTF7() throws Exception {
41  		Charset charset = tested.charsetForName("x-modified-UTF-7");
42  		assertNotNull("charset not found", charset);
43  		assertEquals(ModifiedUTF7Charset.class, charset.getClass());
44  		assertEquals(charset, tested.charsetForName("x-imap-modified-utf-7"));
45  		assertEquals(charset, tested.charsetForName("x-imap4-modified-utf7"));
46  		assertEquals(charset, tested.charsetForName("x-imap4-modified-utf-7"));
47  		assertEquals(charset, tested.charsetForName("x-RFC3501"));
48  		assertEquals(charset, tested.charsetForName("x-RFC-3501"));
49  	}
50  
51  	public void testUTF7() throws Exception {
52  		Charset charset = tested.charsetForName("UTF-7");
53  		assertNotNull("charset not found", charset);
54  		assertEquals(UTF7Charset.class, charset.getClass());
55  		assertEquals(charset, tested.charsetForName("utf-7"));
56  		assertEquals(charset, tested.charsetForName("UNICODE-1-1-UTF-7"));
57  		assertEquals(charset, tested.charsetForName("csUnicode11UTF7"));
58  		assertEquals(charset, tested.charsetForName("x-RFC2152"));
59  		assertEquals(charset, tested.charsetForName("x-RFC-2152"));
60  	}
61  
62  	public void testUTF7optional() throws Exception {
63  		Charset charset = tested.charsetForName("X-UTF-7-OPTIONAL");
64  		assertNotNull("charset not found", charset);
65  		assertEquals(UTF7Charset.class, charset.getClass());
66  		assertEquals(charset, tested.charsetForName("x-utf-7-optional"));
67  		assertEquals(charset, tested.charsetForName("x-RFC2152-optional"));
68  		assertEquals(charset, tested.charsetForName("x-RFC-2152-optional"));
69  	}
70  
71  	public void testNotHere() throws Exception {
72  		assertNull(tested.charsetForName("X-DOES-NOT-EXIST"));
73  	}
74  
75  	public void testIterator() throws Exception {
76  		Iterator iterator = tested.charsets();
77  		HashSet found = new HashSet();
78  		while (iterator.hasNext())
79  			found.add(iterator.next());
80  		assertEquals(3, found.size());
81  		Charset charset1 = tested.charsetForName("x-IMAP4-modified-UTF7");
82  		Charset charset2 = tested.charsetForName("UTF-7");
83  		Charset charset3 = tested.charsetForName("X-UTF-7-OPTIONAL");
84  		assertTrue(found.contains(charset1));
85  		assertTrue(found.contains(charset2));
86  		assertTrue(found.contains(charset3));
87  	}
88  
89  	public void testTurkish() throws Exception {
90  		Locale.setDefault(new Locale("tr", "TR"));
91  		assertEquals(tested.charsetForName("UTF-7"), tested.charsetForName("unicode-1-1-utf-7"));
92  	}
93  }