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.collections4;
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.assertTrue;
22  
23  import org.junit.jupiter.api.Test;
24  
25  public class ArrayUtilsTest {
26  
27      @Test
28      public void testContains() {
29          final Object[] array = { "0", "1", "2", "3", null, "0" };
30          assertFalse(ArrayUtils.contains(null, null));
31          assertFalse(ArrayUtils.contains(null, "1"));
32          assertTrue(ArrayUtils.contains(array, "0"));
33          assertTrue(ArrayUtils.contains(array, "1"));
34          assertTrue(ArrayUtils.contains(array, "2"));
35          assertTrue(ArrayUtils.contains(array, "3"));
36          assertTrue(ArrayUtils.contains(array, null));
37          assertFalse(ArrayUtils.contains(array, "notInArray"));
38      }
39  
40      @Test
41      public void testContains_LANG_1261() {
42          class LANG1261ParentObject {
43              @Override
44              public boolean equals(final Object o) {
45                  return true;
46              }
47          }
48          class LANG1261ChildObject extends LANG1261ParentObject {
49          }
50          final Object[] array = new LANG1261ChildObject[] { new LANG1261ChildObject() };
51          assertTrue(ArrayUtils.contains(array, new LANG1261ParentObject()));
52      }
53  
54      @Test
55      public void testIndexOf() {
56          final Object[] array = { "0", "1", "2", "3", null, "0" };
57          assertEquals(-1, ArrayUtils.indexOf(null, null));
58          assertEquals(-1, ArrayUtils.indexOf(null, "0"));
59          assertEquals(-1, ArrayUtils.indexOf(new Object[0], "0"));
60          assertEquals(0, ArrayUtils.indexOf(array, "0"));
61          assertEquals(1, ArrayUtils.indexOf(array, "1"));
62          assertEquals(2, ArrayUtils.indexOf(array, "2"));
63          assertEquals(3, ArrayUtils.indexOf(array, "3"));
64          assertEquals(4, ArrayUtils.indexOf(array, null));
65          assertEquals(-1, ArrayUtils.indexOf(array, "notInArray"));
66      }
67  }