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