View Javadoc

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