156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes/* Licensed to the Apache Software Foundation (ASF) under one or more 256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * contributor license agreements. See the NOTICE file distributed with 356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * this work for additional information regarding copyright ownership. 456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0 556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * (the "License"); you may not use this file except in compliance with 656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * the License. You may obtain a copy of the License at 756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * 856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * 1056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * Unless required by applicable law or agreed to in writing, software 1156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 1256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * See the License for the specific language governing permissions and 1456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * limitations under the License. 1556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes */ 1656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 17e5fea3d504609d22337a5311d3ce0e72314bceeeNarayan Kamathpackage org.apache.harmony.tests.java.nio.charset; 1856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 1956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.ByteBuffer; 20c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillinimport java.nio.ByteOrder; 2156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.CharBuffer; 2256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.charset.Charset; 2356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.charset.CharsetDecoder; 2456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.charset.CharsetEncoder; 2556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.charset.CoderResult; 2656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.charset.IllegalCharsetNameException; 2756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.charset.spi.CharsetProvider; 28ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughesimport java.nio.charset.UnsupportedCharsetException; 29ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughesimport java.util.ArrayList; 30ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughesimport java.util.Arrays; 31ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughesimport java.util.HashSet; 3256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.util.Iterator; 3356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.util.Locale; 34ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughesimport java.util.Set; 3556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.util.Vector; 361895955344b23f4d8a01596906a62f521c5463efNarayan Kamathimport libcore.java.nio.charset.SettableCharsetProvider; 3756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 3856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport junit.framework.TestCase; 3956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 4056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes/** 4156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * Test class java.nio.Charset. 4256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes */ 4356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughespublic class CharsetTest extends TestCase { 4456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 45ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_allAvailableCharsets() throws Exception { 46ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Check that we can instantiate every Charset, CharsetDecoder, and CharsetEncoder. 47ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes for (String charsetName : Charset.availableCharsets().keySet()) { 48ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs = Charset.forName(charsetName); 49ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertNotNull(cs.newDecoder()); 50ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes if (cs.canEncode()) { 51ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes CharsetEncoder enc = cs.newEncoder(); 52ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertNotNull(enc); 53ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertNotNull(enc.replacement()); 54ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 55ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 56ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 57ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 58ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_defaultCharset() { 59ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("UTF-8", Charset.defaultCharset().name()); 60ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 61ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 62ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_isRegistered() { 63ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Regression for HARMONY-45 64ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 65ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Will contain names of charsets registered with IANA 66ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Set<String> knownRegisteredCharsets = new HashSet<String>(); 67ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 68ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Will contain names of charsets not known to be registered with IANA 69ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Set<String> unknownRegisteredCharsets = new HashSet<String>(); 70ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 71ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Set<String> names = Charset.availableCharsets().keySet(); 72ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes for (Iterator nameItr = names.iterator(); nameItr.hasNext();) { 73ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes String name = (String) nameItr.next(); 74ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes if (name.toLowerCase(Locale.ROOT).startsWith("x-")) { 75ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes unknownRegisteredCharsets.add(name); 76ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } else { 77ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes knownRegisteredCharsets.add(name); 78ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 79ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 80ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 81ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes for (Iterator nameItr = knownRegisteredCharsets.iterator(); nameItr.hasNext();) { 82ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes String name = (String) nameItr.next(); 83ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs = Charset.forName(name); 84ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes if (!cs.isRegistered()) { 85ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes System.err.println("isRegistered was false for " + name + " " + cs.name() + " " + cs.aliases()); 86ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 87ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue("isRegistered was false for " + name + " " + cs.name() + " " + cs.aliases(), cs.isRegistered()); 88ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 89ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes for (Iterator nameItr = unknownRegisteredCharsets.iterator(); nameItr.hasNext();) { 90ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes String name = (String) nameItr.next(); 91ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs = Charset.forName(name); 92ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertFalse("isRegistered was true for " + name + " " + cs.name() + " " + cs.aliases(), cs.isRegistered()); 93ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 94ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 95ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 96ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_guaranteedCharsetsAvailable() throws Exception { 97ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // All Java implementations must support these charsets. 98ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertNotNull(Charset.forName("ISO-8859-1")); 99ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertNotNull(Charset.forName("US-ASCII")); 100ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertNotNull(Charset.forName("UTF-16")); 101ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertNotNull(Charset.forName("UTF-16BE")); 102ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertNotNull(Charset.forName("UTF-16LE")); 103ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertNotNull(Charset.forName("UTF-8")); 104ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 105ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 106ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // http://code.google.com/p/android/issues/detail?id=42769 107ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_42769() throws Exception { 108ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes ArrayList<Thread> threads = new ArrayList<Thread>(); 109ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes for (int i = 0; i < 10; ++i) { 110ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Thread t = new Thread(new Runnable() { 111ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void run() { 112ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes for (int i = 0; i < 50; ++i) { 113ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset.availableCharsets(); 114ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 115ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 116ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes }); 117ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes threads.add(t); 118ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 119ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 120ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes for (Thread t : threads) { 121ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes t.start(); 122ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 123ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes for (Thread t : threads) { 124ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes t.join(); 125ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 126ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 127ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 128ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_have_canonical_EUC_JP() throws Exception { 129ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("EUC-JP", Charset.forName("EUC-JP").name()); 130ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 131ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 132ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_EUC_JP_replacement_character() throws Exception { 1336ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes // We have text either side of the replacement character, because all kinds of errors 1346ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes // could lead to a replacement character being returned. 1356ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes assertEncodes(Charset.forName("EUC-JP"), " \ufffd ", ' ', 0xf4, 0xfe, ' '); 1366ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes assertDecodes(Charset.forName("EUC-JP"), " \ufffd ", ' ', 0xf4, 0xfe, ' '); 137ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 138ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 139ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_SCSU_replacement_character() throws Exception { 1406ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes // We have text either side of the replacement character, because all kinds of errors 1416ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes // could lead to a replacement character being returned. 1426ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes assertEncodes(Charset.forName("SCSU"), " \ufffd ", ' ', 14, 0xff, 0xfd, ' '); 1436ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes assertDecodes(Charset.forName("SCSU"), " \ufffd ", ' ', 14, 0xff, 0xfd, ' '); 144ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 145ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 146ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_Shift_JIS_replacement_character() throws Exception { 1476ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes // We have text either side of the replacement character, because all kinds of errors 1486ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes // could lead to a replacement character being returned. 1496ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes assertEncodes(Charset.forName("Shift_JIS"), " \ufffd ", ' ', 0xfc, 0xfc, ' '); 1506ad37f500b023ef09fd177ad8cd8e2ba0b842caeElliott Hughes assertDecodes(Charset.forName("Shift_JIS"), " \ufffd ", ' ', 0xfc, 0xfc, ' '); 151ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 152ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 153ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_UTF_16() throws Exception { 154ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs = Charset.forName("UTF-16"); 155ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Writes big-endian, with a big-endian BOM. 156ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEncodes(cs, "a\u0666", 0xfe, 0xff, 0, 'a', 0x06, 0x66); 157c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin // Reads whatever the BOM tells it to read, and swallows the BOM... 158ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 0xfe, 0xff, 0, 'a', 0x06, 0x66); 159ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 0xff, 0xfe, 'a', 0, 0x66, 0x06); 160ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // ...and defaults to reading big-endian if there's no BOM. 161ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 0, 'a', 0x06, 0x66); 162ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 163ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 164ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_UTF_16BE() throws Exception { 165ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs = Charset.forName("UTF-16BE"); 166ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Writes big-endian, with no BOM. 167ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEncodes(cs, "a\u0666", 0, 'a', 0x06, 0x66); 168c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin // We don't make any assertion about the handling of a little-endian BOM. 169ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Accepts a big-endian BOM and includes U+FEFF in the decoded output. 170ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "\ufeffa\u0666", 0xfe, 0xff, 0, 'a', 0x06, 0x66); 171ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Defaults to reading big-endian. 172ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 0, 'a', 0x06, 0x66); 173ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 174ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 175ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_UTF_16LE() throws Exception { 176ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs = Charset.forName("UTF-16LE"); 177ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Writes little-endian, with no BOM. 178ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEncodes(cs, "a\u0666", 'a', 0, 0x66, 0x06); 179ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Accepts a little-endian BOM and includes U+FEFF in the decoded output. 180ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "\ufeffa\u0666", 0xff, 0xfe, 'a', 0, 0x66, 0x06); 181c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin // We don't make any assertion about the handling of a big-endian BOM. 182ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Defaults to reading little-endian. 183ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 'a', 0, 0x66, 0x06); 184ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 185ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 186ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_x_UTF_16LE_BOM() throws Exception { 187ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs = Charset.forName("x-UTF-16LE-BOM"); 188ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Writes little-endian, with a BOM. 189ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEncodes(cs, "a\u0666", 0xff, 0xfe, 'a', 0, 0x66, 0x06); 190ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Accepts a little-endian BOM and swallows the BOM. 191ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 0xff, 0xfe, 'a', 0, 0x66, 0x06); 192c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin // We don't make any assertion about the handling of a big-endian BOM. 193ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Defaults to reading little-endian. 194ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 'a', 0, 0x66, 0x06); 195ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 196ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 197ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_UTF_32() throws Exception { 198ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs = Charset.forName("UTF-32"); 199c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin // Writes platform-endian, with a BOM. 200c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin if (ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) { 201c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin assertEncodes(cs, "a\u0666", 0, 0, 0xfe, 0xff, 0, 0, 0, 'a', 0, 0, 0x06, 0x66); 202c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin } else { 203c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin assertEncodes(cs, "a\u0666", 0xff, 0xfe, 0, 0, 'a', 0, 0, 0, 0x66, 0x06, 0, 0); 204c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin } 205c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin // Reads whatever the BOM tells it to read, and swallows the BOM... 206ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 0, 0, 0xfe, 0xff, 0, 0, 0, 'a', 0, 0, 0x06, 0x66); 207ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 0xff, 0xfe, 0, 0, 'a', 0, 0, 0, 0x66, 0x06, 0, 0); 208ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // ...and defaults to reading big-endian if there's no BOM. 209ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 0, 0, 0, 'a', 0, 0, 0x06, 0x66); 210ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 211ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 212ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_UTF_32BE() throws Exception { 213ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs = Charset.forName("UTF-32BE"); 214ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Writes big-endian, with no BOM. 215ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEncodes(cs, "a\u0666", 0, 0, 0, 'a', 0, 0, 0x06, 0x66); 216c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin // We don't make any assertion about the handling of a little-endian BOM. 217c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin // Accepts a big-endian BOM and includes U+FEFF in the decoded output. 218c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin assertDecodes(cs, "\ufeffa\u0666", 0, 0, 0xfe, 0xff, 0, 0, 0, 'a', 0, 0, 0x06, 0x66); 219ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Defaults to reading big-endian. 220ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 0, 0, 0, 'a', 0, 0, 0x06, 0x66); 221ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 222ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 223ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_UTF_32LE() throws Exception { 224ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs = Charset.forName("UTF-32LE"); 225ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Writes little-endian, with no BOM. 226ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEncodes(cs, "a\u0666", 'a', 0, 0, 0, 0x66, 0x06, 0, 0); 227c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin // Accepts a little-endian BOM and includes U+FEFF in the decoded output. 228c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin assertDecodes(cs, "\ufeffa\u0666", 0xff, 0xfe, 0, 0, 'a', 0, 0, 0, 0x66, 0x06, 0, 0); 229c04007c41f7363f5f4cde7e19a582ac13adbc153Pete Gillin // We don't make any assertion about the handling of a big-endian BOM. 230ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Defaults to reading little-endian. 231ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertDecodes(cs, "a\u0666", 'a', 0, 0, 0, 0x66, 0x06, 0, 0); 232ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 233ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 234ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes private byte[] toByteArray(int[] ints) { 235ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes byte[] result = new byte[ints.length]; 236ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes for (int i = 0; i < ints.length; ++i) { 237ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes result[i] = (byte) ints[i]; 238ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 239ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return result; 240ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 241ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 242ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes private void assertEncodes(Charset cs, String s, int... expectedByteInts) throws Exception { 243ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes ByteBuffer out = cs.encode(s); 244ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes byte[] bytes = new byte[out.remaining()]; 245ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes out.get(bytes); 246ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(Arrays.toString(toByteArray(expectedByteInts)), Arrays.toString(bytes)); 247ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 248ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 249ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes private void assertDecodes(Charset cs, String s, int... byteInts) throws Exception { 250ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes ByteBuffer in = ByteBuffer.wrap(toByteArray(byteInts)); 251ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes CharBuffer out = cs.decode(in); 252ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(s, out.toString()); 253ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 254ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 255ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_forNameLjava_lang_String() { 256ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Invoke forName two times with the same canonical name. 257ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // It should return the same reference. 258ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs1 = Charset.forName("UTF-8"); 259ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs2 = Charset.forName("UTF-8"); 260ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertSame(cs1, cs2); 261ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 262ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // test forName: invoke forName two times for the same Charset using 263ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // canonical name and alias, it should return the same reference. 264ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs3 = Charset.forName("ASCII"); 265ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset cs4 = Charset.forName("US-ASCII"); 266ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertSame(cs3, cs4); 267ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 268ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 269ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes static MockCharset charset1 = new MockCharset("mockCharset00", 270ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes new String[] { "mockCharset01", "mockCharset02" }); 271ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 272ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes static MockCharset charset2 = new MockCharset("mockCharset10", 273ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes new String[] { "mockCharset11", "mockCharset12" }); 274ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 275ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Test the required 6 charsets are supported. 276ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testRequiredCharsetSupported() { 277ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.isSupported("US-ASCII")); 278ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.isSupported("ASCII")); 279ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.isSupported("ISO-8859-1")); 280ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.isSupported("ISO8859_1")); 281ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.isSupported("UTF-8")); 282ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.isSupported("UTF8")); 283ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.isSupported("UTF-16")); 284ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.isSupported("UTF-16BE")); 285ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.isSupported("UTF-16LE")); 286ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 287ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset c1 = Charset.forName("US-ASCII"); 288ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("US-ASCII", Charset.forName("US-ASCII").name()); 289ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("US-ASCII", Charset.forName("ASCII").name()); 290ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("ISO-8859-1", Charset.forName("ISO-8859-1").name()); 291ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("ISO-8859-1", Charset.forName("ISO8859_1").name()); 292ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("UTF-8", Charset.forName("UTF-8").name()); 293ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("UTF-8", Charset.forName("UTF8").name()); 294ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("UTF-16", Charset.forName("UTF-16").name()); 295ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("UTF-16BE", Charset.forName("UTF-16BE").name()); 296ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("UTF-16LE", Charset.forName("UTF-16LE").name()); 297ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 298ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertNotSame(Charset.availableCharsets(), Charset.availableCharsets()); 299ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // assertSame(Charset.forName("US-ASCII"), Charset.availableCharsets().get("US-ASCII")); 300ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // assertSame(Charset.forName("US-ASCII"), c1); 301ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.availableCharsets().containsKey("US-ASCII")); 302ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.availableCharsets().containsKey("ISO-8859-1")); 303ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.availableCharsets().containsKey("UTF-8")); 304ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.availableCharsets().containsKey("UTF-16")); 305ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.availableCharsets().containsKey("UTF-16BE")); 306ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(Charset.availableCharsets().containsKey("UTF-16LE")); 307ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 308ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 309ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testIsSupported_Null() { 310ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 311ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset.isSupported(null); 312ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 313ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (IllegalArgumentException expected) { 314ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 315ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 31656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 317c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes public void testIsSupported_EmptyString() { 318c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes try { 319c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes Charset.isSupported(""); 320c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes fail(); 321c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } catch (IllegalArgumentException expected) { 322c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 323c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 324c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes 325c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes public void testIsSupported_InvalidInitialCharacter() { 326c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes try { 327c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes Charset.isSupported(".char"); 328c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes fail(); 329c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } catch (IllegalArgumentException expected) { 330c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 331c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 33256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 333ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testIsSupported_IllegalName() { 334ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 335ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset.isSupported(" ///#$$"); 336ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 337ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (IllegalCharsetNameException expected) { 338ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 339ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 340ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 341ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testIsSupported_NotSupported() { 342ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertFalse(Charset.isSupported("well-formed-name-of-a-charset-that-does-not-exist")); 343ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 344ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 345ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testForName_Null() { 346ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 347ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset.forName(null); 348ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 349ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (IllegalArgumentException expected) { 350ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 351ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 352ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 353ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testForName_EmptyString() { 354ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 355ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset.forName(""); 356ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 357ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (IllegalArgumentException expected) { 358ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 359ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 360ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 361ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testForName_InvalidInitialCharacter() { 362ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 363ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset.forName(".char"); 364ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 365ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (IllegalArgumentException expected) { 366ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 367ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 368ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 369ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testForName_IllegalName() { 370ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 371ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset.forName(" ///#$$"); 372ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 373ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (IllegalCharsetNameException expected) { 374ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 375ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 376ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 377ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testForName_NotSupported() { 378ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 379ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset.forName("impossible"); 380ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 381ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (UnsupportedCharsetException expected) { 382ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 383ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 384ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 385ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testConstructor_Normal() { 386ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes final String mockName = "mockChar1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:-_"; 387ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset(mockName, new String[] { "mock" }); 388ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(mockName, c.name()); 389ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(mockName, c.displayName()); 390ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(mockName, c.displayName(Locale.getDefault())); 391ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("mock", c.aliases().toArray()[0]); 392ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(1, c.aliases().toArray().length); 393ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 39456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 395c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes public void testConstructor_EmptyCanonicalName() { 396c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes try { 397c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes new MockCharset("", new String[0]); 398c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes fail(); 399c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } catch (IllegalCharsetNameException expected) { 400c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 401c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 402c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes 403c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes public void testConstructor_IllegalCanonicalName_Initial() { 404c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes try { 405c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes new MockCharset("-123", new String[] { "mock" }); 406c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes fail(); 407c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } catch (IllegalCharsetNameException expected) { 408c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 409c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 41056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 411ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testConstructor_IllegalCanonicalName_Middle() { 412ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 413ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes new MockCharset("1%%23", new String[] { "mock" }); 414ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 415ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (IllegalCharsetNameException expected) { 416ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 417ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 418ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes new MockCharset("1//23", new String[] { "mock" }); 419ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 420ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (IllegalCharsetNameException expected) { 421ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 422ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 423ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 424ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testConstructor_NullCanonicalName() { 425ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 426ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset(null, new String[] { "mock" }); 427ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 428ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (NullPointerException expected) { 429ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 430ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 431ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 432ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testConstructor_NullAliases() { 433ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset("mockChar", null); 434ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("mockChar", c.name()); 435ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("mockChar", c.displayName()); 436ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("mockChar", c.displayName(Locale.getDefault())); 437ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(0, c.aliases().toArray().length); 438ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 439ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 440ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testConstructor_NullAliase() { 441ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 442ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes new MockCharset("mockChar", new String[] { "mock", null }); 443ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 444ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (NullPointerException expected) { 445ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 446ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 447ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 448ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testConstructor_NoAliases() { 449ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset("mockChar", new String[0]); 450ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("mockChar", c.name()); 451ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("mockChar", c.displayName()); 452ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("mockChar", c.displayName(Locale.getDefault())); 453ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(0, c.aliases().toArray().length); 454ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 45556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 456c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes public void testConstructor_EmptyAliases() { 457c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes try { 458c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes new MockCharset("mockChar", new String[] { "" }); 459c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes fail(); 460c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } catch (IllegalCharsetNameException expected) { 461c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 462c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 463c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes 464c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes // Test the constructor with illegal aliases: starting with neither a digit nor a letter. 465c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes public void testConstructor_IllegalAliases_Initial() { 466c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes try { 467c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes new MockCharset("mockChar", new String[] { "mock", "-123" }); 468c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes fail(); 469c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } catch (IllegalCharsetNameException e) { 470c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 471c44b103bfd1a79762811d2125e9b94ce37300a44Elliott Hughes } 47256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 473ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testConstructor_IllegalAliases_Middle() { 474ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 475ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes new MockCharset("mockChar", new String[] { "mock", "22##ab" }); 476ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 477ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (IllegalCharsetNameException expected) { 478ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 479ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 480ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes new MockCharset("mockChar", new String[] { "mock", "22%%ab" }); 481ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 482ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (IllegalCharsetNameException expected) { 48356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 484ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 48556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 486ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testAliases_Multiple() { 487ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes final String mockName = "mockChar1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:-_"; 488ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset("mockChar", new String[] { "mock", mockName, "mock2" }); 489ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("mockChar", c.name()); 490ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(3, c.aliases().size()); 491ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c.aliases().contains("mock")); 492ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c.aliases().contains(mockName)); 493ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c.aliases().contains("mock2")); 494ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 495ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 496ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c.aliases().clear(); 497ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 498ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (UnsupportedOperationException expected) { 499ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 500ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 501ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 502ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testAliases_Duplicate() { 503ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes final String mockName = "mockChar1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:-_"; 504ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset("mockChar", new String[] { "mockChar", 505ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes "mock", mockName, "mock", "mockChar", "mock", "mock2" }); 506ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("mockChar", c.name()); 507ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(4, c.aliases().size()); 508ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c.aliases().contains("mockChar")); 509ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c.aliases().contains("mock")); 510ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c.aliases().contains(mockName)); 511ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c.aliases().contains("mock2")); 512ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 513ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 514ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testCanEncode() { 515ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset("mock", null); 516ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c.canEncode()); 517ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 518ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 519ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testIsRegistered() { 520ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset("mock", null); 521ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c.isRegistered()); 522ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 523ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 524ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testDisplayName_Locale_Null() { 525ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset("mock", null); 526ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("mock", c.displayName(null)); 527ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 528ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 529ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testCompareTo_Normal() { 530ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("mock", null); 531ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(0, c1.compareTo(c1)); 532ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 533ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c2 = new MockCharset("Mock", null); 534ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(0, c1.compareTo(c2)); 535ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 536ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c2 = new MockCharset("mock2", null); 537ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c1.compareTo(c2) < 0); 538ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c2.compareTo(c1) > 0); 539ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 540ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c2 = new MockCharset("mack", null); 541ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c1.compareTo(c2) > 0); 542ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c2.compareTo(c1) < 0); 543ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 544ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c2 = new MockCharset("m.", null); 545ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c1.compareTo(c2) > 0); 546ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c2.compareTo(c1) < 0); 547ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 548ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c2 = new MockCharset("m:", null); 549ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("mock".compareToIgnoreCase("m:"), c1.compareTo(c2)); 550ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("m:".compareToIgnoreCase("mock"), c2.compareTo(c1)); 551ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 552ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c2 = new MockCharset("m-", null); 553ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c1.compareTo(c2) > 0); 554ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c2.compareTo(c1) < 0); 555ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 556ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c2 = new MockCharset("m_", null); 557ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c1.compareTo(c2) > 0); 558ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c2.compareTo(c1) < 0); 559ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 560ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 561ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testCompareTo_Null() { 562ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("mock", null); 563ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 564ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c1.compareTo(null); 565ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 566ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (NullPointerException expected) { 56756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 568ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 569ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 570ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testCompareTo_DiffCharsetClass() { 571ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("mock", null); 572ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset2 c2 = new MockCharset2("Mock", new String[] { "myname" }); 573ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(0, c1.compareTo(c2)); 574ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(0, c2.compareTo(c1)); 575ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 576ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 577ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEquals_Normal() { 578ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("mock", null); 579ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset2 c2 = new MockCharset2("mock", null); 580ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c1.equals(c2)); 581ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c2.equals(c1)); 58256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 583ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c2 = new MockCharset2("Mock", null); 584ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertFalse(c1.equals(c2)); 585ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertFalse(c2.equals(c1)); 586ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 587ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 588ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEquals_Null() { 589ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("mock", null); 590ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertFalse(c1.equals(null)); 591ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 592ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 593ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEquals_NonCharsetObject() { 594ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("mock", null); 595ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertFalse(c1.equals("test")); 596ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 597ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 598ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEquals_DiffCharsetClass() { 599ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("mock", null); 600ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset2 c2 = new MockCharset2("mock", null); 601ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c1.equals(c2)); 602ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(c2.equals(c1)); 603ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 604ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 605ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testHashCode_DiffCharsetClass() { 606ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("mock", null); 607ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(c1.hashCode(), "mock".hashCode()); 608ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 609ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes final String mockName = "mockChar1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:-_"; 610ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c1 = new MockCharset(mockName, new String[] { "mockChar", "mock", 611ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes mockName, "mock", "mockChar", "mock", "mock2" }); 612ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(mockName.hashCode(), c1.hashCode()); 613ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 614ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 615ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEncode_CharBuffer_Normal() throws Exception { 616ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("testEncode_CharBuffer_Normal_mock", null); 617ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes ByteBuffer bb = c1.encode(CharBuffer.wrap("abcdefg")); 618ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("abcdefg", new String(bb.array(), "iso8859-1")); 619ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes bb = c1.encode(CharBuffer.wrap("")); 620ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("", new String(bb.array(), "iso8859-1")); 621ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 622ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 623ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEncode_CharBuffer_Unmappable() throws Exception { 624ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset c1 = Charset.forName("iso8859-1"); 625ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes ByteBuffer bb = c1.encode(CharBuffer.wrap("abcd\u5D14efg")); 626ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(new String(bb.array(), "iso8859-1"), 627ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes "abcd" + new String(c1.newEncoder().replacement(), "iso8859-1") + "efg"); 628ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 629ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 630ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEncode_CharBuffer_NullCharBuffer() { 631ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset("mock", null); 632ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 633ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c.encode((CharBuffer) null); 634ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 635ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (NullPointerException expected) { 636ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 637ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 638ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 639ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEncode_CharBuffer_NullEncoder() { 640ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset2 c = new MockCharset2("mock2", null); 641ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 642ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c.encode(CharBuffer.wrap("hehe")); 643ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 644ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (NullPointerException expected) { 645ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 646ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 647ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 648ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEncode_String_Normal() throws Exception { 649ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("testEncode_String_Normal_mock", null); 650ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes ByteBuffer bb = c1.encode("abcdefg"); 651ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("abcdefg", new String(bb.array(), "iso8859-1")); 652ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes bb = c1.encode(""); 653ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("", new String(bb.array(), "iso8859-1")); 654ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 655ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 656ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEncode_String_Unmappable() throws Exception { 657ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset c1 = Charset.forName("iso8859-1"); 658ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes ByteBuffer bb = c1.encode("abcd\u5D14efg"); 659ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(new String(bb.array(), "iso8859-1"), 660ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes "abcd" + new String(c1.newEncoder().replacement(), "iso8859-1") + "efg"); 661ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 662ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 663ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEncode_String_NullString() { 664ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset("mock", null); 665ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 666ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c.encode((String) null); 667ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 668ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (NullPointerException expected) { 669ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 670ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 671ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 672ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testEncode_String_NullEncoder() { 673ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset2 c = new MockCharset2("mock2", null); 674ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 675ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c.encode("hehe"); 676ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 677ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (NullPointerException expected) { 678ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 679ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 680ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 681ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testDecode_Normal() throws Exception { 682ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("mock", null); 683ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes CharBuffer cb = c1.decode(ByteBuffer.wrap("abcdefg".getBytes("iso8859-1"))); 684ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("abcdefg", new String(cb.array())); 685ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes cb = c1.decode(ByteBuffer.wrap("".getBytes("iso8859-1"))); 686ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals("", new String(cb.array())); 687ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 688ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 689ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testDecode_Malformed() throws Exception { 690ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Charset c1 = Charset.forName("iso8859-1"); 691ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes CharBuffer cb = c1.decode(ByteBuffer.wrap("abcd\u5D14efg".getBytes("iso8859-1"))); 692ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes byte[] replacement = c1.newEncoder().replacement(); 693ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertEquals(new String(cb.array()).trim(), "abcd" + new String(replacement, "iso8859-1") + "efg"); 694ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 695ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 696ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testDecode_NullByteBuffer() { 697ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c = new MockCharset("mock", null); 698ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 699ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c.decode(null); 700ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 701ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (NullPointerException expected) { 702ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 703ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 704ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 705ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testDecode_NullDecoder() { 706ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset2 c = new MockCharset2("mock2", null); 707ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 708ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes c.decode(ByteBuffer.wrap("hehe".getBytes())); 709ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 710ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (NullPointerException expected) { 711ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 712ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 713ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 714ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testToString() { 715ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes MockCharset c1 = new MockCharset("mock", null); 716ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(-1 != c1.toString().indexOf("mock")); 717ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 718ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 719ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes static final class MockCharset extends Charset { 720ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public MockCharset(String canonicalName, String[] aliases) { 721ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes super(canonicalName, aliases); 722ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 723ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 724ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public boolean contains(Charset cs) { 725ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return false; 726ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 727ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 728ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public CharsetDecoder newDecoder() { 729ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return new MockDecoder(this); 730ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 731ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 732ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public CharsetEncoder newEncoder() { 733ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return new MockEncoder(this); 734ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 735ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 736ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 737ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes static class MockCharset2 extends Charset { 738ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public MockCharset2(String canonicalName, String[] aliases) { 739ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes super(canonicalName, aliases); 740ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 741ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 742ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public boolean contains(Charset cs) { 743ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return false; 744ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 745ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 746ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public CharsetDecoder newDecoder() { 747ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return null; 748ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 749ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 750ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public CharsetEncoder newEncoder() { 751ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return null; 752ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 753ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 754ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 755ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes static class MockEncoder extends java.nio.charset.CharsetEncoder { 756ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public MockEncoder(Charset cs) { 757ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes super(cs, 1, 3, new byte[] { (byte) '?' }); 758ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 759ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 760ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) { 761ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes while (in.remaining() > 0) { 762ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes out.put((byte) in.get()); 763ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // out.put((byte) '!'); 764ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 765ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return CoderResult.UNDERFLOW; 766ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 767ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 768ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 769ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes static class MockDecoder extends java.nio.charset.CharsetDecoder { 770ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public MockDecoder(Charset cs) { 771ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes super(cs, 1, 10); 772ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 773ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 774ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) { 775ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes while (in.remaining() > 0) { 776ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes out.put((char) in.get()); 777ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 778ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return CoderResult.UNDERFLOW; 779ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 780ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 781ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 782ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 783ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Test the method isSupported(String) with charset supported by multiple providers. 784ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testIsSupported_And_ForName_NormalProvider() throws Exception { 7851895955344b23f4d8a01596906a62f521c5463efNarayan Kamath SettableCharsetProvider.setDelegate(new MockCharsetProvider()); 7861895955344b23f4d8a01596906a62f521c5463efNarayan Kamath try { 7871895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.isSupported("mockCharset10")); 7881895955344b23f4d8a01596906a62f521c5463efNarayan Kamath // ignore case problem in mock, intended 7891895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.isSupported("MockCharset11")); 7901895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.isSupported("MockCharset12")); 7911895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.isSupported("MOCKCharset10")); 7921895955344b23f4d8a01596906a62f521c5463efNarayan Kamath // intended case problem in mock 7931895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.isSupported("MOCKCharset11")); 7941895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.isSupported("MOCKCharset12")); 795ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 7961895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.forName("mockCharset10") instanceof MockCharset); 7971895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.forName("mockCharset11") instanceof MockCharset); 7981895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.forName("mockCharset12") instanceof MockCharset); 799ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 8001895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.forName("mockCharset10") == charset2); 8011895955344b23f4d8a01596906a62f521c5463efNarayan Kamath // intended case problem in mock 8021895955344b23f4d8a01596906a62f521c5463efNarayan Kamath Charset.forName("mockCharset11"); 8031895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.forName("mockCharset12") == charset2); 8041895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } finally { 8051895955344b23f4d8a01596906a62f521c5463efNarayan Kamath SettableCharsetProvider.clearDelegate(); 8061895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } 807ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 808ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 809ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Test the method availableCharsets() with charset supported by multiple providers. 810ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testAvailableCharsets_NormalProvider() throws Exception { 8111895955344b23f4d8a01596906a62f521c5463efNarayan Kamath SettableCharsetProvider.setDelegate(new MockCharsetProvider()); 8121895955344b23f4d8a01596906a62f521c5463efNarayan Kamath try { 8131895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.availableCharsets().containsKey("mockCharset00")); 8141895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.availableCharsets().containsKey("MOCKCharset00")); 8151895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.availableCharsets().get("mockCharset00") instanceof MockCharset); 8161895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.availableCharsets().get("MOCKCharset00") instanceof MockCharset); 8171895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertFalse(Charset.availableCharsets().containsKey("mockCharset01")); 8181895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertFalse(Charset.availableCharsets().containsKey("mockCharset02")); 819ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 8201895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.availableCharsets().get("mockCharset10") == charset2); 8211895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.availableCharsets().get("MOCKCharset10") == charset2); 8221895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertFalse(Charset.availableCharsets().containsKey("mockCharset11")); 8231895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertFalse(Charset.availableCharsets().containsKey("mockCharset12")); 824ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 8251895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.availableCharsets().containsKey("mockCharset10")); 8261895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.availableCharsets().containsKey("MOCKCharset10")); 8271895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertTrue(Charset.availableCharsets().get("mockCharset10") == charset2); 8281895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertFalse(Charset.availableCharsets().containsKey("mockCharset11")); 8291895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertFalse(Charset.availableCharsets().containsKey("mockCharset12")); 8301895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } finally { 8311895955344b23f4d8a01596906a62f521c5463efNarayan Kamath SettableCharsetProvider.clearDelegate(); 8321895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } 833ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 834ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 835ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Test the method forName(String) when the charset provider supports a 836ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // built-in charset. 837ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void testForName_DuplicateWithBuiltInCharset() throws Exception { 8381895955344b23f4d8a01596906a62f521c5463efNarayan Kamath SettableCharsetProvider.setDelegate(new MockCharsetProviderASCII()); 8391895955344b23f4d8a01596906a62f521c5463efNarayan Kamath try { 8401895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertFalse(Charset.forName("us-ascii") instanceof MockCharset); 8411895955344b23f4d8a01596906a62f521c5463efNarayan Kamath assertFalse(Charset.availableCharsets().get("us-ascii") instanceof MockCharset); 8421895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } finally { 8431895955344b23f4d8a01596906a62f521c5463efNarayan Kamath SettableCharsetProvider.clearDelegate(); 8441895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } 8451895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } 8461895955344b23f4d8a01596906a62f521c5463efNarayan Kamath 8471895955344b23f4d8a01596906a62f521c5463efNarayan Kamath // Fails on Android with a StackOverflowException. 8481895955344b23f4d8a01596906a62f521c5463efNarayan Kamath public void testForName_withProviderWithRecursiveCall() throws Exception { 8491895955344b23f4d8a01596906a62f521c5463efNarayan Kamath SettableCharsetProvider.setDelegate(new MockCharsetProviderWithRecursiveCall()); 8501895955344b23f4d8a01596906a62f521c5463efNarayan Kamath try { 8511895955344b23f4d8a01596906a62f521c5463efNarayan Kamath Charset.forName("poop"); 8521895955344b23f4d8a01596906a62f521c5463efNarayan Kamath fail(); 8531895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } catch (UnsupportedCharsetException expected) { 8541895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } finally { 8551895955344b23f4d8a01596906a62f521c5463efNarayan Kamath SettableCharsetProvider.clearDelegate(); 8561895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } 8571895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } 8581895955344b23f4d8a01596906a62f521c5463efNarayan Kamath 8591895955344b23f4d8a01596906a62f521c5463efNarayan Kamath public static class MockCharsetProviderWithRecursiveCall extends CharsetProvider { 8601895955344b23f4d8a01596906a62f521c5463efNarayan Kamath @Override 8611895955344b23f4d8a01596906a62f521c5463efNarayan Kamath public Iterator<Charset> charsets() { 8621895955344b23f4d8a01596906a62f521c5463efNarayan Kamath return null; 8631895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } 8641895955344b23f4d8a01596906a62f521c5463efNarayan Kamath 8651895955344b23f4d8a01596906a62f521c5463efNarayan Kamath @Override 8661895955344b23f4d8a01596906a62f521c5463efNarayan Kamath public Charset charsetForName(String charsetName) { 8671895955344b23f4d8a01596906a62f521c5463efNarayan Kamath if (Charset.isSupported(charsetName)) { 8681895955344b23f4d8a01596906a62f521c5463efNarayan Kamath return Charset.forName(charsetName); 8691895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } 8701895955344b23f4d8a01596906a62f521c5463efNarayan Kamath 8711895955344b23f4d8a01596906a62f521c5463efNarayan Kamath return null; 8721895955344b23f4d8a01596906a62f521c5463efNarayan Kamath } 873ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 874ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 875ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public static class MockCharsetProvider extends CharsetProvider { 876ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public Charset charsetForName(String charsetName) { 877ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes if ("MockCharset00".equalsIgnoreCase(charsetName) || 878ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes "MockCharset01".equalsIgnoreCase(charsetName) || 879ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes "MockCharset02".equalsIgnoreCase(charsetName)) { 880ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return charset1; 881ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } else if ("MockCharset10".equalsIgnoreCase(charsetName) || 882ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes "MockCharset11".equalsIgnoreCase(charsetName) || 883ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes "MockCharset12".equalsIgnoreCase(charsetName)) { 884ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return charset2; 885ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 886ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return null; 887ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 888ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 889ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public Iterator charsets() { 890ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Vector v = new Vector(); 891ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes v.add(charset1); 892ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes v.add(charset2); 893ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return v.iterator(); 894ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 895ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 896ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 897ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Another mock charset provider attempting to provide the built-in charset "ascii" again. 898ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public static class MockCharsetProviderASCII extends CharsetProvider { 899ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public Charset charsetForName(String charsetName) { 900ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes if ("US-ASCII".equalsIgnoreCase(charsetName) || "ASCII".equalsIgnoreCase(charsetName)) { 901ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return new MockCharset("US-ASCII", new String[] { "ASCII" }); 902ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 903ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return null; 904ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 905ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 906ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public Iterator charsets() { 907ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Vector v = new Vector(); 908ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes v.add(new MockCharset("US-ASCII", new String[] { "ASCII" })); 909ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes return v.iterator(); 910ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 911ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 91256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes} 913