View Javadoc

1   /*
2    *  jDTAUS Core Utilities
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.nio.util.test;
22  
23  import junit.framework.Assert;
24  import junit.framework.TestCase;
25  import org.jdtaus.core.nio.util.Charsets;
26  
27  /**
28   * Testcase for {@code Charsets} utility.
29   *
30   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
31   * @version $JDTAUS: CharsetsTest.java 8709 2012-10-02 21:07:40Z schulte $
32   */
33  public class CharsetsTest extends TestCase
34  {
35      //--Tests-------------------------------------------------------------------
36  
37      /** String used for testing. */
38      private static final String TEST = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
39  
40      /**
41       * Tests the {@link Charsets#decode(byte[],String)} and
42       * {@link Charsets#encode(String,String)} methods to support the US-ASCII,
43       * UTF-8 and UTF-16 charsets.
44       */
45      public void testPlatformCharsets() throws Exception
46      {
47          final byte[] ascii = Charsets.encode( TEST, "US-ASCII" );
48          final byte[] utf8 = Charsets.encode( TEST, "UTF-8" );
49          final byte[] utf16 = Charsets.encode( TEST, "UTF-16" );
50  
51          final String asciiDecoded = Charsets.decode( ascii, "US-ASCII" );
52          final String utf8Decoded = Charsets.decode( utf8, "UTF-8" );
53          final String utf16Decoded = Charsets.decode( utf16, "UTF-16" );
54  
55          Assert.assertEquals( TEST.length(), ascii.length );
56          Assert.assertEquals( TEST.length(), utf8.length );
57  
58          Assert.assertEquals( TEST, asciiDecoded );
59          Assert.assertEquals( TEST, utf8Decoded );
60          Assert.assertEquals( TEST, utf16Decoded );
61      }
62  
63      //-------------------------------------------------------------------Tests--
64  }