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