001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import java.io.UnsupportedEncodingException;
021import java.util.Map;
022import java.util.TreeMap;
023import junit.framework.TestCase;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.testclassification.MiscTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.junit.ClassRule;
028import org.junit.experimental.categories.Category;
029
030/**
031 * Test order preservation characteristics of ordered Base64 dialect
032 */
033@Category({MiscTests.class, SmallTests.class})
034public class TestBase64 extends TestCase {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038      HBaseClassTestRule.forClass(TestBase64.class);
039
040  // Note: uris is sorted. We need to prove that the ordered Base64
041  // preserves that ordering
042  private String[] uris = {
043      "dns://dns.powerset.com/www.powerset.com",
044      "dns:www.powerset.com",
045      "file:///usr/bin/java",
046      "filename",
047      "ftp://one.two.three/index.html",
048      "http://one.two.three/index.html",
049      "https://one.two.three:9443/index.html",
050      "r:dns://com.powerset.dns/www.powerset.com",
051      "r:ftp://three.two.one/index.html",
052      "r:http://three.two.one/index.html",
053      "r:https://three.two.one:9443/index.html"
054  };
055
056  /**
057   * the test
058   * @throws UnsupportedEncodingException
059   */
060  public void testBase64() throws UnsupportedEncodingException {
061    TreeMap<String, String> sorted = new TreeMap<>();
062
063    for (int i = 0; i < uris.length; i++) {
064      byte[] bytes = uris[i].getBytes("UTF-8");
065      sorted.put(Base64.encodeBytes(bytes, Base64.ORDERED), uris[i]);
066    }
067    System.out.println();
068
069    int i = 0;
070    for (Map.Entry<String, String> e: sorted.entrySet()) {
071      assertTrue(uris[i++].compareTo(e.getValue()) == 0);
072    }
073  }
074
075}
076