OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / nio_char / tests / java / nio / charset / CharsetTest.java
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.apache.harmony.nio_char.tests.java.nio.charset;
18
19 import dalvik.annotation.TestLevel;
20 import dalvik.annotation.TestTargetClass;
21 import dalvik.annotation.TestTargetNew;
22 import junit.framework.TestCase;
23
24 import java.nio.charset.Charset;
25 import java.nio.charset.IllegalCharsetNameException;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.Properties;
29 import java.util.Set;
30 @TestTargetClass(Charset.class)
31 public class CharsetTest extends TestCase {
32
33     // Will contain names of charsets registered with IANA
34     Set knownRegisteredCharsets = new HashSet();
35
36     // Will contain names of charsets not known to be registered with IANA
37     Set unknownRegisteredCharsets = new HashSet();
38
39     /**
40      * JUnit set-up method
41      */
42     public void setUp() {
43
44         // Populate the known charset vars
45         Set names = Charset.availableCharsets().keySet();
46         for (Iterator nameItr = names.iterator(); nameItr.hasNext();) {
47             String name = (String) nameItr.next();
48             if (name.toLowerCase().startsWith("x-"))
49                 unknownRegisteredCharsets.add(name);
50             else
51                 knownRegisteredCharsets.add(name);
52         }
53     }
54
55     /**
56      * @tests java.nio.charset.Charset#isRegistered()
57      */
58     @TestTargetNew(
59         level = TestLevel.COMPLETE,
60         notes = "",
61         method = "isRegistered",
62         args = {}
63     )
64     public void test_isRegistered() {
65         // Regression for HARMONY-45
66         for (Iterator nameItr = knownRegisteredCharsets.iterator(); nameItr.hasNext();) {
67             String name = (String) nameItr.next();
68             assertTrue("Assert 0: isRegistered() failed for " + name,
69                     Charset.forName(name).isRegistered());
70         }
71         for (Iterator nameItr = unknownRegisteredCharsets.iterator(); nameItr.hasNext();) {
72             String name = (String) nameItr.next();
73             assertFalse("Assert 0: isRegistered() failed for " + name,
74                     Charset.forName(name).isRegistered());
75         }
76     }
77
78     /**
79      * @tests java.nio.charset.Charset#isSupported(String)
80      */
81     @TestTargetNew(
82         level = TestLevel.PARTIAL,
83         notes = "Checks IllegalCharsetNameException",
84         method = "isSupported",
85         args = {java.lang.String.class}
86     )
87     public void testIsSupported_EmptyString() {
88         // Regression for HARMONY-113
89         try {
90             Charset.isSupported("");
91             fail("Assert 0: Should throw IllegalCharsetNameException");
92         } catch (IllegalCharsetNameException e) {
93             // Expected
94         }
95     }
96
97     /**
98      * @tests java.nio.charset.Charset#defaultCharset()
99      */
100     @TestTargetNew(
101         level = TestLevel.COMPLETE,
102         notes = "",
103         method = "defaultCharset",
104         args = {}
105     )
106     public void test_defaultCharset() {
107         String charsetName = null;
108         String defaultCharsetName = null;
109         String oldDefaultEncoding = System.getProperty("file.encoding");
110         try {
111             // Normal behavior
112             charsetName = "UTF-8";
113             System.setProperty("file.encoding", charsetName);
114             defaultCharsetName = Charset.defaultCharset().name();
115             assertEquals(charsetName, defaultCharsetName);
116
117             charsetName = "ISO-8859-1";
118             System.setProperty("file.encoding", charsetName);
119             defaultCharsetName = Charset.defaultCharset().name();
120             assertEquals(charsetName, defaultCharsetName);
121
122             // Unsupported behavior
123             charsetName = "IMPOSSIBLE-8";
124             System.setProperty("file.encoding", charsetName);
125             defaultCharsetName = Charset.defaultCharset().name();
126             assertEquals("UTF-8", defaultCharsetName);
127
128             // Null behavior
129             try {
130                 Properties currentProps = System.getProperties();
131                 currentProps.remove("file.encoding");
132                 Charset.defaultCharset().name();
133                 fail("Should throw illegal IllegalArgumentException");
134             } catch (IllegalArgumentException e) {
135                 // expected
136             }
137
138             // IllegalCharsetName behavior
139             try {
140                 charsetName = "IMP~~OSSIBLE-8";
141                 System.setProperty("file.encoding", charsetName);
142                 Charset.defaultCharset().name();
143                 fail("Should throw IllegalCharsetNameException");
144             } catch (IllegalCharsetNameException e) {
145                 // expected
146             }
147         } finally {
148             System.setProperty("file.encoding", oldDefaultEncoding);
149         }
150     }
151
152     /**
153      * @tests java.nio.charset.Charset#forName(java.lang.String)
154      */
155     @TestTargetNew(
156         level = TestLevel.PARTIAL,
157         notes = "Exceptions checking missed.",
158         method = "forName",
159         args = {java.lang.String.class}
160     )
161     public void test_forNameLjava_lang_String() {
162         /*
163          * invoke forName two times with the same canonical name, it
164          * should return the same reference.
165          */
166         Charset cs1 = Charset.forName("UTF-8");
167         Charset cs2 = Charset.forName("UTF-8");
168         assertSame(cs1, cs2);
169
170         /*
171          * test forName: invoke forName two times for the same Charset using
172          * canonical name and alias, it should return the same reference.
173          */
174         Charset cs3 = Charset.forName("ASCII");
175         Charset cs4 = Charset.forName("US-ASCII");
176         assertSame(cs3, cs4);
177     }
178 }