1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.utils; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertNull; 21 import static org.junit.Assert.assertTrue; 22 import static software.amazon.awssdk.utils.StringUtils.replacePrefixIgnoreCase; 23 24 import org.assertj.core.api.Assertions; 25 import org.junit.Test; 26 27 /** 28 * Unit tests for methods of {@link StringUtils}. 29 * 30 * Adapted from https://github.com/apache/commons-lang. 31 */ 32 public class StringUtilsTest { 33 private static final String FOO_UNCAP = "foo"; 34 private static final String FOO_CAP = "Foo"; 35 36 private static final String SENTENCE_UNCAP = "foo bar baz"; 37 private static final String SENTENCE_CAP = "Foo Bar Baz"; 38 39 @Test testUpperCase()40 public void testUpperCase() { 41 assertNull(StringUtils.upperCase(null)); 42 assertEquals("upperCase(String) failed", 43 "FOO TEST THING", StringUtils.upperCase("fOo test THING")); 44 assertEquals("upperCase(empty-string) failed", 45 "", StringUtils.upperCase("")); 46 } 47 48 @Test testLowerCase()49 public void testLowerCase() { 50 assertNull(StringUtils.lowerCase(null)); 51 assertEquals("lowerCase(String) failed", 52 "foo test thing", StringUtils.lowerCase("fOo test THING")); 53 assertEquals("lowerCase(empty-string) failed", 54 "", StringUtils.lowerCase("")); 55 } 56 57 @Test testCapitalize()58 public void testCapitalize() { 59 assertNull(StringUtils.capitalize(null)); 60 61 assertEquals("capitalize(empty-string) failed", 62 "", StringUtils.capitalize("")); 63 assertEquals("capitalize(single-char-string) failed", 64 "X", StringUtils.capitalize("x")); 65 assertEquals("capitalize(String) failed", 66 FOO_CAP, StringUtils.capitalize(FOO_CAP)); 67 assertEquals("capitalize(string) failed", 68 FOO_CAP, StringUtils.capitalize(FOO_UNCAP)); 69 70 assertEquals("capitalize(String) is not using TitleCase", 71 "\u01C8", StringUtils.capitalize("\u01C9")); 72 73 // Javadoc examples 74 assertNull(StringUtils.capitalize(null)); 75 assertEquals("", StringUtils.capitalize("")); 76 assertEquals("Cat", StringUtils.capitalize("cat")); 77 assertEquals("CAt", StringUtils.capitalize("cAt")); 78 assertEquals("'cat'", StringUtils.capitalize("'cat'")); 79 } 80 81 @Test testUnCapitalize()82 public void testUnCapitalize() { 83 assertNull(StringUtils.uncapitalize(null)); 84 85 assertEquals("uncapitalize(String) failed", 86 FOO_UNCAP, StringUtils.uncapitalize(FOO_CAP)); 87 assertEquals("uncapitalize(string) failed", 88 FOO_UNCAP, StringUtils.uncapitalize(FOO_UNCAP)); 89 assertEquals("uncapitalize(empty-string) failed", 90 "", StringUtils.uncapitalize("")); 91 assertEquals("uncapitalize(single-char-string) failed", 92 "x", StringUtils.uncapitalize("X")); 93 94 // Examples from uncapitalize Javadoc 95 assertEquals("cat", StringUtils.uncapitalize("cat")); 96 assertEquals("cat", StringUtils.uncapitalize("Cat")); 97 assertEquals("cAT", StringUtils.uncapitalize("CAT")); 98 } 99 100 @Test testReCapitalize()101 public void testReCapitalize() { 102 // reflection type of tests: Sentences. 103 assertEquals("uncapitalize(capitalize(String)) failed", 104 SENTENCE_UNCAP, StringUtils.uncapitalize(StringUtils.capitalize(SENTENCE_UNCAP))); 105 assertEquals("capitalize(uncapitalize(String)) failed", 106 SENTENCE_CAP, StringUtils.capitalize(StringUtils.uncapitalize(SENTENCE_CAP))); 107 108 // reflection type of tests: One word. 109 assertEquals("uncapitalize(capitalize(String)) failed", 110 FOO_UNCAP, StringUtils.uncapitalize(StringUtils.capitalize(FOO_UNCAP))); 111 assertEquals("capitalize(uncapitalize(String)) failed", 112 FOO_CAP, StringUtils.capitalize(StringUtils.uncapitalize(FOO_CAP))); 113 } 114 115 @Test testStartsWithIgnoreCase()116 public void testStartsWithIgnoreCase() { 117 assertTrue(StringUtils.startsWithIgnoreCase("helloworld", "hello")); 118 assertTrue(StringUtils.startsWithIgnoreCase("hELlOwOrlD", "hello")); 119 assertFalse(StringUtils.startsWithIgnoreCase("hello", "world")); 120 } 121 122 @Test testReplacePrefixIgnoreCase()123 public void testReplacePrefixIgnoreCase() { 124 assertEquals("lloWorld" ,replacePrefixIgnoreCase("helloWorld", "he", "")); 125 assertEquals("lloWORld" ,replacePrefixIgnoreCase("helloWORld", "He", "")); 126 assertEquals("llOwOrld" ,replacePrefixIgnoreCase("HEllOwOrld", "he", "")); 127 } 128 129 @Test findFirstOccurrence()130 public void findFirstOccurrence() { 131 assertEquals((Character) ':', StringUtils.findFirstOccurrence("abc:def/ghi:jkl/mno", ':', '/')); 132 assertEquals((Character) ':', StringUtils.findFirstOccurrence("abc:def/ghi:jkl/mno", '/', ':')); 133 } 134 135 @Test findFirstOccurrence_NoMatch()136 public void findFirstOccurrence_NoMatch() { 137 assertNull(StringUtils.findFirstOccurrence("abc", ':')); 138 } 139 140 @Test safeStringTooBoolean_mixedSpaceTrue_shouldReturnTrue()141 public void safeStringTooBoolean_mixedSpaceTrue_shouldReturnTrue() { 142 assertTrue(StringUtils.safeStringToBoolean("TrUe")); 143 } 144 145 @Test safeStringTooBoolean_mixedSpaceFalse_shouldReturnFalse()146 public void safeStringTooBoolean_mixedSpaceFalse_shouldReturnFalse() { 147 assertFalse(StringUtils.safeStringToBoolean("fAlSE")); 148 } 149 150 @Test(expected = IllegalArgumentException.class) safeStringTooBoolean_invalidValue_shouldThrowException()151 public void safeStringTooBoolean_invalidValue_shouldThrowException() { 152 assertFalse(StringUtils.safeStringToBoolean("foobar")); 153 } 154 155 @Test testRepeat()156 public void testRepeat() { 157 assertNull(StringUtils.repeat(null, 0)); 158 assertNull(StringUtils.repeat(null, 1)); 159 assertNull(StringUtils.repeat(null, 2)); 160 161 assertEquals("", StringUtils.repeat("", 0)); 162 assertEquals("", StringUtils.repeat("", 1)); 163 assertEquals("", StringUtils.repeat("", 2)); 164 165 assertEquals("", StringUtils.repeat("ab", 0)); 166 assertEquals("ab", StringUtils.repeat("ab", 1)); 167 assertEquals("abab", StringUtils.repeat("ab", 2)); 168 assertEquals("ababab", StringUtils.repeat("ab", 3)); 169 } 170 171 @Test(expected = IllegalArgumentException.class) repeat_negativeCount_shouldThrowIae()172 public void repeat_negativeCount_shouldThrowIae() { 173 StringUtils.repeat("a", -1); 174 } 175 176 @Test(expected = OutOfMemoryError.class) repeat_maxCount_shouldThrowOom()177 public void repeat_maxCount_shouldThrowOom() { 178 StringUtils.repeat("a", Integer.MAX_VALUE); 179 } 180 } 181