1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * 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 com.google.common.base; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.common.annotations.GwtIncompatible; 21 22 import junit.framework.TestCase; 23 24 import java.io.UnsupportedEncodingException; 25 import java.nio.charset.Charset; 26 import java.util.Arrays; 27 28 /** 29 * Unit test for {@link Charsets}. 30 * 31 * @author Mike Bostock 32 */ 33 @GwtCompatible(emulated = true) 34 public class CharsetsTest extends TestCase { 35 36 @GwtIncompatible("Non-UTF-8 Charset") testUsAscii()37 public void testUsAscii() { 38 assertEquals(Charset.forName("US-ASCII"), Charsets.US_ASCII); 39 } 40 41 @GwtIncompatible("Non-UTF-8 Charset") testIso88591()42 public void testIso88591() { 43 assertEquals(Charset.forName("ISO-8859-1"), Charsets.ISO_8859_1); 44 } 45 testUtf8()46 public void testUtf8() { 47 assertEquals(Charset.forName("UTF-8"), Charsets.UTF_8); 48 } 49 50 @GwtIncompatible("Non-UTF-8 Charset") testUtf16be()51 public void testUtf16be() { 52 assertEquals(Charset.forName("UTF-16BE"), Charsets.UTF_16BE); 53 } 54 55 @GwtIncompatible("Non-UTF-8 Charset") testUtf16le()56 public void testUtf16le() { 57 assertEquals(Charset.forName("UTF-16LE"), Charsets.UTF_16LE); 58 } 59 60 @GwtIncompatible("Non-UTF-8 Charset") testUtf16()61 public void testUtf16() { 62 assertEquals(Charset.forName("UTF-16"), Charsets.UTF_16); 63 } 64 65 @GwtIncompatible("Non-UTF-8 Charset") testWhyUsAsciiIsDangerous()66 public void testWhyUsAsciiIsDangerous() throws UnsupportedEncodingException { 67 byte[] b1 = "朝日新聞".getBytes(Charsets.US_ASCII.name()); 68 byte[] b2 = "聞朝日新".getBytes(Charsets.US_ASCII.name()); 69 byte[] b3 = "????".getBytes(Charsets.US_ASCII.name()); 70 byte[] b4 = "ニュース".getBytes(Charsets.US_ASCII.name()); 71 byte[] b5 = "スューー".getBytes(Charsets.US_ASCII.name()); 72 // Assert they are all equal (using the transitive property) 73 assertTrue(Arrays.equals(b1, b2)); 74 assertTrue(Arrays.equals(b2, b3)); 75 assertTrue(Arrays.equals(b3, b4)); 76 assertTrue(Arrays.equals(b4, b5)); 77 } 78 } 79