1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.util;
22  
23  import java.io.UnsupportedEncodingException;
24  import java.util.Map;
25  import java.util.TreeMap;
26  
27  import junit.framework.TestCase;
28  import org.apache.hadoop.hbase.SmallTests;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   * Test order preservation characteristics of ordered Base64 dialect
33   */
34  @Category(SmallTests.class)
35  public class TestBase64 extends TestCase {
36    // Note: uris is sorted. We need to prove that the ordered Base64
37    // preserves that ordering
38    private String[] uris = {
39        "dns://dns.powerset.com/www.powerset.com",
40        "dns:www.powerset.com",
41        "file:///usr/bin/java",
42        "filename",
43        "ftp://one.two.three/index.html",
44        "http://one.two.three/index.html",
45        "https://one.two.three:9443/index.html",
46        "r:dns://com.powerset.dns/www.powerset.com",
47        "r:ftp://three.two.one/index.html",
48        "r:http://three.two.one/index.html",
49        "r:https://three.two.one:9443/index.html"
50    };
51  
52    /**
53     * the test
54     * @throws UnsupportedEncodingException
55     */
56    public void testBase64() throws UnsupportedEncodingException {
57      TreeMap<String, String> sorted = new TreeMap<String, String>();
58  
59      for (int i = 0; i < uris.length; i++) {
60        byte[] bytes = uris[i].getBytes("UTF-8");
61        sorted.put(Base64.encodeBytes(bytes, Base64.ORDERED), uris[i]);
62      }
63      System.out.println();
64  
65      int i = 0;
66      for (Map.Entry<String, String> e: sorted.entrySet()) {
67        assertTrue(uris[i++].compareTo(e.getValue()) == 0);
68      }
69    }
70  
71    @org.junit.Rule
72    public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
73      new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
74  }
75