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.assertThrows;
21  
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * NaN bypass in primitive double range validators.
26   * <p>
27   * Validate.inclusiveBetween(double,double,double) and Validate.exclusiveBetween(double,double,double) use ordered comparisons (&lt; > &lt;= >=). All ordered
28   * comparisons involving NaN return false, so Double.NaN passes any range check silently. A caller expecting NaN to be rejected (for example, for quota or
29   * monetary validation) receives no exception.
30   * </p>
31   * <p>
32   * Pre-patch: tests below FAIL (no exception thrown). Post-patch: tests below PASS (IllegalArgumentException thrown for NaN).
33   * </p>
34   */
35  class ValidateDoublesTest extends AbstractLangTest {
36  
37      @Test
38      void exclusiveBetweenRejectsNaN() {
39          assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(0.0, 10.0, Double.NaN),
40                  "NaN should be rejected by exclusiveBetween but is silently accepted");
41      }
42  
43      @Test
44      void exclusiveBetweenWithMessageRejectsNaN() {
45          assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(0.0, 10.0, Double.NaN, "value must be in (0,10)"),
46                  "NaN should be rejected by exclusiveBetween(with message)");
47      }
48  
49      @Test
50      void inclusiveBetweenRejectsNaN() {
51          assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(0.0, 10.0, Double.NaN),
52                  "NaN should be rejected by inclusiveBetween but is silently accepted");
53      }
54  
55      @Test
56      void inclusiveBetweenRejectsNegativeInfinity() {
57          assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(0.0, 10.0, Double.NEGATIVE_INFINITY),
58                  "Positive infinity beyond range end should be rejected");
59      }
60  
61      @Test
62      void inclusiveBetweenRejectsPositiveInfinity() {
63          assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(0.0, 10.0, Double.POSITIVE_INFINITY),
64                  "Positive infinity beyond range end should be rejected");
65      }
66  
67      @Test
68      void inclusiveBetweenWithMessageRejectsNaN() {
69          assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(0.0, 10.0, Double.NaN, "value must be in [0,10]"),
70                  "NaN should be rejected by inclusiveBetween(with message)");
71      }
72  
73      @Test
74      void validFiniteValuesStillAccepted() {
75          Validate.inclusiveBetween(0.0, 10.0, 5.0);
76          Validate.inclusiveBetween(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0.0);
77          Validate.exclusiveBetween(0.0, 10.0, 5.0);
78          Validate.exclusiveBetween(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0.0);
79      }
80  }