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    *      https://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  
18  package org.apache.commons.lang3;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests {@link StringUtils} trim methods.
27   */
28  class StringUtilsTrimTest extends AbstractLangTest {
29  
30      private static final String FOO = "foo";
31  
32      @Test
33      void testAsciiTrimControl() {
34          // null input returns null
35          assertNull(StringUtils.trimAsciiControl(null));
36          // empty string stays empty
37          assertEquals("", StringUtils.trimAsciiControl(""));
38          // empty string stays empty
39          assertEquals("", StringUtils.trimAsciiControl("\u007f"));
40          // no control chars: unchanged
41          assertEquals(FOO, StringUtils.trimAsciiControl(FOO));
42          // spaces are NOT stripped (space is char 32, not a control char)
43          assertEquals(" " + FOO + " ", StringUtils.trimAsciiControl(" " + FOO + " "));
44          // trailing NUL (\u0000) is stripped
45          assertEquals(FOO, StringUtils.trimAsciiControl(FOO + "\u0000"));
46          // leading NUL (\u0000) is stripped
47          assertEquals(FOO, StringUtils.trimAsciiControl("\u0000" + FOO));
48          // control chars on both ends are stripped
49          assertEquals(FOO, StringUtils.trimAsciiControl("\t\r\n" + FOO + "\t\r\n"));
50          // only control chars becomes empty string
51          assertEquals("", StringUtils.trimAsciiControl("\u0001\u0002\u001F"));
52          // embedded control chars are NOT stripped
53          assertEquals("a\u0001b", StringUtils.trimAsciiControl("a\u0001b"));
54          // space in the middle preserved, control chars on ends stripped
55          assertEquals(" " + FOO + " ", StringUtils.trimAsciiControl("\u0001 " + FOO + " \u0001"));
56          // char 31 (unit separator) is stripped; char 32 (space) is not
57          assertEquals(" abc ", StringUtils.trimAsciiControl("\u001F abc \u001F"));
58      }
59  
60      @Test
61      void testTrim() {
62          assertEquals(FOO, StringUtils.trim(FOO + "  "));
63          assertEquals(FOO, StringUtils.trim(" " + FOO + "  "));
64          assertEquals(FOO, StringUtils.trim(" " + FOO));
65          assertEquals(FOO, StringUtils.trim(FOO + ""));
66          assertEquals("", StringUtils.trim(" \t\r\n\b "));
67          assertEquals("", StringUtils.trim(StringUtilsTest.TRIMMABLE));
68          assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils.trim(StringUtilsTest.NON_TRIMMABLE));
69          assertEquals("", StringUtils.trim(""));
70          assertNull(StringUtils.trim(null));
71      }
72  
73      @Test
74      void testTrimToEmpty() {
75          assertEquals(FOO, StringUtils.trimToEmpty(FOO + "  "));
76          assertEquals(FOO, StringUtils.trimToEmpty(" " + FOO + "  "));
77          assertEquals(FOO, StringUtils.trimToEmpty(" " + FOO));
78          assertEquals(FOO, StringUtils.trimToEmpty(FOO + ""));
79          assertEquals("", StringUtils.trimToEmpty(" \t\r\n\b "));
80          assertEquals("", StringUtils.trimToEmpty(StringUtilsTest.TRIMMABLE));
81          assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils.trimToEmpty(StringUtilsTest.NON_TRIMMABLE));
82          assertEquals("", StringUtils.trimToEmpty(""));
83          assertEquals("", StringUtils.trimToEmpty(null));
84      }
85  
86      @Test
87      void testTrimToNull() {
88          assertEquals(FOO, StringUtils.trimToNull(FOO + "  "));
89          assertEquals(FOO, StringUtils.trimToNull(" " + FOO + "  "));
90          assertEquals(FOO, StringUtils.trimToNull(" " + FOO));
91          assertEquals(FOO, StringUtils.trimToNull(FOO + ""));
92          assertNull(StringUtils.trimToNull(" \t\r\n\b "));
93          assertNull(StringUtils.trimToNull(StringUtilsTest.TRIMMABLE));
94          assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils.trimToNull(StringUtilsTest.NON_TRIMMABLE));
95          assertNull(StringUtils.trimToNull(""));
96          assertNull(StringUtils.trimToNull(null));
97      }
98  }