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
29 /**
30 * Test order preservation characteristics of ordered Base64 dialect
31 */
32 public class TestBase64 extends TestCase {
33 // Note: uris is sorted. We need to prove that the ordered Base64
34 // preserves that ordering
35 private String[] uris = {
36 "dns://dns.powerset.com/www.powerset.com",
37 "dns:www.powerset.com",
38 "file:///usr/bin/java",
39 "filename",
40 "ftp://one.two.three/index.html",
41 "http://one.two.three/index.html",
42 "https://one.two.three:9443/index.html",
43 "r:dns://com.powerset.dns/www.powerset.com",
44 "r:ftp://three.two.one/index.html",
45 "r:http://three.two.one/index.html",
46 "r:https://three.two.one:9443/index.html"
47 };
48
49 /**
50 * the test
51 * @throws UnsupportedEncodingException
52 */
53 public void testBase64() throws UnsupportedEncodingException {
54 TreeMap<String, String> sorted = new TreeMap<String, String>();
55
56 for (int i = 0; i < uris.length; i++) {
57 byte[] bytes = uris[i].getBytes("UTF-8");
58 sorted.put(Base64.encodeBytes(bytes, Base64.ORDERED), uris[i]);
59 }
60 System.out.println();
61
62 int i = 0;
63 for (Map.Entry<String, String> e: sorted.entrySet()) {
64 assertTrue(uris[i++].compareTo(e.getValue()) == 0);
65 }
66 }
67 }