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 import java.nio.charset.Charset; 22 import java.util.Arrays; 23 import junit.framework.TestCase; 24 25 /** 26 * Unit test for {@link Charsets}. 27 * 28 * @author Mike Bostock 29 */ 30 @GwtCompatible(emulated = true) 31 public class CharsetsTest extends TestCase { 32 33 @GwtIncompatible // Non-UTF-8 Charset testUsAscii()34 public void testUsAscii() { 35 assertEquals(Charset.forName("US-ASCII"), Charsets.US_ASCII); 36 } 37 38 @GwtIncompatible // Non-UTF-8 Charset testIso88591()39 public void testIso88591() { 40 assertEquals(Charset.forName("ISO-8859-1"), Charsets.ISO_8859_1); 41 } 42 testUtf8()43 public void testUtf8() { 44 assertEquals(Charset.forName("UTF-8"), Charsets.UTF_8); 45 } 46 47 @GwtIncompatible // Non-UTF-8 Charset testUtf16be()48 public void testUtf16be() { 49 assertEquals(Charset.forName("UTF-16BE"), Charsets.UTF_16BE); 50 } 51 52 @GwtIncompatible // Non-UTF-8 Charset testUtf16le()53 public void testUtf16le() { 54 assertEquals(Charset.forName("UTF-16LE"), Charsets.UTF_16LE); 55 } 56 57 @GwtIncompatible // Non-UTF-8 Charset testUtf16()58 public void testUtf16() { 59 assertEquals(Charset.forName("UTF-16"), Charsets.UTF_16); 60 } 61 62 @GwtIncompatible // Non-UTF-8 Charset testWhyUsAsciiIsDangerous()63 public void testWhyUsAsciiIsDangerous() { 64 byte[] b1 = "朝日新聞".getBytes(Charsets.US_ASCII); 65 byte[] b2 = "聞朝日新".getBytes(Charsets.US_ASCII); 66 byte[] b3 = "????".getBytes(Charsets.US_ASCII); 67 byte[] b4 = "ニュース".getBytes(Charsets.US_ASCII); 68 byte[] b5 = "スューー".getBytes(Charsets.US_ASCII); 69 // Assert they are all equal (using the transitive property) 70 assertTrue(Arrays.equals(b1, b2)); 71 assertTrue(Arrays.equals(b2, b3)); 72 assertTrue(Arrays.equals(b3, b4)); 73 assertTrue(Arrays.equals(b4, b5)); 74 } 75 } 76