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  
18  package org.apache.commons.jxpath.util;
19  
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  import org.junit.jupiter.api.Test;
29  
30  public class ValueUtilsTest {
31  
32      @Test
33      public void testGetValueFromArray() {
34          final Object data = new Object();
35          assertSame(data, ValueUtils.getValue(new Object[] { data }, 0));
36      }
37  
38      @Test
39      public void testGetValueFromArrayNegativeIndex() {
40          final Object data = new Object();
41          assertNull(ValueUtils.getValue(new Object[] { data }, -1));
42      }
43  
44      @Test
45      public void testGetValueFromArrayTooSmall() {
46          assertNull(ValueUtils.getValue(new Object[0], 2));
47      }
48  
49      @Test
50      public void testGetValueFromList() {
51          final Object data = new Object();
52          assertSame(data, ValueUtils.getValue(Arrays.asList(data), 0));
53      }
54  
55      @Test
56      public void testGetValueFromListNegativeIndex() {
57          final Object data = new Object();
58          final Object res = ValueUtils.getValue(Arrays.asList(data), -1);
59          assertNull(res, "Expected null, is " + res);
60      }
61  
62      @Test
63      public void testGetValueFromListTooSmall() {
64          assertNull(ValueUtils.getValue(Collections.EMPTY_LIST, 2));
65      }
66  
67      @Test
68      public void testGetValueFromSet() {
69          final Object data = new Object();
70          final Set dataSet = new HashSet();
71          dataSet.add(data);
72          assertSame(data, ValueUtils.getValue(dataSet, 0));
73      }
74  
75      @Test
76      public void testGetValueFromSetNegativeIndex() {
77          final Object data = new Object();
78          final Set dataSet = new HashSet();
79          dataSet.add(data);
80          assertNull(ValueUtils.getValue(dataSet, -1));
81      }
82  
83      /*
84       * This test would break without the patch and an NoSuchElementException being thrown instead.
85       */
86      @Test
87      public void testGetValueFromSetTooSmall() {
88          assertNull(ValueUtils.getValue(Collections.EMPTY_SET, 2));
89      }
90  }