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.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Unit tests {@link StringUtils} - StartsWith/EndsWith methods
26   */
27  public class StringUtilsStartsEndsWithTest extends AbstractLangTest {
28      private static final String foo    = "foo";
29      private static final String bar    = "bar";
30      private static final String foobar = "foobar";
31      private static final String FOO    = "FOO";
32      private static final String BAR    = "BAR";
33      private static final String FOOBAR = "FOOBAR";
34  
35      /**
36       * Test StringUtils.endsWith()
37       */
38      @Test
39      public void testEndsWith() {
40          assertTrue(StringUtils.endsWith(null, null), "endsWith(null, null)");
41          assertFalse(StringUtils.endsWith(FOOBAR, null), "endsWith(FOOBAR, null)");
42          assertFalse(StringUtils.endsWith(null, FOO), "endsWith(null, FOO)");
43          assertTrue(StringUtils.endsWith(FOOBAR, ""), "endsWith(FOOBAR, \"\")");
44  
45          assertFalse(StringUtils.endsWith(foobar, foo), "endsWith(foobar, foo)");
46          assertFalse(StringUtils.endsWith(FOOBAR, FOO), "endsWith(FOOBAR, FOO)");
47          assertFalse(StringUtils.endsWith(foobar, FOO), "endsWith(foobar, FOO)");
48          assertFalse(StringUtils.endsWith(FOOBAR, foo), "endsWith(FOOBAR, foo)");
49  
50          assertFalse(StringUtils.endsWith(foo, foobar), "endsWith(foo, foobar)");
51          assertFalse(StringUtils.endsWith(bar, foobar), "endsWith(foo, foobar)");
52  
53          assertTrue(StringUtils.endsWith(foobar, bar), "endsWith(foobar, bar)");
54          assertTrue(StringUtils.endsWith(FOOBAR, BAR), "endsWith(FOOBAR, BAR)");
55          assertFalse(StringUtils.endsWith(foobar, BAR), "endsWith(foobar, BAR)");
56          assertFalse(StringUtils.endsWith(FOOBAR, bar), "endsWith(FOOBAR, bar)");
57  
58          // "alpha, beta, gamma, delta".endsWith("delta")
59          assertTrue(StringUtils.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u03B4"),
60                  "endsWith(\u03B1\u03B2\u03B3\u03B4, \u03B4)");
61          // "alpha, beta, gamma, delta".endsWith("gamma, DELTA")
62          assertFalse(StringUtils.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u03B3\u0394"),
63                  "endsWith(\u03B1\u03B2\u03B3\u03B4, \u03B3\u0394)");
64      }
65  
66      @Test
67      public void testEndsWithAny() {
68          assertFalse(StringUtils.endsWithAny(null, (String) null), "StringUtils.endsWithAny(null, null)");
69          assertFalse(StringUtils.endsWithAny(null, "abc"), "StringUtils.endsWithAny(null, new String[] {abc})");
70          assertFalse(StringUtils.endsWithAny("abcxyz", (String) null), "StringUtils.endsWithAny(abcxyz, null)");
71          assertTrue(StringUtils.endsWithAny("abcxyz", ""), "StringUtils.endsWithAny(abcxyz, new String[] {\"\"})");
72          assertTrue(StringUtils.endsWithAny("abcxyz", "xyz"), "StringUtils.endsWithAny(abcxyz, new String[] {xyz})");
73          assertTrue(StringUtils.endsWithAny("abcxyz", null, "xyz", "abc"), "StringUtils.endsWithAny(abcxyz, new String[] {null, xyz, abc})");
74          assertFalse(StringUtils.endsWithAny("defg", null, "xyz", "abc"), "StringUtils.endsWithAny(defg, new String[] {null, xyz, abc})");
75          assertTrue(StringUtils.endsWithAny("abcXYZ", "def", "XYZ"));
76          assertFalse(StringUtils.endsWithAny("abcXYZ", "def", "xyz"));
77          assertTrue(StringUtils.endsWithAny("abcXYZ", "def", "YZ"));
78  
79          /*
80           * Type null of the last argument to method endsWithAny(CharSequence, CharSequence...)
81           * doesn't exactly match the vararg parameter type.
82           * Cast to CharSequence[] to confirm the non-varargs invocation,
83           * or pass individual arguments of type CharSequence for a varargs invocation.
84           *
85           * assertFalse(StringUtils.endsWithAny("abcXYZ", null)); // replace with specific types to avoid warning
86           */
87          assertFalse(StringUtils.endsWithAny("abcXYZ", (CharSequence) null));
88          assertFalse(StringUtils.endsWithAny("abcXYZ", (CharSequence[]) null));
89          assertTrue(StringUtils.endsWithAny("abcXYZ", ""));
90  
91          assertTrue(StringUtils.endsWithAny("abcxyz", new StringBuilder("abc"), new StringBuffer("xyz")), "StringUtils.endsWithAny(abcxyz, StringBuilder(abc), StringBuffer(xyz))");
92          assertTrue(StringUtils.endsWithAny(new StringBuffer("abcxyz"), new StringBuilder("abc"), new StringBuffer("xyz")), "StringUtils.endsWithAny(StringBuffer(abcxyz), StringBuilder(abc), StringBuffer(xyz))");
93      }
94  
95      /**
96       * Test StringUtils.endsWithIgnoreCase()
97       */
98      @Test
99      public void testEndsWithIgnoreCase() {
100         assertTrue(StringUtils.endsWithIgnoreCase(null, null), "endsWithIgnoreCase(null, null)");
101         assertFalse(StringUtils.endsWithIgnoreCase(FOOBAR, null), "endsWithIgnoreCase(FOOBAR, null)");
102         assertFalse(StringUtils.endsWithIgnoreCase(null, FOO), "endsWithIgnoreCase(null, FOO)");
103         assertTrue(StringUtils.endsWithIgnoreCase(FOOBAR, ""), "endsWithIgnoreCase(FOOBAR, \"\")");
104 
105         assertFalse(StringUtils.endsWithIgnoreCase(foobar, foo), "endsWithIgnoreCase(foobar, foo)");
106         assertFalse(StringUtils.endsWithIgnoreCase(FOOBAR, FOO), "endsWithIgnoreCase(FOOBAR, FOO)");
107         assertFalse(StringUtils.endsWithIgnoreCase(foobar, FOO), "endsWithIgnoreCase(foobar, FOO)");
108         assertFalse(StringUtils.endsWithIgnoreCase(FOOBAR, foo), "endsWithIgnoreCase(FOOBAR, foo)");
109 
110         assertFalse(StringUtils.endsWithIgnoreCase(foo, foobar), "endsWithIgnoreCase(foo, foobar)");
111         assertFalse(StringUtils.endsWithIgnoreCase(bar, foobar), "endsWithIgnoreCase(foo, foobar)");
112 
113         assertTrue(StringUtils.endsWithIgnoreCase(foobar, bar), "endsWithIgnoreCase(foobar, bar)");
114         assertTrue(StringUtils.endsWithIgnoreCase(FOOBAR, BAR), "endsWithIgnoreCase(FOOBAR, BAR)");
115         assertTrue(StringUtils.endsWithIgnoreCase(foobar, BAR), "endsWithIgnoreCase(foobar, BAR)");
116         assertTrue(StringUtils.endsWithIgnoreCase(FOOBAR, bar), "endsWithIgnoreCase(FOOBAR, bar)");
117 
118         // javadoc
119         assertTrue(StringUtils.endsWithIgnoreCase("abcdef", "def"));
120         assertTrue(StringUtils.endsWithIgnoreCase("ABCDEF", "def"));
121         assertFalse(StringUtils.endsWithIgnoreCase("ABCDEF", "cde"));
122 
123         // "alpha, beta, gamma, delta".endsWith("DELTA")
124         assertTrue(StringUtils.endsWithIgnoreCase("\u03B1\u03B2\u03B3\u03B4", "\u0394"),
125                 "endsWith(\u03B1\u03B2\u03B3\u03B4, \u0394)");
126         // "alpha, beta, gamma, delta".endsWith("GAMMA")
127         assertFalse(StringUtils.endsWithIgnoreCase("\u03B1\u03B2\u03B3\u03B4", "\u0393"),
128                 "endsWith(\u03B1\u03B2\u03B3\u03B4, \u0393)");
129     }
130 
131     /**
132      * Test StringUtils.startsWith()
133      */
134     @Test
135     public void testStartsWith() {
136         assertTrue(StringUtils.startsWith(null, null), "startsWith(null, null)");
137         assertFalse(StringUtils.startsWith(FOOBAR, null), "startsWith(FOOBAR, null)");
138         assertFalse(StringUtils.startsWith(null, FOO), "startsWith(null, FOO)");
139         assertTrue(StringUtils.startsWith(FOOBAR, ""), "startsWith(FOOBAR, \"\")");
140 
141         assertTrue(StringUtils.startsWith(foobar, foo), "startsWith(foobar, foo)");
142         assertTrue(StringUtils.startsWith(FOOBAR, FOO), "startsWith(FOOBAR, FOO)");
143         assertFalse(StringUtils.startsWith(foobar, FOO), "startsWith(foobar, FOO)");
144         assertFalse(StringUtils.startsWith(FOOBAR, foo), "startsWith(FOOBAR, foo)");
145 
146         assertFalse(StringUtils.startsWith(foo, foobar), "startsWith(foo, foobar)");
147         assertFalse(StringUtils.startsWith(bar, foobar), "startsWith(foo, foobar)");
148 
149         assertFalse(StringUtils.startsWith(foobar, bar), "startsWith(foobar, bar)");
150         assertFalse(StringUtils.startsWith(FOOBAR, BAR), "startsWith(FOOBAR, BAR)");
151         assertFalse(StringUtils.startsWith(foobar, BAR), "startsWith(foobar, BAR)");
152         assertFalse(StringUtils.startsWith(FOOBAR, bar), "startsWith(FOOBAR, bar)");
153     }
154 
155     @Test
156     public void testStartsWithAny() {
157         assertFalse(StringUtils.startsWithAny(null, (String[]) null));
158         assertFalse(StringUtils.startsWithAny(null, "abc"));
159         assertFalse(StringUtils.startsWithAny("abcxyz", (String[]) null));
160         assertFalse(StringUtils.startsWithAny("abcxyz"));
161         assertTrue(StringUtils.startsWithAny("abcxyz", "abc"));
162         assertTrue(StringUtils.startsWithAny("abcxyz", null, "xyz", "abc"));
163         assertFalse(StringUtils.startsWithAny("abcxyz", null, "xyz", "abcd"));
164         assertTrue(StringUtils.startsWithAny("abcxyz", ""));
165         assertFalse(StringUtils.startsWithAny("abcxyz", null, "xyz", "ABCX"));
166         assertFalse(StringUtils.startsWithAny("ABCXYZ", null, "xyz", "abc"));
167 
168         assertTrue(StringUtils.startsWithAny("abcxyz", new StringBuilder("xyz"), new StringBuffer("abc")), "StringUtils.startsWithAny(abcxyz, StringBuilder(xyz), StringBuffer(abc))");
169         assertTrue(StringUtils.startsWithAny(new StringBuffer("abcxyz"), new StringBuilder("xyz"), new StringBuffer("abc")), "StringUtils.startsWithAny(StringBuffer(abcxyz), StringBuilder(xyz), StringBuffer(abc))");
170     }
171 
172     /**
173      * Test StringUtils.testStartsWithIgnoreCase()
174      */
175     @Test
176     public void testStartsWithIgnoreCase() {
177         assertTrue(StringUtils.startsWithIgnoreCase(null, null), "startsWithIgnoreCase(null, null)");
178         assertFalse(StringUtils.startsWithIgnoreCase(FOOBAR, null), "startsWithIgnoreCase(FOOBAR, null)");
179         assertFalse(StringUtils.startsWithIgnoreCase(null, FOO), "startsWithIgnoreCase(null, FOO)");
180         assertTrue(StringUtils.startsWithIgnoreCase(FOOBAR, ""), "startsWithIgnoreCase(FOOBAR, \"\")");
181 
182         assertTrue(StringUtils.startsWithIgnoreCase(foobar, foo), "startsWithIgnoreCase(foobar, foo)");
183         assertTrue(StringUtils.startsWithIgnoreCase(FOOBAR, FOO), "startsWithIgnoreCase(FOOBAR, FOO)");
184         assertTrue(StringUtils.startsWithIgnoreCase(foobar, FOO), "startsWithIgnoreCase(foobar, FOO)");
185         assertTrue(StringUtils.startsWithIgnoreCase(FOOBAR, foo), "startsWithIgnoreCase(FOOBAR, foo)");
186 
187         assertFalse(StringUtils.startsWithIgnoreCase(foo, foobar), "startsWithIgnoreCase(foo, foobar)");
188         assertFalse(StringUtils.startsWithIgnoreCase(bar, foobar), "startsWithIgnoreCase(foo, foobar)");
189 
190         assertFalse(StringUtils.startsWithIgnoreCase(foobar, bar), "startsWithIgnoreCase(foobar, bar)");
191         assertFalse(StringUtils.startsWithIgnoreCase(FOOBAR, BAR), "startsWithIgnoreCase(FOOBAR, BAR)");
192         assertFalse(StringUtils.startsWithIgnoreCase(foobar, BAR), "startsWithIgnoreCase(foobar, BAR)");
193         assertFalse(StringUtils.startsWithIgnoreCase(FOOBAR, bar), "startsWithIgnoreCase(FOOBAR, bar)");
194     }
195 
196 }