[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.util; |
---|
| 20 | |
---|
| 21 | import junit.framework.TestCase; |
---|
| 22 | |
---|
| 23 | public class TestStringUtils extends TestCase { |
---|
| 24 | final private static String NULL_STR = null; |
---|
| 25 | final private static String EMPTY_STR = ""; |
---|
| 26 | final private static String STR_WO_SPECIAL_CHARS = "AB"; |
---|
| 27 | final private static String STR_WITH_COMMA = "A,B"; |
---|
| 28 | final private static String ESCAPED_STR_WITH_COMMA = "A\\,B"; |
---|
| 29 | final private static String STR_WITH_ESCAPE = "AB\\"; |
---|
| 30 | final private static String ESCAPED_STR_WITH_ESCAPE = "AB\\\\"; |
---|
| 31 | final private static String STR_WITH_BOTH2 = ",A\\,,B\\\\,"; |
---|
| 32 | final private static String ESCAPED_STR_WITH_BOTH2 = |
---|
| 33 | "\\,A\\\\\\,\\,B\\\\\\\\\\,"; |
---|
| 34 | |
---|
| 35 | public void testEscapeString() throws Exception { |
---|
| 36 | assertEquals(NULL_STR, StringUtils.escapeString(NULL_STR)); |
---|
| 37 | assertEquals(EMPTY_STR, StringUtils.escapeString(EMPTY_STR)); |
---|
| 38 | assertEquals(STR_WO_SPECIAL_CHARS, |
---|
| 39 | StringUtils.escapeString(STR_WO_SPECIAL_CHARS)); |
---|
| 40 | assertEquals(ESCAPED_STR_WITH_COMMA, |
---|
| 41 | StringUtils.escapeString(STR_WITH_COMMA)); |
---|
| 42 | assertEquals(ESCAPED_STR_WITH_ESCAPE, |
---|
| 43 | StringUtils.escapeString(STR_WITH_ESCAPE)); |
---|
| 44 | assertEquals(ESCAPED_STR_WITH_BOTH2, |
---|
| 45 | StringUtils.escapeString(STR_WITH_BOTH2)); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | public void testSplit() throws Exception { |
---|
| 49 | assertEquals(NULL_STR, StringUtils.split(NULL_STR)); |
---|
| 50 | String[] splits = StringUtils.split(EMPTY_STR); |
---|
| 51 | assertEquals(0, splits.length); |
---|
| 52 | splits = StringUtils.split(",,"); |
---|
| 53 | assertEquals(0, splits.length); |
---|
| 54 | splits = StringUtils.split(STR_WO_SPECIAL_CHARS); |
---|
| 55 | assertEquals(1, splits.length); |
---|
| 56 | assertEquals(STR_WO_SPECIAL_CHARS, splits[0]); |
---|
| 57 | splits = StringUtils.split(STR_WITH_COMMA); |
---|
| 58 | assertEquals(2, splits.length); |
---|
| 59 | assertEquals("A", splits[0]); |
---|
| 60 | assertEquals("B", splits[1]); |
---|
| 61 | splits = StringUtils.split(ESCAPED_STR_WITH_COMMA); |
---|
| 62 | assertEquals(1, splits.length); |
---|
| 63 | assertEquals(ESCAPED_STR_WITH_COMMA, splits[0]); |
---|
| 64 | splits = StringUtils.split(STR_WITH_ESCAPE); |
---|
| 65 | assertEquals(1, splits.length); |
---|
| 66 | assertEquals(STR_WITH_ESCAPE, splits[0]); |
---|
| 67 | splits = StringUtils.split(STR_WITH_BOTH2); |
---|
| 68 | assertEquals(3, splits.length); |
---|
| 69 | assertEquals(EMPTY_STR, splits[0]); |
---|
| 70 | assertEquals("A\\,", splits[1]); |
---|
| 71 | assertEquals("B\\\\", splits[2]); |
---|
| 72 | splits = StringUtils.split(ESCAPED_STR_WITH_BOTH2); |
---|
| 73 | assertEquals(1, splits.length); |
---|
| 74 | assertEquals(ESCAPED_STR_WITH_BOTH2, splits[0]); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | public void testUnescapeString() throws Exception { |
---|
| 78 | assertEquals(NULL_STR, StringUtils.unEscapeString(NULL_STR)); |
---|
| 79 | assertEquals(EMPTY_STR, StringUtils.unEscapeString(EMPTY_STR)); |
---|
| 80 | assertEquals(STR_WO_SPECIAL_CHARS, |
---|
| 81 | StringUtils.unEscapeString(STR_WO_SPECIAL_CHARS)); |
---|
| 82 | try { |
---|
| 83 | StringUtils.unEscapeString(STR_WITH_COMMA); |
---|
| 84 | fail("Should throw IllegalArgumentException"); |
---|
| 85 | } catch (IllegalArgumentException e) { |
---|
| 86 | // expected |
---|
| 87 | } |
---|
| 88 | assertEquals(STR_WITH_COMMA, |
---|
| 89 | StringUtils.unEscapeString(ESCAPED_STR_WITH_COMMA)); |
---|
| 90 | try { |
---|
| 91 | StringUtils.unEscapeString(STR_WITH_ESCAPE); |
---|
| 92 | fail("Should throw IllegalArgumentException"); |
---|
| 93 | } catch (IllegalArgumentException e) { |
---|
| 94 | // expected |
---|
| 95 | } |
---|
| 96 | assertEquals(STR_WITH_ESCAPE, |
---|
| 97 | StringUtils.unEscapeString(ESCAPED_STR_WITH_ESCAPE)); |
---|
| 98 | try { |
---|
| 99 | StringUtils.unEscapeString(STR_WITH_BOTH2); |
---|
| 100 | fail("Should throw IllegalArgumentException"); |
---|
| 101 | } catch (IllegalArgumentException e) { |
---|
| 102 | // expected |
---|
| 103 | } |
---|
| 104 | assertEquals(STR_WITH_BOTH2, |
---|
| 105 | StringUtils.unEscapeString(ESCAPED_STR_WITH_BOTH2)); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | public void testTraditionalBinaryPrefix() throws Exception { |
---|
| 109 | String[] symbol = {"k", "m", "g", "t", "p", "e"}; |
---|
| 110 | long m = 1024; |
---|
| 111 | for(String s : symbol) { |
---|
| 112 | assertEquals(0, StringUtils.TraditionalBinaryPrefix.string2long(0 + s)); |
---|
| 113 | assertEquals(m, StringUtils.TraditionalBinaryPrefix.string2long(1 + s)); |
---|
| 114 | m *= 1024; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | assertEquals(0L, StringUtils.TraditionalBinaryPrefix.string2long("0")); |
---|
| 118 | assertEquals(-1259520L, StringUtils.TraditionalBinaryPrefix.string2long("-1230k")); |
---|
| 119 | assertEquals(956703965184L, StringUtils.TraditionalBinaryPrefix.string2long("891g")); |
---|
| 120 | } |
---|
| 121 | } |
---|