View Javadoc
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
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
46      public void testContainsAny_StringString() {
47          assertFalse(CharSetUtils.containsAny(null, (String) null));
48          assertFalse(CharSetUtils.containsAny(null, ""));
49  
50          assertFalse(CharSetUtils.containsAny("", (String) null));
51          assertFalse(CharSetUtils.containsAny("", ""));
52          assertFalse(CharSetUtils.containsAny("", "a-e"));
53  
54          assertFalse(CharSetUtils.containsAny("hello", (String) null));
55          assertFalse(CharSetUtils.containsAny("hello", ""));
56          assertTrue(CharSetUtils.containsAny("hello", "a-e"));
57          assertTrue(CharSetUtils.containsAny("hello", "l-p"));
58      }
59  
60      @Test
61      public void testContainsAny_StringStringarray() {
62          assertFalse(CharSetUtils.containsAny(null, (String[]) null));
63          assertFalse(CharSetUtils.containsAny(null));
64          assertFalse(CharSetUtils.containsAny(null, null));
65          assertFalse(CharSetUtils.containsAny(null, "a-e"));
66  
67          assertFalse(CharSetUtils.containsAny("", (String[]) null));
68          assertFalse(CharSetUtils.containsAny(""));
69          assertFalse(CharSetUtils.containsAny("", null));
70          assertFalse(CharSetUtils.containsAny("", "a-e"));
71  
72          assertFalse(CharSetUtils.containsAny("hello", (String[]) null));
73          assertFalse(CharSetUtils.containsAny("hello"));
74          assertFalse(CharSetUtils.containsAny("hello", null));
75          assertTrue(CharSetUtils.containsAny("hello", "a-e"));
76  
77          assertTrue(CharSetUtils.containsAny("hello", "el"));
78          assertFalse(CharSetUtils.containsAny("hello", "x"));
79          assertTrue(CharSetUtils.containsAny("hello", "e-i"));
80          assertTrue(CharSetUtils.containsAny("hello", "a-z"));
81          assertFalse(CharSetUtils.containsAny("hello", ""));
82      }
83  
84      @Test
85      public void testCount_StringString() {
86          assertEquals(0, CharSetUtils.count(null, (String) null));
87          assertEquals(0, CharSetUtils.count(null, ""));
88  
89          assertEquals(0, CharSetUtils.count("", (String) null));
90          assertEquals(0, CharSetUtils.count("", ""));
91          assertEquals(0, CharSetUtils.count("", "a-e"));
92  
93          assertEquals(0, CharSetUtils.count("hello", (String) null));
94          assertEquals(0, CharSetUtils.count("hello", ""));
95          assertEquals(1, CharSetUtils.count("hello", "a-e"));
96          assertEquals(3, CharSetUtils.count("hello", "l-p"));
97      }
98  
99      @Test
100     public void testCount_StringStringarray() {
101         assertEquals(0, CharSetUtils.count(null, (String[]) null));
102         assertEquals(0, CharSetUtils.count(null));
103         assertEquals(0, CharSetUtils.count(null, null));
104         assertEquals(0, CharSetUtils.count(null, "a-e"));
105 
106         assertEquals(0, CharSetUtils.count("", (String[]) null));
107         assertEquals(0, CharSetUtils.count(""));
108         assertEquals(0, CharSetUtils.count("", null));
109         assertEquals(0, CharSetUtils.count("", "a-e"));
110 
111         assertEquals(0, CharSetUtils.count("hello", (String[]) null));
112         assertEquals(0, CharSetUtils.count("hello"));
113         assertEquals(0, CharSetUtils.count("hello", null));
114         assertEquals(1, CharSetUtils.count("hello", "a-e"));
115 
116         assertEquals(3, CharSetUtils.count("hello", "el"));
117         assertEquals(0, CharSetUtils.count("hello", "x"));
118         assertEquals(2, CharSetUtils.count("hello", "e-i"));
119         assertEquals(5, CharSetUtils.count("hello", "a-z"));
120         assertEquals(0, CharSetUtils.count("hello", ""));
121     }
122 
123     @Test
124     public void testDelete_StringString() {
125         assertNull(CharSetUtils.delete(null, (String) null));
126         assertNull(CharSetUtils.delete(null, ""));
127 
128         assertEquals("", CharSetUtils.delete("", (String) null));
129         assertEquals("", CharSetUtils.delete("", ""));
130         assertEquals("", CharSetUtils.delete("", "a-e"));
131 
132         assertEquals("hello", CharSetUtils.delete("hello", (String) null));
133         assertEquals("hello", CharSetUtils.delete("hello", ""));
134         assertEquals("hllo", CharSetUtils.delete("hello", "a-e"));
135         assertEquals("he", CharSetUtils.delete("hello", "l-p"));
136         assertEquals("hello", CharSetUtils.delete("hello", "z"));
137     }
138 
139     @Test
140     public void testDelete_StringStringarray() {
141         assertNull(CharSetUtils.delete(null, (String[]) null));
142         assertNull(CharSetUtils.delete(null));
143         assertNull(CharSetUtils.delete(null, null));
144         assertNull(CharSetUtils.delete(null, "el"));
145 
146         assertEquals("", CharSetUtils.delete("", (String[]) null));
147         assertEquals("", CharSetUtils.delete(""));
148         assertEquals("", CharSetUtils.delete("", null));
149         assertEquals("", CharSetUtils.delete("", "a-e"));
150 
151         assertEquals("hello", CharSetUtils.delete("hello", (String[]) null));
152         assertEquals("hello", CharSetUtils.delete("hello"));
153         assertEquals("hello", CharSetUtils.delete("hello", null));
154         assertEquals("hello", CharSetUtils.delete("hello", "xyz"));
155 
156         assertEquals("ho", CharSetUtils.delete("hello", "el"));
157         assertEquals("", CharSetUtils.delete("hello", "elho"));
158         assertEquals("hello", CharSetUtils.delete("hello", ""));
159         assertEquals("hello", CharSetUtils.delete("hello", ""));
160         assertEquals("", CharSetUtils.delete("hello", "a-z"));
161         assertEquals("", CharSetUtils.delete("----", "-"));
162         assertEquals("heo", CharSetUtils.delete("hello", "l"));
163     }
164 
165     @Test
166     public void testKeep_StringString() {
167         assertNull(CharSetUtils.keep(null, (String) null));
168         assertNull(CharSetUtils.keep(null, ""));
169 
170         assertEquals("", CharSetUtils.keep("", (String) null));
171         assertEquals("", CharSetUtils.keep("", ""));
172         assertEquals("", CharSetUtils.keep("", "a-e"));
173 
174         assertEquals("", CharSetUtils.keep("hello", (String) null));
175         assertEquals("", CharSetUtils.keep("hello", ""));
176         assertEquals("", CharSetUtils.keep("hello", "xyz"));
177         assertEquals("hello", CharSetUtils.keep("hello", "a-z"));
178         assertEquals("hello", CharSetUtils.keep("hello", "oleh"));
179         assertEquals("ell", CharSetUtils.keep("hello", "el"));
180     }
181 
182     @Test
183     public void testKeep_StringStringarray() {
184         assertNull(CharSetUtils.keep(null, (String[]) null));
185         assertNull(CharSetUtils.keep(null));
186         assertNull(CharSetUtils.keep(null, null));
187         assertNull(CharSetUtils.keep(null, "a-e"));
188 
189         assertEquals("", CharSetUtils.keep("", (String[]) null));
190         assertEquals("", CharSetUtils.keep(""));
191         assertEquals("", CharSetUtils.keep("", null));
192         assertEquals("", CharSetUtils.keep("", "a-e"));
193 
194         assertEquals("", CharSetUtils.keep("hello", (String[]) null));
195         assertEquals("", CharSetUtils.keep("hello"));
196         assertEquals("", CharSetUtils.keep("hello", null));
197         assertEquals("e", CharSetUtils.keep("hello", "a-e"));
198 
199         assertEquals("e", CharSetUtils.keep("hello", "a-e"));
200         assertEquals("ell", CharSetUtils.keep("hello", "el"));
201         assertEquals("hello", CharSetUtils.keep("hello", "elho"));
202         assertEquals("hello", CharSetUtils.keep("hello", "a-z"));
203         assertEquals("----", CharSetUtils.keep("----", "-"));
204         assertEquals("ll", CharSetUtils.keep("hello", "l"));
205     }
206 
207     @Test
208     public void testSqueeze_StringString() {
209         assertNull(CharSetUtils.squeeze(null, (String) null));
210         assertNull(CharSetUtils.squeeze(null, ""));
211 
212         assertEquals("", CharSetUtils.squeeze("", (String) null));
213         assertEquals("", CharSetUtils.squeeze("", ""));
214         assertEquals("", CharSetUtils.squeeze("", "a-e"));
215 
216         assertEquals("hello", CharSetUtils.squeeze("hello", (String) null));
217         assertEquals("hello", CharSetUtils.squeeze("hello", ""));
218         assertEquals("hello", CharSetUtils.squeeze("hello", "a-e"));
219         assertEquals("helo", CharSetUtils.squeeze("hello", "l-p"));
220         assertEquals("heloo", CharSetUtils.squeeze("helloo", "l"));
221         assertEquals("hello", CharSetUtils.squeeze("helloo", "^l"));
222     }
223 
224     @Test
225     public void testSqueeze_StringStringarray() {
226         assertNull(CharSetUtils.squeeze(null, (String[]) null));
227         assertNull(CharSetUtils.squeeze(null));
228         assertNull(CharSetUtils.squeeze(null, null));
229         assertNull(CharSetUtils.squeeze(null, "el"));
230 
231         assertEquals("", CharSetUtils.squeeze("", (String[]) null));
232         assertEquals("", CharSetUtils.squeeze(""));
233         assertEquals("", CharSetUtils.squeeze("", null));
234         assertEquals("", CharSetUtils.squeeze("", "a-e"));
235 
236         assertEquals("hello", CharSetUtils.squeeze("hello", (String[]) null));
237         assertEquals("hello", CharSetUtils.squeeze("hello"));
238         assertEquals("hello", CharSetUtils.squeeze("hello", null));
239         assertEquals("hello", CharSetUtils.squeeze("hello", "a-e"));
240 
241         assertEquals("helo", CharSetUtils.squeeze("hello", "el"));
242         assertEquals("hello", CharSetUtils.squeeze("hello", "e"));
243         assertEquals("fofof", CharSetUtils.squeeze("fooffooff", "of"));
244         assertEquals("fof", CharSetUtils.squeeze("fooooff", "fo"));
245     }
246 
247 }