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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.stream.Stream;
25  
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.params.ParameterizedTest;
28  import org.junit.jupiter.params.provider.MethodSource;
29  
30  /**
31   * Tests {@link Strings}.
32   */
33  class StringsTest {
34  
35      public static Stream<Strings> stringsFactory() {
36          return Stream.of(Strings.CS, Strings.CI);
37      }
38  
39      @Test
40      void testBuilder() {
41          assertTrue(Strings.builder().setIgnoreCase(false).get().isCaseSensitive());
42          assertFalse(Strings.builder().setIgnoreCase(true).get().isCaseSensitive());
43          //
44          assertTrue(Strings.builder().setNullIsLess(false).get().isCaseSensitive());
45          assertTrue(Strings.builder().setNullIsLess(true).get().isCaseSensitive());
46      }
47  
48      @Test
49      void testBuilderDefaults() {
50          final Strings strings = Strings.builder().get();
51          assertTrue(strings.isCaseSensitive());
52      }
53  
54      @Test
55      void testCaseInsensitiveConstant() {
56          assertNotNull(Strings.CI);
57          assertFalse(Strings.CI.isCaseSensitive());
58      }
59  
60      /**
61       * Expanding the existing test group {@link StringUtilsStartsEndsWithTest#testStartsWithAny()} to include case-insensitive cases
62       */
63      @Test
64      void testCaseInsensitiveStartsWithAny() {
65          // LANG-1682
66          assertFalse(Strings.CI.startsWithAny(null, (String[]) null));
67          assertFalse(Strings.CI.startsWithAny(null, "aBc"));
68          assertFalse(Strings.CI.startsWithAny("AbCxYz", (String[]) null));
69          assertFalse(Strings.CI.startsWithAny("AbCxYz"));
70          assertTrue(Strings.CI.startsWithAny("AbCxYz", "aBc"));
71          assertTrue(Strings.CI.startsWithAny("AbCxYz", null, "XyZ", "aBc"));
72          assertFalse(Strings.CI.startsWithAny("AbCxYz", null, "XyZ", "aBcD"));
73          assertTrue(Strings.CI.startsWithAny("AbCxYz", ""));
74          assertTrue(Strings.CI.startsWithAny("abcxyz", null, "XyZ", "ABCX"));
75          assertTrue(Strings.CI.startsWithAny("ABCXYZ", null, "XyZ", "abc"));
76  
77          assertTrue(Strings.CI.startsWithAny("AbCxYz", new StringBuilder("XyZ"), new StringBuffer("aBc")));
78          assertTrue(Strings.CI.startsWithAny(new StringBuffer("AbCxYz"), new StringBuilder("XyZ"), new StringBuffer("abc")));
79      }
80  
81      @Test
82      void testCaseSensitiveConstant() {
83          assertNotNull(Strings.CS);
84          assertTrue(Strings.CS.isCaseSensitive());
85      }
86  
87      @ParameterizedTest
88      @MethodSource("stringsFactory")
89      void testEqualsCharSequence(final Strings strings) {
90          final CharSequence nullCharSequence = null;
91          assertTrue(strings.equals(nullCharSequence, nullCharSequence));
92          assertFalse(strings.equals(nullCharSequence, ""));
93          assertFalse(strings.equals("", nullCharSequence));
94      }
95  
96      @ParameterizedTest
97      @MethodSource("stringsFactory")
98      void testEqualsStrings(final Strings strings) {
99          final String nullStr = null;
100         assertTrue(strings.equals(nullStr, nullStr));
101         assertFalse(strings.equals(nullStr, ""));
102         assertFalse(strings.equals("", nullStr));
103     }
104 }