1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.lang3; 18 19 import static org.junit.jupiter.api.Assertions.assertEquals; 20 import static org.junit.jupiter.api.Assertions.assertFalse; 21 import static org.junit.jupiter.api.Assertions.assertNotNull; 22 import static org.junit.jupiter.api.Assertions.assertNull; 23 import static org.junit.jupiter.api.Assertions.assertTrue; 24 25 import java.lang.reflect.Constructor; 26 import java.lang.reflect.Modifier; 27 28 import org.junit.jupiter.api.Test; 29 30 /** 31 * Unit tests {@link org.apache.commons.lang3.CharSetUtils}. 32 */ 33 public class CharSetUtilsTest extends AbstractLangTest { 34 35 @Test testConstructor()36 public void testConstructor() { 37 assertNotNull(new CharSetUtils()); 38 final Constructor<?>[] cons = CharSetUtils.class.getDeclaredConstructors(); 39 assertEquals(1, cons.length); 40 assertTrue(Modifier.isPublic(cons[0].getModifiers())); 41 assertTrue(Modifier.isPublic(CharSetUtils.class.getModifiers())); 42 assertFalse(Modifier.isFinal(CharSetUtils.class.getModifiers())); 43 } 44 45 @Test testSqueeze_StringString()46 public void testSqueeze_StringString() { 47 assertNull(CharSetUtils.squeeze(null, (String) null)); 48 assertNull(CharSetUtils.squeeze(null, "")); 49 50 assertEquals("", CharSetUtils.squeeze("", (String) null)); 51 assertEquals("", CharSetUtils.squeeze("", "")); 52 assertEquals("", CharSetUtils.squeeze("", "a-e")); 53 54 assertEquals("hello", CharSetUtils.squeeze("hello", (String) null)); 55 assertEquals("hello", CharSetUtils.squeeze("hello", "")); 56 assertEquals("hello", CharSetUtils.squeeze("hello", "a-e")); 57 assertEquals("helo", CharSetUtils.squeeze("hello", "l-p")); 58 assertEquals("heloo", CharSetUtils.squeeze("helloo", "l")); 59 assertEquals("hello", CharSetUtils.squeeze("helloo", "^l")); 60 } 61 62 @Test testSqueeze_StringStringarray()63 public void testSqueeze_StringStringarray() { 64 assertNull(CharSetUtils.squeeze(null, (String[]) null)); 65 assertNull(CharSetUtils.squeeze(null)); 66 assertNull(CharSetUtils.squeeze(null, null)); 67 assertNull(CharSetUtils.squeeze(null, "el")); 68 69 assertEquals("", CharSetUtils.squeeze("", (String[]) null)); 70 assertEquals("", CharSetUtils.squeeze("")); 71 assertEquals("", CharSetUtils.squeeze("", null)); 72 assertEquals("", CharSetUtils.squeeze("", "a-e")); 73 74 assertEquals("hello", CharSetUtils.squeeze("hello", (String[]) null)); 75 assertEquals("hello", CharSetUtils.squeeze("hello")); 76 assertEquals("hello", CharSetUtils.squeeze("hello", null)); 77 assertEquals("hello", CharSetUtils.squeeze("hello", "a-e")); 78 79 assertEquals("helo", CharSetUtils.squeeze("hello", "el")); 80 assertEquals("hello", CharSetUtils.squeeze("hello", "e")); 81 assertEquals("fofof", CharSetUtils.squeeze("fooffooff", "of")); 82 assertEquals("fof", CharSetUtils.squeeze("fooooff", "fo")); 83 } 84 85 @Test testContainsAny_StringString()86 public void testContainsAny_StringString() { 87 assertFalse(CharSetUtils.containsAny(null, (String) null)); 88 assertFalse(CharSetUtils.containsAny(null, "")); 89 90 assertFalse(CharSetUtils.containsAny("", (String) null)); 91 assertFalse(CharSetUtils.containsAny("", "")); 92 assertFalse(CharSetUtils.containsAny("", "a-e")); 93 94 assertFalse(CharSetUtils.containsAny("hello", (String) null)); 95 assertFalse(CharSetUtils.containsAny("hello", "")); 96 assertTrue(CharSetUtils.containsAny("hello", "a-e")); 97 assertTrue(CharSetUtils.containsAny("hello", "l-p")); 98 } 99 100 @Test testContainsAny_StringStringarray()101 public void testContainsAny_StringStringarray() { 102 assertFalse(CharSetUtils.containsAny(null, (String[]) null)); 103 assertFalse(CharSetUtils.containsAny(null)); 104 assertFalse(CharSetUtils.containsAny(null, null)); 105 assertFalse(CharSetUtils.containsAny(null, "a-e")); 106 107 assertFalse(CharSetUtils.containsAny("", (String[]) null)); 108 assertFalse(CharSetUtils.containsAny("")); 109 assertFalse(CharSetUtils.containsAny("", null)); 110 assertFalse(CharSetUtils.containsAny("", "a-e")); 111 112 assertFalse(CharSetUtils.containsAny("hello", (String[]) null)); 113 assertFalse(CharSetUtils.containsAny("hello")); 114 assertFalse(CharSetUtils.containsAny("hello", null)); 115 assertTrue(CharSetUtils.containsAny("hello", "a-e")); 116 117 assertTrue(CharSetUtils.containsAny("hello", "el")); 118 assertFalse(CharSetUtils.containsAny("hello", "x")); 119 assertTrue(CharSetUtils.containsAny("hello", "e-i")); 120 assertTrue(CharSetUtils.containsAny("hello", "a-z")); 121 assertFalse(CharSetUtils.containsAny("hello", "")); 122 } 123 124 @Test testCount_StringString()125 public void testCount_StringString() { 126 assertEquals(0, CharSetUtils.count(null, (String) null)); 127 assertEquals(0, CharSetUtils.count(null, "")); 128 129 assertEquals(0, CharSetUtils.count("", (String) null)); 130 assertEquals(0, CharSetUtils.count("", "")); 131 assertEquals(0, CharSetUtils.count("", "a-e")); 132 133 assertEquals(0, CharSetUtils.count("hello", (String) null)); 134 assertEquals(0, CharSetUtils.count("hello", "")); 135 assertEquals(1, CharSetUtils.count("hello", "a-e")); 136 assertEquals(3, CharSetUtils.count("hello", "l-p")); 137 } 138 139 @Test testCount_StringStringarray()140 public void testCount_StringStringarray() { 141 assertEquals(0, CharSetUtils.count(null, (String[]) null)); 142 assertEquals(0, CharSetUtils.count(null)); 143 assertEquals(0, CharSetUtils.count(null, null)); 144 assertEquals(0, CharSetUtils.count(null, "a-e")); 145 146 assertEquals(0, CharSetUtils.count("", (String[]) null)); 147 assertEquals(0, CharSetUtils.count("")); 148 assertEquals(0, CharSetUtils.count("", null)); 149 assertEquals(0, CharSetUtils.count("", "a-e")); 150 151 assertEquals(0, CharSetUtils.count("hello", (String[]) null)); 152 assertEquals(0, CharSetUtils.count("hello")); 153 assertEquals(0, CharSetUtils.count("hello", null)); 154 assertEquals(1, CharSetUtils.count("hello", "a-e")); 155 156 assertEquals(3, CharSetUtils.count("hello", "el")); 157 assertEquals(0, CharSetUtils.count("hello", "x")); 158 assertEquals(2, CharSetUtils.count("hello", "e-i")); 159 assertEquals(5, CharSetUtils.count("hello", "a-z")); 160 assertEquals(0, CharSetUtils.count("hello", "")); 161 } 162 163 @Test testKeep_StringString()164 public void testKeep_StringString() { 165 assertNull(CharSetUtils.keep(null, (String) null)); 166 assertNull(CharSetUtils.keep(null, "")); 167 168 assertEquals("", CharSetUtils.keep("", (String) null)); 169 assertEquals("", CharSetUtils.keep("", "")); 170 assertEquals("", CharSetUtils.keep("", "a-e")); 171 172 assertEquals("", CharSetUtils.keep("hello", (String) null)); 173 assertEquals("", CharSetUtils.keep("hello", "")); 174 assertEquals("", CharSetUtils.keep("hello", "xyz")); 175 assertEquals("hello", CharSetUtils.keep("hello", "a-z")); 176 assertEquals("hello", CharSetUtils.keep("hello", "oleh")); 177 assertEquals("ell", CharSetUtils.keep("hello", "el")); 178 } 179 180 @Test testKeep_StringStringarray()181 public void testKeep_StringStringarray() { 182 assertNull(CharSetUtils.keep(null, (String[]) null)); 183 assertNull(CharSetUtils.keep(null)); 184 assertNull(CharSetUtils.keep(null, null)); 185 assertNull(CharSetUtils.keep(null, "a-e")); 186 187 assertEquals("", CharSetUtils.keep("", (String[]) null)); 188 assertEquals("", CharSetUtils.keep("")); 189 assertEquals("", CharSetUtils.keep("", null)); 190 assertEquals("", CharSetUtils.keep("", "a-e")); 191 192 assertEquals("", CharSetUtils.keep("hello", (String[]) null)); 193 assertEquals("", CharSetUtils.keep("hello")); 194 assertEquals("", CharSetUtils.keep("hello", null)); 195 assertEquals("e", CharSetUtils.keep("hello", "a-e")); 196 197 assertEquals("e", CharSetUtils.keep("hello", "a-e")); 198 assertEquals("ell", CharSetUtils.keep("hello", "el")); 199 assertEquals("hello", CharSetUtils.keep("hello", "elho")); 200 assertEquals("hello", CharSetUtils.keep("hello", "a-z")); 201 assertEquals("----", CharSetUtils.keep("----", "-")); 202 assertEquals("ll", CharSetUtils.keep("hello", "l")); 203 } 204 205 @Test testDelete_StringString()206 public void testDelete_StringString() { 207 assertNull(CharSetUtils.delete(null, (String) null)); 208 assertNull(CharSetUtils.delete(null, "")); 209 210 assertEquals("", CharSetUtils.delete("", (String) null)); 211 assertEquals("", CharSetUtils.delete("", "")); 212 assertEquals("", CharSetUtils.delete("", "a-e")); 213 214 assertEquals("hello", CharSetUtils.delete("hello", (String) null)); 215 assertEquals("hello", CharSetUtils.delete("hello", "")); 216 assertEquals("hllo", CharSetUtils.delete("hello", "a-e")); 217 assertEquals("he", CharSetUtils.delete("hello", "l-p")); 218 assertEquals("hello", CharSetUtils.delete("hello", "z")); 219 } 220 221 @Test testDelete_StringStringarray()222 public void testDelete_StringStringarray() { 223 assertNull(CharSetUtils.delete(null, (String[]) null)); 224 assertNull(CharSetUtils.delete(null)); 225 assertNull(CharSetUtils.delete(null, null)); 226 assertNull(CharSetUtils.delete(null, "el")); 227 228 assertEquals("", CharSetUtils.delete("", (String[]) null)); 229 assertEquals("", CharSetUtils.delete("")); 230 assertEquals("", CharSetUtils.delete("", null)); 231 assertEquals("", CharSetUtils.delete("", "a-e")); 232 233 assertEquals("hello", CharSetUtils.delete("hello", (String[]) null)); 234 assertEquals("hello", CharSetUtils.delete("hello")); 235 assertEquals("hello", CharSetUtils.delete("hello", null)); 236 assertEquals("hello", CharSetUtils.delete("hello", "xyz")); 237 238 assertEquals("ho", CharSetUtils.delete("hello", "el")); 239 assertEquals("", CharSetUtils.delete("hello", "elho")); 240 assertEquals("hello", CharSetUtils.delete("hello", "")); 241 assertEquals("hello", CharSetUtils.delete("hello", "")); 242 assertEquals("", CharSetUtils.delete("hello", "a-z")); 243 assertEquals("", CharSetUtils.delete("----", "-")); 244 assertEquals("heo", CharSetUtils.delete("hello", "l")); 245 } 246 247 } 248