View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, http://www.snakeyaml.org
    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 biz.source_code.base64Coder;
    18
    19  import java.io.UnsupportedEncodingException;
    20
    21  import junit.framework.TestCase;
    22
    23  import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
    24
    25  public class Base64CoderTest extends TestCase {
    26
    27      public void testDecode() throws UnsupportedEncodingException {
    28          check("Aladdin:open sesame", "QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
    29          check("a", "YQ==");
    30          check("aa", "YWE=");
    31          check("a=", "YT0=");
    32          check("", "");
    33      }
    34
    35      public void testFailure1() throws UnsupportedEncodingException {
    36          try {
    37              Base64Coder.decode("YQ=".toCharArray());
    38              fail();
    39          } catch (Exception e) {
    40              assertEquals("Length of Base64 encoded input string is not a multiple of 4.",
    41                      e.getMessage());
    42          }
    43      }
    44
    45      public void testFailure2() throws UnsupportedEncodingException {
    46          checkInvalid("\tWE=");
    47          checkInvalid("Y\tE=");
    48          checkInvalid("YW\t=");
    49          checkInvalid("YWE\t");
    50          //
    51          checkInvalid("©WE=");
    52          checkInvalid("Y©E=");
    53          checkInvalid("YW©=");
    54          checkInvalid("YWE©");
    55      }
    56
    57      private void checkInvalid(String encoded) {
    58          try {
    59              Base64Coder.decode(encoded.toCharArray());
    60              fail("Illegal chanracter.");
    61          } catch (Exception e) {
    62              assertEquals("Illegal character in Base64 encoded data.", e.getMessage());
    63          }
    64      }
    65
    66      private void check(String text, String encoded) throws UnsupportedEncodingException {
    67          char[] s1 = Base64Coder.encode(text.getBytes("UTF-8"));
    68          String t1 = new String(s1);
    69          assertEquals(encoded, t1);
    70          byte[] s2 = Base64Coder.decode(encoded.toCharArray());
    71          String t2 = new String(s2, "UTF-8");
    72          assertEquals(text, t2);
    73      }
    74  }