Skip to content

Commit e1b6c87

Browse files
Fix "Doomed test for equality to NaN" SpotBugs warning in NumericConstraint (#4363)
Refactored `NumericConstraint.java` to use `Double.isNaN()` instead of `== Double.NaN`, which was always false. Updated `.github/scripts/generate-quality-report.py` to enforce `FE_TEST_IF_EQUAL_TO_NOT_A_NUMBER`. Added unit test `NumericConstraintTest.java` to verify the fix. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 2dba7f4 commit e1b6c87

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

.github/scripts/generate-quality-report.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,8 @@ def main() -> None:
767767
"IA_AMBIGUOUS_INVOCATION_OF_INHERITED_OR_OUTER_METHOD",
768768
"RpC_REPEATED_CONDITIONAL_TEST",
769769
"ES_COMPARING_PARAMETER_STRING_WITH_EQ",
770-
"FE_FLOATING_POINT_EQUALITY"
770+
"FE_FLOATING_POINT_EQUALITY",
771+
"FE_TEST_IF_EQUAL_TO_NOT_A_NUMBER"
771772
}
772773
violations = [
773774
f for f in spotbugs.findings

CodenameOne/src/com/codename1/ui/validation/NumericConstraint.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ public boolean isValid(Object value) {
8282
}
8383

8484
private boolean checkRange(double v) {
85-
if (minimum != Double.NaN) {
86-
if (maximum != Double.NaN) {
85+
if (!Double.isNaN(minimum)) {
86+
if (!Double.isNaN(maximum)) {
8787
return v >= minimum && v <= maximum;
8888
}
8989
return v >= minimum;
9090
} else {
91-
if (maximum != Double.NaN) {
91+
if (!Double.isNaN(maximum)) {
9292
return v <= maximum;
9393
}
9494
}
@@ -105,13 +105,13 @@ public String getDefaultFailMessage() {
105105
if (!dec) {
106106
round = "round ";
107107
}
108-
if (minimum != Double.NaN) {
109-
if (maximum != Double.NaN) {
108+
if (!Double.isNaN(minimum)) {
109+
if (!Double.isNaN(maximum)) {
110110
return "The value must be a valid " + round + "number between " + minimum + " and " + maximum;
111111
}
112112
return "The value must be a valid " + round + "number larger than " + minimum;
113113
} else {
114-
if (maximum != Double.NaN) {
114+
if (!Double.isNaN(maximum)) {
115115
return "The value must be a valid " + round + "number larger than " + maximum;
116116
}
117117
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
* This code is free software; you can redistribute it and/or modify it
5+
* under the terms of the GNU General Public License version 2 only, as
6+
* published by the Free Software Foundation. Codename One designates this
7+
* particular file as subject to the "Classpath" exception as provided
8+
* by Oracle in the LICENSE file that accompanied this code.
9+
*
10+
* This code is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* version 2 for more details (a copy is included in the LICENSE file that
14+
* accompanied this code).
15+
*
16+
* You should have received a copy of the GNU General Public License version
17+
* 2 along with this work; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Please contact Codename One through http://www.codenameone.com/ if you
21+
* need additional information or have any questions.
22+
*/
23+
package com.codename1.ui.validation;
24+
25+
import org.junit.jupiter.api.Test;
26+
import static org.junit.jupiter.api.Assertions.*;
27+
28+
public class NumericConstraintTest {
29+
30+
@Test
31+
public void testNumericConstraintValidation() {
32+
// Test with minimum and maximum
33+
NumericConstraint range = new NumericConstraint(true, 5.0, 10.0, "Must be between 5 and 10");
34+
assertTrue(range.isValid(5.0));
35+
assertTrue(range.isValid(7.5));
36+
assertTrue(range.isValid(10.0));
37+
assertFalse(range.isValid(4.9));
38+
assertFalse(range.isValid(10.1));
39+
assertFalse(range.isValid("abc")); // Invalid number
40+
41+
// Test with only minimum (maximum is NaN)
42+
NumericConstraint minOnly = new NumericConstraint(true, 5.0, Double.NaN, "Must be > 5");
43+
assertTrue(minOnly.isValid(5.0));
44+
assertTrue(minOnly.isValid(100.0));
45+
assertFalse(minOnly.isValid(4.9));
46+
47+
// Test with only maximum (minimum is NaN)
48+
NumericConstraint maxOnly = new NumericConstraint(true, Double.NaN, 10.0, "Must be < 10");
49+
assertTrue(maxOnly.isValid(10.0));
50+
assertTrue(maxOnly.isValid(-100.0));
51+
assertFalse(maxOnly.isValid(10.1));
52+
53+
// Test with no limits (both NaN)
54+
NumericConstraint noLimit = new NumericConstraint(true, Double.NaN, Double.NaN, "Any number");
55+
assertTrue(noLimit.isValid(Double.MIN_VALUE));
56+
assertTrue(noLimit.isValid(Double.MAX_VALUE));
57+
assertTrue(noLimit.isValid(0));
58+
}
59+
}

0 commit comments

Comments
 (0)