View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.util;
16  
17  import static org.junit.jupiter.api.Assertions.assertFalse;
18  import static org.junit.jupiter.api.Assertions.assertThrows;
19  import static org.junit.jupiter.api.Assertions.assertTrue;
20  
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link ArrayUtils}.
25   *
26   * @author Ronald Brill
27   */
28  public class ArrayUtilsTest {
29  
30      /**
31       * @throws Exception if the test fails
32       */
33      @Test
34      public void contains() throws Exception {
35          assertTrue(ArrayUtils.contains(new String[] {"ab"}, "ab"));
36          assertTrue(ArrayUtils.contains(new String[] {"o", "ab", "cd"}, "ab"));
37          assertTrue(ArrayUtils.contains(new String[] {"cd", "ab"}, "ab"));
38  
39          assertFalse(ArrayUtils.contains(null, "ab"));
40          assertFalse(ArrayUtils.contains(new String[] {}, "ab"));
41          assertFalse(ArrayUtils.contains(ArrayUtils.EMPTY_STRING_ARRAY, "ab"));
42          assertFalse(ArrayUtils.contains(new String[] {"cd", "ab"}, "x"));
43  
44          assertThrows(IllegalArgumentException.class, () -> ArrayUtils.contains(new String[] {}, null));
45          assertThrows(IllegalArgumentException.class, () -> ArrayUtils.contains(ArrayUtils.EMPTY_STRING_ARRAY, null));
46      }
47  
48      /**
49       * @throws Exception if the test fails
50       */
51      @Test
52      public void containsIgnoreCase() throws Exception {
53          assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"ab"}, "ab"));
54          assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"o", "ab", "cd"}, "ab"));
55          assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"cd", "ab"}, "ab"));
56  
57          assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"ab"}, "aB"));
58          assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"o", "ab", "cd"}, "Ab"));
59          assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"cd", "ab"}, "AB"));
60  
61          assertFalse(ArrayUtils.containsIgnoreCase(null, "ab"));
62          assertFalse(ArrayUtils.containsIgnoreCase(new String[] {}, "ab"));
63          assertFalse(ArrayUtils.containsIgnoreCase(ArrayUtils.EMPTY_STRING_ARRAY, "ab"));
64          assertFalse(ArrayUtils.containsIgnoreCase(new String[] {"cd", "ab"}, "x"));
65  
66          assertThrows(IllegalArgumentException.class, () -> ArrayUtils.containsIgnoreCase(new String[] {}, null));
67          assertThrows(IllegalArgumentException.class,
68                  () -> ArrayUtils.containsIgnoreCase(ArrayUtils.EMPTY_STRING_ARRAY, null));
69      }
70  
71      /**
72       * @throws Exception if the test fails
73       */
74      @Test
75      public void containsChar() throws Exception {
76          assertTrue(ArrayUtils.contains(new char[] {1}, (char) 1));
77          assertTrue(ArrayUtils.contains(new char[] {7, 1, 9}, (char) 1));
78          assertTrue(ArrayUtils.contains(new char[] {5, 2}, (char) 2));
79  
80          assertFalse(ArrayUtils.contains(null, (char) 7));
81          assertFalse(ArrayUtils.contains(new char[] {}, (char) 1));
82          assertFalse(ArrayUtils.contains(ArrayUtils.EMPTY_CHAR_ARRAY, (char) 1));
83          assertFalse(ArrayUtils.contains(new char[] {7, 9}, (char) 4));
84      }
85  
86      /**
87       * @throws Exception if the test fails
88       */
89      @Test
90      public void containsByte() throws Exception {
91          assertTrue(ArrayUtils.contains(new byte[] {1}, (byte) 1));
92          assertTrue(ArrayUtils.contains(new byte[] {7, 1, 9}, (byte) 1));
93          assertTrue(ArrayUtils.contains(new byte[] {5, 2}, (byte) 2));
94  
95          assertFalse(ArrayUtils.contains(null, (byte) 7));
96          assertFalse(ArrayUtils.contains(new byte[] {}, (byte) 1));
97          assertFalse(ArrayUtils.contains(ArrayUtils.EMPTY_BYTE_ARRAY, (byte) 1));
98          assertFalse(ArrayUtils.contains(new byte[] {7, 9}, (byte) 4));
99      }
100 }