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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.apache.harmony.nio_char.tests.java.nio.charset;
19 import dalvik.annotation.TestLevel;
20 import dalvik.annotation.TestTargetClass;
21 import dalvik.annotation.TestTargetNew;
22 import junit.framework.TestCase;
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;
30 @TestTargetClass(Charset.class)
31 public class CharsetTest extends TestCase {
33 // Will contain names of charsets registered with IANA
34 Set knownRegisteredCharsets = new HashSet();
36 // Will contain names of charsets not known to be registered with IANA
37 Set unknownRegisteredCharsets = new HashSet();
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);
51 knownRegisteredCharsets.add(name);
56 * @tests java.nio.charset.Charset#isRegistered()
59 level = TestLevel.COMPLETE,
61 method = "isRegistered",
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());
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());
79 * @tests java.nio.charset.Charset#isSupported(String)
82 level = TestLevel.PARTIAL,
83 notes = "Checks IllegalCharsetNameException",
84 method = "isSupported",
85 args = {java.lang.String.class}
87 public void testIsSupported_EmptyString() {
88 // Regression for HARMONY-113
90 Charset.isSupported("");
91 fail("Assert 0: Should throw IllegalCharsetNameException");
92 } catch (IllegalCharsetNameException e) {
98 * @tests java.nio.charset.Charset#defaultCharset()
101 level = TestLevel.COMPLETE,
103 method = "defaultCharset",
106 public void test_defaultCharset() {
107 String charsetName = null;
108 String defaultCharsetName = null;
109 String oldDefaultEncoding = System.getProperty("file.encoding");
112 charsetName = "UTF-8";
113 System.setProperty("file.encoding", charsetName);
114 defaultCharsetName = Charset.defaultCharset().name();
115 assertEquals(charsetName, defaultCharsetName);
117 charsetName = "ISO-8859-1";
118 System.setProperty("file.encoding", charsetName);
119 defaultCharsetName = Charset.defaultCharset().name();
120 assertEquals(charsetName, defaultCharsetName);
122 // Unsupported behavior
123 charsetName = "IMPOSSIBLE-8";
124 System.setProperty("file.encoding", charsetName);
125 defaultCharsetName = Charset.defaultCharset().name();
126 assertEquals("UTF-8", defaultCharsetName);
130 Properties currentProps = System.getProperties();
131 currentProps.remove("file.encoding");
132 Charset.defaultCharset().name();
133 fail("Should throw illegal IllegalArgumentException");
134 } catch (IllegalArgumentException e) {
138 // IllegalCharsetName behavior
140 charsetName = "IMP~~OSSIBLE-8";
141 System.setProperty("file.encoding", charsetName);
142 Charset.defaultCharset().name();
143 fail("Should throw IllegalCharsetNameException");
144 } catch (IllegalCharsetNameException e) {
148 System.setProperty("file.encoding", oldDefaultEncoding);
153 * @tests java.nio.charset.Charset#forName(java.lang.String)
156 level = TestLevel.PARTIAL,
157 notes = "Exceptions checking missed.",
159 args = {java.lang.String.class}
161 public void test_forNameLjava_lang_String() {
163 * invoke forName two times with the same canonical name, it
164 * should return the same reference.
166 Charset cs1 = Charset.forName("UTF-8");
167 Charset cs2 = Charset.forName("UTF-8");
168 assertSame(cs1, cs2);
171 * test forName: invoke forName two times for the same Charset using
172 * canonical name and alias, it should return the same reference.
174 Charset cs3 = Charset.forName("ASCII");
175 Charset cs4 = Charset.forName("US-ASCII");
176 assertSame(cs3, cs4);